You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I have a generic class (and it's child classes) that I'd like to patch. Not every child class overrides the method that I'm patching, so I'm modifying the base generic class. However, only the last patch is executed and "alive" but no error is thrown.
To Reproduce
Steps to reproduce the behavior:
Create a generic class with a method (does not need to include the generic type in the method)
Patch the code with a post method.
No error, but if patched with multiple types, only the last one is executed
Expected behavior
Every patch called with the type that was specified
Screenshots / Code
The patch:
publicstaticclassRepositoryPatch<T,M>whereT:Repository<T,M>whereM: NSEipix.Base.Model
{publicdelegatevoid onAction(Trepo);publicstaticeventonActionPostDeserialization;privatestaticvoidDeserializePost(JsonRepository<T,M>__instance){if(PostDeserialization!=null&& __instance is T repo ){
PostDeserialization(repo);}}publicstaticvoidApplyPatch(Harmonyharmony){varorigDeser=typeof(T).GetMethod("Deserialize", BindingFlags.Instance | BindingFlags.NonPublic);if(origDeser==null|| origDeser.IsVirtual ){
Logger.Instance.info("No specific deserializer implementation for this repository. Falling back to JsonRepository (this is normal)");origDeser=typeof(JsonRepository<T,M>).GetMethod("Deserialize", BindingFlags.Instance | BindingFlags.NonPublic);}vardeserPost=typeof(RepositoryPatch<T,M>).GetMethod("DeserializePost", BindingFlags.Static | BindingFlags.NonPublic);
harmony.Patch(origDeser, postfix:new HarmonyMethod(deserPost));
Logger.Instance.info("The repository patching <"+typeof(T).Name +", "+typeof(M).Name +"> was done.");}}
where the patches are applyed:
try{varharmony=new Harmony("com.modloader.nsmeadival");
MainMenuPatch.ApplyPatch(harmony);// Works fine
RoomTypePatch.ApplyPatch(harmony);// Works fine
RoomTypeTooltipViewPatch.ApplyPatch(harmony);//Works fine
RoomViewPatch.ApplyPatch(harmony);// Works fineRepositoryPatch<CropfieldRepository,Cropfield>.ApplyPatch(harmony);//No error, doesn't get appliedRepositoryPatch<ResearchRepository,ResearchModel>.ApplyPatch(harmony);//No error, doesn't get appliedRepositoryPatch<RoomTypeRepository,RoomType>.ApplyPatch(harmony);// Works fine}catch(Exceptione){
Logger.Instance.info("Error happened while loading patches.\n"+e);throw;}
Runtime environment (please complete the following information):
OS: Windows 10 Enterprise build:19042.1052
.NET version: v4.6.2
Harmony version: 2.0.4.0
Name of game or host application: Going Medieval
Additional context
Changing the order of the method calls does affect which patch is called.
Maybe related issues:
Unfortunately this will be a WONTFIX due to lack of low level documentation on the internals of the .NET JIT compiler and how it structures assembler trampolines and other arcane structures that compiling IL to assembler does.
Even if documentation would exist the sheer amount of customization for all combinations of .NET version, processor architecture and environment will make this very hard to implement.
Just a small comment on something that comes up from time to time: Using generics in patch methods is a really bad idea.
Just because it works in your compiler doesn’t mean that it works at runtime too. The compiled IL makes the JIT process do funky stuff to call or multiply generic methods which simply does not work when embedding a call to such a prefix/postfix inside a replacement method. And while it will look like it works for a single use case does not mean it will work for other or multiple patches.
Describe the bug
I have a generic class (and it's child classes) that I'd like to patch. Not every child class overrides the method that I'm patching, so I'm modifying the base generic class. However, only the last patch is executed and "alive" but no error is thrown.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Every patch called with the type that was specified
Screenshots / Code
The patch:
where the patches are applyed:
Runtime environment (please complete the following information):
Additional context
Changing the order of the method calls does affect which patch is called.
Maybe related issues:
The text was updated successfully, but these errors were encountered: