Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harmony fails to patch method and throws exception in exported Godot build #572

Closed
Awarets opened this issue Feb 7, 2024 · 13 comments
Closed
Assignees

Comments

@Awarets
Copy link

Awarets commented Feb 7, 2024

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:

using System;
using System.Collections;
using System.Collections.Generic;
using Godot;
using HarmonyLib;

public partial class Main : Node
{
    private static int testField;

    private static void MethodToTryPatching() => GD.Print(nameof(MethodToTryPatching));

    private static void PrefixPatchMethod() => GD.Print($"{nameof(PrefixPatchMethod)} (This will print before {nameof(MethodToTryPatching)} if method was successfully patched)");

    public sealed override void _Ready()
    {
        HarmonyLib.Harmony.DEBUG = true;

        var harmony = new HarmonyLib.Harmony(id: "com.rumbler.patch");

        var original = typeof(Main).GetMethod(name: nameof(MethodToTryPatching), bindingAttr: System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
        var prefix = new HarmonyLib.HarmonyMethod(method: typeof(Main).GetMethod(name: nameof(PrefixPatchMethod), bindingAttr: System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic));

        try
        {
            harmony.Patch(
                original: original,
                prefix: prefix);
        }
        catch (Exception e)
        {
            GD.PushError(e);
        }

        MethodToTryPatching();
    }
}

Expected behavior
For the console to print:

PrefixPatchMethod (This will print before MethodToTryPatching if method was successfully patched)
MethodToTryPatching

Instead, it only prints this (after the exception):

MethodToTryPatching

Harmony debug log:

### Harmony id=com.rumbler.patch, version=2.3.0.0, location=C:\HarmonyExportIssueBuild\data_HarmonyExportIssue_windows_x86_64\0Harmony.dll, env/clr=6.0.21, platform=Win32NT
### Started from virtual System.Void Main::_Ready(), location C:\HarmonyExportIssueBuild\data_HarmonyExportIssue_windows_x86_64\HarmonyExportIssue.dll
### At 2024-02-07 05.12.53
### Patch: static System.Void Main::MethodToTryPatching()
### Replacement: static System.Void Main::Main.MethodToTryPatching_Patch1()

Exception StackTrace:

ERROR: System.ArgumentException: GenericArguments[0], 'MonoMod.Utils.Cil.CecilILGenerator', on 'MonoMod.Utils.Cil.ILGeneratorProxy[TTarget]' violates the constraint of type 'TTarget'.
 ---> System.TypeLoadException: GenericArguments[0], 'MonoMod.Utils.Cil.CecilILGenerator', on 'MonoMod.Utils.Cil.ILGeneratorProxy[TTarget]' violates the constraint of type parameter 'TTarget'.
   at System.RuntimeTypeHandle.Instantiate(RuntimeType inst)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   --- End of inner exception stack trace ---
   at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   at MonoMod.Utils.Cil.ILGeneratorShim.GetProxy()
   at MonoMod.Utils.DynamicMethodDefinition.GetILGenerator()
   at HarmonyLib.MethodPatcher..ctor(MethodBase original, MethodBase source, List`1 prefixes, List`1 postfixes, List`1 transpilers, List`1 finalizers, Boolean debug)
   at HarmonyLib.PatchFunctions.UpdateWrapper(MethodBase original, PatchInfo patchInfo)
   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 26
   at: void Main._Ready() (C:/Godot Projects/HarmonyExportIssue/Main.cs:26)

Runtime environment (please complete the following information):

  • OS: Windows 10, 64bit
  • .NET version: .NET 6.0
  • Harmony 2.3-prerelease.7
  • Godot 4.2.1

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.

@pardeike
Copy link
Owner

pardeike commented Feb 7, 2024

A TypeLoadException means that not all dependencies were available at runtime. Did you use the fat or the thin version of the prerelease?

@Awarets
Copy link
Author

Awarets commented Feb 7, 2024

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)

@pardeike
Copy link
Owner

Have you tried to build from master and make a local nuget package by using the pack command on the Harmony project?

@Awarets
Copy link
Author

Awarets commented Feb 16, 2024

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.

@pardeike
Copy link
Owner

Not sure what your build environment looks like but Harmony builds and tests fine both in Azure and with GitHub actions today.

@pardeike
Copy link
Owner

@Awarets
Copy link
Author

Awarets commented Feb 17, 2024

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.

