Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Assembly/Type/Generic overloads for GetPluginConfig #1959

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Dalamud/Configuration/PluginConfigurations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,32 @@ public T LoadForType<T>(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
}

/// <summary>
/// Load Plugin configuration. Deserialized to the specified type.
/// </summary>
/// <param name="pluginName">Plugin Name.</param>
/// <param name="type">Configuration Type.</param>
/// <returns>Plugin Configuration.</returns>
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,
],
});
}

/// <summary>
/// Get FileInfo to plugin config file.
Expand Down
44 changes: 44 additions & 0 deletions Dalamud/Plugin/DalamudPluginInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ public void SavePluginConfig(IPluginConfiguration? currentConfig)

/// <summary>
/// Get a previously saved plugin configuration or null if none was saved before.
/// This uses the first IPluginConfiguration type found in the calling assembly.
/// </summary>
/// <returns>A previously saved config or null if none was saved before.</returns>
public IPluginConfiguration? GetPluginConfig()
Expand All @@ -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);
}

/// <summary>
/// Get a previously saved plugin configuration or null if none was saved before.
/// This uses the first IPluginConfiguration type found in the specified assembly.
/// </summary>
/// <param name="assembly">The assembly to search for the configuration type.</param>
/// <returns>A previously saved config or null if none was saved before.</returns>
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);
}

/// <summary>
/// Get a previously saved plugin configuration or null if none was saved before.
/// </summary>
/// <typeparam name="TPluginConfiguration">The type of the plugin configuration.</typeparam>
/// <returns>A previously saved config or null if none was saved before.</returns>
public TPluginConfiguration? GetPluginConfig<TPluginConfiguration>()
where TPluginConfiguration : class, IPluginConfiguration
{
return this.configs.LoadForType<TPluginConfiguration>(this.plugin.InternalName);
}

/// <summary>
/// Get a previously saved plugin configuration or null if none was saved before.
/// </summary>
/// <param name="type">The type of the plugin configuration.</param>
/// <returns>A previously saved config or null if none was saved before.</returns>
public IPluginConfiguration? GetPluginConfig(Type type)
{
return this.configs.LoadForType(this.plugin.InternalName, type);
}

/// <summary>
/// Get the config directory.
Expand Down
25 changes: 25 additions & 0 deletions Dalamud/Plugin/IDalamudPluginInterface.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -264,10 +265,34 @@ public interface IDalamudPluginInterface

/// <summary>
/// Get a previously saved plugin configuration or null if none was saved before.
/// This uses the first IPluginConfiguration type found in the calling assembly.
/// </summary>
/// <returns>A previously saved config or null if none was saved before.</returns>
IPluginConfiguration? GetPluginConfig();

/// <summary>
/// Get a previously saved plugin configuration or null if none was saved before.
/// This uses the first IPluginConfiguration type found in the specified assembly.
/// </summary>
/// <param name="assembly">The assembly to search for the configuration type.</param>
/// <returns>A previously saved config or null if none was saved before.</returns>
IPluginConfiguration? GetPluginConfig(Assembly assembly);

/// <summary>
/// Get a previously saved plugin configuration or null if none was saved before.
/// </summary>
/// <typeparam name="TPluginConfiguration">The type of the plugin configuration.</typeparam>
/// <returns>A previously saved config or null if none was saved before.</returns>
TPluginConfiguration? GetPluginConfig<TPluginConfiguration>()
where TPluginConfiguration : class, IPluginConfiguration;

/// <summary>
/// Get a previously saved plugin configuration or null if none was saved before.
/// </summary>
/// <param name="type">The type of the plugin configuration.</param>
/// <returns>A previously saved config or null if none was saved before.</returns>
IPluginConfiguration? GetPluginConfig(Type type);

/// <summary>
/// Get the config directory.
/// </summary>
Expand Down
Loading