diff --git a/.gitmodules b/.gitmodules index 6079211f..362a8e01 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,7 +2,3 @@ path = HarmonyX url = https://github.com/ResoniteModding/HarmonyX.git branch = patchy -[submodule "Hardware.Info"] - path = Hardware.Info - url = https://github.com/ResoniteModding/Hardware.Info.git - branch = patchy diff --git a/Hardware.Info b/Hardware.Info deleted file mode 160000 index b0e99d8c..00000000 --- a/Hardware.Info +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b0e99d8c1eb2a5eb1e55efe144d38f47d52ec1c4 diff --git a/Runtimes/NET/BepisLoader/BepisLoader.cs b/Runtimes/NET/BepisLoader/BepisLoader.cs index a243b9d7..fa1e7698 100644 --- a/Runtimes/NET/BepisLoader/BepisLoader.cs +++ b/Runtimes/NET/BepisLoader/BepisLoader.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.InteropServices; using System.Runtime.Loader; namespace BepisLoader; @@ -18,7 +17,7 @@ static void Main(string[] args) alc = new BepisLoadContext(); - // TODO: removing this breaks stuff, idk why + // The game runs in the Default AssemblyLoadContext, not our custom BepisLoadContext. When code in the Default ALC requests a dependency, BepisLoadContext.Load() is never called, only this global AssemblyResolve event fires as a fallback. AppDomain.CurrentDomain.AssemblyResolve += ResolveGameDll; var bepinPath = Path.Combine(resoDir, "BepInEx"); @@ -31,8 +30,7 @@ static void Main(string[] args) var asm = alc.LoadFromAssemblyPath(Path.Combine(bepinPath, "core", "BepInEx.NET.CoreCLR.dll")); - var resoDllPath = Path.Combine(resoDir, "Renderite.Host.dll"); - if (!File.Exists(resoDllPath)) resoDllPath = Path.Combine(resoDir, "Resonite.dll"); + var resoDllPath = GetResoDllPath(); var t = asm.GetType("StartupHook"); var m = t.GetMethod("Initialize", BindingFlags.Public | BindingFlags.Static, [typeof(string), typeof(string), typeof(AssemblyLoadContext)]); @@ -71,8 +69,6 @@ static void Main(string[] args) return found; } - if (assemblyName.Name == "System.Management") return null; - var targetPath = Path.Combine(resoDir, assemblyName.Name + ".dll"); if (File.Exists(targetPath)) { @@ -83,74 +79,47 @@ static void Main(string[] args) return null; } + private static string GetResoDllPath() + { + var path = Path.Combine(resoDir, "Renderite.Host.dll"); + return File.Exists(path) ? path : Path.Combine(resoDir, "Resonite.dll"); + } + private class BepisLoadContext : AssemblyLoadContext { + private readonly AssemblyDependencyResolver? _resolver; + + public BepisLoadContext() : base(isCollectible: false) + { + var resoDllPath = GetResoDllPath(); + + if (File.Exists(resoDllPath)) + _resolver = new AssemblyDependencyResolver(resoDllPath); + } + protected override Assembly? Load(AssemblyName assemblyName) { - return ResolveInternal(assemblyName); + // Check already-loaded assemblies first + var found = AppDomain.CurrentDomain.GetAssemblies() + .FirstOrDefault(x => x.GetName().Name == assemblyName.Name); + if (found != null) return found; + + // Use deps.json resolution + string? assemblyPath = _resolver?.ResolveAssemblyToPath(assemblyName); + return assemblyPath != null ? LoadFromAssemblyPath(assemblyPath) : null; } protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) { - var rid = GetRuntimeIdentifier(); - - var nativeLibs = Path.Join(resoDir, "runtimes", rid, "native"); - IEnumerable potentialPaths = [unmanagedDllName, Path.Combine(nativeLibs, GetUnmanagedLibraryName(unmanagedDllName))]; - if (unmanagedDllName.EndsWith("steam_api64.so")) potentialPaths = ((IEnumerable)["libsteam_api.so"]).Concat(potentialPaths); - Log("NativeLib " + unmanagedDllName); - foreach (var path in potentialPaths) + string? libraryPath = _resolver?.ResolveUnmanagedDllToPath(unmanagedDllName); + if (libraryPath != null) { - Log(" Testing: " + path); - if (File.Exists(path)) - { - Log(" Exists! " + path); - var dll = LoadUnmanagedDllFromPath(path); - if (dll != IntPtr.Zero) - { - Log(" Loaded! " + path); - return dll; - } - } + Log(" Resolved: " + libraryPath); + return LoadUnmanagedDllFromPath(libraryPath); } - return IntPtr.Zero; } - - - private static string GetRuntimeIdentifier() - { - string os; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - os = "win"; - else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - os = "osx"; - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - os = "linux"; - else - throw new PlatformNotSupportedException(); - - string arch = RuntimeInformation.OSArchitecture switch - { - Architecture.X86 => "-x86", - Architecture.X64 => "-x64", - Architecture.Arm64 => "-arm64", - _ => "" - }; - - return $"{os}{arch}"; - } - private static string GetUnmanagedLibraryName(string name) - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - return $"{name}.dll"; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - return $"lib{name}.so"; - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - return $"lib{name}.dylib"; - - throw new PlatformNotSupportedException(); - } } #if DEBUG diff --git a/Runtimes/NET/BepisLoader/BepisLoader.csproj b/Runtimes/NET/BepisLoader/BepisLoader.csproj index a2617df8..e5a7da5a 100644 --- a/Runtimes/NET/BepisLoader/BepisLoader.csproj +++ b/Runtimes/NET/BepisLoader/BepisLoader.csproj @@ -12,16 +12,6 @@ embedded - - - - - - - - - -