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.

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