using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace IPA.Config
{
///
/// Allows to get and set preferences for your mod.
///
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;
///
/// Constructs a ModPrefs object for the provide plugin.
///
/// the plugin to get the preferences file for
public ModPrefs(IBeatSaberPlugin plugin) {
Instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "ModPrefs", $"{plugin.Name}.ini"));
ModPrefses.Add(plugin, this);
}
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 != "")
return value;
else if (autoSave)
(this as IModPrefs).SetString(section, name, defaultValue);
return defaultValue;
}
///
/// 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 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;
else if (autoSave)
(this as IModPrefs).SetInt(section, name, defaultValue);
return defaultValue;
}
///
/// 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 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;
else if (autoSave)
(this as IModPrefs).SetFloat(section, name, defaultValue);
return defaultValue;
}
///
/// 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 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")
{
return sVal == "1";
}
else if (autoSave)
{
(this as IModPrefs).SetBool(section, name, defaultValue);
}
return defaultValue;
}
///
/// 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 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 static 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 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 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 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 static void SetBool(string section, string name, bool value)
=> StaticInstace.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.ModPrefses.First(o => o.Key == plugin).Value;
}
}
}