From 54bcf2e30efc700b6669afc35be3a62ce736d6e9 Mon Sep 17 00:00:00 2001 From: DaNike Date: Tue, 29 Jan 2019 00:36:13 -0600 Subject: [PATCH] Changed config to take PluginMetadata --- IPA.Loader/Config/Config.cs | 46 +++++++++++++------ .../ConfigProviders/JsonConfigProvider.cs | 2 + IPA.Loader/Config/ModPrefs.cs | 23 +--------- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/IPA.Loader/Config/Config.cs b/IPA.Loader/Config/Config.cs index a2232fd7..68169fca 100644 --- a/IPA.Loader/Config/Config.cs +++ b/IPA.Loader/Config/Config.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Reflection; using IPA.Config.ConfigProviders; @@ -65,6 +66,29 @@ namespace IPA.Config } } + /// + /// + /// Specifies a preferred config name, instead of using the plugin's name. + /// + public class NameAttribute : Attribute + { + /// + /// The name to use for the config. + /// + // ReSharper disable once UnusedAutoPropertyAccessor.Global + public string Name { get; private set; } + + /// + /// + /// Constructs the attribute with a specific name. + /// + /// the name to use for the config. + public NameAttribute(string name) + { + Name = name; + } + } + private static readonly Dictionary registeredProviders = new Dictionary(); /// @@ -96,36 +120,32 @@ namespace IPA.Config /// /// Gets an using the specified list pf preferred config types. /// - /// the name of the file to associate it with + /// the name of the mod for this config /// the preferred config types to try to get /// an of the requested type, or of type JSON. - public static IConfigProvider GetProviderFor(string filename, params string[] extensions) + public static IConfigProvider GetProviderFor(string configName, params string[] extensions) { var chosenExt = extensions.FirstOrDefault(s => registeredProviders.ContainsKey(s)) ?? "json"; var type = registeredProviders[chosenExt]; var provider = Activator.CreateInstance(type) as IConfigProvider; if (provider != null) { - provider.Filename = filename; + provider.Filename = Path.Combine(BeatSaber.UserDataPath, configName); configProviders.Add(provider.LastModified, provider); } return provider; } - - /// - /// Gets an using the specified list pf preferred config types. - /// - /// the name of the file to associate it with - /// the parameter info to try and get info for - /// an of the requested type, or of type JSON. - public static IConfigProvider GetProviderFor(string filename, ParameterInfo info) + + internal static IConfigProvider GetProviderFor(string modName, ParameterInfo info) { var prefs = new string[0]; - if (info.GetCustomAttribute(typeof(PreferAttribute)) is PreferAttribute prefer) + if (info.GetCustomAttribute() is PreferAttribute prefer) prefs = prefer.PreferenceOrder; + if (info.GetCustomAttribute() is NameAttribute name) + modName = name.Name; - return GetProviderFor(filename, prefs); + return GetProviderFor(modName, prefs); } private static Dictionary linkedProviders = diff --git a/IPA.Loader/Config/ConfigProviders/JsonConfigProvider.cs b/IPA.Loader/Config/ConfigProviders/JsonConfigProvider.cs index 84396786..be81baea 100644 --- a/IPA.Loader/Config/ConfigProviders/JsonConfigProvider.cs +++ b/IPA.Loader/Config/ConfigProviders/JsonConfigProvider.cs @@ -103,6 +103,8 @@ namespace IPA.Config.ConfigProviders public void Save() { Logger.config.Debug($"Saving file {Filename}.json"); + if (!Directory.Exists(Path.GetDirectoryName(Filename))) + Directory.CreateDirectory(Path.GetDirectoryName(Filename) ?? throw new InvalidOperationException()); File.WriteAllText(Filename + ".json", JsonConvert.SerializeObject(jsonObj, Formatting.Indented)); HasChanged = false; diff --git a/IPA.Loader/Config/ModPrefs.cs b/IPA.Loader/Config/ModPrefs.cs index 16dd5c7b..e63002f7 100644 --- a/IPA.Loader/Config/ModPrefs.cs +++ b/IPA.Loader/Config/ModPrefs.cs @@ -1,8 +1,7 @@ using System; -using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Linq; +using IPA.Loader; namespace IPA.Config { @@ -93,19 +92,15 @@ namespace IPA.Config private static ModPrefs _staticInstance; private static IModPrefs StaticInstance => _staticInstance ?? (_staticInstance = new ModPrefs()); - // ReSharper disable once IdentifierTypo - internal static Dictionary ModPrefss { get; set; } = new Dictionary(); - private readonly IniFile _instance; /// /// Constructs a ModPrefs object for the provide plugin. /// /// the plugin to get the preferences file for - public ModPrefs(IBeatSaberPlugin plugin) { + public ModPrefs(PluginLoader.PluginMetadata plugin) { _instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "ModPrefs", $"{plugin.Name}.ini")); - ModPrefss.Add(plugin, this); } private ModPrefs() @@ -263,18 +258,4 @@ namespace IPA.Config public static void SetBool(string section, string name, bool value) => StaticInstance.SetBool(section, name, value); } - - /// - /// An extension class for IBeatSaberPlugins. - /// - public static class ModPrefsExtensions { - /// - /// Gets the ModPrefs object for the provided plugin. - /// - /// the plugin wanting the prefrences - /// the ModPrefs object - public static IModPrefs GetModPrefs(this IBeatSaberPlugin plugin) { - return ModPrefs.ModPrefss.First(o => o.Key == plugin).Value; - } - } }