Config.cs 7.1 KB

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