Here's the build output log if that helps:
Build started at 05:58...
Restored C:\Harmony-master\TestLibrary\TestLibrary.csproj (in 55 ms).
Restored C:\Harmony-master\Harmony\Documentation\Documentation.csproj (in 94 ms).
Restored C:\Harmony-master\Harmony\Harmony.csproj (in 118 ms).
Restored C:\Harmony-master\HarmonyTests\HarmonyTests.csproj (in 579 ms).
1>------ Build started: Project: Harmony, Configuration: ReleaseFat Any CPU ------
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\netstandard2.0\0Harmony.dll
1>Added assembly 'bin\ReleaseFat\netstandard2.0\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\MonoMod.Utils.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\System.Reflection.Emit.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\System.Reflection.Emit.ILGeneration.dll'
1>Added assembly 'bin\ReleaseFat\netstandard2.0\System.Reflection.Emit.Lightweight.dll'
1>Merging 13 assembies to 'bin\ReleaseFat\netstandard2.0\\0Harmony.dll'
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\net472\0Harmony.dll
1>Added assembly 'bin\ReleaseFat\net472\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\net472\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\net472\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\net472\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\net472\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\net472\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\net472\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\net472\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\net472\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\net472\MonoMod.Utils.dll'
1>Added assembly 'bin\ReleaseFat\net472\System.ValueTuple.dll'
1>Merging 11 assembies to 'bin\ReleaseFat\net472\\0Harmony.dll'
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\net452\0Harmony.dll
1>Added assembly 'bin\ReleaseFat\net452\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\net452\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\net452\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\net452\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\net452\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\net452\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\net452\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\net452\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\net452\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\net452\MonoMod.Utils.dll'
1>Added assembly 'bin\ReleaseFat\net452\System.ValueTuple.dll'
1>Merging 11 assembies to 'bin\ReleaseFat\net452\\0Harmony.dll'
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\net35\0Harmony.dll
1>Added assembly 'bin\ReleaseFat\net35\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\net35\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\net35\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\net35\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\net35\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\net35\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\net35\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\net35\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\net35\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\net35\MonoMod.Utils.dll'
1>Merging 10 assembies to 'bin\ReleaseFat\net35\\0Harmony.dll'
1>Merge succeeded in 1.4776917 s
1>Merge succeeded in 2.3744514 s
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\net48\0Harmony.dll
1>Added assembly 'bin\ReleaseFat\net48\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\net48\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\net48\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\net48\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\net48\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\net48\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\net48\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\net48\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\net48\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\net48\MonoMod.Utils.dll'
1>Added assembly 'bin\ReleaseFat\net48\System.ValueTuple.dll'
1>Merging 11 assembies to 'bin\ReleaseFat\net48\\0Harmony.dll'
1>Merge succeeded in 2.460817 s
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\netcoreapp3.0\0Harmony.dll
1>Added assembly 'bin\ReleaseFat\netcoreapp3.0\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.0\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.0\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.0\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.0\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.0\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.0\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.0\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.0\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.0\MonoMod.Utils.dll'
1>Merging 10 assembies to 'bin\ReleaseFat\netcoreapp3.0\\0Harmony.dll'
1>Merge succeeded in 1.5580721 s
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\netcoreapp3.1\0Harmony.dll
1>Merge succeeded in 4.0784896 s
1>Added assembly 'bin\ReleaseFat\netcoreapp3.1\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.1\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.1\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.1\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.1\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.1\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.1\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.1\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.1\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\netcoreapp3.1\MonoMod.Utils.dll'
1>Merging 10 assembies to 'bin\ReleaseFat\netcoreapp3.1\\0Harmony.dll'
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\net5.0\0Harmony.dll
1>Added assembly 'bin\ReleaseFat\net5.0\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\net5.0\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\net5.0\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\net5.0\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\net5.0\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\net5.0\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\net5.0\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\net5.0\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\net5.0\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\net5.0\MonoMod.Utils.dll'
1>Added assembly 'bin\ReleaseFat\net5.0\System.Text.Json.dll'
1>Merging 11 assembies to 'bin\ReleaseFat\net5.0\\0Harmony.dll'
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\net6.0\0Harmony.dll
1>Added assembly 'bin\ReleaseFat\net6.0\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\net6.0\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\net6.0\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\net6.0\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\net6.0\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\net6.0\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\net6.0\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\net6.0\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\net6.0\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\net6.0\MonoMod.Utils.dll'
1>Added assembly 'bin\ReleaseFat\net6.0\System.Text.Json.dll'
1>Merging 11 assembies to 'bin\ReleaseFat\net6.0\\0Harmony.dll'
1>Merge succeeded in 3.3704322 s
1>C:\Harmony-master\ILRepack.targets(7,3): error : Failed to resolve System.Reflection.BindingFlags
1>Done building project "Harmony.csproj" -- FAILED.
1>Merge succeeded in 6.2760922 s
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\net7.0\0Harmony.dll
1>Added assembly 'bin\ReleaseFat\net7.0\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\net7.0\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\net7.0\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\net7.0\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\net7.0\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\net7.0\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\net7.0\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\net7.0\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\net7.0\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\net7.0\MonoMod.Utils.dll'
1>Merging 10 assembies to 'bin\ReleaseFat\net7.0\\0Harmony.dll'
1>Harmony -> C:\Harmony-master\Harmony\bin\ReleaseFat\net8.0\0Harmony.dll
1>Added assembly 'bin\ReleaseFat\net8.0\0Harmony.dll'
1>Added assembly 'bin\ReleaseFat\net8.0\Mono.Cecil.dll'
1>Added assembly 'bin\ReleaseFat\net8.0\Mono.Cecil.Mdb.dll'
1>Added assembly 'bin\ReleaseFat\net8.0\Mono.Cecil.Pdb.dll'
1>Added assembly 'bin\ReleaseFat\net8.0\Mono.Cecil.Rocks.dll'
1>Added assembly 'bin\ReleaseFat\net8.0\MonoMod.Backports.dll'
1>Added assembly 'bin\ReleaseFat\net8.0\MonoMod.Core.dll'
1>Added assembly 'bin\ReleaseFat\net8.0\MonoMod.Iced.dll'
1>Added assembly 'bin\ReleaseFat\net8.0\MonoMod.ILHelpers.dll'
1>Added assembly 'bin\ReleaseFat\net8.0\MonoMod.Utils.dll'
1>Merging 10 assembies to 'bin\ReleaseFat\net8.0\\0Harmony.dll'
1>Merge succeeded in 3.1084627 s
1>Merge succeeded in 7.1624303 s
1>Merge succeeded in 3.4050145 s
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 05:59 and took 17.481 seconds ==========

