pardeike / Harmony

A library for patching, replacing and decorating .NET and Mono methods during runtime
https://www.patreon.com/pardeike
MIT License
5.15k stars 485 forks source link

System.Exception: Parameter "xx" not found in method #599

Closed yangboyd closed 5 months ago

yangboyd commented 5 months ago

Harmony 2.0 Windows x86 .Net 4..7

The target method has Unicode parameter name, the patch method use ascii parameter name. It seems that harmony require parameter name should be the same.

hook exception: System.Exception: Parameter "obj" not found in method static loader. loader.  ::(System.Object  , System.Reflection.MethodBase  , System.Byte[]  , System.Byte[]   , System.Byte[]  , System.Byte[]  , System.Int32[]  , System.Int32   ) 在 HarmonyLib.MethodPatcher.EmitCallParameter(MethodInfo patch, Dictionary2 variables, LocalBuilder runOriginalVariable, Boolean allowFirsParamPassthrough, LocalBuilder& tmpObjectVar, List1 tmpBoxVars) 在 HarmonyLib.MethodPatcher.<>c__DisplayClass35_0.b__0(MethodInfo fix) 在 HarmonyLib.CollectionExtensions.Do[T](IEnumerable1 sequence, Action1 action) 在 HarmonyLib.MethodPatcher.AddPrefixes(Dictionary2 variables, LocalBuilder runOriginalVariable) 在 HarmonyLib.MethodPatcher.CreateReplacement(Dictionary2& finalInstructions) 在 HarmonyLib.PatchFunctions.UpdateWrapper(MethodBase original, PatchInfo patchInfo) 在 HarmonyLib.PatchProcessor.Patch() 在 HarmonyLib.Harmony.Patch(MethodBase original, HarmonyMethod prefix, HarmonyMethod postfix, HarmonyMethod transpiler, HarmonyMethod finalizer)

pardeike commented 5 months ago

Are you using Harmony or HarmonyX ?

yangboyd commented 5 months ago

It's Harmony.dll 2.2.2.0

Are you using Harmony or HarmonyX ?

pardeike commented 5 months ago

The parameter name is of course dependent on your source code’s file encoding. You are responsible to use the right string or use the relative parameter position instead. You can also use an annotation that uses a custom name or you can use a parameter array and use the relative position in that array. Many possibilities.

yangboyd commented 5 months ago

The parameter name is of course dependent on your source code’s file encoding. You are responsible to use the right string or use the relative parameter position instead. You can also use an annotation that uses a custom name or you can use a parameter array and use the relative position in that array. Many possibilities.

Thanks, It seems that HarmonyParameter do the work, but it's removed. Would you please show some example?

pardeike commented 5 months ago

Here is the documentation build from the actual source code: https://harmony.pardeike.net/api/HarmonyLib.HarmonyArgument.html

yangboyd commented 5 months ago

Here is the documentation build from the actual source code: https://harmony.pardeike.net/api/HarmonyLib.HarmonyArgument.html

It's an great annotation. How to manually use this HarmonyArgument? As I call the harmony.patch directly, I need patch the dll manually as it's loaded later sometime.

pardeike commented 5 months ago

It’s part of HarmonyMethod which you are using if you are doing manual patching. Edit: what I meant is that it’s part of the patch method which you are using anyway even with manual patching.

yangboyd commented 5 months ago

It’s part of HarmonyMethod which you are using if you are doing manual patching. Edit: what I meant is that it’s part of the patch method which you are using anyway even with manual patching.

Great!

yangboyd commented 5 months ago

It’s part of HarmonyMethod which you are using if you are doing manual patching. Edit: what I meant is that it’s part of the patch method which you are using anyway even with manual patching.

It does not work this way: [HarmonyArgument("\u0004\u000F\u0007\u0005\u0006\u0003\u0008\u000C", "obj")] public static bool ObfuscateMethod1Prefix(ref object __result, object obj, MethodBase mb, byte[] param1, byte[] param2, byte[] param3, byte[] param4, int[] param5, int param6) {

What's the proper usage?

pardeike commented 5 months ago

You put it on a parameter or the class depending on its function: https://harmony.pardeike.net/api/HarmonyLib.HarmonyArgument.htm - note the annotation usage that are itself annotations. If this version does not work then try one that uses the index for example.