From 9f9a41c76865438f3ae62551bd701423eb0d27a3 Mon Sep 17 00:00:00 2001 From: AzureGem Date: Sat, 17 Apr 2021 01:18:36 -0400 Subject: [PATCH] Do not load plugins before performing checks Modify the logic to perform all checks before proceeding to load the assembly and create an instance of the plugin - Removed `bool preloaded` in exchange to a `null` check in `pluginDef`. - Moved all checks into a common place before loading the assembly and before creating an instance. - Applicable Version check - Dalamud API check - Banned Plugins check - Checks will only be performed on raw / developer plugins. - Removed the old banned plugin code since the data is now in `bannedplugins.json`. All checks in the types foreach is removed since all the checks would had already be performed before then *(Only developer plugins has no pluginDef)* This should avoid mysterious crashes in future updates without making any major API / behavior changes. --- Dalamud/Plugin/PluginManager.cs | 87 ++++++++++----------------------- 1 file changed, 27 insertions(+), 60 deletions(-) diff --git a/Dalamud/Plugin/PluginManager.cs b/Dalamud/Plugin/PluginManager.cs index ba74a7167..7eab04130 100644 --- a/Dalamud/Plugin/PluginManager.cs +++ b/Dalamud/Plugin/PluginManager.cs @@ -152,7 +152,7 @@ namespace Dalamud.Plugin { try { - this.LoadPluginFromAssembly(dllFile, isRaw, PluginLoadReason.Boot, true, definition); + this.LoadPluginFromAssembly(dllFile, isRaw, PluginLoadReason.Boot, definition); } catch (Exception ex) { @@ -206,10 +206,9 @@ namespace Dalamud.Plugin /// The associated with the main assembly of this plugin. /// Whether or not the plugin is a dev plugin. /// The reason this plugin was loaded. - /// Whether or not to skip loading a definition from a file path. - /// The already loaded definition, when is set to true. + /// The already loaded definition, if available /// Whether or not the plugin was loaded successfully. - public bool LoadPluginFromAssembly(FileInfo dllFile, bool isRaw, PluginLoadReason reason, bool preloaded = false, PluginDefinition preloadedDef = null) + public bool LoadPluginFromAssembly(FileInfo dllFile, bool isRaw, PluginLoadReason reason, PluginDefinition pluginDef = null) { Log.Information("Loading plugin at {0}", dllFile.Directory.FullName); @@ -230,28 +229,8 @@ namespace Dalamud.Plugin return false; } - PluginDefinition pluginDef = null; - // Preloaded - if (preloaded) - { - if (preloadedDef == null && !isRaw) - { - Log.Information("Plugin DLL {0} has no definition.", dllFile.FullName); - return false; - } - - if (preloadedDef != null && - preloadedDef.ApplicableVersion != this.dalamud.StartInfo.GameVersion && - preloadedDef.ApplicableVersion != "any") - { - Log.Information("Plugin {0} has not applicable version.", dllFile.FullName); - return false; - } - - pluginDef = preloadedDef; - } - else + if (pluginDef == null) { // read the plugin def if present - again, fail before actually trying to load the dll if there is a problem var defJsonFile = new FileInfo(Path.Combine(dllFile.Directory.FullName, $"{Path.GetFileNameWithoutExtension(dllFile.Name)}.json")); @@ -264,16 +243,35 @@ namespace Dalamud.Plugin pluginDef = JsonConvert.DeserializeObject( File.ReadAllText(defJsonFile.FullName)); + } + } - if (pluginDef.ApplicableVersion != this.dalamud.StartInfo.GameVersion && pluginDef.ApplicableVersion != "any") + // Perform checks + if (!isRaw) + { + if (pluginDef != null) + { + if (pluginDef.ApplicableVersion != this.dalamud.StartInfo.GameVersion && + pluginDef.ApplicableVersion != "any") { Log.Information("Plugin {0} has not applicable version.", dllFile.FullName); return false; } - } - // but developer plugins don't require one to load - else if (!isRaw) + if (pluginDef.DalamudApiLevel < DalamudApiLevel) + { + Log.Error("Incompatible API level: {0}", dllFile.FullName); + return false; + } + + if (this.bannedPlugins.Any(x => x.Name == pluginDef.InternalName && + x.AssemblyVersion == pluginDef.AssemblyVersion)) + { + Log.Error($"[PLUGINM] Banned plugin {pluginDef.InternalName} {pluginDef.AssemblyVersion}"); + return false; + } + } + else { Log.Information("Plugin DLL {0} has no definition.", dllFile.FullName); return false; @@ -322,37 +320,6 @@ namespace Dalamud.Plugin DalamudApiLevel = DalamudApiLevel, }; - if (this.bannedPlugins.Any(x => x.Name == pluginDef.InternalName && - x.AssemblyVersion == pluginDef.AssemblyVersion)) - { - Log.Error($"[PLUGINM] Banned plugin {pluginDef.InternalName} with {pluginDef.AssemblyVersion}"); - return false; - } - - if (pluginDef.InternalName == "PingPlugin" && pluginDef.AssemblyVersion == "1.11.0.0") - { - Log.Error("Banned PingPlugin"); - return false; - } - - if (pluginDef.InternalName == "FPSPlugin" && pluginDef.AssemblyVersion == "1.4.2.0") - { - Log.Error("Banned PingPlugin"); - return false; - } - - if (pluginDef.InternalName == "SonarPlugin" && pluginDef.AssemblyVersion == "0.1.3.1") - { - Log.Error("Banned SonarPlugin"); - return false; - } - - if (pluginDef.DalamudApiLevel < DalamudApiLevel) - { - Log.Error("Incompatible API level: {0}", dllFile.FullName); - return false; - } - Log.Verbose("Plugin Initialize..."); var dalamudInterface = new DalamudPluginInterface(this.dalamud, type.Assembly.GetName().Name, this.pluginConfigs, reason);