Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Format example
- Master {
- ClientConnection {
- ListenIP = 0.0.0.0
- ListenPort = 25000
- Backlog = 32
- MaxInactiveTime1 = 600.0
- MaxInactiveTime2 = 900.0
- }
- LoginServerConnection {
- IP = 127.0.0.1
- Port = 26000
- }
- DB {
- Data Source = example.net
- Port = 3306
- Database = example_db
- User Id = userid
- Password = pwd
- }
- }
- */
- public class ConfigObject
- {
- protected ConfigObject()
- {
- Name = "<root>";
- Parent = null;
- }
- public ConfigObject(string name, ConfigObject parent)
- {
- Name = name;
- Parent = parent;
- }
- private Dictionary<string, ConfigObject> Objects { get; } = new Dictionary<string, ConfigObject>();
- private Dictionary<string, string> Data { get; } = new Dictionary<string, string>();
- public string Name { get; }
- public ConfigObject Parent { get; }
- public IEnumerable<ConfigObject> AllObjects => Objects.Values;
- public IEnumerable<string> AllObjectNames => Objects.Keys;
- public bool ContainsObject(string name) => Objects.ContainsKey(name);
- public ConfigObject GetObject(string name)
- {
- ConfigObject o;
- return Objects.TryGetValue(name, out o) ? o : null;
- }
- public ConfigObject AddNewObject(string name)
- {
- var o = new ConfigObject(name, this);
- Objects[name] = o;
- return o;
- }
- public ConfigObject GetOrCreateObject(string name)
- {
- return GetObject(name) ?? AddNewObject(name);
- }
- public bool RemoveObject(string name)
- {
- return Objects.Remove(name);
- }
- public void RemoveAllObjects() => Objects.Clear();
- public IEnumerable<string> AllFields => Data.Values;
- public IEnumerable<string> AllFieldNames => Data.Keys;
- public bool ContainsField(string name) => Data.ContainsKey(name);
- public string GetStringField(string name, string defaultValue = null)
- {
- string v;
- return Data.TryGetValue(name, out v) ? v : defaultValue;
- }
- public long GetIntField(string name, long defaultValue = 0)
- {
- var svalue = GetStringField(name);
- if (string.IsNullOrWhiteSpace(svalue))
- return defaultValue;
- long value;
- if (long.TryParse(svalue, out value))
- return value;
- if (long.TryParse(svalue, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out value))
- return value;
- return defaultValue;
- }
- public double GetFloatField(string name, double defaultValue = 0)
- {
- var svalue = GetStringField(name);
- if (string.IsNullOrWhiteSpace(svalue))
- return defaultValue;
- double value;
- return double.TryParse(svalue, out value) ? value : defaultValue;
- }
- public bool GetBoolField(string name, bool defaultValue = false)
- {
- var sv = GetStringField(name).ToLowerInvariant().Trim();
- switch (sv)
- {
- case "true":
- case "yes":
- case "y":
- case "t":
- return true;
- case "false":
- case "no":
- case "n":
- case "f":
- return false;
- }
- var iv = GetIntField(name, defaultValue ? 1 : 0);
- return iv != 0;
- }
- public void SetField(string name, object value)
- {
- Data[name] = value.ToString();
- }
- protected internal void SaveTo(StreamWriter w)
- {
- w.WriteLine(Name + " {");
- foreach (var name in AllObjectNames)
- {
- GetObject(name).SaveTo(w);
- }
- w.WriteLine();
- foreach (var name in AllFieldNames)
- {
- w.WriteLine($"{name} = {GetStringField(name)}");
- }
- w.WriteLine("}");
- }
- public override string ToString()
- {
- var o = this;
- var s = Name;
- o = o.Parent;
- while (o?.Parent != null)
- {
- s = $"{o.Name}." + s;
- o = o.Parent;
- }
- return s;
- }
- public ConfigObject ResolveObject(string name)
- {
- var id = name.IndexOf(".", StringComparison.Ordinal);
- if (id < 0)
- return GetObject(name.Trim());
- var obj = GetObject(name.Substring(0, id).Trim());
- return obj?.ResolveObject(name.Substring(id + 1).Trim());
- }
- public ConfigObject ResolveOrCreateObject(string name)
- {
- var id = name.IndexOf(".", StringComparison.Ordinal);
- if (id < 0)
- return GetOrCreateObject(name.Trim());
- var obj = GetOrCreateObject(name.Substring(0, id).Trim());
- return obj.ResolveOrCreateObject(name.Substring(id + 1).Trim());
- }
- public object Resolve(string name)
- {
- var id = name.IndexOf(".", StringComparison.Ordinal);
- if (id < 0)
- return (object) GetObject(name.Trim()) ?? GetStringField(name.Trim());
- var obj = GetObject(name.Substring(0, id).Trim());
- return obj?.Resolve(name.Substring(id + 1).Trim());
- }
- }
- public class Config : ConfigObject
- {
- public Config(StreamReader r)
- {
- var stack = new Stack<ConfigObject>();
- ConfigObject currentObj = this;
- while (!r.EndOfStream)
- {
- var str = r.ReadLine();
- if (string.IsNullOrWhiteSpace(str) || str.Trim().StartsWith("//"))
- continue;
- str = str.Trim();
- if (str.EndsWith("{"))
- {
- stack.Push(currentObj);
- var path = str.Substring(0, str.Length-1).Trim();
- currentObj = currentObj.ResolveOrCreateObject(path);
- continue;
- }
- if (str.StartsWith("}"))
- {
- currentObj = stack.Any() ? stack.Pop() : this;
- continue;
- }
- var id = str.IndexOf("=", StringComparison.Ordinal);
- if (id < 0)
- continue;
- var key = str.Substring(0, id).Trim();
- var value = str.Substring(id + 1).Trim();
- var keyPath = key.Split('.');
- var obj = currentObj;
- for (int i = 0; i < keyPath.Length-1; i++)
- {
- var name = keyPath[i].Trim();
- if (string.IsNullOrWhiteSpace(str))
- continue;
- obj = obj.GetOrCreateObject(name);
- }
- obj.SetField(keyPath.Last(), value);
- }
- }
- public void Save(StreamWriter w)
- {
- w.WriteLine("// Root fields");
- foreach (var name in AllFieldNames)
- {
- w.WriteLine($"{name} = {GetStringField(name)}");
- }
- foreach (var name in AllObjectNames)
- {
- w.WriteLine();
- GetObject(name).SaveTo(w);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement