mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 10:17:22 +01:00
refactor: new code style in Configuration
This commit is contained in:
parent
8e43b1703d
commit
cd9a2297f5
5 changed files with 102 additions and 58 deletions
|
|
@ -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_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/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/=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>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=unsanitized/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||||
|
|
@ -50,12 +50,12 @@ namespace Dalamud.Configuration
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether or not plugin testing builds should be shown.
|
/// Gets or sets a value indicating whether or not plugin testing builds should be shown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool DoPluginTest { get; set; } = false;
|
public bool DoPluginTest { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether or not Dalamud testing builds should be used.
|
/// Gets or sets a value indicating whether or not Dalamud testing builds should be used.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool DoDalamudTest { get; set; } = false;
|
public bool DoDalamudTest { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a list of custom repos.
|
/// Gets or sets a list of custom repos.
|
||||||
|
|
@ -95,7 +95,7 @@ namespace Dalamud.Configuration
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether or not plugins should be auto-updated.
|
/// Gets or sets a value indicating whether or not plugins should be auto-updated.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool AutoUpdatePlugins { get; set; } = false;
|
public bool AutoUpdatePlugins { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether or not the debug log should scroll automatically.
|
/// Gets or sets a value indicating whether or not the debug log should scroll automatically.
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Dalamud.Configuration
|
namespace Dalamud.Configuration
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Configuration to store settings for a dalamud plugin.
|
||||||
|
/// </summary>
|
||||||
public interface IPluginConfiguration
|
public interface IPluginConfiguration
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets configuration version.
|
||||||
|
/// </summary>
|
||||||
int Version { get; set; }
|
int Version { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,83 +1,115 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Dalamud.Configuration
|
namespace Dalamud.Configuration
|
||||||
{
|
{
|
||||||
public class PluginConfigurations {
|
/// <summary>
|
||||||
private DirectoryInfo configDirectory;
|
/// 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 = new DirectoryInfo(storageFolder);
|
||||||
this.configDirectory.Create();
|
this.configDirectory.Create();
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: Save/Load are still using Type information for now, despite LoadForType<> superseding Load
|
/// <summary>
|
||||||
// and not requiring or using it. It might be worth removing the Type info from Save, to strip it from
|
/// Save/Load plugin configuration.
|
||||||
// all future saved configs, and then Load() can probably be removed entirey.
|
/// NOTE: Save/Load are still using Type information for now,
|
||||||
|
/// despite LoadForType superseding Load and not requiring or using it.
|
||||||
public void Save(IPluginConfiguration config, string pluginName) {
|
/// It might be worth removing the Type info from Save, to strip it from all future saved configs,
|
||||||
File.WriteAllText(GetConfigFile(pluginName).FullName, JsonConvert.SerializeObject(config, Formatting.Indented, new JsonSerializerSettings
|
/// 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,
|
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
|
||||||
TypeNameHandling = TypeNameHandling.Objects
|
TypeNameHandling = TypeNameHandling.Objects,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public IPluginConfiguration Load(string pluginName) {
|
/// <summary>
|
||||||
var path = GetConfigFile(pluginName);
|
/// 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)
|
if (!path.Exists)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return JsonConvert.DeserializeObject<IPluginConfiguration>(File.ReadAllText(path.FullName),
|
return JsonConvert.DeserializeObject<IPluginConfiguration>(
|
||||||
new JsonSerializerSettings {
|
File.ReadAllText(path.FullName),
|
||||||
TypeNameAssemblyFormatHandling =
|
new JsonSerializerSettings
|
||||||
TypeNameAssemblyFormatHandling.Simple,
|
{
|
||||||
TypeNameHandling = TypeNameHandling.Objects
|
TypeNameAssemblyFormatHandling =
|
||||||
});
|
TypeNameAssemblyFormatHandling.Simple,
|
||||||
|
TypeNameHandling = TypeNameHandling.Objects,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetDirectory(string pluginName) {
|
/// <summary>
|
||||||
try {
|
/// Get plugin directory.
|
||||||
var path = GetDirectoryPath(pluginName);
|
/// </summary>
|
||||||
if (!path.Exists) {
|
/// <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();
|
path.Create();
|
||||||
}
|
}
|
||||||
|
|
||||||
return path.FullName;
|
return path.FullName;
|
||||||
} catch {
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parameterized deserialization
|
/// <summary>
|
||||||
// Currently this is called via reflection from DalamudPluginInterface.GetPluginConfig()
|
/// Load Plugin configuration. Parameterized deserialization.
|
||||||
// Eventually there may be an additional pluginInterface method that can call this directly
|
/// Currently this is called via reflection from DalamudPluginInterface.GetPluginConfig().
|
||||||
// without reflection - for now this is in support of the existing plugin api
|
/// 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
|
public T LoadForType<T>(string pluginName) where T : IPluginConfiguration
|
||||||
{
|
{
|
||||||
var path = GetConfigFile(pluginName);
|
var path = this.GetConfigFile(pluginName);
|
||||||
|
|
||||||
if (!path.Exists)
|
return !path.Exists ? default : JsonConvert.DeserializeObject<T>(File.ReadAllText(path.FullName));
|
||||||
return default(T);
|
|
||||||
|
|
||||||
// intentionally no type handling - it will break when updating a plugin at runtime
|
// 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
|
// and turns out to be unnecessary when we fully qualify the object type
|
||||||
return JsonConvert.DeserializeObject<T>(File.ReadAllText(path.FullName));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get FileInfo to plugin config file.
|
/// Get FileInfo to plugin config file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="pluginName">InternalName of the plugin.</param>
|
/// <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"));
|
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));
|
private DirectoryInfo GetDirectoryPath(string pluginName) => new DirectoryInfo(Path.Combine(this.configDirectory.FullName, pluginName));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,30 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Dalamud.Configuration
|
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; }
|
public string Url { get; set; }
|
||||||
public bool IsEnabled { get;set; }
|
|
||||||
|
|
||||||
public ThirdRepoSetting Clone() {
|
/// <summary>
|
||||||
return new ThirdRepoSetting {
|
/// Gets or sets a value indicating whether the third party repo is enabled.
|
||||||
|
/// </summary>
|
||||||
|
public bool IsEnabled { get; set; }
|
||||||
|
|
||||||
|
/// <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,
|
Url = this.Url,
|
||||||
IsEnabled = this.IsEnabled
|
IsEnabled = this.IsEnabled,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue