Browse Source

Add sections to config

Bepis 6 years ago
parent
commit
8ebdbe3de2
3 changed files with 83 additions and 24 deletions
  1. 69 10
      BepInEx/Config.cs
  2. 4 4
      Plugins/ColorCorrector/ColorCorrector.cs
  3. 10 10
      Plugins/Screencap/ScreenshotManager.cs

+ 69 - 10
BepInEx/Config.cs

@@ -10,7 +10,7 @@ namespace BepInEx
     /// </summary>
     public static class Config
     {
-        private static Dictionary<string, string> cache = new Dictionary<string, string>();
+        private static Dictionary<string, Dictionary<string, string>> cache = new Dictionary<string, Dictionary<string, string>>();
 
         private static string configPath => Path.Combine(Utility.PluginsDirectory, "config.ini");
 
@@ -38,13 +38,31 @@ namespace BepInEx
         {
             cache.Clear();
 
-            foreach (string line in File.ReadAllLines(configPath))
+            string currentSection = "";
+
+            foreach (string rawLine in File.ReadAllLines(configPath))
             {
-                string[] split = line.Split('=');
-                if (split.Length != 2)
+                string line = rawLine.Trim();
+
+                int commentIndex = line.IndexOf("//");
+
+                if (commentIndex != -1) //trim comment
+                    line = line.Remove(commentIndex);
+
+                if (line.StartsWith("[") && line.EndsWith("]")) //section
+                {
+                    currentSection = line.Substring(1, line.Length - 2);
                     continue;
+                }
+
+                string[] split = line.Split('='); //actual config line
+                if (split.Length != 2)
+                    continue; //empty/invalid line
 
-                cache[split[0]] = split[1];
+                if (!cache.ContainsKey(currentSection))
+                    cache[currentSection] = new Dictionary<string, string>();
+
+                cache[currentSection][split[0]] = split[1];
             }
         }
 
@@ -53,7 +71,16 @@ namespace BepInEx
         /// </summary>
         public static void SaveConfig()
         {
-            File.WriteAllLines(configPath, cache.Select(x => $"{x.Key}={x.Value}").ToArray());
+            using (StreamWriter writer = new StreamWriter(File.Create(configPath), System.Text.Encoding.UTF8))
+                foreach (var sectionKv in cache)
+                {
+                    writer.WriteLine($"[{sectionKv.Key}]");
+
+                    foreach (var entryKv in sectionKv.Value)
+                        writer.WriteLine($"{entryKv.Key}={entryKv.Value}");
+
+                    writer.WriteLine();
+                }
         }
 
         /// <summary>
@@ -62,9 +89,18 @@ namespace BepInEx
         /// <param name="key">The key to search for.</param>
         /// <param name="defaultValue">The default value to return if the key is not found.</param>
         /// <returns>The value of the key.</returns>
-        public static string GetEntry(string key, string defaultValue)
+        public static string GetEntry(string key, string defaultValue = "", string section = "")
         {
-            if (cache.TryGetValue(key, out string value))
+            if (section.IsNullOrWhiteSpace())
+                section = "Global";
+
+            Dictionary<string, string> subdict;
+
+            if (!cache.TryGetValue(section, out subdict))
+                return defaultValue;
+                
+
+            if (subdict.TryGetValue(key, out string value))
                 return value;
             else
                 return defaultValue;
@@ -75,12 +111,35 @@ namespace BepInEx
         /// </summary>
         /// <param name="key">The key to set the value to.</param>
         /// <param name="value">The value to set.</param>
-        public static void SetEntry(string key, string value)
+        public static void SetEntry(string key, string value, string section = "")
         {
-            cache[key] = value;
+            if (section.IsNullOrWhiteSpace())
+                section = "Global";
+
+            Dictionary<string, string> subdict;
+
+            if (!cache.TryGetValue(section, out subdict))
+            {
+                subdict = new Dictionary<string, string>();
+                cache[section] = subdict;
+            }
+
+            subdict[key] = value;
 
             if (SaveOnConfigSet)
                 SaveConfig();
         }
+
+        #region Extensions
+        public static string GetEntry(this BaseUnityPlugin plugin, string key, string defaultValue = "")
+        {
+            return GetEntry(key, defaultValue, plugin.ID);
+        }
+
+        public static void SetEntry(this BaseUnityPlugin plugin, string key, string value)
+        {
+            SetEntry(key, value, plugin.ID);
+        }
+        #endregion
     }
 }

+ 4 - 4
Plugins/ColorCorrector/ColorCorrector.cs

@@ -15,14 +15,14 @@ namespace ColorCorrector
         #region Config properties
         private bool SaturationEnabled
         {
-            get => bool.Parse(BepInEx.Config.GetEntry("colorcorrector-saturationenabled", "True"));
-            set => BepInEx.Config.SetEntry("colorcorrector-saturationenabled", value.ToString());
+            get => bool.Parse(this.GetEntry("saturation-enabled", "True"));
+            set => this.SetEntry("saturation-enabled", value.ToString());
         }
 
         private bool BloomEnabled
         {
-            get => bool.Parse(BepInEx.Config.GetEntry("colorcorrector-bloomenabled", "True"));
-            set => BepInEx.Config.SetEntry("colorcorrector-bloomenabled", value.ToString());
+            get => bool.Parse(this.GetEntry("bloom-enabled", "True"));
+            set => this.SetEntry("bloom-enabled", value.ToString());
         }
         #endregion
 

+ 10 - 10
Plugins/Screencap/ScreenshotManager.cs

@@ -27,32 +27,32 @@ namespace Screencap
 
         private int ResolutionX
         {
-            get => int.Parse(BepInEx.Config.GetEntry("screenshotrenderer-resolution-x", "1024"));
-            set => BepInEx.Config.SetEntry("screenshotrenderer-resolution-x", value.ToString());
+            get => int.Parse(this.GetEntry("resolution-x", "1024"));
+            set => this.SetEntry("resolution-x", value.ToString());
         }
 
         private int ResolutionY
         {
-            get => int.Parse(BepInEx.Config.GetEntry("screenshotrenderer-resolution-y", "1024"));
-            set => BepInEx.Config.SetEntry("screenshotrenderer-resolution-y", value.ToString());
+            get => int.Parse(this.GetEntry("resolution-y", "1024"));
+            set => this.SetEntry("resolution-y", value.ToString());
         }
         
         private int AntiAliasing
         {
-            get => int.Parse(BepInEx.Config.GetEntry("screenshotrenderer-antialiasing", "4"));
-            set => BepInEx.Config.SetEntry("screenshotrenderer-antialiasing", value.ToString());
+            get => int.Parse(this.GetEntry("antialiasing", "4"));
+            set => this.SetEntry("antialiasing", value.ToString());
         }
 
         private int DownscalingRate
         {
-            get => int.Parse(BepInEx.Config.GetEntry("screenshotrenderer-downscalerate", "1"));
-            set => BepInEx.Config.SetEntry("screenshotrenderer-downscalerate", value.ToString());
+            get => int.Parse(this.GetEntry("downscalerate", "1"));
+            set => this.SetEntry("downscalerate", value.ToString());
         }
 
         private int RenderMethod
         {
-            get => int.Parse(BepInEx.Config.GetEntry("screenshotrenderer-rendermethod", "1"));
-            set => BepInEx.Config.SetEntry("screenshotrenderer-rendermethod", value.ToString());
+            get => int.Parse(this.GetEntry("rendermethod", "1"));
+            set => this.SetEntry("rendermethod", value.ToString());
         }
 
         #endregion