Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion Hardware.Info
Submodule Hardware.Info deleted from b0e99d
91 changes: 30 additions & 61 deletions Runtimes/NET/BepisLoader/BepisLoader.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;

namespace BepisLoader;

public class BepisLoader

Check warning on line 6 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BepisLoader'

Check warning on line 6 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BepisLoader'
{
internal static string resoDir = string.Empty;
internal static AssemblyLoadContext alc = null!;
static void Main(string[] args)
{
resoDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

Check warning on line 12 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 12 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
#if DEBUG
logPath = Path.Combine(resoDir, "BepisLoader.log");
File.WriteAllText(logPath, "BepisLoader started\n");
Expand All @@ -18,10 +17,10 @@

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");

Check warning on line 23 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'path1' in 'string Path.Combine(string path1, string path2)'.

Check warning on line 23 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'path1' in 'string Path.Combine(string path1, string path2)'.
var bepinArg = Array.IndexOf(args.Select(x => x?.ToLowerInvariant()).ToArray(), "--bepinex-target");
if (bepinArg != -1 && args.Length > bepinArg + 1)
{
Expand All @@ -31,12 +30,11 @@

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)]);

Check warning on line 36 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 36 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
m.Invoke(null, [resoDllPath, bepinPath, alc]);

Check warning on line 37 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 37 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

// Find and load Resonite
var resoAsm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name == "Renderite.Host");
Expand Down Expand Up @@ -71,8 +69,6 @@
return found;
}

if (assemblyName.Name == "System.Management") return null;

var targetPath = Path.Combine(resoDir, assemblyName.Name + ".dll");
if (File.Exists(targetPath))
{
Expand All @@ -83,81 +79,54 @@
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<string> potentialPaths = [unmanagedDllName, Path.Combine(nativeLibs, GetUnmanagedLibraryName(unmanagedDllName))];
if (unmanagedDllName.EndsWith("steam_api64.so")) potentialPaths = ((IEnumerable<string>)["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
private static string logPath;
private static object _lock = new object();
#endif
public static void Log(string message)

Check warning on line 129 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BepisLoader.Log(string)'

Check warning on line 129 in Runtimes/NET/BepisLoader/BepisLoader.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BepisLoader.Log(string)'
{
#if DEBUG
lock (_lock)
Expand Down
10 changes: 0 additions & 10 deletions Runtimes/NET/BepisLoader/BepisLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@
<DebugType>embedded</DebugType>
</PropertyGroup>

<ItemGroup>
<Compile Remove="..\..\..\Hardware.Info\**" />
<EmbeddedResource Remove="..\..\..\Hardware.Info\**" />
<None Remove="..\..\..\Hardware.Info\**" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Hardware.Info\Hardware.Info\Hardware.Info.csproj" />
</ItemGroup>

<Target Name="RemoveExeAfterBuild" AfterTargets="Build">
<Delete Condition="Exists('$(TargetDir)$(AssemblyName).exe')" Files="$(TargetDir)$(AssemblyName).exe" />
<Delete Condition="Exists('$(TargetDir)$(AssemblyName)')" Files="$(TargetDir)$(AssemblyName)" />
Expand Down
Loading