Config.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using BepInEx.Common;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  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(Utility.PluginsDirectory, "config.ini");
  14. /// <summary>
  15. /// If enabled, writes the config to disk every time a value is set.
  16. /// </summary>
  17. public static bool SaveOnConfigSet { get; set; } = true;
  18. static Config()
  19. {
  20. if (File.Exists(configPath))
  21. {
  22. ReloadConfig();
  23. }
  24. else
  25. {
  26. SaveConfig();
  27. }
  28. }
  29. /// <summary>
  30. /// Reloads the config from disk. Unwritten changes are lost.
  31. /// </summary>
  32. public static void ReloadConfig()
  33. {
  34. cache.Clear();
  35. string currentSection = "";
  36. foreach (string rawLine in File.ReadAllLines(configPath))
  37. {
  38. string line = rawLine.Trim();
  39. int commentIndex = line.IndexOf("//");
  40. if (commentIndex != -1) //trim comment
  41. line = line.Remove(commentIndex);
  42. if (line.StartsWith("[") && line.EndsWith("]")) //section
  43. {
  44. currentSection = line.Substring(1, line.Length - 2);
  45. continue;
  46. }
  47. string[] split = line.Split('='); //actual config line
  48. if (split.Length != 2)
  49. continue; //empty/invalid line
  50. if (!cache.ContainsKey(currentSection))
  51. cache[currentSection] = new Dictionary<string, string>();
  52. cache[currentSection][split[0]] = split[1];
  53. }
  54. }
  55. /// <summary>
  56. /// Writes the config to disk.
  57. /// </summary>
  58. public static void SaveConfig()
  59. {
  60. using (StreamWriter writer = new StreamWriter(File.Create(configPath), System.Text.Encoding.UTF8))
  61. foreach (var sectionKv in cache)
  62. {
  63. writer.WriteLine($"[{sectionKv.Key}]");
  64. foreach (var entryKv in sectionKv.Value)
  65. writer.WriteLine($"{entryKv.Key}={entryKv.Value}");
  66. writer.WriteLine();
  67. }
  68. }
  69. /// <summary>
  70. /// Returns the value of the key if found, otherwise returns the default value.
  71. /// </summary>
  72. /// <param name="key">The key to search for.</param>
  73. /// <param name="defaultValue">The default value to return if the key is not found.</param>
  74. /// <returns>The value of the key.</returns>
  75. public static string GetEntry(string key, string defaultValue = "", string section = "")
  76. {
  77. if (section.IsNullOrWhiteSpace())
  78. section = "Global";
  79. Dictionary<string, string> subdict;
  80. if (!cache.TryGetValue(section, out subdict))
  81. return defaultValue;
  82. if (subdict.TryGetValue(key, out string value))
  83. return value;
  84. else
  85. return defaultValue;
  86. }
  87. /// <summary>
  88. /// Sets the value of the key in the config.
  89. /// </summary>
  90. /// <param name="key">The key to set the value to.</param>
  91. /// <param name="value">The value to set.</param>
  92. public static void SetEntry(string key, string value, string section = "")
  93. {
  94. if (section.IsNullOrWhiteSpace())
  95. section = "Global";
  96. Dictionary<string, string> subdict;
  97. if (!cache.TryGetValue(section, out subdict))
  98. {
  99. subdict = new Dictionary<string, string>();
  100. cache[section] = subdict;
  101. }
  102. subdict[key] = value;
  103. if (SaveOnConfigSet)
  104. SaveConfig();
  105. }
  106. #region Extensions
  107. public static string GetEntry(this BaseUnityPlugin plugin, string key, string defaultValue = "")
  108. {
  109. return GetEntry(key, defaultValue, plugin.ID);
  110. }
  111. public static void SetEntry(this BaseUnityPlugin plugin, string key, string value)
  112. {
  113. SetEntry(key, value, plugin.ID);
  114. }
  115. #endregion
  116. }
  117. }