Fix DarkTardMissingNamespacePatch, add some compiler stuff from normal plugin loader, and some code cleanup

This commit is contained in:
2023-07-03 16:23:42 -04:00
parent 856e7dce59
commit 40fa225b1c
11 changed files with 57 additions and 25 deletions

View File

@@ -30,14 +30,26 @@ public class LocalPlugin : PluginData
}
}
public override Assembly GetAssembly()
public override Assembly? GetAssembly()
{
if (File.Exists(Id))
if (!File.Exists(Id)) return null;
//prevent random unloading if being used by another process
int maxRetries = 10;
while (maxRetries > 0)
{
AppDomain.CurrentDomain.AssemblyResolve += LoadFromSameFolder;
var a = Assembly.LoadFile(Id);
Version = a.GetName().Version;
return a;
try
{
AppDomain.CurrentDomain.AssemblyResolve += LoadFromSameFolder;
var a = Assembly.LoadFile(Id);
Version = a?.GetName()?.Version ?? Version;
return a;
}
catch (IOException)
{
LogFile.WriteLine($"Waiting to load {Id} because it's being used by another process");
Thread.Sleep(250);
maxRetries--;
}
}
return null;
@@ -55,27 +67,28 @@ public class LocalPlugin : PluginData
Process.Start("explorer.exe", $"/select, \"{file}\"");
}
private Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
private Assembly? LoadFromSameFolder(object sender, ResolveEventArgs args)
{
if (args.RequestingAssembly.IsDynamic)
if (args.RequestingAssembly?.IsDynamic ?? false)
return null;
if (args.Name.Contains("0Harmony") || args.Name.Contains("SEPluginManager"))
return null;
var location = args.RequestingAssembly.Location;
var location = args.RequestingAssembly?.Location;
if (string.IsNullOrWhiteSpace(location) || !Path.GetFullPath(location)
.StartsWith(Path.GetDirectoryName(Id),
.StartsWith(Path.GetDirectoryName(Id)!,
StringComparison.OrdinalIgnoreCase))
return null;
var folderPath = Path.GetDirectoryName(location);
var folderPath = Path.GetDirectoryName(Id);
if (string.IsNullOrEmpty(folderPath)) return null;
var assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll");
if (!File.Exists(assemblyPath))
return null;
var assembly = Assembly.LoadFile(assemblyPath);
LogFile.WriteLine("Resolving " + assembly.GetName().Name + " for " + args.RequestingAssembly.FullName);
LogFile.WriteLine("Resolving " + assembly.GetName().Name + " for " + args.RequestingAssembly?.FullName);
var main = Main.Instance;
if (!main.Config.IsEnabled(assemblyPath))