Zetrith / Prepatcher

Structured assembly rewriting library/mod for RW
MIT License
74 stars 7 forks source link

Unexpected Assembly Loading Behavior #22

Closed RealTelefonmast closed 5 months ago

RealTelefonmast commented 5 months ago

During my attempt to use Prepatcher I have discovered an odd behavior in the way it loads assemblies. I modified the Prepatcher source locally to add more in-depth logging for this analysis.

I have a mod with multiple assemblies: image

When starting the game from Steam, it fails to discover all assemblies. I created a simple method that logs all found assemblies for all mods currently running, which I call right at the beginning of Loader.DoReload(). image

The strange thing happens when I restart the game from within the game, achieved by adjusting the mod-list and letting it restart that way. Doing that, it ends up finding all assemblies that were expected. image

The way I discovered this is when I created a [FreePatch] method in my TeleCore.Patching module, in there I used my custom TLog type which is provided by TeleCore.Loader - since that assembly was not loaded into the context, I kept getting an error thrown.

Interestingly, the only assembly discovered when starting normally, is the one containing a FreePatch method, was it intended for Prepatcher to only load assemblies that have that attribute?

I attached two log files, one where I started the game normally, one where I restarted from within the game (which also disabled a mod to make that happen, the mod is not related to this issue). PrepatcherIssue_StartedNormally.txt PrepatcherIssue_RestartedIngame.txt

RealTelefonmast commented 5 months ago

Upon analyzing the logs more carefully, I noticed one difference - restarting the game, from within the game, does not initialize Doorstop. I have Doorstop set-up so I can debug the game more conveniently, it partially makes sense why it does not vibe well with Prepatcher, though I personally dont see why it would. Especially since it already breaks at the step during the Prepatcher Mod class initialization.

Not sure if this can be be made compatible.

RealTelefonmast commented 5 months ago

After discussing on Discord it turns out this is simply infeasible to make compatible for all cases, Doorstop is merely a dev tool after all anyway and should not be used by players anyway. The version I used in particular was by Bradson, he cleared it up! But this may still be interesting to consider for developers.