You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
12 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace IllusionPlugin
  8. {
  9. /// <summary>
  10. /// Allows to get and set preferences for your mod.
  11. /// </summary>
  12. public interface IModPrefs
  13. {
  14. /// <summary>
  15. /// Gets a string from the ini.
  16. /// </summary>
  17. /// <param name="section">Section of the key.</param>
  18. /// <param name="name">Name of the key.</param>
  19. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  20. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  21. /// <returns></returns>
  22. string GetString(string section, string name, string defaultValue = "", bool autoSave = false);
  23. /// <summary>
  24. /// Gets an int from the ini.
  25. /// </summary>
  26. /// <param name="section">Section of the key.</param>
  27. /// <param name="name">Name of the key.</param>
  28. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  29. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  30. /// <returns></returns>
  31. int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false);
  32. /// <summary>
  33. /// Gets a float from the ini.
  34. /// </summary>
  35. /// <param name="section">Section of the key.</param>
  36. /// <param name="name">Name of the key.</param>
  37. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  38. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  39. /// <returns></returns>
  40. float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false);
  41. /// <summary>
  42. /// Gets a bool from the ini.
  43. /// </summary>
  44. /// <param name="section">Section of the key.</param>
  45. /// <param name="name">Name of the key.</param>
  46. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  47. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  48. /// <returns></returns>
  49. bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false);
  50. /// <summary>
  51. /// Checks whether or not a key exists in the ini.
  52. /// </summary>
  53. /// <param name="section">Section of the key.</param>
  54. /// <param name="name">Name of the key.</param>
  55. /// <returns></returns>
  56. bool HasKey(string section, string name);
  57. /// <summary>
  58. /// Sets a float in the ini.
  59. /// </summary>
  60. /// <param name="section">Section of the key.</param>
  61. /// <param name="name">Name of the key.</param>
  62. /// <param name="value">Value that should be written.</param>
  63. void SetFloat(string section, string name, float value);
  64. /// <summary>
  65. /// Sets an int in the ini.
  66. /// </summary>
  67. /// <param name="section">Section of the key.</param>
  68. /// <param name="name">Name of the key.</param>
  69. /// <param name="value">Value that should be written.</param>
  70. void SetInt(string section, string name, int value);
  71. /// <summary>
  72. /// Sets a string in the ini.
  73. /// </summary>
  74. /// <param name="section">Section of the key.</param>
  75. /// <param name="name">Name of the key.</param>
  76. /// <param name="value">Value that should be written.</param>
  77. void SetString(string section, string name, string value);
  78. /// <summary>
  79. /// Sets a bool in the ini.
  80. /// </summary>
  81. /// <param name="section">Section of the key.</param>
  82. /// <param name="name">Name of the key.</param>
  83. /// <param name="value">Value that should be written.</param>
  84. void SetBool(string section, string name, bool value);
  85. }
  86. /// <summary>
  87. /// Allows to get and set preferences for your mod.
  88. /// </summary>
  89. public class ModPrefs : IModPrefs
  90. {
  91. private static ModPrefs _staticInstance = null;
  92. private static IModPrefs StaticInstace
  93. {
  94. get
  95. {
  96. if (_staticInstance == null)
  97. _staticInstance = new ModPrefs();
  98. return _staticInstance;
  99. }
  100. }
  101. internal static Dictionary<IBeatSaberPlugin, ModPrefs> ModPrefses { get; set; } = new Dictionary<IBeatSaberPlugin, ModPrefs>();
  102. private IniFile Instance;
  103. /// <summary>
  104. /// Constructs a ModPrefs object for the provide plugin.
  105. /// </summary>
  106. /// <param name="plugin">the plugin to get the preferences file for</param>
  107. public ModPrefs(IBeatSaberPlugin plugin) {
  108. Instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "ModPrefs", $"{plugin.Name}.ini"));
  109. ModPrefses.Add(plugin, this);
  110. }
  111. private ModPrefs()
  112. {
  113. Instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "modprefs.ini"));
  114. }
  115. string IModPrefs.GetString(string section, string name, string defaultValue, bool autoSave)
  116. {
  117. var value = Instance.IniReadValue(section, name);
  118. if (value != "")
  119. return value;
  120. else if (autoSave)
  121. SetString(section, name, defaultValue);
  122. return defaultValue;
  123. }
  124. /// <summary>
  125. /// Gets a string from the ini.
  126. /// </summary>
  127. /// <param name="section">Section of the key.</param>
  128. /// <param name="name">Name of the key.</param>
  129. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  130. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  131. /// <returns></returns>
  132. public static string GetString(string section, string name, string defaultValue = "", bool autoSave = false)
  133. => StaticInstace.GetString(section, name, defaultValue, autoSave);
  134. int IModPrefs.GetInt(string section, string name, int defaultValue, bool autoSave)
  135. {
  136. if (int.TryParse(Instance.IniReadValue(section, name), out var value))
  137. return value;
  138. else if (autoSave)
  139. SetInt(section, name, defaultValue);
  140. return defaultValue;
  141. }
  142. /// <summary>
  143. /// Gets an int from the ini.
  144. /// </summary>
  145. /// <param name="section">Section of the key.</param>
  146. /// <param name="name">Name of the key.</param>
  147. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  148. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  149. /// <returns></returns>
  150. public static int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false)
  151. => StaticInstace.GetInt(section, name, defaultValue, autoSave);
  152. float IModPrefs.GetFloat(string section, string name, float defaultValue, bool autoSave)
  153. {
  154. if (float.TryParse(Instance.IniReadValue(section, name), out var value))
  155. return value;
  156. else if (autoSave)
  157. SetFloat(section, name, defaultValue);
  158. return defaultValue;
  159. }
  160. /// <summary>
  161. /// Gets a float from the ini.
  162. /// </summary>
  163. /// <param name="section">Section of the key.</param>
  164. /// <param name="name">Name of the key.</param>
  165. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  166. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  167. /// <returns></returns>
  168. public static float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false)
  169. => StaticInstace.GetFloat(section, name, defaultValue, autoSave);
  170. bool IModPrefs.GetBool(string section, string name, bool defaultValue, bool autoSave)
  171. {
  172. string sVal = GetString(section, name, null);
  173. if (sVal == "1" || sVal == "0")
  174. {
  175. return sVal == "1";
  176. } else if (autoSave)
  177. {
  178. SetBool(section, name, defaultValue);
  179. }
  180. return defaultValue;
  181. }
  182. /// <summary>
  183. /// Gets a bool from the ini.
  184. /// </summary>
  185. /// <param name="section">Section of the key.</param>
  186. /// <param name="name">Name of the key.</param>
  187. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  188. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  189. /// <returns></returns>
  190. public static bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false)
  191. => StaticInstace.GetBool(section, name, defaultValue, autoSave);
  192. bool IModPrefs.HasKey(string section, string name)
  193. {
  194. return Instance.IniReadValue(section, name) != null;
  195. }
  196. /// <summary>
  197. /// Checks whether or not a key exists in the ini.
  198. /// </summary>
  199. /// <param name="section">Section of the key.</param>
  200. /// <param name="name">Name of the key.</param>
  201. /// <returns></returns>
  202. public bool HasKey(string section, string name) => StaticInstace.HasKey(section, name);
  203. void IModPrefs.SetFloat(string section, string name, float value)
  204. {
  205. Instance.IniWriteValue(section, name, value.ToString());
  206. }
  207. /// <summary>
  208. /// Sets a float in the ini.
  209. /// </summary>
  210. /// <param name="section">Section of the key.</param>
  211. /// <param name="name">Name of the key.</param>
  212. /// <param name="value">Value that should be written.</param>
  213. public static void SetFloat(string section, string name, float value)
  214. => StaticInstace.SetFloat(section, name, value);
  215. void IModPrefs.SetInt(string section, string name, int value)
  216. {
  217. Instance.IniWriteValue(section, name, value.ToString());
  218. }
  219. /// <summary>
  220. /// Sets an int in the ini.
  221. /// </summary>
  222. /// <param name="section">Section of the key.</param>
  223. /// <param name="name">Name of the key.</param>
  224. /// <param name="value">Value that should be written.</param>
  225. public static void SetInt(string section, string name, int value)
  226. => StaticInstace.SetInt(section, name, value);
  227. void IModPrefs.SetString(string section, string name, string value)
  228. {
  229. Instance.IniWriteValue(section, name, value);
  230. }
  231. /// <summary>
  232. /// Sets a string in the ini.
  233. /// </summary>
  234. /// <param name="section">Section of the key.</param>
  235. /// <param name="name">Name of the key.</param>
  236. /// <param name="value">Value that should be written.</param>
  237. public static void SetString(string section, string name, string value)
  238. => StaticInstace.SetString(section, name, value);
  239. void IModPrefs.SetBool(string section, string name, bool value)
  240. {
  241. Instance.IniWriteValue(section, name, value ? "1" : "0");
  242. }
  243. /// <summary>
  244. /// Sets a bool in the ini.
  245. /// </summary>
  246. /// <param name="section">Section of the key.</param>
  247. /// <param name="name">Name of the key.</param>
  248. /// <param name="value">Value that should be written.</param>
  249. public static void SetBool(string section, string name, bool value)
  250. => StaticInstace.SetBool(section, name, value);
  251. }
  252. /// <summary>
  253. /// An extension class for IBeatSaberPlugins.
  254. /// </summary>
  255. public static class ModPrefsExtensions {
  256. /// <summary>
  257. /// Gets the ModPrefs object for the provided plugin.
  258. /// </summary>
  259. /// <param name="plugin">the plugin wanting the prefrences</param>
  260. /// <returns>the ModPrefs object</returns>
  261. public static IModPrefs GetModPrefs(this IBeatSaberPlugin plugin) {
  262. return ModPrefs.ModPrefses.First(o => o.Key == plugin).Value;
  263. }
  264. }
  265. }