using BepInEx.Common; using System.Collections.Generic; using System.IO; using System.Linq; namespace BepInEx { /// /// A helper class to handle persistent data. /// public static class Config { private static Dictionary cache = new Dictionary(); private static string configPath => Path.Combine(Utility.PluginsDirectory, "config.ini"); /// /// If enabled, writes the config to disk every time a value is set. /// public static bool SaveOnConfigSet { get; set; } = true; static Config() { if (File.Exists(configPath)) { ReloadConfig(); } else { SaveConfig(); } } /// /// Reloads the config from disk. Unwritten changes are lost. /// public static void ReloadConfig() { cache.Clear(); foreach (string line in File.ReadAllLines(configPath)) { string[] split = line.Split('='); if (split.Length != 2) continue; cache[split[0]] = split[1]; } } /// /// Writes the config to disk. /// public static void SaveConfig() { File.WriteAllLines(configPath, cache.Select(x => $"{x.Key}={x.Value}").ToArray()); } /// /// Returns the value of the key if found, otherwise returns the default value. /// /// The key to search for. /// The default value to return if the key is not found. /// The value of the key. public static string GetEntry(string key, string defaultValue) { if (cache.TryGetValue(key, out string value)) return value; else return defaultValue; } /// /// Sets the value of the key in the config. /// /// The key to set the value to. /// The value to set. public static void SetEntry(string key, string value) { cache[key] = value; if (SaveOnConfigSet) SaveConfig(); } } }