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.

280 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
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",
  102. $"{plugin.Name}.ini"));
  103. ModPrefss.Add(plugin, this);
  104. }
  105. private ModPrefs()
  106. {
  107. _instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "modprefs.ini"));
  108. }
  109. string IModPrefs.GetString(string section, string name, string defaultValue, bool autoSave)
  110. {
  111. var value = _instance.IniReadValue(section, name);
  112. if (value != "")
  113. return value;
  114. else if (autoSave)
  115. (this as IModPrefs).SetString(section, name, defaultValue);
  116. return defaultValue;
  117. }
  118. /// <summary>
  119. /// Gets a string from the ini.
  120. /// </summary>
  121. /// <param name="section">Section of the key.</param>
  122. /// <param name="name">Name of the key.</param>
  123. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  124. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  125. /// <returns></returns>
  126. public static string GetString(string section, string name, string defaultValue = "", bool autoSave = false)
  127. => StaticInstance.GetString(section, name, defaultValue, autoSave);
  128. int IModPrefs.GetInt(string section, string name, int defaultValue, bool autoSave)
  129. {
  130. if (int.TryParse(_instance.IniReadValue(section, name), out var value))
  131. return value;
  132. else if (autoSave)
  133. (this as IModPrefs).SetInt(section, name, defaultValue);
  134. return defaultValue;
  135. }
  136. /// <summary>
  137. /// Gets an int from the ini.
  138. /// </summary>
  139. /// <param name="section">Section of the key.</param>
  140. /// <param name="name">Name of the key.</param>
  141. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  142. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  143. /// <returns></returns>
  144. public static int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false)
  145. => StaticInstance.GetInt(section, name, defaultValue, autoSave);
  146. float IModPrefs.GetFloat(string section, string name, float defaultValue, bool autoSave)
  147. {
  148. if (float.TryParse(_instance.IniReadValue(section, name), out var value))
  149. return value;
  150. else if (autoSave)
  151. (this as IModPrefs).SetFloat(section, name, defaultValue);
  152. return defaultValue;
  153. }
  154. /// <summary>
  155. /// Gets a float from the ini.
  156. /// </summary>
  157. /// <param name="section">Section of the key.</param>
  158. /// <param name="name">Name of the key.</param>
  159. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  160. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  161. /// <returns></returns>
  162. public static float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false)
  163. => StaticInstance.GetFloat(section, name, defaultValue, autoSave);
  164. bool IModPrefs.GetBool(string section, string name, bool defaultValue, bool autoSave)
  165. {
  166. string sVal = GetString(section, name, null);
  167. if (sVal == "1" || sVal == "0")
  168. {
  169. return sVal == "1";
  170. }
  171. else if (autoSave)
  172. {
  173. (this as IModPrefs).SetBool(section, name, defaultValue);
  174. }
  175. return defaultValue;
  176. }
  177. /// <summary>
  178. /// Gets a bool from the ini.
  179. /// </summary>
  180. /// <param name="section">Section of the key.</param>
  181. /// <param name="name">Name of the key.</param>
  182. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  183. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  184. /// <returns></returns>
  185. public static bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false)
  186. => StaticInstance.GetBool(section, name, defaultValue, autoSave);
  187. bool IModPrefs.HasKey(string section, string name)
  188. {
  189. return _instance.IniReadValue(section, name) != null;
  190. }
  191. /// <summary>
  192. /// Checks whether or not a key exists in the ini.
  193. /// </summary>
  194. /// <param name="section">Section of the key.</param>
  195. /// <param name="name">Name of the key.</param>
  196. /// <returns></returns>
  197. public static bool HasKey(string section, string name) => StaticInstance.HasKey(section, name);
  198. void IModPrefs.SetFloat(string section, string name, float value)
  199. {
  200. _instance.IniWriteValue(section, name, value.ToString(CultureInfo.InvariantCulture));
  201. }
  202. /// <summary>
  203. /// Sets a float in the ini.
  204. /// </summary>
  205. /// <param name="section">Section of the key.</param>
  206. /// <param name="name">Name of the key.</param>
  207. /// <param name="value">Value that should be written.</param>
  208. public static void SetFloat(string section, string name, float value)
  209. => StaticInstance.SetFloat(section, name, value);
  210. void IModPrefs.SetInt(string section, string name, int value)
  211. {
  212. _instance.IniWriteValue(section, name, value.ToString());
  213. }
  214. /// <summary>
  215. /// Sets an int in the ini.
  216. /// </summary>
  217. /// <param name="section">Section of the key.</param>
  218. /// <param name="name">Name of the key.</param>
  219. /// <param name="value">Value that should be written.</param>
  220. public static void SetInt(string section, string name, int value)
  221. => StaticInstance.SetInt(section, name, value);
  222. void IModPrefs.SetString(string section, string name, string value)
  223. {
  224. _instance.IniWriteValue(section, name, value);
  225. }
  226. /// <summary>
  227. /// Sets a string in the ini.
  228. /// </summary>
  229. /// <param name="section">Section of the key.</param>
  230. /// <param name="name">Name of the key.</param>
  231. /// <param name="value">Value that should be written.</param>
  232. public static void SetString(string section, string name, string value)
  233. => StaticInstance.SetString(section, name, value);
  234. void IModPrefs.SetBool(string section, string name, bool value)
  235. {
  236. _instance.IniWriteValue(section, name, value ? "1" : "0");
  237. }
  238. /// <summary>
  239. /// Sets a bool in the ini.
  240. /// </summary>
  241. /// <param name="section">Section of the key.</param>
  242. /// <param name="name">Name of the key.</param>
  243. /// <param name="value">Value that should be written.</param>
  244. public static void SetBool(string section, string name, bool value)
  245. => StaticInstance.SetBool(section, name, value);
  246. }
  247. /// <summary>
  248. /// An extension class for IBeatSaberPlugins.
  249. /// </summary>
  250. public static class ModPrefsExtensions {
  251. /// <summary>
  252. /// Gets the ModPrefs object for the provided plugin.
  253. /// </summary>
  254. /// <param name="plugin">the plugin wanting the prefrences</param>
  255. /// <returns>the ModPrefs object</returns>
  256. public static IModPrefs GetModPrefs(this IBeatSaberPlugin plugin) {
  257. return ModPrefs.ModPrefss.First(o => o.Key == plugin).Value;
  258. }
  259. }
  260. }