Closed Awarets closed 6 months ago
A TypeLoadException means that not all dependencies were available at runtime. Did you use the fat or the thin version of the prerelease?
I was using the NuGet package, but I just tried both the fat and thin prereleases and got the same result for both (same harmony debug log and exception stacktrace)
Have you tried to build from master and make a local nuget package by using the pack command on the Harmony project?
I just attempted all 4 configurations. I was unable to pack ReleaseFat due to the following error:
C:\Harmony-master\ILRepack.targets(7,3): error : Failed to resolve System.Reflection.BindingFlags
DebugFat, DebugThin, and ReleaseThin didn't have this issue, but all three of them gave me the same error in the Godot editor:
E 0:00:01:0384 void Main._Ready(): System.TypeInitializationException: The type initializer for 'HarmonyLib.HarmonySharedState' threw an exception.
---> System.IO.FileNotFoundException: Could not load file or assembly 'Mono.Cecil, Version=0.11.5.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e'. The system cannot find the file specified.
File name: 'Mono.Cecil, Version=0.11.5.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e'
at HarmonyLib.HarmonySharedState.GetOrCreateSharedStateType()
at HarmonyLib.HarmonySharedState..cctor()
--- End of inner exception stack trace ---
at HarmonyLib.HarmonySharedState.GetPatchInfo(MethodBase method)
at HarmonyLib.PatchProcessor.Patch()
at HarmonyLib.Harmony.Patch(MethodBase original, HarmonyMethod prefix, HarmonyMethod postfix, HarmonyMethod transpiler, HarmonyMethod finalizer)
at Main._Ready() in C:\Godot Projects\HarmonyExportIssue\Main.cs:line 24
<C# Source> Main.cs:24 @ void Main._Ready()
<Stack Trace> GD.cs:382 @ void Godot.GD.PushError(System.Object[])
Main.cs:24 @ void Main._Ready()
Node.cs:2117 @ bool Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
Main_ScriptMethods.generated.cs:58 @ bool Main.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(nint, Godot.NativeInterop.godot_string_name*, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant_call_error*, Godot.NativeInterop.godot_variant*)
Same error in exported Godot build too.
I noticed in the Harmony log that env/clr had changed to 8.0.2. (Godot uses .Net 6.0) Maybe that is what's causing the error, I'm not sure if I did something wrong when packing it
### Harmony id=com.rumbler.patch, version=2.3.0.0, location=, env/clr=8.0.2, platform=Win32NT
### Started from virtual System.Void Main::_Ready(), location
### At 2024-02-16 12.22.15
I then tried to directly use the dlls within the /net6.0 folder of all four configurations and they all behaved just like before - works correctly in the editor, but has the same TypeLoadException in an exported build.
Not sure what your build environment looks like but Harmony builds and tests fine both in Azure and with GitHub actions today.
Yeah IDK, just running the latest version of Visual Studio 2022 on Windows 10. I keep getting that error every time I try to build ReleaseFat.
This looks like a dependency problem. Without more information, I can't help you and need to close this issue.
Here's as much more information as I can give:
Using Harmony 32e3f7b66d20adc9c5b3ea6607ddc110e721598d
With the following dlls, as included within the /ReleaseFat/net6.0/ folder 0Harmony.dll Mono.Cecil.dll (0.11.5) Mono.Cecil.Mdb.dll (0.11.5) Mono.Cecil.Pdb.dll (0.11.5) Mono.Cecil.Rocks.dll (0.11.5) MonoMod.Backports.dll (1.1.0) MonoMod.Core.dll (1.1.0) MonoMod.Iced.dll (1.17.0) MonoMod.ILHelpers.dll (1.0.1) MonoMod.Utils.dll (25.0.4) System.Text.Json.dll (6.0.2523.51912)
All I can gather from the exception stacktrace is that the problem is related to MonoMod.Utils, but all of its dependencies (Mono.Cecil, MonoMod.Backports, and MonoMod.ILHelpers (none of which seem to have any further dependencies)) appear to be present.
If there's any more info you think could help fix this, please let me know.
When I talked about dependencies I meant the system libraries. If BindingFlags is not available then the problem is more on a general .NET version level.
Turns out that while the exported godot buld uses .Net 6.0.27, the editor itself uses .Net 8.0.2! Thanks for helping to identify that.
So I guess one of the dependencies within the /net6.0/ folder isn't actually compatible with .Net 6? I'll see if I can look into this further.
I did some more tests:
The patch fails with all four configurations, but ReleaseFat is throwing System.TypeLoadException
, whereas the other three are throwing System.IO.FileNotFoundException
I tested with a .Net 6.0 console app and it worked with no issues. I'll close the issue seeing as the problem does not appear to be on Harmony's end.
I'm still thoroughly confused by this problem though. It seems as though the Godot export uses a different set of system dependencies from the console app. Their filesizes differ, despite them both using .Net 6.0.27 and having the exact same file metadata. I have no idea what the difference between them would be.
If anyone knows what's going on here, please let me know.
This appears to be a known issue with Godot and external .dll files. https://github.com/godotengine/godot/issues/75160
It's possible to work around this by using reflection as described in this answer: https://github.com/godotengine/godot/issues/75160#issuecomment-2071203840
public partial class ModLoader : Node
{
public override void _Ready()
{
string modPath = ProjectSettings.GlobalizePath("user://TestMod.dll");
var alc = AssemblyLoadContext.GetLoadContext(Assembly.GetExecutingAssembly());
Assembly assembly = alc.LoadFromAssemblyPath(modPath);
Type t = assembly.GetType("TestMod.TestModInit");
t.GetMethod("Init")?.Invoke(null, null);
}
}
Do note that at runtime you will need to get the dll file using
OS.GetExecutablePath().GetBaseDir().PathJoin(file: "dllname.dll")
instead
Describe the bug In an exported Godot build, when attempting to patch a method with a prefix, postfix, or finalizer method, Harmony fails to patch the method and throws an exception.
To Reproduce Use the following code with Harmony 2.3-prerelease.7 in Godot 4.2.1:
Expected behavior For the console to print:
Instead, it only prints this (after the exception):
Harmony debug log:
Exception StackTrace:
Runtime environment (please complete the following information):
Additional context This problem only occurs in exported builds, whether in debug mode or release mode. Code runs as expected within the editor.
The issue may be related to mismatched dependencies as discussed in #562 , but I am unable to identify which dependency is mismatched.