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.

279 lines
12 KiB

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