fix: only create load context for plugins that are actually supposed to load

This commit is contained in:
goat 2023-10-03 23:19:44 +02:00
parent 0cc9de9fab
commit daeec9a13f
No known key found for this signature in database
GPG key ID: 49E2AA8C6A76498B

View file

@ -64,54 +64,6 @@ internal class LocalPlugin : IDisposable
this.DllFile = dllFile;
this.State = PluginState.Unloaded;
try
{
this.loader = PluginLoader.CreateFromAssemblyFile(this.DllFile.FullName, SetupLoaderConfig);
}
catch (InvalidOperationException ex)
{
Log.Error(ex, "Loader.CreateFromAssemblyFile() failed");
this.State = PluginState.DependencyResolutionFailed;
throw;
}
try
{
this.pluginAssembly = this.loader.LoadDefaultAssembly();
}
catch (Exception ex)
{
this.pluginAssembly = null;
this.pluginType = null;
this.loader.Dispose();
Log.Error(ex, $"Not a plugin: {this.DllFile.FullName}");
throw new InvalidPluginException(this.DllFile);
}
try
{
this.pluginType = this.pluginAssembly.GetTypes().FirstOrDefault(type => type.IsAssignableTo(typeof(IDalamudPlugin)));
}
catch (ReflectionTypeLoadException ex)
{
Log.Error(ex, $"Could not load one or more types when searching for IDalamudPlugin: {this.DllFile.FullName}");
// Something blew up when parsing types, but we still want to look for IDalamudPlugin. Let Load() handle the error.
this.pluginType = ex.Types.FirstOrDefault(type => type != null && type.IsAssignableTo(typeof(IDalamudPlugin)));
}
if (this.pluginType == default)
{
this.pluginAssembly = null;
this.pluginType = null;
this.loader.Dispose();
Log.Error($"Nothing inherits from IDalamudPlugin: {this.DllFile.FullName}");
throw new InvalidPluginException(this.DllFile);
}
var assemblyVersion = this.pluginAssembly.GetName().Version;
// Although it is conditionally used here, we need to set the initial value regardless.
this.manifestFile = LocalPluginManifest.GetManifestFile(this.DllFile);
@ -123,7 +75,7 @@ internal class LocalPlugin : IDisposable
Author = "developer",
Name = Path.GetFileNameWithoutExtension(this.DllFile.Name),
InternalName = Path.GetFileNameWithoutExtension(this.DllFile.Name),
AssemblyVersion = assemblyVersion ?? new Version("1.0.0.0"),
AssemblyVersion = new Version("1.0.0.0"),
Description = string.Empty,
ApplicableVersion = GameVersion.Any,
DalamudApiLevel = PluginManager.DalamudApiLevel,
@ -410,6 +362,8 @@ internal class LocalPlugin : IDisposable
this.State = PluginState.Loading;
Log.Information($"Loading {this.DllFile.Name}");
this.EnsureLoader();
if (this.DllFile.DirectoryName != null &&
File.Exists(Path.Combine(this.DllFile.DirectoryName, "Dalamud.dll")))
@ -700,4 +654,56 @@ internal class LocalPlugin : IDisposable
config.SharedAssemblies.Add(typeof(Lumina.GameData).Assembly.GetName());
config.SharedAssemblies.Add(typeof(Lumina.Excel.ExcelSheetImpl).Assembly.GetName());
}
private void EnsureLoader()
{
if (this.loader != null)
return;
try
{
this.loader = PluginLoader.CreateFromAssemblyFile(this.DllFile.FullName, SetupLoaderConfig);
}
catch (InvalidOperationException ex)
{
Log.Error(ex, "Loader.CreateFromAssemblyFile() failed");
this.State = PluginState.DependencyResolutionFailed;
throw;
}
try
{
this.pluginAssembly = this.loader.LoadDefaultAssembly();
}
catch (Exception ex)
{
this.pluginAssembly = null;
this.pluginType = null;
this.loader.Dispose();
Log.Error(ex, $"Not a plugin: {this.DllFile.FullName}");
throw new InvalidPluginException(this.DllFile);
}
try
{
this.pluginType = this.pluginAssembly.GetTypes().FirstOrDefault(type => type.IsAssignableTo(typeof(IDalamudPlugin)));
}
catch (ReflectionTypeLoadException ex)
{
Log.Error(ex, $"Could not load one or more types when searching for IDalamudPlugin: {this.DllFile.FullName}");
// Something blew up when parsing types, but we still want to look for IDalamudPlugin. Let Load() handle the error.
this.pluginType = ex.Types.FirstOrDefault(type => type != null && type.IsAssignableTo(typeof(IDalamudPlugin)));
}
if (this.pluginType == default)
{
this.pluginAssembly = null;
this.pluginType = null;
this.loader.Dispose();
Log.Error($"Nothing inherits from IDalamudPlugin: {this.DllFile.FullName}");
throw new InvalidPluginException(this.DllFile);
}
}
}