diff --git a/Dalamud/Configuration/PluginConfigurations.cs b/Dalamud/Configuration/PluginConfigurations.cs index 74404abe7..d108c5777 100644 --- a/Dalamud/Configuration/PluginConfigurations.cs +++ b/Dalamud/Configuration/PluginConfigurations.cs @@ -16,6 +16,10 @@ namespace Dalamud.Configuration this.configDirectory.Create(); } + // NOTE: Save/Load are still using Type information for now, despite LoadForType<> superseding Load + // and not requiring or using it. It might be worth removing the Type info from Save, to strip it from + // all future saved configs, and then Load() can probably be removed entirey. + public void Save(IPluginConfiguration config, string pluginName) { File.WriteAllText(GetPath(pluginName).FullName, JsonConvert.SerializeObject(config, Formatting.Indented, new JsonSerializerSettings { @@ -38,6 +42,22 @@ namespace Dalamud.Configuration }); } + // Parameterized deserialization + // Currently this is called via reflection from DalamudPluginInterface.GetPluginConfig() + // Eventually there may be an additional pluginInterface method that can call this directly + // without reflection - for now this is in support of the existing plugin api + public T LoadForType(string pluginName) where T : IPluginConfiguration + { + var path = GetPath(pluginName); + + if (!path.Exists) + return default(T); + + // intentionally no type handling - it will break when updating a plugin at runtime + // and turns out to be unnecessary when we fully qualify the object type + return JsonConvert.DeserializeObject(File.ReadAllText(path.FullName)); + } + private FileInfo GetPath(string pluginName) => new FileInfo(Path.Combine(this.configDirectory.FullName, $"{pluginName}.json")); } } diff --git a/Dalamud/Plugin/DalamudPluginInterface.cs b/Dalamud/Plugin/DalamudPluginInterface.cs index 8c0797e30..19f36c8cc 100644 --- a/Dalamud/Plugin/DalamudPluginInterface.cs +++ b/Dalamud/Plugin/DalamudPluginInterface.cs @@ -94,6 +94,24 @@ namespace Dalamud.Plugin /// /// A previously saved config or null if none was saved before. public IPluginConfiguration GetPluginConfig() { + // This is done to support json deserialization of plugin configurations + // even after running an in-game update of plugins, where the assembly version + // changes. + // Eventually it might make sense to have a separate method on this class + // T GetPluginConfig() where T : IPluginConfiguration + // that can invoke LoadForType() directly instead of via reflection + // This is here for now to support the current plugin API + foreach (var type in Assembly.GetCallingAssembly().GetTypes()) + { + if (type.GetInterface(typeof(IPluginConfiguration).FullName) != null) + { + var mi = this.configs.GetType().GetMethod("LoadForType"); + var fn = mi.MakeGenericMethod(type); + return (IPluginConfiguration)fn.Invoke(this.configs, new object[] { this.pluginName }); + } + } + + // this shouldn't be a thing, I think, but just in case return this.configs.Load(this.pluginName); } diff --git a/Dalamud/Plugin/PluginInstallerWindow.cs b/Dalamud/Plugin/PluginInstallerWindow.cs index a857524f3..fcc26fba6 100644 --- a/Dalamud/Plugin/PluginInstallerWindow.cs +++ b/Dalamud/Plugin/PluginInstallerWindow.cs @@ -99,15 +99,19 @@ namespace Dalamud.Plugin { Log.Information("Eligible for update: {0}", remoteInfo.InternalName); - foreach (var sortedVersion in sortedVersions) { - File.Create(Path.Combine(sortedVersion.FullName, ".disabled")); - } + // DisablePlugin() below immediately creates a .disabled file anyway, but will fail + // with an exception if we try to do it twice in row like this + // TODO: not sure if doing this for all versions is really necessary, since the + // others really needed to be disabled before anyway + //foreach (var sortedVersion in sortedVersions) { + // File.Create(Path.Combine(sortedVersion.FullName, ".disabled")); + //} // Try to disable plugin if it is loaded try { this.manager.DisablePlugin(info); - } catch { - // ignored + } catch (Exception ex) { + Log.Error(ex, "Plugin disable failed"); } InstallPlugin(remoteInfo); diff --git a/Dalamud/Plugin/PluginManager.cs b/Dalamud/Plugin/PluginManager.cs index 952c19393..bcf68f7ad 100644 --- a/Dalamud/Plugin/PluginManager.cs +++ b/Dalamud/Plugin/PluginManager.cs @@ -57,9 +57,50 @@ namespace Dalamud.Plugin } public bool LoadPluginFromAssembly(FileInfo dllFile, bool raw) { + Log.Information("Loading plugin at {0}", dllFile.Directory.FullName); + + // If this entire folder has been marked as a disabled plugin, don't even try to load anything + var disabledFile = new FileInfo(Path.Combine(dllFile.Directory.FullName, ".disabled")); + if (disabledFile.Exists && !raw) // should raw/dev plugins really not respect this? + { + Log.Information("Plugin {0} is disabled.", dllFile.FullName); + return false; + } + + // 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")); + + PluginDefinition pluginDef = null; + // load the definition if it exists, even for raw/developer plugins + if (defJsonFile.Exists) + { + Log.Information("Loading definition for plugin DLL {0}", dllFile.FullName); + + pluginDef = + JsonConvert.DeserializeObject( + File.ReadAllText(defJsonFile.FullName)); + + 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 (!raw) + { + Log.Information("Plugin DLL {0} has no definition.", dllFile.FullName); + return false; + } + + // TODO: given that it exists, the pluginDef's InternalName should probably be used + // as the actual assembly to load + // But plugins should also probably be loaded by directory and not by looking for every dll + Log.Information("Loading assembly at {0}", dllFile); - var assemblyName = AssemblyName.GetAssemblyName(dllFile.FullName); - var pluginAssembly = Assembly.Load(assemblyName); + + // Assembly.Load() by name here will not load multiple versions with the same name, in the case of updates + var pluginAssembly = Assembly.LoadFile(dllFile.FullName); if (pluginAssembly != null) { @@ -74,38 +115,6 @@ namespace Dalamud.Plugin if (type.GetInterface(interfaceType.FullName) != null) { - var disabledFile = new FileInfo(Path.Combine(dllFile.Directory.FullName, ".disabled")); - - if (disabledFile.Exists && !raw) { - Log.Information("Plugin {0} is disabled.", dllFile.FullName); - return false; - } - - var defJsonFile = new FileInfo(Path.Combine(dllFile.Directory.FullName, $"{Path.GetFileNameWithoutExtension(dllFile.Name)}.json")); - - PluginDefinition pluginDef = null; - // load the definition if it exists, even for raw/developer plugins - if (defJsonFile.Exists) - { - Log.Information("Loading definition for plugin DLL {0}", dllFile.FullName); - - pluginDef = - JsonConvert.DeserializeObject( - File.ReadAllText(defJsonFile.FullName)); - - 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 (!raw) - { - Log.Information("Plugin DLL {0} has no definition.", dllFile.FullName); - return false; - } - if (this.Plugins.Any(x => x.Plugin.GetType().Assembly.GetName().Name == type.Assembly.GetName().Name)) { Log.Error("Duplicate plugin found: {0}", dllFile.FullName); return false; @@ -120,7 +129,7 @@ namespace Dalamud.Plugin { Author = "developer", Name = plugin.Name, - InternalName = "devPlugin_" + plugin.Name, + InternalName = Path.GetFileNameWithoutExtension(dllFile.Name), AssemblyVersion = plugin.GetType().Assembly.GetName().Version.ToString(), Description = "", ApplicableVersion = "any",