Skip to content

Commit

Permalink
Attempt to fix EOS Module Crashes #764
Browse files Browse the repository at this point in the history
  • Loading branch information
HerpDerpinstine committed Oct 11, 2024
1 parent 32849ca commit 15da0b5
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions Dependencies/CompatibilityLayers/EOS/Module.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using MelonLoader.Modules;
using MelonLoader.Modules;
using MelonLoader.NativeUtils;
using System;
using System.Runtime.InteropServices;

namespace MelonLoader.CompatibilityLayers
{
internal class EOS_Module : MelonModule
{
private delegate IntPtr dLoadLibrary(IntPtr path);
private dLoadLibrary _loadLibrary;
private delegate IntPtr LoadLibraryDetour(IntPtr path);
private static NativeHook<LoadLibraryDetour> _hookWin;

public unsafe override void OnInitialize()
~EOS_Module()
=> Detach();

public override void OnInitialize()
{
var platform = Environment.OSVersion.Platform;
switch (platform)
Expand All @@ -18,10 +22,17 @@ public unsafe override void OnInitialize()
case PlatformID.Win32Windows:
case PlatformID.Win32NT:
case PlatformID.WinCE:
IntPtr trampoline = Marshal.GetFunctionPointerForDelegate((dLoadLibrary)LoadLibrary);
IntPtr detour = Marshal.GetFunctionPointerForDelegate((dLoadLibrary)DetourWin);
MelonUtils.NativeHookAttach((IntPtr)(&trampoline), detour);
_loadLibrary = (dLoadLibrary)Marshal.GetDelegateForFunctionPointer(trampoline, typeof(dLoadLibrary));
NativeLibrary lib = NativeLibrary.Load("kernel32");
if (lib != null)
{
IntPtr loadLibraryWPtr = lib.GetExport("LoadLibraryW");
if (loadLibraryWPtr != IntPtr.Zero)
{
IntPtr detourPtr = Marshal.GetFunctionPointerForDelegate((LoadLibraryDetour)DetourWin);
_hookWin = new NativeHook<LoadLibraryDetour>(loadLibraryWPtr, detourPtr);
_hookWin.Attach();
}
}
break;

case PlatformID.Unix:
Expand All @@ -36,39 +47,29 @@ public unsafe override void OnInitialize()
}
}

private unsafe IntPtr DetourWin(IntPtr path)
private static IntPtr DetourWin(IntPtr path)
{
if (path == IntPtr.Zero)
return IntPtr.Zero;
return _hookWin.Trampoline(path);

var pathString = Marshal.PtrToStringUni(path);
if (string.IsNullOrEmpty(pathString))
return IntPtr.Zero;
return _hookWin.Trampoline(path);

if (pathString.EndsWith("EOSOVH-Win64-Shipping.dll")
|| pathString.EndsWith("EOSOVH-Win32-Shipping.dll"))
{
IntPtr trampoline = Marshal.GetFunctionPointerForDelegate((dLoadLibrary)LoadLibrary);
IntPtr detour = Marshal.GetFunctionPointerForDelegate((dLoadLibrary)DetourWin);
MelonUtils.NativeHookDetach((IntPtr)(&trampoline), detour);
Detach();
return IntPtr.Zero;
}

return _loadLibrary(path);
return _hookWin.Trampoline(path);
}

unsafe ~EOS_Module()
private static void Detach()
{
if (_loadLibrary != null)
{
IntPtr trampoline = Marshal.GetFunctionPointerForDelegate((dLoadLibrary)LoadLibrary);
IntPtr detour = Marshal.GetFunctionPointerForDelegate((dLoadLibrary)DetourWin);
MelonUtils.NativeHookDetach((IntPtr)(&trampoline), detour);
_loadLibrary = null;
}
_hookWin?.Detach();
_hookWin = null;
}

[DllImport("kernel32")]
private static extern IntPtr LoadLibrary(IntPtr lpLibFileName);
}
}
}

0 comments on commit 15da0b5

Please sign in to comment.