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.

266 lines
12 KiB

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