From eefcf83a5de1dab6cd04e3497ab1623275bffeae Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sat, 7 Dec 2019 14:47:18 -0600 Subject: [PATCH] Finalized new Config usage --- IPA.Loader/Config/Config.cs | 92 ++++++++++++---------- IPA.Loader/Config/SelfConfig.cs | 2 +- IPA.Loader/Config/Stores/GeneratedStore.cs | 29 +++++++ IPA.Loader/Loader/DisabledConfig.cs | 2 +- 4 files changed, 80 insertions(+), 45 deletions(-) diff --git a/IPA.Loader/Config/Config.cs b/IPA.Loader/Config/Config.cs index a9114a19..ff9be00e 100644 --- a/IPA.Loader/Config/Config.cs +++ b/IPA.Loader/Config/Config.cs @@ -9,6 +9,7 @@ using IPA.Utilities; #if NET3 using Net3_Proxy; using Path = Net3_Proxy.Path; +using Array = Net3_Proxy.Array; #endif namespace IPA.Config @@ -16,39 +17,13 @@ namespace IPA.Config /// /// A class to handle updating ConfigProviders automatically /// - public static class Config + public class Config { static Config() { //JsonConfigProvider.RegisterConfig(); } - /// - /// - /// Defines the type of the - /// - [AttributeUsage(AttributeTargets.Class)] - public class TypeAttribute : Attribute - { - /// - /// The extension associated with this type, without the '.' - /// - /// the extension to register the config provider as - // ReSharper disable once UnusedAutoPropertyAccessor.Global - public string Extension { get; private set; } - - /// - /// - /// Constructs the attribute with a specified extension. - /// - /// the extension associated with this type, without the '.' - public TypeAttribute(string ext) - { - Extension = ext; - } - } - - /// /// /// Specifies that a particular parameter is preferred to be a specific type of . If it is not available, also specifies backups. If none are available, the default is used. /// @@ -73,10 +48,10 @@ namespace IPA.Config } } - /// /// /// Specifies a preferred config name, instead of using the plugin's name. /// + [AttributeUsage(AttributeTargets.Parameter)] public class NameAttribute : Attribute { /// @@ -97,7 +72,7 @@ namespace IPA.Config } } - private static readonly Dictionary registeredProviders = new Dictionary(); + private static readonly Dictionary registeredProviders = new Dictionary(); /// /// Registers a to use for configs. @@ -118,39 +93,70 @@ namespace IPA.Config if (registeredProviders.ContainsKey(inst.Extension)) throw new InvalidOperationException($"Extension provider for {inst.Extension} already exists"); - registeredProviders.Add(inst.Extension, type); + registeredProviders.Add(inst.Extension, inst); } - private static List configProviders = new List(); - private static ConditionalWeakTable file = new ConditionalWeakTable(); + private static Dictionary files = new Dictionary(); /// - /// Gets an using the specified list of preferred config types. + /// Gets a object using the specified list of preferred config types. /// /// 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 configName, params string[] extensions) + /// a using the requested format, or of type JSON. + public static Config GetConfigFor(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; - configProviders.Add(provider); + var provider = registeredProviders[chosenExt]; + + var config = new Config(configName, provider); - // TODO: rething this one a bit + var filename = Path.Combine(BeatSaber.UserDataPath, configName + "." + provider.Extension); + files.Add(config, new FileInfo(filename)); - return provider; + RegisterConfigObject(config); + + return config; } - internal static IConfigProvider GetProviderFor(string modName, ParameterInfo info) + internal static Config GetProviderFor(string modName, ParameterInfo info) { - var prefs = new string[0]; + var prefs = Array.Empty(); if (info.GetCustomAttribute() is PreferAttribute prefer) prefs = prefer.PreferenceOrder; if (info.GetCustomAttribute() is NameAttribute name) modName = name.Name; - return GetProviderFor(modName, prefs); + return GetConfigFor(modName, prefs); + } + + private static void RegisterConfigObject(Config obj) + { + // TODO: implement + } + + /// + /// Gets the name associated with this object. + /// + public string Name { get; private set; } + /// + /// Gets the associated with this object. + /// + public IConfigProvider Provider { get; private set; } + + internal readonly HashSet Stores = new HashSet(); + + /// + /// Adds an to this object. + /// + /// the to add to this instance + /// if the was not already registered to this object, + /// otherwise + public bool AddStore(IConfigStore store) => Stores.Add(store); + + private Config(string name, IConfigProvider provider) + { + Name = name; Provider = provider; } } } diff --git a/IPA.Loader/Config/SelfConfig.cs b/IPA.Loader/Config/SelfConfig.cs index 56cde03d..ff79205a 100644 --- a/IPA.Loader/Config/SelfConfig.cs +++ b/IPA.Loader/Config/SelfConfig.cs @@ -35,7 +35,7 @@ namespace IPA.Config public static void Load() { - LoaderConfig = Config.GetProviderFor(IPAName, "json"); + LoaderConfig = Config.GetConfigFor(IPAName, "json"); } public static void ReadCommandLine(string[] args) diff --git a/IPA.Loader/Config/Stores/GeneratedStore.cs b/IPA.Loader/Config/Stores/GeneratedStore.cs index b2c69b7b..0f4dd0d2 100644 --- a/IPA.Loader/Config/Stores/GeneratedStore.cs +++ b/IPA.Loader/Config/Stores/GeneratedStore.cs @@ -16,6 +16,35 @@ using Array = Net3_Proxy.Array; namespace IPA.Config.Stores { + /// + /// A class providing an extension for to make it easy to use generated + /// config stores. + /// + public static class GeneratedStoreExtensions + { + /// + /// Creates a generated of type , registers it to + /// the object, and returns it. + /// + /// + /// + /// must be a non- . + /// + /// + /// TODO: describe details of generated stores + /// + /// + /// the type to wrap + /// the to register to + /// a generated instance of as a special + public static T Generated(this Config cfg) where T : class + { + var ret = GeneratedStore.Create(); + cfg.AddStore(ret as IConfigStore); + return ret; + } + } + internal static class GeneratedStore { private interface IGeneratedStore diff --git a/IPA.Loader/Loader/DisabledConfig.cs b/IPA.Loader/Loader/DisabledConfig.cs index 255be034..8be87292 100644 --- a/IPA.Loader/Loader/DisabledConfig.cs +++ b/IPA.Loader/Loader/DisabledConfig.cs @@ -32,7 +32,7 @@ namespace IPA.Loader public static void Load() { - Provider = Config.Config.GetProviderFor("Disabled Mods", "json"); + Provider = Config.Config.GetConfigFor("Disabled Mods", "json"); } public bool Reset = true;