pardeike / Harmony

A library for patching, replacing and decorating .NET and Mono methods during runtime
https://www.patreon.com/pardeike
MIT License
5.15k stars 485 forks source link

PatchFunctions.UpdateWrapper NotImplementedException on MacOS #607

Closed AqlaSolutions closed 4 months ago

AqlaSolutions commented 4 months ago

This exception occurs when launching from Rider: image

We don't have this problem on Windows or inside asp.net linux docker containers.

The exception occurs even when launching without debugging.

The patch code:

[HarmonyPatch(typeof(ClassName), "MethodName")]
[UsedImplicitly]
static class Patch
{
    [UsedImplicitly]
    [HarmonyPrefix]
    static bool Prefix(ClassName __instance)
    {
        if (__instance.Property is AnotherClass { AnotherProperty: true })
            return false;
        return true;
    }
}

Runtime environment (please complete the following information):

Our host application is ASP.NET Web Api app.

pardeike commented 4 months ago

Does this happen with the non-thin version too? If so, you are not supplying the correct dependencies.

pardeike commented 4 months ago

For reference, I am as I type, using Harmony 2.3.3 on my MacBook Air M2 with RimWorld just fine.

AqlaSolutions commented 4 months ago

The non-thin version doesn't work for us (exception when debugging from Rider on Windows). https://github.com/pardeike/Harmony/issues/585

What are the dependencies? The docs mentions that one may need to add them but what exactly to add?

pardeike commented 4 months ago

If you use an IDE (like Visual Studio, don’t know about Rider, never used it) it will automatically download the dependencies. The nuget package also lists the dependencies as does a decompiler like dnSpy

AqlaSolutions commented 4 months ago

These are nuget package dependencies:

<dependencies>
  <group targetFramework=".NETFramework3.5">
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
  </group>
  <group targetFramework=".NETFramework4.5.2">
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
  </group>
  <group targetFramework=".NETFramework4.7.2">
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
  </group>
  <group targetFramework=".NETFramework4.8">
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
  </group>
  <group targetFramework=".NETCoreApp3.0">
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
  </group>
  <group targetFramework=".NETCoreApp3.1">
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
  </group>
  <group targetFramework="net5.0">
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
    <dependency id="System.Text.Json" version="5.0.2" exclude="Build,Analyzers" />
  </group>
  <group targetFramework="net6.0">
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
    <dependency id="System.Text.Json" version="6.0.9" exclude="Build,Analyzers" />
  </group>
  <group targetFramework="net7.0">
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
    <dependency id="System.Text.Json" version="7.0.4" exclude="Build,Analyzers" />
  </group>
  <group targetFramework="net8.0">
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
    <dependency id="System.Text.Json" version="8.0.1" exclude="Build,Analyzers" />
  </group>
  <group targetFramework=".NETStandard2.0">
    <dependency id="Lib.Harmony.Ref" version="2.3.3" exclude="Build,Analyzers" />
    <dependency id="MonoMod.Core" version="1.1.0" exclude="Build,Analyzers" />
  </group>
</dependencies>

The assembly explorer shows more: image

So for .NET 7 I need to manually add the nuget packages System.Text.Json and MonoMod.Core, correct?

AqlaSolutions commented 4 months ago

We've found the cause of this exception. MonoMod doesn't support ARM architecture on MacOS. Is there something we can do to make it work there?

image image

pardeike commented 4 months ago

Contribute to the complex task of patching low level architecture in the MonoMod repository? Or simply use MacCatalyst for now?

AqlaSolutions commented 4 months ago

Excuse me, what do you mean by using MacCatalyst? I'm not a Mac user so I'm not familiar with MacCatalyst, how it can help?

Do you know any Harmony-like library that supports MacOS ARM?

pardeike commented 4 months ago

You missed my comment earlier: https://github.com/pardeike/Harmony/issues/607#issuecomment-2061115901

AqlaSolutions commented 4 months ago

Perhaps RimWorld works because Unity uses its own fork of dotnet runtime based on Mono and it may also use something like rosetta emulation. I haven't found a way to do it with dotnet.

pardeike commented 4 months ago

I close this issue as the real work has to be done in MonoMod (and is picking up a bit lately)