@pardeike
Copy link
Owner

This looks like a dependency problem. Without more information, I can't help you and need to close this issue.

@Awarets
Copy link
Author

Awarets commented Feb 19, 2024

Here's as much more information as I can give:

Using Harmony 32e3f7b

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.

@pardeike
Copy link
Owner

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.

@Awarets
Copy link
Author

Awarets commented Feb 20, 2024

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.

@Awarets
Copy link
Author

Awarets commented Feb 20, 2024

I did some more tests:

test.sh Results
Test run for C:\Harmony-master\HarmonyTests\bin\Debug\net35\HarmonyTests.dll (.NETFramework,Version=v3.5)
Starting test execution, please wait...
Framework35 is not supported. For projects targeting .Net Framework 3.5, test will run in CLR 4.0 "compatibility mode".
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:   227, Skipped:     2, Total:   229, Duration: 10 s - HarmonyTests.dll (net35)
Test run for C:\Harmony-master\HarmonyTests\bin\Debug\net452\HarmonyTests.dll (.NETFramework,Version=v4.5.2)
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:   227, Skipped:     2, Total:   229, Duration: 8 s - HarmonyTests.dll (net452)
Test run for C:\Harmony-master\HarmonyTests\bin\Debug\net472\HarmonyTests.dll (.NETFramework,Version=v4.7.2)
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:   227, Skipped:     2, Total:   229, Duration: 8 s - HarmonyTests.dll (net472)
Test run for C:\Harmony-master\HarmonyTests\bin\Debug\net48\HarmonyTests.dll (.NETFramework,Version=v4.8)
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:   227, Skipped:     2, Total:   229, Duration: 7 s - HarmonyTests.dll (net48)
Test run for C:\Harmony-master\HarmonyTests\bin\Debug\netcoreapp3.0\HarmonyTests.dll (.NETCoreApp,Version=v3.0)
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:   227, Skipped:     2, Total:   229, Duration: 5 s - HarmonyTests.dll (netcoreapp3.0)
Test run for C:\Harmony-master\HarmonyTests\bin\Debug\netcoreapp3.1\HarmonyTests.dll (.NETCoreApp,Version=v3.1)
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:   227, Skipped:     2, Total:   229, Duration: 4 s - HarmonyTests.dll (netcoreapp3.1)
Test run for C:\Harmony-master\HarmonyTests\bin\Debug\net5.0\HarmonyTests.dll (.NETCoreApp,Version=v5.0)
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:   227, Skipped:     2, Total:   229, Duration: 4 s - HarmonyTests.dll (net5.0)
Test run for C:\Harmony-master\HarmonyTests\bin\Debug\net6.0\HarmonyTests.dll (.NETCoreApp,Version=v6.0)
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:   227, Skipped:     2, Total:   229, Duration: 4 s - HarmonyTests.dll (net6.0)
Test run for C:\Harmony-master\HarmonyTests\bin\Debug\net7.0\HarmonyTests.dll (.NETCoreApp,Version=v7.0)
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:   227, Skipped:     2, Total:   229, Duration: 3 s - HarmonyTests.dll (net7.0)
Test run for C:\Harmony-master\HarmonyTests\bin\Debug\net8.0\HarmonyTests.dll (.NETCoreApp,Version=v8.0)
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:   227, Skipped:     2, Total:   229, Duration: 3 s - HarmonyTests.dll (net8.0)
HarmonyExportIssueBuild with DebugFat/DebugThin
C:\HarmonyExportIssueBuild>HarmonyExportIssue.console.exe
Godot Engine v4.2.1.stable.mono.official.b09f793f5 - https://godotengine.org
Vulkan API 1.2.170 - Forward+ - Using Vulkan Device #0: AMD - AMD Radeon (TM) R9 390 Series

