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.

167 lines
6.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace IllusionPlugin
  8. {
  9. /// <summary>
  10. /// Allows to get and set preferences for your mod.
  11. /// </summary>
  12. public class ModPrefs {
  13. internal static Dictionary<IPlugin, ModPrefs> ModPrefses { get; set; } = new Dictionary<IPlugin, ModPrefs>();
  14. private IniFile Instance;
  15. public ModPrefs(IPlugin plugin) {
  16. Instance = new IniFile(Path.Combine(Environment.CurrentDirectory, $"UserData/ModPrefs/{plugin.Name}.ini"));
  17. ModPrefses.Add(plugin, this);
  18. }
  19. /// <summary>
  20. /// Gets a string from the ini.
  21. /// </summary>
  22. /// <param name="section">Section of the key.</param>
  23. /// <param name="name">Name of the key.</param>
  24. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  25. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  26. /// <returns></returns>
  27. public string GetString(string section, string name, string defaultValue = "", bool autoSave = false)
  28. {
  29. var value = Instance.IniReadValue(section, name);
  30. if (value != "")
  31. return value;
  32. else if (autoSave)
  33. SetString(section, name, defaultValue);
  34. return defaultValue;
  35. }
  36. /// <summary>
  37. /// Gets an int from the ini.
  38. /// </summary>
  39. /// <param name="section">Section of the key.</param>
  40. /// <param name="name">Name of the key.</param>
  41. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  42. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  43. /// <returns></returns>
  44. public int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false)
  45. {
  46. if (int.TryParse(Instance.IniReadValue(section, name), out var value))
  47. return value;
  48. else if (autoSave)
  49. SetInt(section, name, defaultValue);
  50. return defaultValue;
  51. }
  52. /// <summary>
  53. /// Gets a float from the ini.
  54. /// </summary>
  55. /// <param name="section">Section of the key.</param>
  56. /// <param name="name">Name of the key.</param>
  57. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  58. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  59. /// <returns></returns>
  60. public float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false)
  61. {
  62. if (float.TryParse(Instance.IniReadValue(section, name), out var value))
  63. return value;
  64. else if (autoSave)
  65. SetFloat(section, name, defaultValue);
  66. return defaultValue;
  67. }
  68. /// <summary>
  69. /// Gets a bool from the ini.
  70. /// </summary>
  71. /// <param name="section">Section of the key.</param>
  72. /// <param name="name">Name of the key.</param>
  73. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  74. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  75. /// <returns></returns>
  76. public bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false)
  77. {
  78. string sVal = GetString(section, name, null);
  79. if (sVal == "1" || sVal == "0")
  80. {
  81. return sVal == "1";
  82. } else if (autoSave)
  83. {
  84. SetBool(section, name, defaultValue);
  85. }
  86. return defaultValue;
  87. }
  88. /// <summary>
  89. /// Checks whether or not a key exists in the ini.
  90. /// </summary>
  91. /// <param name="section">Section of the key.</param>
  92. /// <param name="name">Name of the key.</param>
  93. /// <returns></returns>
  94. public bool HasKey(string section, string name)
  95. {
  96. return Instance.IniReadValue(section, name) != null;
  97. }
  98. /// <summary>
  99. /// Sets a float in the ini.
  100. /// </summary>
  101. /// <param name="section">Section of the key.</param>
  102. /// <param name="name">Name of the key.</param>
  103. /// <param name="value">Value that should be written.</param>
  104. public void SetFloat(string section, string name, float value)
  105. {
  106. Instance.IniWriteValue(section, name, value.ToString());
  107. }
  108. /// <summary>
  109. /// Sets an int in the ini.
  110. /// </summary>
  111. /// <param name="section">Section of the key.</param>
  112. /// <param name="name">Name of the key.</param>
  113. /// <param name="value">Value that should be written.</param>
  114. public void SetInt(string section, string name, int value)
  115. {
  116. Instance.IniWriteValue(section, name, value.ToString());
  117. }
  118. /// <summary>
  119. /// Sets a string in 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="value">Value that should be written.</param>
  124. public void SetString(string section, string name, string value)
  125. {
  126. Instance.IniWriteValue(section, name, value);
  127. }
  128. /// <summary>
  129. /// Sets a bool in the ini.
  130. /// </summary>
  131. /// <param name="section">Section of the key.</param>
  132. /// <param name="name">Name of the key.</param>
  133. /// <param name="value">Value that should be written.</param>
  134. public void SetBool(string section, string name, bool value)
  135. {
  136. Instance.IniWriteValue(section, name, value ? "1" : "0");
  137. }
  138. }
  139. public static class ModPrefsExtensions {
  140. public static ModPrefs GetModPrefs(this IPlugin plugin) {
  141. return ModPrefs.ModPrefses.First(o => o.Key == plugin).Value;
  142. }
  143. }
  144. }