refactor: new code style in Configuration

This commit is contained in:
kalilistic 2021-04-10 14:15:54 -04:00
parent 8e43b1703d
commit cd9a2297f5
5 changed files with 102 additions and 58 deletions

View file

@ -48,4 +48,5 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EFormat_002ESettingsUpgrade_002EAlignmentTabFillStyleMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dalamud/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Gpose/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unsanitized/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View file

@ -50,12 +50,12 @@ namespace Dalamud.Configuration
/// <summary>
/// Gets or sets a value indicating whether or not plugin testing builds should be shown.
/// </summary>
public bool DoPluginTest { get; set; } = false;
public bool DoPluginTest { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not Dalamud testing builds should be used.
/// </summary>
public bool DoDalamudTest { get; set; } = false;
public bool DoDalamudTest { get; set; }
/// <summary>
/// Gets or sets a list of custom repos.
@ -95,7 +95,7 @@ namespace Dalamud.Configuration
/// <summary>
/// Gets or sets a value indicating whether or not plugins should be auto-updated.
/// </summary>
public bool AutoUpdatePlugins { get; set; } = false;
public bool AutoUpdatePlugins { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not the debug log should scroll automatically.

View file

@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dalamud.Configuration
{
/// <summary>
/// Configuration to store settings for a dalamud plugin.
/// </summary>
public interface IPluginConfiguration
{
/// <summary>
/// Gets or sets configuration version.
/// </summary>
int Version { get; set; }
}
}

View file

@ -1,83 +1,115 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Dalamud.Configuration
{
public class PluginConfigurations {
private DirectoryInfo configDirectory;
/// <summary>
/// Configuration to store settings for a dalamud plugin.
/// </summary>
public class PluginConfigurations
{
private readonly DirectoryInfo configDirectory;
public PluginConfigurations(string storageFolder) {
/// <summary>
/// Initializes a new instance of the <see cref="PluginConfigurations"/> class.
/// </summary>
/// <param name="storageFolder">Directory for storage of plugin configuration files.</param>
public PluginConfigurations(string storageFolder)
{
this.configDirectory = new DirectoryInfo(storageFolder);
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(GetConfigFile(pluginName).FullName, JsonConvert.SerializeObject(config, Formatting.Indented, new JsonSerializerSettings
/// <summary>
/// Save/Load plugin configuration.
/// 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 entirely.
/// </summary>
/// <param name="config">Plugin configuration.</param>
/// <param name="pluginName">Plugin name.</param>
public void Save(IPluginConfiguration config, string pluginName)
{
File.WriteAllText(this.GetConfigFile(pluginName).FullName, JsonConvert.SerializeObject(config, Formatting.Indented, new JsonSerializerSettings
{
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
TypeNameHandling = TypeNameHandling.Objects
TypeNameHandling = TypeNameHandling.Objects,
}));
}
public IPluginConfiguration Load(string pluginName) {
var path = GetConfigFile(pluginName);
/// <summary>
/// Load plugin configuration.
/// </summary>
/// <param name="pluginName">Plugin name.</param>
/// <returns>Plugin configuration.</returns>
public IPluginConfiguration Load(string pluginName)
{
var path = this.GetConfigFile(pluginName);
if (!path.Exists)
return null;
return JsonConvert.DeserializeObject<IPluginConfiguration>(File.ReadAllText(path.FullName),
new JsonSerializerSettings {
return JsonConvert.DeserializeObject<IPluginConfiguration>(
File.ReadAllText(path.FullName),
new JsonSerializerSettings
{
TypeNameAssemblyFormatHandling =
TypeNameAssemblyFormatHandling.Simple,
TypeNameHandling = TypeNameHandling.Objects
TypeNameHandling = TypeNameHandling.Objects,
});
}
public string GetDirectory(string pluginName) {
try {
var path = GetDirectoryPath(pluginName);
if (!path.Exists) {
/// <summary>
/// Get plugin directory.
/// </summary>
/// <param name="pluginName">Plugin name.</param>
/// <returns>Plugin directory path.</returns>
public string GetDirectory(string pluginName)
{
try
{
var path = this.GetDirectoryPath(pluginName);
if (!path.Exists)
{
path.Create();
}
return path.FullName;
} catch {
}
catch
{
return string.Empty;
}
}
// 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
/// <summary>
/// Load Plugin 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.
/// </summary>
/// <param name="pluginName">Plugin Name.</param>
/// <typeparam name="T">Configuration Type.</typeparam>
/// <returns>Plugin Configuration.</returns>
public T LoadForType<T>(string pluginName) where T : IPluginConfiguration
{
var path = GetConfigFile(pluginName);
var path = this.GetConfigFile(pluginName);
if (!path.Exists)
return default(T);
return !path.Exists ? default : JsonConvert.DeserializeObject<T>(File.ReadAllText(path.FullName));
// 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<T>(File.ReadAllText(path.FullName));
}
/// <summary>
/// Get FileInfo to plugin config file.
/// </summary>
/// <param name="pluginName">InternalName of the plugin.</param>
/// <returns>FileInfo of the config file</returns>
/// <returns>FileInfo of the config file.</returns>
public FileInfo GetConfigFile(string pluginName) => new FileInfo(Path.Combine(this.configDirectory.FullName, $"{pluginName}.json"));
private DirectoryInfo GetDirectoryPath(string pluginName) => new DirectoryInfo(Path.Combine(this.configDirectory.FullName, pluginName));
}
}

View file

@ -1,19 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dalamud.Configuration
{
class ThirdRepoSetting {
/// <summary>
/// Third party repository for dalamud plugins.
/// </summary>
internal class ThirdRepoSetting
{
/// <summary>
/// Gets or sets the third party repo url.
/// </summary>
public string Url { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the third party repo is enabled.
/// </summary>
public bool IsEnabled { get; set; }
public ThirdRepoSetting Clone() {
return new ThirdRepoSetting {
/// <summary>
/// Create new instance of third party repo object.
/// </summary>
/// <returns>New instance of third party repo.</returns>
public ThirdRepoSetting Clone()
{
return new ThirdRepoSetting
{
Url = this.Url,
IsEnabled = this.IsEnabled
IsEnabled = this.IsEnabled,
};
}
}