-
-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24f65a6
commit 1a13d80
Showing
9 changed files
with
239 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
Dependencies/Modules/Runtimes/Il2Cpp/Runtime.Il2Cpp.Shared/Il2CppLibrary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
Dependencies/Modules/Runtimes/Il2Cpp/Runtime.Il2Cpp/Il2CppLoader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Dependencies/Modules/Runtimes/Mono/Runtime.Mono.Shared/MonoLibrary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace MelonLoader.Runtime.Mono | ||
{ | ||
public class MonoLibrary | ||
{ | ||
#region Mono Method | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] | ||
public unsafe delegate IntPtr d_mono_method_get_name(IntPtr method); | ||
public d_mono_method_get_name mono_method_get_name; | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] | ||
public unsafe delegate IntPtr d_mono_runtime_invoke(IntPtr method, IntPtr obj, void** param, ref IntPtr exc); | ||
public d_mono_runtime_invoke mono_runtime_invoke; | ||
|
||
#endregion | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Dependencies/Modules/Runtimes/Mono/Runtime.Mono.Shared/Runtime.Mono.Shared.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<RootNamespace>MelonLoader.Runtime.Mono</RootNamespace> | ||
<TargetFrameworks>net35;net6</TargetFrameworks> | ||
<LangVersion>Latest</LangVersion> | ||
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath> | ||
<OutputPath>$(MLOutDir)/MelonLoader/Dependencies/Runtimes/Mono/</OutputPath> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<DebugType>embedded</DebugType> | ||
<AssemblyName>MelonLoader.Runtime.Mono.Shared</AssemblyName> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)\MelonLoader\MelonLoader.csproj" ExcludeAssets="all" Private="False"> | ||
<CopyLocalSatelliteAssemblies>False</CopyLocalSatelliteAssemblies> | ||
</ProjectReference> | ||
</ItemGroup> | ||
</Project> |
125 changes: 125 additions & 0 deletions
125
Dependencies/Modules/Runtimes/Mono/Runtime.Mono/MonoLoader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using MelonLoader.Modules; | ||
using MelonLoader.NativeUtils; | ||
#pragma warning disable 0649 | ||
|
||
namespace MelonLoader.Runtime.Mono | ||
{ | ||
public unsafe static class MonoLoader | ||
{ | ||
#region Private Members | ||
|
||
private static MonoLibrary _lib; | ||
|
||
private static MelonNativeDetour<MonoLibrary.d_mono_runtime_invoke> mono_runtime_invoke_detour; | ||
|
||
#endregion | ||
|
||
#region Public Members | ||
|
||
public static MelonEngineModule EngineModule { get; private set; } | ||
public static MonoRuntimeInfo RuntimeInfo { get; private set; } | ||
|
||
#endregion | ||
|
||
#region Loader | ||
|
||
public static void Initialize(MelonEngineModule engineModule, | ||
MonoRuntimeInfo runtimeInfo) | ||
{ | ||
// Apply the information | ||
EngineModule = engineModule; | ||
RuntimeInfo = runtimeInfo; | ||
|
||
// Check if it found any Mono variant library | ||
if (RuntimeInfo == null | ||
|| string.IsNullOrEmpty(RuntimeInfo.LibPath)) | ||
{ | ||
MelonLogger.ThrowInternalFailure("Failed to find Mono Library!"); | ||
return; | ||
} | ||
|
||
// Load Library | ||
if (!LoadLib()) | ||
return; | ||
|
||
// Check Exports | ||
if (!CheckExports()) | ||
return; | ||
|
||
// Create mono_runtime_invoke Detour | ||
mono_runtime_invoke_detour = new(_lib.mono_runtime_invoke, h_mono_runtime_invoke, false); | ||
|
||
// Attach mono_runtime_invoke Detour | ||
MelonDebug.Msg($"Attaching mono_runtime_invoke Detour..."); | ||
mono_runtime_invoke_detour.Attach(); | ||
} | ||
|
||
private static bool LoadLib() | ||
{ | ||
// Load the Mono variant library | ||
MelonDebug.Msg($"Loading Mono Library..."); | ||
_lib = new MelonNativeLibrary<MonoLibrary>(MelonNativeLibrary.LoadLib(RuntimeInfo.LibPath)).Instance; | ||
|
||
// Check for Failure | ||
if (_lib == null) | ||
{ | ||
MelonLogger.ThrowInternalFailure($"Failed to load Il2Cpp Library from {RuntimeInfo.LibPath}!"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static bool CheckExports() | ||
{ | ||
Dictionary<string, Delegate> listOfExports = new(); | ||
|
||
listOfExports[nameof(_lib.mono_method_get_name)] = _lib.mono_method_get_name; | ||
listOfExports[nameof(_lib.mono_runtime_invoke)] = _lib.mono_runtime_invoke; | ||
|
||
foreach (var exportPair in listOfExports) | ||
{ | ||
if (exportPair.Value != null) | ||
continue; | ||
|
||
MelonLogger.ThrowInternalFailure($"Failed to find {exportPair.Key} Export in Il2Cpp Library!"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
#endregion | ||
|
||
#region Hooks | ||
|
||
private static IntPtr h_mono_runtime_invoke(IntPtr method, IntPtr obj, void** param, ref IntPtr exc) | ||
{ | ||
// Get Method Name | ||
string methodName = _lib.mono_method_get_name(method).ToAnsiString(); | ||
|
||
// Check for Trigger Method | ||
foreach (string triggerMethod in RuntimeInfo.TriggerMethods) | ||
{ | ||
if (!methodName.Contains(triggerMethod)) | ||
continue; | ||
|
||
// Detach mono_runtime_invoke Detour | ||
mono_runtime_invoke_detour.Detach(); | ||
|
||
// Initiate Stage2 | ||
EngineModule.Stage2(); | ||
|
||
// Return original Invoke without Trampoline | ||
return _lib.mono_runtime_invoke(method, obj, param, ref exc); | ||
} | ||
|
||
// Return original Invoke | ||
return mono_runtime_invoke_detour.Trampoline(method, obj, param, ref exc); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Dependencies/Modules/Runtimes/Mono/Runtime.Mono/MonoRuntimeInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using MelonLoader.Modules; | ||
|
||
namespace MelonLoader.Runtime.Mono | ||
{ | ||
public class MonoRuntimeInfo : MelonRuntimeInfo | ||
{ | ||
#region Public Members | ||
|
||
public string[] TriggerMethods { get; private set; } | ||
|
||
#endregion | ||
|
||
#region Constructors | ||
|
||
public MonoRuntimeInfo( | ||
string libPath, | ||
string[] triggerMethods) | ||
{ | ||
LibPath = libPath; | ||
TriggerMethods = triggerMethods; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Dependencies/Modules/Runtimes/Mono/Runtime.Mono/Runtime.Mono.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<RootNamespace>MelonLoader.Runtime.Mono</RootNamespace> | ||
<TargetFramework>net6</TargetFramework> | ||
<LangVersion>Latest</LangVersion> | ||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
<OutputPath>$(MLOutDir)/MelonLoader/Dependencies/Runtimes/Mono/</OutputPath> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<DebugType>embedded</DebugType> | ||
<AssemblyName>MelonLoader.Runtime.Mono</AssemblyName> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)\MelonLoader\MelonLoader.csproj" Private="false" ExcludeAssets="all"> | ||
<CopyLocalSatelliteAssemblies>False</CopyLocalSatelliteAssemblies> | ||
</ProjectReference> | ||
<ProjectReference Include="..\Runtime.Mono.Shared\Runtime.Mono.Shared.csproj" Private="false" ExcludeAssets="all"> | ||
<CopyLocalSatelliteAssemblies>False</CopyLocalSatelliteAssemblies> | ||
</ProjectReference> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters