This repository was archived by the owner on Sep 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBepisLoader.cs
More file actions
163 lines (137 loc) · 5.38 KB
/
BepisLoader.cs
File metadata and controls
163 lines (137 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
namespace BepisLoader;
public class BepisLoader
{
internal static string resoDir = string.Empty;
internal static AssemblyLoadContext alc = null!;
static void Main(string[] args)
{
#if DEBUG
File.WriteAllText("BepisLoader.log", "BepisLoader started\n");
#endif
resoDir = Directory.GetCurrentDirectory();
alc = new BepisLoadContext();
// TODO: removing this breaks stuff, idk why
AppDomain.CurrentDomain.AssemblyResolve += ResolveGameDll;
var bepinPath = Path.Combine(resoDir, "BepInEx");
var bepinArg = Array.IndexOf(args.Select(x => x?.ToLowerInvariant()).ToArray(), "--bepinex-target");
if(bepinArg != -1 && args.Length > bepinArg + 1)
{
bepinPath = args[bepinArg + 1];
}
Log("Loading BepInEx from " + bepinPath);
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 t = asm.GetType("StartupHook");
var m = t.GetMethod("Initialize", BindingFlags.Public | BindingFlags.Static, [typeof(string), typeof(string), typeof(AssemblyLoadContext)]);
m.Invoke(null, [resoDllPath, bepinPath, alc]);
// Find and load Resonite
var resoAsm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name == "Resonite");
if (resoAsm == null)
{
resoAsm = alc.LoadFromAssemblyPath(resoDllPath);
}
try
{
var result = resoAsm.EntryPoint!.Invoke(null, [args]);
if (result is Task task) task.Wait();
}
catch (Exception e)
{
File.WriteAllLines("BepisCrash.log", [DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - Resonite crashed", e.ToString()]);
}
}
static Assembly? ResolveGameDll(object? sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
return ResolveInternal(assemblyName);
}
static Assembly? ResolveInternal(AssemblyName assemblyName)
{
var found = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name == assemblyName.Name);
if (found != null)
{
return found;
}
if (assemblyName.Name == "System.Management") return null;
var targetPath = Path.Combine(resoDir, assemblyName.Name + ".dll");
if (File.Exists(targetPath))
{
var asm = alc.LoadFromAssemblyPath(targetPath);
return asm;
}
return null;
}
private class BepisLoadContext : AssemblyLoadContext
{
protected override Assembly? Load(AssemblyName assemblyName)
{
return ResolveInternal(assemblyName);
}
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) {
Log(" Testing: " + path);
if (File.Exists(path)) {
Log(" Exists! " + path);
var dll = LoadUnmanagedDllFromPath(path);
if (dll != IntPtr.Zero) {
Log(" Loaded! " + path);
return dll;
}
}
}
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 object _lock = new object();
#endif
public static void Log(string message)
{
#if DEBUG
lock(_lock)
{
File.AppendAllLines("BepisLoader.log", [message]);
}
#endif
}
}