System.Environment.Version: 6.0.27
ERROR: 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() in C:\Harmony-master\Harmony\Internal\HarmonySharedState.cs:line 47
   --- End of inner exception stack trace ---
   at HarmonyLib.HarmonySharedState.GetPatchInfo(MethodBase method) in C:\Harmony-master\Harmony\Internal\HarmonySharedState.cs:line 107
   at HarmonyLib.PatchProcessor.Patch() in C:\Harmony-master\Harmony\Public\PatchProcessor.cs:line 135
   at HarmonyLib.Harmony.Patch(MethodBase original, HarmonyMethod prefix, HarmonyMethod postfix, HarmonyMethod transpiler, HarmonyMethod finalizer) in C:\Harmony-master\Harmony\Public\Harmony.cs:line 167
   at Main._Ready() in C:\Godot Projects\HarmonyExportIssue\Main.cs:line 27
   at: void Main._Ready() (C:/Godot Projects/HarmonyExportIssue/Main.cs:27)
MethodToTryPatching
HarmonyExportIssueBuild with ReleaseFat
C:\HarmonyExportIssueBuild>HarmonyExportIssue.console.exe
Godot Engine v4.2.1.stable.mono.official.b09f793f5 - https://godotengine.org
Vulkan API 1.2.170 - Forward+ - Using Vulkan Device #0: AMD - AMD Radeon (TM) R9 390 Series

System.Environment.Version: 6.0.27
ERROR: System.ArgumentException: GenericArguments[0], 'MonoMod.Utils.Cil.CecilILGenerator', on 'MonoMod.Utils.Cil.ILGeneratorProxy[TTarget]' violates the constraint of type 'TTarget'.
 ---> System.TypeLoadException: GenericArguments[0], 'MonoMod.Utils.Cil.CecilILGenerator', on 'MonoMod.Utils.Cil.ILGeneratorProxy[TTarget]' violates the constraint of type parameter 'TTarget'.
   at System.RuntimeTypeHandle.Instantiate(RuntimeType inst)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   --- End of inner exception stack trace ---
   at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   at MonoMod.Utils.Cil.ILGeneratorShim.GetProxy()
   at MonoMod.Utils.DynamicMethodDefinition.GetILGenerator()
   at HarmonyLib.MethodPatcher..ctor(MethodBase original, MethodBase source, List`1 prefixes, List`1 postfixes, List`1 transpilers, List`1 finalizers, Boolean debug)
   at HarmonyLib.PatchFunctions.UpdateWrapper(MethodBase original, PatchInfo patchInfo)
   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 27
   at: void Main._Ready() (C:/Godot Projects/HarmonyExportIssue/Main.cs:27)
MethodToTryPatching
HarmonyExportIssueBuild with ReleaseThin
C:\HarmonyExportIssueBuild>HarmonyExportIssue.console.exe
Godot Engine v4.2.1.stable.mono.official.b09f793f5 - https://godotengine.org
Vulkan API 1.2.170 - Forward+ - Using Vulkan Device #0: AMD - AMD Radeon (TM) R9 390 Series

System.Environment.Version: 6.0.27
ERROR: 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 27
   at: void Main._Ready() (C:/Godot Projects/HarmonyExportIssue/Main.cs:27)
MethodToTryPatching

The patch fails with all four configurations, but ReleaseFat is throwing System.TypeLoadException, whereas the other three are throwing System.IO.FileNotFoundException

@Awarets
Copy link
Author

Awarets commented Feb 24, 2024

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.
image

If anyone knows what's going on here, please let me know.

@Awarets Awarets closed this as completed Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants