diff --git a/Dalamud/Configuration/PluginConfigurations.cs b/Dalamud/Configuration/PluginConfigurations.cs index 8e32fa992..1620a5c91 100644 --- a/Dalamud/Configuration/PluginConfigurations.cs +++ b/Dalamud/Configuration/PluginConfigurations.cs @@ -133,6 +133,32 @@ public T LoadForType(string pluginName) where T : IPluginConfiguration // 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 } + + /// + /// Load Plugin configuration. Deserialized to the specified type. + /// + /// Plugin Name. + /// Configuration Type. + /// Plugin Configuration. + public IPluginConfiguration? LoadForType(string pluginName, Type type) + { + if (!typeof(IPluginConfiguration).IsAssignableFrom(type)) + throw new ArgumentException("Type must be assignable to IPluginConfiguration.", nameof(type)); + + if (this.GetConfigFile(pluginName) is not { Exists: true } path) + return null; + + return (IPluginConfiguration?)JsonConvert.DeserializeObject( + File.ReadAllText(path.FullName), + type, + new JsonSerializerSettings + { + Converters = + [ + DalamudAssemblyTypeNameForcingJsonConverter.Instance, + ], + }); + } /// /// Get FileInfo to plugin config file. diff --git a/Dalamud/Plugin/DalamudPluginInterface.cs b/Dalamud/Plugin/DalamudPluginInterface.cs index bb92b6b0c..e3abc8412 100644 --- a/Dalamud/Plugin/DalamudPluginInterface.cs +++ b/Dalamud/Plugin/DalamudPluginInterface.cs @@ -384,6 +384,7 @@ public void SavePluginConfig(IPluginConfiguration? currentConfig) /// /// Get a previously saved plugin configuration or null if none was saved before. + /// This uses the first IPluginConfiguration type found in the calling assembly. /// /// A previously saved config or null if none was saved before. public IPluginConfiguration? GetPluginConfig() @@ -408,6 +409,49 @@ public void SavePluginConfig(IPluginConfiguration? currentConfig) // this shouldn't be a thing, I think, but just in case return this.configs.Load(this.plugin.InternalName, this.plugin.EffectiveWorkingPluginId); } + + /// + /// Get a previously saved plugin configuration or null if none was saved before. + /// This uses the first IPluginConfiguration type found in the specified assembly. + /// + /// The assembly to search for the configuration type. + /// A previously saved config or null if none was saved before. + public IPluginConfiguration? GetPluginConfig(Assembly assembly) + { + foreach (var type in assembly.GetTypes()) + { + if (type.IsAssignableTo(typeof(IPluginConfiguration))) + { + var mi = this.configs.GetType().GetMethod("LoadForType"); + var fn = mi!.MakeGenericMethod(type); + return (IPluginConfiguration)fn.Invoke(this.configs, new object[] { this.plugin.InternalName }); + } + } + + // this shouldn't be a thing, I think, but just in case + return this.configs.Load(this.plugin.InternalName, this.plugin.EffectiveWorkingPluginId); + } + + /// + /// Get a previously saved plugin configuration or null if none was saved before. + /// + /// The type of the plugin configuration. + /// A previously saved config or null if none was saved before. + public TPluginConfiguration? GetPluginConfig() + where TPluginConfiguration : class, IPluginConfiguration + { + return this.configs.LoadForType(this.plugin.InternalName); + } + + /// + /// Get a previously saved plugin configuration or null if none was saved before. + /// + /// The type of the plugin configuration. + /// A previously saved config or null if none was saved before. + public IPluginConfiguration? GetPluginConfig(Type type) + { + return this.configs.LoadForType(this.plugin.InternalName, type); + } /// /// Get the config directory. diff --git a/Dalamud/Plugin/IDalamudPluginInterface.cs b/Dalamud/Plugin/IDalamudPluginInterface.cs index 6393dc5ab..660730e88 100644 --- a/Dalamud/Plugin/IDalamudPluginInterface.cs +++ b/Dalamud/Plugin/IDalamudPluginInterface.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Reflection; using Dalamud.Configuration; using Dalamud.Game.Text; @@ -264,10 +265,34 @@ public interface IDalamudPluginInterface /// /// Get a previously saved plugin configuration or null if none was saved before. + /// This uses the first IPluginConfiguration type found in the calling assembly. /// /// A previously saved config or null if none was saved before. IPluginConfiguration? GetPluginConfig(); + /// + /// Get a previously saved plugin configuration or null if none was saved before. + /// This uses the first IPluginConfiguration type found in the specified assembly. + /// + /// The assembly to search for the configuration type. + /// A previously saved config or null if none was saved before. + IPluginConfiguration? GetPluginConfig(Assembly assembly); + + /// + /// Get a previously saved plugin configuration or null if none was saved before. + /// + /// The type of the plugin configuration. + /// A previously saved config or null if none was saved before. + TPluginConfiguration? GetPluginConfig() + where TPluginConfiguration : class, IPluginConfiguration; + + /// + /// Get a previously saved plugin configuration or null if none was saved before. + /// + /// The type of the plugin configuration. + /// A previously saved config or null if none was saved before. + IPluginConfiguration? GetPluginConfig(Type type); + /// /// Get the config directory. ///