HarmonyX icon indicating copy to clipboard operation
HarmonyX copied to clipboard

Add a clear exception when trying to patch a target with unset generic arguments

Open ghorsington opened this issue 1 year ago • 0 comments

When trying to patch methods like

  • Foo<T>.Method
  • Foo.Method<T>

patching fails with a somewhat cryptic error like

HarmonyLib.HarmonyException: IL Compile Error (unknown location) ---> HarmonyLib.HarmonyException: IL Compile Error (unknown location) ---> System.NotSupportedException: Specified method is not supported.
  at MonoMod.Utils.MMReflectionImporter.ImportGenericParameter (System.Type type, Mono.Cecil.IGenericParameterProvider context) [0x0008f] in <6733e342b5b549bba815373898724469>:0
  at MonoMod.Utils.MMReflectionImporter._ImportReference (System.Type type, Mono.Cecil.IGenericParameterProvider context, MonoMod.Utils.MMReflectionImporter+GenericImportKind importKind) [0x00130] in <6733e342b5b549bba815373898724469>:0
  at MonoMod.Utils.MMReflectionImporter._ImportGenericInstance (System.Type type, Mono.Cecil.IGenericParameterProvider context, Mono.Cecil.TypeReference typeRef) [0x0001c] in <6733e342b5b549bba815373898724469>:0
  at MonoMod.Utils.MMReflectionImporter._ImportReference (System.Type type, Mono.Cecil.IGenericParameterProvider context, MonoMod.Utils.MMReflectionImporter+GenericImportKind importKind) [0x00122] in <6733e342b5b549bba815373898724469>:0
  at MonoMod.Utils.MMReflectionImporter.ImportReference (System.Type type, Mono.Cecil.IGenericParameterProvider context) [0x00000] in <6733e342b5b549bba815373898724469>:0

Instead of letting the invalid target propagate to MonoMod, it's better to fail early with a clear message like

Generic methods cannot be patched. Assign the generic parameters (e.g. Foo<int> instead of Foo<T>).

and don't let patching proceed further.

ghorsington avatar Jul 29 '22 18:07 ghorsington