酷代码 AI
菜单
服务商

HarmonyLib.HarmonyException: Patching exception in method System.Void Verse.Map::FillComponents() ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: Transpiler pattern 'FillComponents_CheckRand' was expected to match at least 1 times, but matched 0 times (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)

Error analysis The error message indicates that there is an issue when patching the `Verse.Map::FillComponents()` method using HarmonyLib. Specifically, a transpiler pattern named `FillComponents_CheckRand` was expected to match at least once in the target method, but it didn't match any part of the method's code. Possible reasons 1. Incorrect pattern: The pattern defined in the transpiler might be wrong. It could be due to incorrect opcode sequences, operand values, or the pattern might not exist in the target method's IL code. 2. Method signature or implementation change: The `FillComponents` method might have been updated, and the pattern no longer matches the new IL code. 3. Harmony patching order: If there are multiple patches on the same method, the order of patching might affect the IL code, causing the pattern to fail. Suggested solution 1. Verify the pattern: Double - check the `FillComponents_CheckRand` pattern. Make sure the opcode sequences and operand values are correct. You can use a tool like ILSpy to view the IL code of the `FillComponents` method and compare it with your pattern. 2. Handle pattern mismatch gracefully: Instead of expecting the pattern to match at least once, you can add some error handling in your transpiler code. For example, if the pattern doesn't match, you can log a warning and return the original instructions. 3. Check for method changes: If the `FillComponents` method has been updated, you need to update your pattern accordingly. Here is an example of how you can handle the pattern mismatch gracefully in your transpiler code: ```csharp using HarmonyLib; using System.Collections.Generic; using System.Reflection.Emit; // Assume this is your transpiler method [HarmonyPatch(typeof(Verse.Map), "FillComponents")] class FillComponentsTranspiler { static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) { List<CodeInstruction> instructionList = new List<CodeInstruction>(instructions); // Try to find the pattern int matchCount = FindPattern(instructionList); if (matchCount == 0) { // Pattern not found, log a warning and return the original instructions UnityEngine.Debug.LogWarning("FillComponents_CheckRand pattern not found. Returning original instructions."); return instructionList; } // If the pattern is found, do your patching here //... return instructionList; } static int FindPattern(List<CodeInstruction> instructions) { // Implement your pattern matching logic here // Return the number of times the pattern is found return 0; } } ``` I in the past development process, encountered the following points need to be especially careful to deal with: 1. When using transpilers, always make sure to test your patterns on different versions of the target method. Method implementations can change, and your pattern might break. 2. Be aware of the Harmony patching order. If you have multiple patches on the same method, the order can affect the IL code and cause unexpected behavior. 3. Use proper logging in your transpiler code. This will help you debug issues when the pattern doesn't match or when there are other problems during patching. [2026-02-08 11:18:14 | AI写代码神器 | 730点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多