From 1ffd2d9433363bffb1624cd8ad40585e9fdc7b4c Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sun, 29 Jul 2018 22:59:10 -0500 Subject: [PATCH] Added backwards compatability for ModPrefs --- IllusionPlugin/ModPrefs.cs | 190 +++++++++++++++++++++++++++++-------- 1 file changed, 148 insertions(+), 42 deletions(-) diff --git a/IllusionPlugin/ModPrefs.cs b/IllusionPlugin/ModPrefs.cs index 4f49e3ee..0db65aff 100644 --- a/IllusionPlugin/ModPrefs.cs +++ b/IllusionPlugin/ModPrefs.cs @@ -10,7 +10,97 @@ namespace IllusionPlugin /// /// Allows to get and set preferences for your mod. /// - public class ModPrefs { + public interface IModPrefs + { + /// + /// Gets a string from the ini. + /// + /// Section of the key. + /// Name of the key. + /// Value that should be used when no value is found. + /// Whether or not the default value should be written if no value is found. + /// + string GetString(string section, string name, string defaultValue = "", bool autoSave = false); + /// + /// Gets an int from the ini. + /// + /// Section of the key. + /// Name of the key. + /// Value that should be used when no value is found. + /// Whether or not the default value should be written if no value is found. + /// + int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false); + /// + /// Gets a float from the ini. + /// + /// Section of the key. + /// Name of the key. + /// Value that should be used when no value is found. + /// Whether or not the default value should be written if no value is found. + /// + float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false); + /// + /// Gets a bool from the ini. + /// + /// Section of the key. + /// Name of the key. + /// Value that should be used when no value is found. + /// Whether or not the default value should be written if no value is found. + /// + bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false); + /// + /// Checks whether or not a key exists in the ini. + /// + /// Section of the key. + /// Name of the key. + /// + bool HasKey(string section, string name); + /// + /// Sets a float in the ini. + /// + /// Section of the key. + /// Name of the key. + /// Value that should be written. + void SetFloat(string section, string name, float value); + /// + /// Sets an int in the ini. + /// + /// Section of the key. + /// Name of the key. + /// Value that should be written. + void SetInt(string section, string name, int value); + /// + /// Sets a string in the ini. + /// + /// Section of the key. + /// Name of the key. + /// Value that should be written. + void SetString(string section, string name, string value); + /// + /// Sets a bool in the ini. + /// + /// Section of the key. + /// Name of the key. + /// Value that should be written. + void SetBool(string section, string name, bool value); + } + + /// + /// Allows to get and set preferences for your mod. + /// + public class ModPrefs : IModPrefs + { + private static ModPrefs _staticInstance = null; + private static IModPrefs StaticInstace + { + get + { + if (_staticInstance == null) + _staticInstance = new ModPrefs(); + return _staticInstance; + } + } + internal static Dictionary ModPrefses { get; set; } = new Dictionary(); private IniFile Instance; @@ -20,19 +110,16 @@ namespace IllusionPlugin /// /// the plugin to get the preferences file for public ModPrefs(IBeatSaberPlugin plugin) { - Instance = new IniFile(Path.Combine(Environment.CurrentDirectory, $"UserData/ModPrefs/{plugin.Name}.ini")); + Instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "ModPrefs", $"{plugin.Name}.ini")); ModPrefses.Add(plugin, this); } - /// - /// Gets a string from the ini. - /// - /// Section of the key. - /// Name of the key. - /// Value that should be used when no value is found. - /// Whether or not the default value should be written if no value is found. - /// - public string GetString(string section, string name, string defaultValue = "", bool autoSave = false) + private ModPrefs() + { + Instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "modprefs.ini")); + } + + string IModPrefs.GetString(string section, string name, string defaultValue, bool autoSave) { var value = Instance.IniReadValue(section, name); if (value != "") @@ -42,16 +129,18 @@ namespace IllusionPlugin return defaultValue; } - /// - /// Gets an int from the ini. + /// Gets a string from the ini. /// /// Section of the key. /// Name of the key. /// Value that should be used when no value is found. /// Whether or not the default value should be written if no value is found. /// - public int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false) + public static string GetString(string section, string name, string defaultValue = "", bool autoSave = false) + => StaticInstace.GetString(section, name, defaultValue, autoSave); + + int IModPrefs.GetInt(string section, string name, int defaultValue, bool autoSave) { if (int.TryParse(Instance.IniReadValue(section, name), out var value)) return value; @@ -60,17 +149,18 @@ namespace IllusionPlugin return defaultValue; } - - /// - /// Gets a float from the ini. + /// Gets an int from the ini. /// /// Section of the key. /// Name of the key. /// Value that should be used when no value is found. /// Whether or not the default value should be written if no value is found. /// - public float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false) + public static int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false) + => StaticInstace.GetInt(section, name, defaultValue, autoSave); + + float IModPrefs.GetFloat(string section, string name, float defaultValue, bool autoSave) { if (float.TryParse(Instance.IniReadValue(section, name), out var value)) return value; @@ -79,16 +169,18 @@ namespace IllusionPlugin return defaultValue; } - /// - /// Gets a bool from the ini. + /// Gets a float from the ini. /// /// Section of the key. /// Name of the key. /// Value that should be used when no value is found. /// Whether or not the default value should be written if no value is found. /// - public bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false) + public static float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false) + => StaticInstace.GetFloat(section, name, defaultValue, autoSave); + + bool IModPrefs.GetBool(string section, string name, bool defaultValue, bool autoSave) { string sVal = GetString(section, name, null); if (sVal == "1" || sVal == "0") @@ -101,66 +193,80 @@ namespace IllusionPlugin return defaultValue; } - - /// - /// Checks whether or not a key exists in the ini. + /// Gets a bool from the ini. /// /// Section of the key. /// Name of the key. + /// Value that should be used when no value is found. + /// Whether or not the default value should be written if no value is found. /// - public bool HasKey(string section, string name) + public static bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false) + => StaticInstace.GetBool(section, name, defaultValue, autoSave); + + bool IModPrefs.HasKey(string section, string name) { return Instance.IniReadValue(section, name) != null; } + /// + /// Checks whether or not a key exists in the ini. + /// + /// Section of the key. + /// Name of the key. + /// + public bool HasKey(string section, string name) => StaticInstace.HasKey(section, name); + void IModPrefs.SetFloat(string section, string name, float value) + { + Instance.IniWriteValue(section, name, value.ToString()); + } /// /// Sets a float in the ini. /// /// Section of the key. /// Name of the key. /// Value that should be written. - public void SetFloat(string section, string name, float value) + public static void SetFloat(string section, string name, float value) + => StaticInstace.SetFloat(section, name, value); + + void IModPrefs.SetInt(string section, string name, int value) { Instance.IniWriteValue(section, name, value.ToString()); } - /// /// Sets an int in the ini. /// /// Section of the key. /// Name of the key. /// Value that should be written. - public void SetInt(string section, string name, int value) - { - Instance.IniWriteValue(section, name, value.ToString()); + public static void SetInt(string section, string name, int value) + => StaticInstace.SetInt(section, name, value); + void IModPrefs.SetString(string section, string name, string value) + { + Instance.IniWriteValue(section, name, value); } - /// /// Sets a string in the ini. /// /// Section of the key. /// Name of the key. /// Value that should be written. - public void SetString(string section, string name, string value) - { - Instance.IniWriteValue(section, name, value); + public static void SetString(string section, string name, string value) + => StaticInstace.SetString(section, name, value); + void IModPrefs.SetBool(string section, string name, bool value) + { + Instance.IniWriteValue(section, name, value ? "1" : "0"); } - /// /// Sets a bool in the ini. /// /// Section of the key. /// Name of the key. /// Value that should be written. - public void SetBool(string section, string name, bool value) - { - Instance.IniWriteValue(section, name, value ? "1" : "0"); - - } - + public static void SetBool(string section, string name, bool value) + => StaticInstace.SetBool(section, name, value); } /// @@ -172,7 +278,7 @@ namespace IllusionPlugin /// /// the plugin wanting the prefrences /// the ModPrefs object - public static ModPrefs GetModPrefs(this IBeatSaberPlugin plugin) { + public static IModPrefs GetModPrefs(this IBeatSaberPlugin plugin) { return ModPrefs.ModPrefses.First(o => o.Key == plugin).Value; } }