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.

263 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
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. [Obsolete("Uses IniFile, which uses 16 bit system calls. Use BS Utils INI system for now.")]
  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. [Obsolete("Uses IniFile, which uses 16 bit system calls. Use BS Utils INI system for now.")]
  90. public class ModPrefs : IModPrefs
  91. {
  92. private static ModPrefs _staticInstance;
  93. private static IModPrefs StaticInstance => _staticInstance ?? (_staticInstance = new ModPrefs());
  94. private readonly IniFile _instance;
  95. /// <summary>
  96. /// Constructs a ModPrefs object for the provide plugin.
  97. /// </summary>
  98. /// <param name="plugin">the plugin to get the preferences file for</param>
  99. public ModPrefs(PluginLoader.PluginMetadata plugin) {
  100. _instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "ModPrefs",
  101. $"{plugin.Name}.ini"));
  102. }
  103. private ModPrefs()
  104. {
  105. _instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData", "modprefs.ini"));
  106. }
  107. string IModPrefs.GetString(string section, string name, string defaultValue, bool autoSave)
  108. {
  109. var value = _instance.IniReadValue(section, name);
  110. if (value != "")
  111. return value;
  112. else if (autoSave)
  113. (this as IModPrefs).SetString(section, name, defaultValue);
  114. return defaultValue;
  115. }
  116. /// <summary>
  117. /// Gets a string from the ini.
  118. /// </summary>
  119. /// <param name="section">Section of the key.</param>
  120. /// <param name="name">Name of the key.</param>
  121. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  122. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  123. /// <returns></returns>
  124. public static string GetString(string section, string name, string defaultValue = "", bool autoSave = false)
  125. => StaticInstance.GetString(section, name, defaultValue, autoSave);
  126. int IModPrefs.GetInt(string section, string name, int defaultValue, bool autoSave)
  127. {
  128. if (int.TryParse(_instance.IniReadValue(section, name), out var value))
  129. return value;
  130. else if (autoSave)
  131. (this as IModPrefs).SetInt(section, name, defaultValue);
  132. return defaultValue;
  133. }
  134. /// <summary>
  135. /// Gets an int from the ini.
  136. /// </summary>
  137. /// <param name="section">Section of the key.</param>
  138. /// <param name="name">Name of the key.</param>
  139. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  140. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  141. /// <returns></returns>
  142. public static int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false)
  143. => StaticInstance.GetInt(section, name, defaultValue, autoSave);
  144. float IModPrefs.GetFloat(string section, string name, float defaultValue, bool autoSave)
  145. {
  146. if (float.TryParse(_instance.IniReadValue(section, name), out var value))
  147. return value;
  148. else if (autoSave)
  149. (this as IModPrefs).SetFloat(section, name, defaultValue);
  150. return defaultValue;
  151. }
  152. /// <summary>
  153. /// Gets a float from the ini.
  154. /// </summary>
  155. /// <param name="section">Section of the key.</param>
  156. /// <param name="name">Name of the key.</param>
  157. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  158. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  159. /// <returns></returns>
  160. public static float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false)
  161. => StaticInstance.GetFloat(section, name, defaultValue, autoSave);
  162. bool IModPrefs.GetBool(string section, string name, bool defaultValue, bool autoSave)
  163. {
  164. string sVal = GetString(section, name, null);
  165. if (sVal == "1" || sVal == "0")
  166. {
  167. return sVal == "1";
  168. }
  169. else if (autoSave)
  170. {
  171. (this as IModPrefs).SetBool(section, name, defaultValue);
  172. }
  173. return defaultValue;
  174. }
  175. /// <summary>
  176. /// Gets a bool from the ini.
  177. /// </summary>
  178. /// <param name="section">Section of the key.</param>
  179. /// <param name="name">Name of the key.</param>
  180. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  181. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  182. /// <returns></returns>
  183. public static bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false)
  184. => StaticInstance.GetBool(section, name, defaultValue, autoSave);
  185. bool IModPrefs.HasKey(string section, string name)
  186. {
  187. return _instance.IniReadValue(section, name) != null;
  188. }
  189. /// <summary>
  190. /// Checks whether or not a key exists in the ini.
  191. /// </summary>
  192. /// <param name="section">Section of the key.</param>
  193. /// <param name="name">Name of the key.</param>
  194. /// <returns></returns>
  195. public static bool HasKey(string section, string name) => StaticInstance.HasKey(section, name);
  196. void IModPrefs.SetFloat(string section, string name, float value)
  197. {
  198. _instance.IniWriteValue(section, name, value.ToString(CultureInfo.InvariantCulture));
  199. }
  200. /// <summary>
  201. /// Sets a float in the ini.
  202. /// </summary>
  203. /// <param name="section">Section of the key.</param>
  204. /// <param name="name">Name of the key.</param>
  205. /// <param name="value">Value that should be written.</param>
  206. public static void SetFloat(string section, string name, float value)
  207. => StaticInstance.SetFloat(section, name, value);
  208. void IModPrefs.SetInt(string section, string name, int value)
  209. {
  210. _instance.IniWriteValue(section, name, value.ToString());
  211. }
  212. /// <summary>
  213. /// Sets an int in the ini.
  214. /// </summary>
  215. /// <param name="section">Section of the key.</param>
  216. /// <param name="name">Name of the key.</param>
  217. /// <param name="value">Value that should be written.</param>
  218. public static void SetInt(string section, string name, int value)
  219. => StaticInstance.SetInt(section, name, value);
  220. void IModPrefs.SetString(string section, string name, string value)
  221. {
  222. _instance.IniWriteValue(section, name, value);
  223. }
  224. /// <summary>
  225. /// Sets a string in the ini.
  226. /// </summary>
  227. /// <param name="section">Section of the key.</param>
  228. /// <param name="name">Name of the key.</param>
  229. /// <param name="value">Value that should be written.</param>
  230. public static void SetString(string section, string name, string value)
  231. => StaticInstance.SetString(section, name, value);
  232. void IModPrefs.SetBool(string section, string name, bool value)
  233. {
  234. _instance.IniWriteValue(section, name, value ? "1" : "0");
  235. }
  236. /// <summary>
  237. /// Sets a bool in the ini.
  238. /// </summary>
  239. /// <param name="section">Section of the key.</param>
  240. /// <param name="name">Name of the key.</param>
  241. /// <param name="value">Value that should be written.</param>
  242. public static void SetBool(string section, string name, bool value)
  243. => StaticInstance.SetBool(section, name, value);
  244. }
  245. }