Config.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using BepInEx.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. namespace BepInEx
  8. {
  9. public static class Config
  10. {
  11. private static Dictionary<string, string> cache = new Dictionary<string, string>();
  12. private static string configPath => Path.Combine(Utility.PluginsDirectory, "config.ini");
  13. public static bool SaveOnConfigSet { get; set; } = true;
  14. static Config()
  15. {
  16. if (File.Exists(configPath))
  17. {
  18. ReloadConfig();
  19. }
  20. else
  21. {
  22. SaveConfig();
  23. }
  24. }
  25. public static void ReloadConfig()
  26. {
  27. cache.Clear();
  28. foreach (string line in File.ReadAllLines(configPath))
  29. {
  30. string[] split = line.Split('=');
  31. if (split.Length != 2)
  32. continue;
  33. cache[split[0]] = split[1];
  34. }
  35. }
  36. public static void SaveConfig()
  37. {
  38. File.WriteAllLines(configPath, cache.Select(x => $"{x.Key}={x.Value}").ToArray());
  39. }
  40. public static string GetEntry(string key, string defaultValue)
  41. {
  42. if (cache.TryGetValue(key, out string value))
  43. return value;
  44. else
  45. return defaultValue;
  46. }
  47. public static void SetEntry(string key, string value)
  48. {
  49. cache[key] = value;
  50. if (SaveOnConfigSet)
  51. SaveConfig();
  52. }
  53. }
  54. }