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
NativeDetour for 32 bit Unity __thiscall functions doesn't work #165
Comments
Given the API usage, this looks like legacy RuntimeDetour. Legacy I'd suggest trying |
Yes, I tried with latest github release, which seems to be considered legacy, and BepInEx 5 lts version. I'm pretty sure there are methods without |
I've dirty patched BepInEx and Harmony to work with using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using Mono.Cecil;
using MonoMod.RuntimeDetour;
internal static class FixPluginTypesSerializationPatcher
{
public static IEnumerable<string> TargetDLLs { get; } = new string[0];
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate void AwakeFromLoadDelegate(nint _monoManager, int awakeMode);
private static NativeHook _hook;
public static void Patch(AssemblyDefinition ass)
{
}
public static void Initialize()
{
static bool IsUnityPlayer(ProcessModule p)
{
return p.ModuleName.ToLowerInvariant().Contains("unityplayer");
}
var proc = Process.GetCurrentProcess().Modules
.Cast<ProcessModule>()
.FirstOrDefault(IsUnityPlayer) ?? Process.GetCurrentProcess().MainModule;
var from = (IntPtr)(proc.BaseAddress.ToInt64() + 0x4C6D70);
_hook = new NativeHook(from, AwakeFromLoad);
}
private static void OnAwakeFromLoad(AwakeFromLoadDelegate orig, nint _monoManager, int awakeMode)
{
orig(_monoManager, awakeMode);
}
} I will try to investigate a bit more |
Description
Trying to make a NativeDetour for some Unity functions in a BepInEx patcher on Windows, it works for 64 bit Unity game, but not 32 bit.
The one that I currently have issues with is
MonoManager::AwakeFromLoad(MonoManager *this, AwakeFromLoadMode param_1)
. Instead of getting a pointer inthis
and3
inparam_1
I get3
inthis
and something inparam_1
that changes between Unity versions (probably whatever is on the stack at the moment). So even just calling the original functions results in a crash.In Ghydra I can see that the function is marked as
__thiscall
for 32 bit, but it's__cdecl
for 64 bit, which is where the difference and the issue is.From https://learn.microsoft.com/en-us/cpp/cpp/argument-passing-and-naming-conventions?view=msvc-170:
I'm not sure if that's mono being mono again or the way that MonoMod creates detour and trampoline.
Example
One of the test versions: Unity 2023.2.5f1 32bit
BepInEx 5.4.22
Let me know if you need more info.
The text was updated successfully, but these errors were encountered: