Config.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. namespace BepInEx
  6. {
  7. /// <summary>
  8. /// A helper class to handle persistent data.
  9. /// </summary>
  10. public static class Config
  11. {
  12. private static Dictionary<string, Dictionary<string, string>> cache = new Dictionary<string, Dictionary<string, string>>();
  13. private static string configPath => Path.Combine(Common.Utility.PluginsDirectory, "config.ini");
  14. private static Regex sanitizeKeyRegex = new Regex("[^a-zA-Z0-9]+");
  15. private static void RaiseConfigReloaded()
  16. {
  17. var handler = ConfigReloaded;
  18. if (handler != null)
  19. handler.Invoke();
  20. }
  21. public static event Action ConfigReloaded;
  22. /// <summary>
  23. /// If enabled, writes the config to disk every time a value is set.
  24. /// </summary>
  25. public static bool SaveOnConfigSet { get; set; } = true;
  26. static Config()
  27. {
  28. if (File.Exists(configPath))
  29. {
  30. ReloadConfig();
  31. }
  32. else
  33. {
  34. SaveConfig();
  35. }
  36. }
  37. /// <summary>
  38. /// Returns the value of the key if found, otherwise returns the default value.
  39. /// </summary>
  40. /// <param name="key">The key to search for.</param>
  41. /// <param name="defaultValue">The default value to return if the key is not found.</param>
  42. /// <returns>The value of the key.</returns>
  43. public static string GetEntry(string key, string defaultValue = "", string section = "")
  44. {
  45. key = Sanitize(key);
  46. if (section.IsNullOrWhiteSpace())
  47. section = "Global";
  48. else
  49. section = Sanitize(section);
  50. Dictionary<string, string> subdict;
  51. if (!cache.TryGetValue(section, out subdict))
  52. {
  53. SetEntry(key, defaultValue, section);
  54. return defaultValue;
  55. }
  56. if (subdict.TryGetValue(key, out string value))
  57. {
  58. return value;
  59. }
  60. else
  61. {
  62. SetEntry(key, defaultValue, section);
  63. return defaultValue;
  64. }
  65. }
  66. /// <summary>
  67. /// Reloads the config from disk. Unwritten changes are lost.
  68. /// </summary>
  69. public static void ReloadConfig()
  70. {
  71. cache.Clear();
  72. string currentSection = "";
  73. foreach (string rawLine in File.ReadAllLines(configPath))
  74. {
  75. string line = rawLine.Trim();
  76. bool commentIndex = line.StartsWith(";") || line.StartsWith("#");
  77. if (commentIndex) //trim comment
  78. continue;
  79. if (line.StartsWith("[") && line.EndsWith("]")) //section
  80. {
  81. currentSection = line.Substring(1, line.Length - 2);
  82. continue;
  83. }
  84. string[] split = line.Split('='); //actual config line
  85. if (split.Length != 2)
  86. continue; //empty/invalid line
  87. if (!cache.ContainsKey(currentSection))
  88. cache[currentSection] = new Dictionary<string, string>();
  89. cache[currentSection][split[0]] = split[1];
  90. }
  91. RaiseConfigReloaded();
  92. }
  93. /// <summary>
  94. /// Writes the config to disk.
  95. /// </summary>
  96. public static void SaveConfig()
  97. {
  98. using (StreamWriter writer = new StreamWriter(File.Create(configPath), System.Text.Encoding.UTF8))
  99. foreach (var sectionKv in cache)
  100. {
  101. writer.WriteLine($"[{sectionKv.Key}]");
  102. foreach (var entryKv in sectionKv.Value)
  103. writer.WriteLine($"{entryKv.Key}={entryKv.Value}");
  104. writer.WriteLine();
  105. }
  106. }
  107. /// <summary>
  108. /// Sets the value of the key in the config.
  109. /// </summary>
  110. /// <param name="key">The key to set the value to.</param>
  111. /// <param name="value">The value to set.</param>
  112. public static void SetEntry(string key, string value, string section = "")
  113. {
  114. key = Sanitize(key);
  115. if (section.IsNullOrWhiteSpace())
  116. section = "Global";
  117. else
  118. section = Sanitize(section);
  119. Dictionary<string, string> subdict;
  120. if (!cache.TryGetValue(section, out subdict))
  121. {
  122. subdict = new Dictionary<string, string>();
  123. cache[section] = subdict;
  124. }
  125. subdict[key] = value;
  126. if (SaveOnConfigSet)
  127. SaveConfig();
  128. }
  129. /// <summary>
  130. /// Returns wether a value is currently set.
  131. /// </summary>
  132. /// <param name="key">The key to check against</param>
  133. /// <param name="section">The section to check in</param>
  134. /// <returns>True if the key is present</returns>
  135. public static bool HasEntry(string key, string section = "")
  136. {
  137. key = Sanitize(key);
  138. if (section.IsNullOrWhiteSpace())
  139. section = "Global";
  140. else
  141. section = Sanitize(section);
  142. return cache.ContainsKey(section) && cache[section].ContainsKey(key);
  143. }
  144. /// <summary>
  145. /// Removes a value from the config.
  146. /// </summary>
  147. /// <param name="key">The key to remove</param>
  148. /// <param name="section">The section to remove from</param>
  149. /// <returns>True if the key was removed</returns>
  150. public static bool UnsetEntry(string key, string section = "")
  151. {
  152. key = Sanitize(key);
  153. if (section.IsNullOrWhiteSpace())
  154. section = "Global";
  155. else
  156. section = Sanitize(section);
  157. if (!HasEntry(key, section))
  158. return false;
  159. cache[section].Remove(key);
  160. return true;
  161. }
  162. public static string Sanitize(string key)
  163. {
  164. return sanitizeKeyRegex.Replace(key, "_");
  165. }
  166. #region Extensions
  167. public static string GetEntry(this BaseUnityPlugin plugin, string key, string defaultValue = "")
  168. {
  169. return GetEntry(key, defaultValue, TypeLoader.GetMetadata(plugin).GUID);
  170. }
  171. public static void SetEntry(this BaseUnityPlugin plugin, string key, string value)
  172. {
  173. SetEntry(key, value, TypeLoader.GetMetadata(plugin).GUID);
  174. }
  175. public static bool HasEntry(this BaseUnityPlugin plugin, string key)
  176. {
  177. return HasEntry(key, TypeLoader.GetMetadata(plugin).GUID);
  178. }
  179. #endregion Extensions
  180. }
  181. }