Skip to content

Commit

Permalink
Remove duplication and Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
RinLovesYou committed Dec 28, 2023
1 parent 17d4580 commit 0f99afd
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<OutputPath>$(SolutionDir)Output\$(Configuration)\MelonLoader\</OutputPath>
<DocumentationFile></DocumentationFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableDynamicLoading>true</EnableDynamicLoading>
<DebugType>embedded</DebugType>
<PublishTrimmed>false</PublishTrimmed>

Expand Down
2 changes: 1 addition & 1 deletion MelonLoader/MelonLoader.NativeHost/HostExports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MelonLoader.NativeHost
{
unsafe struct HostExports
internal unsafe struct HostExports
{
internal delegate* unmanaged<void*, void*, void*> HookAttach;
internal delegate* unmanaged<void*, void> HookDetach;
Expand Down
4 changes: 2 additions & 2 deletions MelonLoader/MelonLoader.NativeHost/HostImports.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace MelonLoader.NativeHost
{
unsafe struct HostImports
internal unsafe struct HostImports
{
public delegate* unmanaged[Stdcall]<IntPtr, IntPtr, IntPtr, void**, void> LoadAssemblyAndGetPtr;
public delegate* unmanaged[Stdcall]<IntPtr, IntPtr, IntPtr, out IntPtr, void> LoadAssemblyAndGetPtr;
public delegate* unmanaged[Stdcall]<IntPtr, int, int> LoadAssemblyFromByteArray;
public delegate* unmanaged[Stdcall]<int, IntPtr, int> GetTypeByName;
public delegate* unmanaged[Stdcall]<int, int, IntPtr*, IntPtr*, int> ConstructType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<DebugType>embedded</DebugType>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,11 +17,6 @@
<ExcludeAssets>runtime</ExcludeAssets>
<CopyLocalSatelliteAssemblies>False</CopyLocalSatelliteAssemblies>
</ProjectReference>
<ProjectReference Include="..\Modules\CoreCLR\CoreCLR.Bootstrap\CoreCLR.Bootstrap.csproj">
<Private>False</Private>
<ExcludeAssets>runtime</ExcludeAssets>
<CopyLocalSatelliteAssemblies>False</CopyLocalSatelliteAssemblies>
</ProjectReference>
</ItemGroup>

</Project>
15 changes: 1 addition & 14 deletions MelonLoader/MelonLoader.NativeHost/MelonLoaderInvoker.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
using MelonLoader;
using MelonLoader.Bootstrap;
using MelonLoader.CoreClr.Bootstrap.Fixes;

namespace MelonLoader.NativeHost
{
internal class MelonLoaderInvoker
{
internal static unsafe void Initialize(bool firstRun)
{
BootstrapInterop.HookAttach = HookAttach;
BootstrapInterop.HookAttach = NativeEntryPoint.Exports.HookAttach;
BootstrapInterop.HookDetach = NativeEntryPoint.Exports.HookDetach;
BootstrapInterop.WriteLogToFile = NativeEntryPoint.Exports.WriteLogToFile;
if (firstRun)
Entrypoint.Entry();
}

private static unsafe void* HookAttach(void* target, void* detour)
{
IntPtr detourPtr = (IntPtr)detour;
//if (!CoreClrDelegateFixer.SanityCheckDetour(ref detourPtr))
// return (void*)0;

void* trampoline = NativeEntryPoint.Exports.HookAttach(target, (void*)detourPtr);
NativeStackWalk.RegisterHookAddr((ulong)detour, $"Requested detour of 0x{(IntPtr)target:X}");
return trampoline;
}
}
}
5 changes: 4 additions & 1 deletion MelonLoader/MelonLoader.NativeHost/NativeEntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public static class NativeEntryPoint
static unsafe void LoadStage1(HostImports* imports)
{
Console.WriteLine("[NewEntryPoint] Passing ptr to LoadAssemblyAndGetFuncPtr back to host...");
imports->LoadAssemblyAndGetPtr = &Stereo.LoadAssemblyAndGetFuncPtr;

//hack because [UnmanagedCallersOnly] doesn't support out parameters
delegate* unmanaged[Stdcall]<IntPtr, IntPtr, IntPtr, void**, void> ptr = &Stereo.LoadAssemblyAndGetFuncPtr;
imports->LoadAssemblyAndGetPtr = (delegate* unmanaged[Stdcall]<IntPtr, IntPtr, IntPtr, out IntPtr, void>)ptr;
imports->LoadAssemblyFromByteArray = &Stereo.LoadAssemblyFromByteArray;
imports->GetTypeByName = &Stereo.GetTypeByName;
imports->ConstructType = &Stereo.ConstructType;
Expand Down
3 changes: 3 additions & 0 deletions MelonLoader/MelonLoader.NativeHost/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("MelonLoader.CoreCLR.Bootstrap")]
11 changes: 3 additions & 8 deletions MelonLoader/MelonLoader.Shared/BootstrapInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,10 @@ public static IntPtr NativeLoadLib(string name)
=> NativeLibrary.Load(name);
public static IntPtr NativeGetExport(IntPtr handle, string name)
=> NativeLibrary.GetExport(handle, name);

public delegate void* dHookAttach(void* target, void* detour);
public static dHookAttach HookAttach;

public static delegate* unmanaged<void*, void*, void*> HookAttach;
public static IntPtr NativeHookAttach(IntPtr target, IntPtr detour)
{
var trampoline = new IntPtr(HookAttach(target.ToPointer(), detour.ToPointer()));
//MelonLogger.Msg(trampoline.ToString("X"));
return trampoline;
}
=> (IntPtr)HookAttach(target.ToPointer(), detour.ToPointer());

public static delegate* unmanaged<void*, void> HookDetach;
public static void NativeHookDetach(IntPtr target)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="3.1.456101" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\MelonLoader.NativeHost\MelonLoader.NativeHost.csproj" />
<ProjectReference Include="..\..\..\MelonLoader.Shared\MelonLoader.Shared.csproj">
<Private>False</Private>
<ExcludeAssets>runtime</ExcludeAssets>
Expand Down
9 changes: 5 additions & 4 deletions MelonLoader/Modules/CoreCLR/CoreCLR.Bootstrap/DotnetLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using MelonLoader.NativeHost;
using MelonLoader.NativeUtils;
using MelonLoader.Utils;

Expand Down Expand Up @@ -117,17 +118,17 @@ private static unsafe void ReloadIntoDefaultAlc()

var exports = new HostExports
{
_writeLogFile = (IntPtr)BootstrapInterop.WriteLogToFile,
_hookAttach = (IntPtr)BootstrapInterop.HookDetach,
_hookDetach = (IntPtr)BootstrapInterop.HookDetach
WriteLogToFile = BootstrapInterop.WriteLogToFile,
HookAttach = BootstrapInterop.HookAttach,
HookDetach = BootstrapInterop.HookDetach
};

MelonDebug.Msg("[Dotnet] Invoking LoadStage2");
var loadStage2Delegate = Marshal.GetDelegateForFunctionPointer<DLoadStage2>(loadStage2);
loadStage2Delegate(&imports, &exports);

MelonDebug.Msg("[Dotnet] Invoking Initialize");
imports.Initialize(StereoBool.False);
imports.Initialize(Stereo.StereoBool.False);

var asmBuffer = File.ReadAllBytes(sharedPath);
int asmHandle = 0;
Expand Down
43 changes: 0 additions & 43 deletions MelonLoader/Modules/CoreCLR/CoreCLR.Shared/HostFxrLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,6 @@ public enum hostfxr_delegate_type
hdt_load_assembly_bytes,
}

public unsafe struct HostImports
{
public delegate* unmanaged[Stdcall]<IntPtr, IntPtr, IntPtr, out IntPtr, void> LoadAssemblyAndGetPtr;
public delegate* unmanaged[Stdcall]<IntPtr, int, int> LoadAssemblyFromByteArray;
public delegate* unmanaged[Stdcall]<int, IntPtr, int> GetTypeByName;
public delegate* unmanaged[Stdcall]<int, int, IntPtr*, IntPtr*, int> ConstructType;
public delegate* unmanaged[Stdcall]<int, IntPtr, int, int, IntPtr*, IntPtr*, int> InvokeMethod;

public delegate* unmanaged[Stdcall]<StereoBool, void> Initialize;
}

public enum StereoBool : byte
{
False = 0,
True = 1
}

public struct HostExports
{
public IntPtr _hookAttach;
public IntPtr _hookDetach;
public IntPtr _writeLogFile;
}

public class HostFxrLibrary
{
#region Private Members
Expand All @@ -64,13 +40,6 @@ public static HostFxrLibrary Instance
}
}

public struct hostfxr_initialize_parameters
{
public UIntPtr size;
public IntPtr host_path;
public IntPtr dotnet_root;
};

#endregion

#region Exports
Expand All @@ -79,17 +48,5 @@ public struct hostfxr_initialize_parameters
public delegate int d_hostfxr_get_runtime_delegate(IntPtr host_context_handle, hostfxr_delegate_type type, out IntPtr delegate_handle);
public d_hostfxr_get_runtime_delegate hostfxr_get_runtime_delegate;

[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate int d_hostfxr_initialize_for_runtime_config(IntPtr runtime_config_path, IntPtr init_params, out IntPtr host_context_handle);
public d_hostfxr_initialize_for_runtime_config hostfxr_initialize_for_runtime_config;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void d_hostfxr_close(IntPtr host_context_handle);
public d_hostfxr_close hostfxr_close;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public unsafe delegate int d_hostfxr_initialize_for_dotnet_command_line(int argc, IntPtr argv, hostfxr_initialize_parameters* parameters, out IntPtr host_context_handle);
public d_hostfxr_initialize_for_dotnet_command_line hostfxr_initialize_for_dotnet_command_line;

#endregion
}
4 changes: 2 additions & 2 deletions Rust/MelonBootstrap/src/dotnet/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn start() -> Result<(), Box<dyn Error>> {
.join("MelonLoader")
.join("net6");

let config_path = runtime_dir.join("MelonLoader.Bootstrap.runtimeconfig.json");
let config_path = runtime_dir.join("MelonLoader.NativeHost.runtimeconfig.json");
if !config_path.exists() {
return Err("MelonLoader.Bootstrap.runtimeconfig.json does not exist!".into());
}
Expand All @@ -68,7 +68,7 @@ pub fn start() -> Result<(), Box<dyn Error>> {
let bootstrap_path = runtime_dir.join("MelonLoader.NativeHost.dll");

if !bootstrap_path.exists() {
return Err("MelonLoader.Bootstrap.dll does not exist!".into());
return Err("MelonLoader.NativeHost.dll does not exist!".into());
}

let loader =
Expand Down

0 comments on commit 0f99afd

Please sign in to comment.