forked from BepInEx/BepInEx
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseChainloader.cs
More file actions
557 lines (469 loc) · 22.5 KB
/
Copy pathBaseChainloader.cs
File metadata and controls
557 lines (469 loc) · 22.5 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
using BepInEx.Configuration;
using BepInEx.Logging;
using Mono.Cecil;
using Mono.Cecil.Cil;
using MonoMod.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
namespace BepInEx.Bootstrap;
public abstract class BaseChainloader<TPlugin>
{
protected static readonly string CurrentAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
protected static readonly Version CurrentAssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
private static Regex allowedGuidRegex { get; } = new(@"^[a-zA-Z0-9\._\-]+$");
/// <summary>
/// Analyzes the given type definition and attempts to convert it to a valid <see cref="PluginInfo" />
/// </summary>
/// <param name="type">Type definition to analyze.</param>
/// <param name="assemblyLocation">The filepath of the assembly, to keep as metadata.</param>
/// <returns>If the type represent a valid plugin, returns a <see cref="PluginInfo" /> instance. Otherwise, return null.</returns>
public static PluginInfo ToPluginInfo(TypeDefinition type, string assemblyLocation)
{
if (type.IsInterface || type.IsAbstract)
return null;
try
{
if (!type.IsSubtypeOf(typeof(TPlugin)))
return null;
}
catch (AssemblyResolutionException)
{
// Can happen if this type inherits a type from an assembly that can't be found. Safe to assume it's not a plugin.
return null;
}
var metadata = BepInPlugin.FromCecilType(type);
// Perform checks that will prevent the plugin from being loaded in ALL cases
if (metadata == null)
{
Logger.Log(LogLevel.Warning, $"Skipping over type [{type.FullName}] as no metadata attribute is specified");
return null;
}
if (string.IsNullOrEmpty(metadata.GUID) || !allowedGuidRegex.IsMatch(metadata.GUID))
{
Logger.Log(LogLevel.Warning,
$"Skipping type [{type.FullName}] because its GUID [{metadata.GUID}] is of an illegal format.");
return null;
}
if (metadata.Version == null)
{
Logger.Log(LogLevel.Warning, $"Skipping type [{type.FullName}] because its version is invalid.");
return null;
}
if (metadata.Name == null)
{
Logger.Log(LogLevel.Warning, $"Skipping type [{type.FullName}] because its name is null.");
return null;
}
var filters = BepInProcess.FromCecilType(type);
var dependencies = BepInDependency.FromCecilType(type);
var incompatibilities = BepInIncompatibility.FromCecilType(type);
var bepinVersion =
type.Module.AssemblyReferences.FirstOrDefault(reference => reference.Name == "BepInEx.Core")?.Version ??
new Version();
return new PluginInfo
{
Metadata = metadata,
Processes = filters,
Dependencies = dependencies,
Incompatibilities = incompatibilities,
TypeName = type.FullName,
TargettedBepInExVersion = bepinVersion,
Location = assemblyLocation
};
}
protected static bool HasBepinPlugins(AssemblyDefinition ass)
{
if (ass.MainModule.AssemblyReferences.All(r => r.Name != CurrentAssemblyName))
return false;
if (ass.MainModule.GetTypeReferences().All(r => r.FullName != typeof(BepInPlugin).FullName))
return false;
return true;
}
protected static bool PluginTargetsWrongBepin(PluginInfo pluginInfo)
{
var pluginTarget = pluginInfo.TargettedBepInExVersion;
// X.X.X.x - compare normally. x.x.x.X - nightly build number, ignore
if (pluginTarget.Major != CurrentAssemblyVersion.Major) return true;
if (pluginTarget.Minor > CurrentAssemblyVersion.Minor) return true;
if (pluginTarget.Minor < CurrentAssemblyVersion.Minor) return false;
return pluginTarget.Build > CurrentAssemblyVersion.Build;
}
#region Contract
protected virtual string ConsoleTitle => $"BepInEx {Paths.BepInExVersion} - {Paths.ProcessName}";
private bool _initialized;
/// <summary>
/// List of all <see cref="PluginInfo" /> instances loaded via the chainloader.
/// </summary>
public Dictionary<string, PluginInfo> Plugins { get; } = new();
/// <summary>
/// Collection of error chainloader messages that occured during plugin loading.
/// Contains information about what certain plugins were not loaded.
/// </summary>
public List<string> DependencyErrors { get; } = new();
/// <summary>
/// Occurs after a plugin is loaded.
/// </summary>
public event Action<PluginInfo> PluginLoaded;
/// <summary>
/// Occurs after all plugins are loaded.
/// </summary>
public event Action Finished;
public virtual void Initialize(string gameExePath = null)
{
if (_initialized)
throw new InvalidOperationException("Chainloader cannot be initialized multiple times");
// Set vitals
if (gameExePath != null)
// Checking for null allows a more advanced initialization workflow, where the Paths class has been initialized before calling Chainloader.Initialize
// This is used by Preloader to use environment variables, for example
Paths.SetExecutablePath(gameExePath);
InitializeLoggers();
if (!Directory.Exists(Paths.PluginPath))
Directory.CreateDirectory(Paths.PluginPath);
if (!Directory.Exists(Paths.PatcherPluginPath))
Directory.CreateDirectory(Paths.PatcherPluginPath);
_initialized = true;
Logger.Log(LogLevel.Message, "Chainloader initialized");
}
protected virtual void InitializeLoggers()
{
if (ConsoleManager.ConsoleEnabled && !ConsoleManager.ConsoleActive)
ConsoleManager.CreateConsole();
if (ConsoleManager.ConsoleActive)
{
if (!Logger.Listeners.Any(x => x is ConsoleLogListener))
Logger.Listeners.Add(new ConsoleLogListener());
ConsoleManager.SetConsoleTitle(ConsoleTitle);
}
if (ConfigDiskLogging.Value)
Logger.Listeners.Add(new DiskLogListener("LogOutput.log", ConfigDiskLoggingDisplayedLevel.Value,
ConfigDiskAppend.Value, ConfigDiskLoggingInstantFlushing.Value,
ConfigDiskLoggingFileLimit.Value));
if (!TraceLogSource.IsListening)
Logger.Sources.Add(TraceLogSource.CreateSource());
if (!Logger.Sources.Any(x => x is HarmonyLogSource))
Logger.Sources.Add(new HarmonyLogSource());
}
/// <summary>
/// Discovers all plugins in the plugin directory without loading them.
/// </summary>
/// <remarks>
/// This is useful for discovering BepInEx plugin metadata.
/// </remarks>
/// <param name="path">Path from which to search the plugins.</param>
/// <param name="cacheName">Cache name to use. If null, results are not cached.</param>
/// <returns>List of discovered plugins and their metadata.</returns>
protected IList<PluginInfo> DiscoverPluginsFrom(string path, string cacheName = "chainloader")
{
var pluginsToLoad =
TypeLoader.FindPluginTypes(path, ToPluginInfo, HasBepinPlugins, cacheName);
return pluginsToLoad.SelectMany(p => p.Value).ToList();
}
/// <summary>
/// Discovers plugins to load.
/// </summary>
/// <returns>List of plugins to be loaded.</returns>
protected virtual IList<PluginInfo> DiscoverPlugins()
{
return DiscoverPluginsFrom(Paths.PluginPath);
}
/// <summary>
/// Preprocess the plugins and modify the load order.
/// </summary>
/// <remarks>Some plugins may be skipped if they cannot be loaded (wrong metadata, etc).</remarks>
/// <param name="plugins">Plugins to process.</param>
/// <returns>List of plugins to load in the correct load order.</returns>
protected virtual IList<PluginInfo> ModifyLoadOrder(IList<PluginInfo> plugins)
{
// We use a sorted dictionary to ensure consistent load order
var dependencyDict =
new SortedDictionary<string, IEnumerable<string>>(StringComparer.InvariantCultureIgnoreCase);
var pluginsByGuid = new Dictionary<string, PluginInfo>();
foreach (var pluginInfoGroup in plugins.GroupBy(info => info.Metadata.GUID))
{
if (Plugins.TryGetValue(pluginInfoGroup.Key, out var loadedPlugin))
{
Logger.Log(LogLevel.Warning,
$"Skipping [{pluginInfoGroup.Key}] because a plugin with a similar GUID ([{loadedPlugin}]) has been already loaded.");
continue;
}
PluginInfo loadedVersion = null;
foreach (var pluginInfo in pluginInfoGroup.OrderByDescending(x => x.Metadata.Version))
{
if (loadedVersion != null)
{
Logger.Log(LogLevel.Warning,
$"Skipping [{pluginInfo}] because a newer version exists ({loadedVersion})");
continue;
}
// Perform checks that will prevent loading plugins in this run
var filters = pluginInfo.Processes.ToList();
var invalidProcessName = filters.Count != 0 &&
filters.All(x => !string.Equals(x.ProcessName.Replace(".exe", ""),
Paths.ProcessName,
StringComparison
.InvariantCultureIgnoreCase));
if (Paths.ProcessName != "libil2cpp" && invalidProcessName)
{
Logger.Log(LogLevel.Warning,
$"Skipping [{pluginInfo}] because of process filters ({string.Join(", ", pluginInfo.Processes.Select(p => p.ProcessName).ToArray())})");
continue;
}
loadedVersion = pluginInfo;
dependencyDict[pluginInfo.Metadata.GUID] = pluginInfo.Dependencies.Select(d => d.DependencyGUID);
pluginsByGuid[pluginInfo.Metadata.GUID] = pluginInfo;
}
}
foreach (var pluginInfo in pluginsByGuid.Values.ToList())
if (pluginInfo.Incompatibilities.Any(incompatibility =>
pluginsByGuid.ContainsKey(incompatibility.IncompatibilityGUID)
|| Plugins.ContainsKey(incompatibility.IncompatibilityGUID))
)
{
pluginsByGuid.Remove(pluginInfo.Metadata.GUID);
dependencyDict.Remove(pluginInfo.Metadata.GUID);
var incompatiblePluginsNew = pluginInfo.Incompatibilities.Select(x => x.IncompatibilityGUID)
.Where(x => pluginsByGuid.ContainsKey(x));
var incompatiblePluginsExisting = pluginInfo.Incompatibilities.Select(x => x.IncompatibilityGUID)
.Where(x => Plugins.ContainsKey(x));
var incompatiblePlugins = incompatiblePluginsNew.Concat(incompatiblePluginsExisting).ToArray();
var message =
$@"Could not load [{pluginInfo}] because it is incompatible with: {string.Join(", ", incompatiblePlugins)}";
DependencyErrors.Add(message);
Logger.Log(LogLevel.Error, message);
}
else if (PluginTargetsWrongBepin(pluginInfo))
{
var message =
$@"Plugin [{pluginInfo}] targets a wrong version of BepInEx ({pluginInfo.TargettedBepInExVersion}) and might not work until you update";
DependencyErrors.Add(message);
Logger.Log(LogLevel.Warning, message);
}
// We don't add already loaded plugins to the dependency graph as they are already loaded
var emptyDependencies = new string[0];
// Sort plugins by their dependencies.
// Give missing dependencies no dependencies of its own, which will cause missing plugins to be first in the resulting list.
var sortedPlugins = Utility.TopologicalSort(dependencyDict.Keys,
x =>
dependencyDict.TryGetValue(x, out var deps)
? deps
: emptyDependencies).ToList();
return sortedPlugins.Where(pluginsByGuid.ContainsKey).Select(x => pluginsByGuid[x]).ToList();
}
/// <summary>
/// Run the chainloader and load all plugins from the plugins folder.
/// </summary>
public virtual void Execute()
{
try
{
var plugins = DiscoverPlugins();
Logger.Log(LogLevel.Info, $"{plugins.Count} plugin{(plugins.Count == 1 ? "" : "s")} to load");
LoadPlugins(plugins);
Finish();
}
catch (Exception ex)
{
try
{
ConsoleManager.CreateConsole();
}
catch { }
Logger.Log(LogLevel.Error, $"Error occurred loading plugins: {ex}");
}
Logger.Log(LogLevel.Message, "Chainloader startup complete");
}
protected void Finish()
{
Finished?.Invoke();
}
protected IList<PluginInfo> LoadPlugins(IList<PluginInfo> plugins, Action<PluginInfo, Exception> onPluginLoadError = null)
{
var sortedPlugins = ModifyLoadOrder(plugins);
var invalidPlugins = new HashSet<string>();
var processedPlugins = new Dictionary<string, SemanticVersioning.Version>();
var loadedAssemblies = new Dictionary<string, Assembly>();
var loadedPlugins = new List<PluginInfo>();
foreach (var plugin in sortedPlugins)
{
var dependsOnInvalidPlugin = false;
var missingDependencies = new List<BepInDependency>();
foreach (var dependency in plugin.Dependencies)
{
static bool IsHardDependency(BepInDependency dep) =>
(dep.Flags & BepInDependency.DependencyFlags.HardDependency) != 0;
// If the dependency wasn't already processed, it's missing altogether
var dependencyExists =
processedPlugins.TryGetValue(dependency.DependencyGUID, out var pluginVersion);
// Alternatively, if the dependency hasn't been loaded before, it's missing too
if (!dependencyExists)
{
dependencyExists = Plugins.TryGetValue(dependency.DependencyGUID, out var pluginInfo);
pluginVersion = pluginInfo?.Metadata.Version;
}
if (!dependencyExists || dependency.VersionRange != null &&
!dependency.VersionRange.IsSatisfied(pluginVersion))
{
// If the dependency is hard, collect it into a list to show
if (IsHardDependency(dependency))
missingDependencies.Add(dependency);
continue;
}
// If the dependency is a hard and is invalid (e.g. has missing dependencies), report that to the user
if (invalidPlugins.Contains(dependency.DependencyGUID) && IsHardDependency(dependency))
{
dependsOnInvalidPlugin = true;
break;
}
}
processedPlugins.Add(plugin.Metadata.GUID, plugin.Metadata.Version);
if (dependsOnInvalidPlugin)
{
var message =
$"Skipping [{plugin}] because it has a dependency that was not loaded. See previous errors for details.";
DependencyErrors.Add(message);
Logger.Log(LogLevel.Warning, message);
continue;
}
if (missingDependencies.Count != 0)
{
var message = $@"Could not load [{plugin}] because it has missing dependencies: {
string.Join(", ", missingDependencies.Select(s => s.VersionRange == null ? s.DependencyGUID : $"{s.DependencyGUID} ({s.VersionRange})").ToArray())
}";
DependencyErrors.Add(message);
Logger.Log(LogLevel.Error, message);
invalidPlugins.Add(plugin.Metadata.GUID);
continue;
}
try
{
Logger.Log(LogLevel.Info, $"Loading [{plugin}]");
if (!plugin.Trusted && !IsAssemblySafe(plugin.Location))
{
throw new SecurityException($"Plugin '{plugin.Metadata.Name}' failed security validation. It contains blacklisted instructions or unsafe code.");
}
if (!loadedAssemblies.TryGetValue(plugin.Location, out var ass))
loadedAssemblies[plugin.Location] = ass = Assembly.LoadFrom(plugin.Location);
Plugins[plugin.Metadata.GUID] = plugin;
TryRunModuleCtor(plugin, ass);
plugin.Instance = LoadPlugin(plugin, ass);
loadedPlugins.Add(plugin);
PluginLoaded?.Invoke(plugin);
}
catch (Exception ex)
{
invalidPlugins.Add(plugin.Metadata.GUID);
Plugins.Remove(plugin.Metadata.GUID);
Logger.Log(LogLevel.Error,
$"Error loading [{plugin}]: {(ex is ReflectionTypeLoadException re ? TypeLoader.TypeLoadExceptionToString(re) : ex.ToString())}");
onPluginLoadError(plugin, ex);
}
}
return loadedPlugins;
}
/// <summary>
/// Detects and loads all plugins in the specified directories.
/// </summary>
/// <remarks>
/// It is better to collect all paths at once and use a single call to LoadPlugins than multiple calls.
/// This allows to run proper dependency resolving and to load all plugins in one go.
/// </remarks>
/// <param name="pluginsPaths">Directories to search the plugins from.</param>
/// <returns>List of loaded plugin infos.</returns>
public IList<PluginInfo> LoadPlugins(params string[] pluginsPaths)
{
// TODO: This is a temporary solution for 3rd party loaders. Instead, this should be done via metaplugins.
var plugins = new List<PluginInfo>();
foreach (var pluginsPath in pluginsPaths)
plugins.AddRange(DiscoverPluginsFrom(pluginsPath));
return LoadPlugins(plugins);
}
private static void TryRunModuleCtor(PluginInfo plugin, Assembly assembly)
{
try
{
RuntimeHelpers.RunModuleConstructor(assembly.GetType(plugin.TypeName).Module.ModuleHandle);
}
catch (Exception e)
{
Logger.Log(LogLevel.Warning,
$"Couldn't run Module constructor for {assembly.FullName}::{plugin.TypeName}: {e}");
}
}
private static bool IsAssemblySafe(string assemblyPath)
{
try
{
using var module = ModuleDefinition.ReadModule(assemblyPath);
foreach (var type in module.Types)
{
foreach (var method in type.Methods)
{
if (method.IsPInvokeImpl)
{
return false;
}
// TODO: better unsafe blocking
/*if (!module.Attributes.Has(ModuleAttributes.ILOnly))
{
return false;
}*/
if (!method.HasBody) continue;
foreach (var instruction in method.Body.Instructions)
{
if (instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt)
{
if (instruction.Operand is MethodReference methodRef)
{
var fullName = methodRef.FullName;
if (fullName.Contains("System.Reflection.Assembly::Load")) return false;
}
}
}
}
}
return true;
}
catch (Exception)
{
return false;
}
}
public abstract TPlugin LoadPlugin(PluginInfo pluginInfo, Assembly pluginAssembly);
#endregion
#region Config
private static readonly ConfigEntry<bool> ConfigDiskAppend = ConfigFile.CoreConfig.Bind(
"Logging.Disk", "AppendLog",
false,
"Appends to the log file instead of overwriting, on game startup.");
private static readonly ConfigEntry<bool> ConfigDiskLogging = ConfigFile.CoreConfig.Bind(
"Logging.Disk", "Enabled",
true,
"Enables writing log messages to disk.");
private static readonly ConfigEntry<LogLevel> ConfigDiskLoggingDisplayedLevel = ConfigFile.CoreConfig.Bind(
"Logging.Disk", "LogLevels",
LogLevel.Fatal | LogLevel.Error | LogLevel.Warning | LogLevel.Message | LogLevel.Info,
"Only displays the specified log levels in the disk log output.");
private static readonly ConfigEntry<bool> ConfigDiskLoggingInstantFlushing = ConfigFile.CoreConfig.Bind(
"Logging.Disk", "InstantFlushing",
false,
new StringBuilder()
.AppendLine("If true, instantly writes any received log entries to disk.")
.AppendLine("This incurs a major performance hit if a lot of log messages are being written, however it is really useful for debugging crashes.")
.ToString());
private static readonly ConfigEntry<int> ConfigDiskLoggingFileLimit = ConfigFile.CoreConfig.Bind(
"Logging.Disk", "ConcurrentFileLimit",
5,
new StringBuilder()
.AppendLine("The maximum amount of concurrent log files that will be written to disk.")
.AppendLine("As one log file is used per open game instance, you may find it necessary to increase this limit when debugging multiple instances at the same time.")
.ToString());
#endregion
}