2 Assemblies Patching the Same Method While Using Statics Will Inconsistently Causes Bad Pointer References.
Made another set of mods that demonstrates the issue
The Issue:
If 2 assembly C# mods add patches to the same method and uses some static vars then, the moment second mod adds its patch, patch of the first mod will start referencing static vars from assembly where this mod was first loaded
It doesn't happen if you use one mod at a time It doesn't happen with "in memory" mods It doesn't happen if you use hooks instead of patches It doesn't happen consistently on every patch, on most patches it seems to happen randomly midgame. It happens consistently on GameMain.Update postfix or on any patch if first thing you did after opening the game was entering sub editor and quiting And it happens only inside harmony patches, all the other objects from the mod behave normally, like nothing happened
This looks like a Harmony bug, or perhaps plugin system bug
This is a problem because it doesn't let you use 2 assembly mods that patches the same method, which is exactly what harmony is not about
Do you think there is a way around this? Idk, mb i could store mod instances somewhere outside the plugin system, mb in ModStore? Or mb it could somehow get the latest assembly and get the var from there, but that's mb too slow for e.g. update postfixes
So
Here's 2 mods Cursed.zip They add postfix to GameMain.Update on command and log mod instance hash to console
Look at zis log, this is so cursed (press ctrl+f on hashes)
Lua! Version 6da26ff
Cs! Version 6da26ff
[Client] Loading assemblies for CPackage Cursed B
[Client] Assemblies from CPackage Cursed B loaded with Guid 8f55519d-8c06-4e73-94b0-fd4ce2145096.
[Client] Loading assemblies for CPackage Cursed A
[Client] Assemblies from CPackage Cursed A loaded with Guid 40dd2eea-1c9e-459f-a201-98931e1c331e.
[Client] Loading type: ModB
[Client] Loading type: ModA
ModB [33345988] Initialized
ModA [31678442] Initialized
[Client] LuaCsSetup: Completed assembly loading. Total time 661ms.
Using LuaSetup.lua from the Barotrauma Lua/ folder.
patcha
Patch in ModA referencing [31678442]
Patch in ModA referencing [31678442]
Patch in ModA referencing [31678442]
Patch in ModA referencing [31678442]
Patch in ModA referencing [31678442]
Patch in ModA referencing [31678442]
Patch in ModA referencing [31678442]
patchb
Patch in ModB referencing [33345988]
Patch in ModA referencing [31678442]
Patch in ModB referencing [33345988]
Patch in ModA referencing [31678442]
Patch in ModB referencing [33345988]
Patch in ModA referencing [31678442]
Patch in ModB referencing [33345988]
Patch in ModA referencing [31678442]
Patch in ModB referencing [33345988]
Patch in ModA referencing [31678442]
cl_reloadlua
ModB [33345988] Disposed
ModA [31678442] Disposed
Stopping Lua...
Lua! Version 6da26ff
Cs! Version 6da26ff
[Client] The below ACLs are still unloading:
[Client] ACL Name: Cursed B
[Client] -- Assembly: CursedB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] ACL Name: Cursed A
[Client] -- Assembly: CursedA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] Loading assemblies for CPackage Cursed B
[Client] Assemblies from CPackage Cursed B loaded with Guid a51fdef8-b472-48a2-8b1f-ccb7663a75ac.
[Client] Loading assemblies for CPackage Cursed A
[Client] Assemblies from CPackage Cursed A loaded with Guid affc7873-9c2b-4afa-986a-e585090a2630.
[Client] Loading type: ModB
[Client] Loading type: ModA
ModB [63777619] Initialized
ModA [65713524] Initialized
[Client] LuaCsSetup: Completed assembly loading. Total time 542ms.
Using LuaSetup.lua from the Barotrauma Lua/ folder.
patchb
Patch in ModB referencing [63777619]
Patch in ModB referencing [63777619]
Patch in ModB referencing [63777619]
Patch in ModB referencing [63777619]
Patch in ModB referencing [63777619]
patcha
Patch in ModB referencing [33345988]
Patch in ModA referencing [65713524]
Patch in ModB referencing [33345988]
Patch in ModA referencing [65713524]
Patch in ModB referencing [33345988]
Patch in ModA referencing [65713524]
Patch in ModB referencing [33345988]
Patch in ModA referencing [65713524]
Patch in ModB referencing [33345988]
Patch in ModA referencing [65713524]
cl_reloadlua
ModB [63777619] Disposed
ModA [65713524] Disposed
Stopping Lua...
Lua! Version 6da26ff
Cs! Version 6da26ff
[Client] The below ACLs are still unloading:
[Client] ACL Name: Cursed B
[Client] -- Assembly: CursedB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] ACL Name: Cursed A
[Client] -- Assembly: CursedA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] ACL Name: Cursed A
[Client] -- Assembly: CursedA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] ACL Name: Cursed B
[Client] -- Assembly: CursedB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] Loading assemblies for CPackage Cursed B
[Client] Assemblies from CPackage Cursed B loaded with Guid d7f8d6c5-f824-466a-91a5-4d908239540d.
[Client] Loading assemblies for CPackage Cursed A
[Client] Assemblies from CPackage Cursed A loaded with Guid 8d8a64e7-244f-403e-9dca-5f16cc6d78da.
[Client] Loading type: ModB
[Client] Loading type: ModA
ModB [43920097] Initialized
ModA [758120] Initialized
[Client] LuaCsSetup: Completed assembly loading. Total time 567ms.
Using LuaSetup.lua from the Barotrauma Lua/ folder.
patcha
Patch in ModA referencing [758120]
Patch in ModA referencing [758120]
Patch in ModA referencing [758120]
Patch in ModA referencing [758120]
Patch in ModA referencing [758120]
patchb
Patch in ModA referencing [31678442]
Patch in ModB referencing [43920097]
Patch in ModA referencing [31678442]
Patch in ModB referencing [43920097]
Patch in ModA referencing [31678442]
Patch in ModB referencing [43920097]
Patch in ModA referencing [31678442]
Patch in ModB referencing [43920097]
cl_reloadlua
ModB [43920097] Disposed
ModA [758120] Disposed
Stopping Lua...
Lua! Version 6da26ff
Cs! Version 6da26ff
[Client] The below ACLs are still unloading:
[Client] ACL Name: Cursed B
[Client] -- Assembly: CursedB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] ACL Name: Cursed A
[Client] -- Assembly: CursedA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] ACL Name: Cursed A
[Client] -- Assembly: CursedA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] ACL Name: Cursed B
[Client] -- Assembly: CursedB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] ACL Name: Cursed A
[Client] -- Assembly: CursedA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] ACL Name: Cursed B
[Client] -- Assembly: CursedB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[Client] Loading assemblies for CPackage Cursed B
[Client] Assemblies from CPackage Cursed B loaded with Guid 1e304d26-9276-482e-a271-4589ba0b7e30.
[Client] Loading assemblies for CPackage Cursed A
[Client] Assemblies from CPackage Cursed A loaded with Guid 3059889a-6ad1-4927-bf4c-ff8e214e600e.
[Client] Loading type: ModB
[Client] Loading type: ModA
ModB [30828931] Initialized
ModA [14115467] Initialized
[Client] LuaCsSetup: Completed assembly loading. Total time 609ms.
Using LuaSetup.lua from the Barotrauma Lua/ folder.
patchb
Patch in ModB referencing [30828931]
Patch in ModB referencing [30828931]
Patch in ModB referencing [30828931]
Patch in ModB referencing [30828931]
Patch in ModB referencing [30828931]
patcha
Patch in ModB referencing [33345988]
Patch in ModA referencing [14115467]
Patch in ModB referencing [33345988]
Patch in ModA referencing [14115467]
Patch in ModB referencing [33345988]
Patch in ModA referencing [14115467]
cl_reloadlua
ModB [30828931] Disposed
ModA [14115467] Disposed
Stopping Lua...
No, there seems to be no workaround
Exception: [A]CrabUI.CUI cannot be cast to [B]CrabUI.CUI.
Type A originates from 'QuickInteractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Program Files (x86)\Steam\steamapps\common\Barotrauma\LocalMods\Quick Interactions\bin\Client\Windows\QuickInteractions.dll'.
Type B originates from 'QuickInteractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Program Files (x86)\Steam\steamapps\common\Barotrauma\LocalMods\Quick Interactions\bin\Client\Windows\QuickInteractions.dll'.
(System.InvalidCastException)
Target site: System.Object ChkCast_Helper(Void*, System.Object)
Rip assembly modding
@MapleWheels Do you think this won't appear in the upcoming luatrauma rework?
I didn't get a notification for this until your ping lol. Let me look into this.
We're switching to HarmonyX (which uses MonoMod) instead of Harmony 2. The API is the same but the detouring logic is different. We'll see if this reproducible then and if so address it.
And what i mean by "Inconsistently" https://steamcommunity.com/sharedfiles/filedetails/?id=3449389548
This seems to be fixed in HarmonyX, there's no more broken references to dead assemblies Not Cursed.zip But for some reason compiled Graveyard doesn't work at all, but that seems to be due to some other bug or mb mistake in the code I'll report it separatelly if it's another bug