Last active
September 2, 2017 17:14
-
-
Save jesta88/e485d40940c1d7e0f7c16f199c3ed8fa to your computer and use it in GitHub Desktop.
C# Config Vars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Text; | |
using System.Collections.Generic; | |
public static class Config | |
{ | |
public const string TypeUint8 = "uint8"; | |
public const string TypeUint16 = "uint16"; | |
public const string TypeUint32 = "uint32"; | |
public const string TypeUint64 = "uint64"; | |
public const string TypeInt8 = "int8"; | |
public const string TypeInt16 = "int16"; | |
public const string TypeInt32 = "int32"; | |
public const string TypeInt64 = "int64"; | |
public const string TypeFloat = "float"; | |
public const string TypeDouble = "double"; | |
public const string TypeBool = "bool"; | |
public const string TypeString = "string"; | |
private const int ApproximateCharsPerLine = 24; | |
private static readonly Dictionary<ConfigVarType, string> _typeStrings = new Dictionary<ConfigVarType, string> | |
{ | |
[ConfigVarType.Uint8] = TypeUint8, | |
[ConfigVarType.Uint16] = TypeUint16, | |
[ConfigVarType.Uint32] = TypeUint32, | |
[ConfigVarType.Uint64] = TypeUint64, | |
[ConfigVarType.Int8] = TypeInt8, | |
[ConfigVarType.Int16] = TypeInt16, | |
[ConfigVarType.Int32] = TypeInt32, | |
[ConfigVarType.Int64] = TypeInt64, | |
[ConfigVarType.Float] = TypeFloat, | |
[ConfigVarType.Double] = TypeDouble, | |
[ConfigVarType.Bool] = TypeBool, | |
[ConfigVarType.String] = TypeString, | |
}; | |
private static readonly List<string> _varsToWrite = new List<string>(128); | |
private static readonly Dictionary<string, ConfigVar> _vars = new Dictionary<string, ConfigVar>(128); | |
public static void AddVar(string name, byte value = 0, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, ushort value = 0, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, uint value = 0, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, ulong value = 0, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, sbyte value = 0, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, short value = 0, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, int value = 0, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, long value = 0, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, float value = 0, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, double value = 0, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, bool value = false, ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static void AddVar(string name, string value = "", ConfigVarFlags flags = ConfigVarFlags.None) | |
{ | |
if ((flags & ConfigVarFlags.WriteToFile) != 0) | |
{ | |
_varsToWrite.Add(name); | |
} | |
_vars.Add(name, new ConfigVar(value)); | |
} | |
public static ConfigVar GetVar(string name) => _vars[name]; | |
public static bool TryGetVar(string name, out ConfigVar value) | |
{ | |
if (_vars.TryGetValue(name, out value)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
public static void SetVar(string name, byte value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, ushort value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, uint value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, ulong value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, sbyte value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, short value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, int value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, long value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, float value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, double value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, bool value) => _vars[name] = new ConfigVar(value); | |
public static void SetVar(string name, string value) => _vars[name] = new ConfigVar(value); | |
public static void WriteVarsToFile(string path) | |
{ | |
if (_varsToWrite.Count == 0) | |
{ | |
return; | |
} | |
StringBuilder stringBuilder = new StringBuilder(_varsToWrite.Count * ApproximateCharsPerLine); | |
for (int i = 0; i < _varsToWrite.Count; i++) | |
{ | |
string name = _varsToWrite[i]; | |
ConfigVar value = _vars[name]; | |
if (i > 0) | |
{ | |
stringBuilder.AppendLine(); | |
} | |
stringBuilder.Append(name).Append(':').Append(_typeStrings[value.Type]).Append('=').Append(value); | |
} | |
using (StreamWriter writer = new StreamWriter(path)) | |
{ | |
writer.Write(stringBuilder.ToString()); | |
} | |
} | |
public static void AddVarsFromFile(string path) | |
{ | |
string line; | |
using (StreamReader reader = new StreamReader(path)) | |
{ | |
while (true) | |
{ | |
line = reader.ReadLine(); | |
// Reached end of file | |
if (line == null) | |
{ | |
break; | |
} | |
if (line[0] == '/' || // Skip comment | |
line[0] == '[' || // Skip category | |
line.Length == 0) // Skip empty line | |
{ | |
continue; | |
} | |
// Read the data | |
int colonIndex = line.IndexOf(':'); | |
int equalIndex = line.IndexOf('='); | |
string key = line.Substring(0, colonIndex); | |
Assert.IsFalse(_vars.ContainsKey(key), $"{key} was already added to the config."); | |
string type = line.Substring(colonIndex + 1, equalIndex - colonIndex - 1); | |
string value = line.Substring(equalIndex + 1); | |
// Parse the data | |
switch (type) | |
{ | |
case TypeUint8: | |
{ | |
if (byte.TryParse(value, out byte result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to byte."); | |
} | |
break; | |
} | |
case TypeUint16: | |
{ | |
if (ushort.TryParse(value, out ushort result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to ushort"); | |
} | |
break; | |
} | |
case TypeUint32: | |
{ | |
if (uint.TryParse(value, out uint result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to uint"); | |
} | |
break; | |
} | |
case TypeUint64: | |
{ | |
if (ulong.TryParse(value, out ulong result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to ulong"); | |
} | |
break; | |
} | |
case TypeInt8: | |
{ | |
if (sbyte.TryParse(value, out sbyte result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to sbyte."); | |
} | |
break; | |
} | |
case TypeInt16: | |
{ | |
if (short.TryParse(value, out short result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to short"); | |
} | |
break; | |
} | |
case TypeInt32: | |
{ | |
if (int.TryParse(value, out int result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to int"); | |
} | |
break; | |
} | |
case TypeInt64: | |
{ | |
if (long.TryParse(value, out long result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to long"); | |
} | |
break; | |
} | |
case TypeFloat: | |
{ | |
if (float.TryParse(value, out float result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to float"); | |
} | |
break; | |
} | |
case TypeDouble: | |
{ | |
if (double.TryParse(value, out double result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to double"); | |
} | |
break; | |
} | |
case TypeBool: | |
{ | |
if (bool.TryParse(value, out bool result)) | |
{ | |
AddVar(key, result, ConfigVarFlags.WriteToFile); | |
} | |
else | |
{ | |
Log.Warning($"Could not parse value {value} to bool"); | |
} | |
break; | |
} | |
case TypeString: | |
{ | |
AddVar(key, value, ConfigVarFlags.WriteToFile); | |
break; | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
[Flags] | |
public enum ConfigVarFlags | |
{ | |
None = 0, | |
WriteToFile = 1 << 0, | |
} | |
public enum ConfigVarType : byte | |
{ | |
Uint8, | |
Uint16, | |
Uint32, | |
Uint64, | |
Int8, | |
Int16, | |
Int32, | |
Int64, | |
Float, | |
Double, | |
Bool, | |
String | |
} | |
[StructLayout(LayoutKind.Explicit)] | |
public unsafe struct ConfigVar | |
{ | |
[FieldOffset(0)] public ConfigVarType Type; | |
[FieldOffset(1)] public byte Uint8; | |
[FieldOffset(1)] public ushort Uint16; | |
[FieldOffset(1)] public uint Uint32; | |
[FieldOffset(1)] public ulong Uint64; | |
[FieldOffset(1)] public sbyte Int8; | |
[FieldOffset(1)] public short Int16; | |
[FieldOffset(1)] public int Int32; | |
[FieldOffset(1)] public long Int64; | |
[FieldOffset(1)] public float Float; | |
[FieldOffset(1)] public double Double; | |
[FieldOffset(1)] public bool Bool; | |
[FieldOffset(1)] public char* String; | |
public override string ToString() | |
{ | |
switch (Type) | |
{ | |
case ConfigVarType.Uint8: return Uint8.ToString(); | |
case ConfigVarType.Uint16: return Uint16.ToString(); | |
case ConfigVarType.Uint32: return Uint32.ToString(); | |
case ConfigVarType.Uint64: return Uint64.ToString(); | |
case ConfigVarType.Int8: return Int8.ToString(); | |
case ConfigVarType.Int16: return Int16.ToString(); | |
case ConfigVarType.Int32: return Int32.ToString(); | |
case ConfigVarType.Int64: return Int64.ToString(); | |
case ConfigVarType.Float: return Float.ToString(); | |
case ConfigVarType.Double: return Double.ToString(); | |
case ConfigVarType.Bool: return Bool.ToString(); | |
case ConfigVarType.String: return new string(String); | |
default: return ""; | |
} | |
} | |
public ConfigVar(byte value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Uint8; | |
Uint8 = value; | |
} | |
public ConfigVar(ushort value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Uint16; | |
Uint16 = value; | |
} | |
public ConfigVar(uint value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Uint32; | |
Uint32 = value; | |
} | |
public ConfigVar(ulong value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Uint64; | |
Uint64 = value; | |
} | |
public ConfigVar(sbyte value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Int8; | |
Int8 = value; | |
} | |
public ConfigVar(short value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Int16; | |
Int16 = value; | |
} | |
public ConfigVar(int value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Int32; | |
Int32 = value; | |
} | |
public ConfigVar(long value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Int64; | |
Int64 = value; | |
} | |
public ConfigVar(float value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Float; | |
Float = value; | |
} | |
public ConfigVar(double value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Double; | |
Double = value; | |
} | |
public ConfigVar(bool value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.Bool; | |
Bool = value; | |
} | |
public ConfigVar(string value) | |
{ | |
this = default(ConfigVar); | |
Type = ConfigVarType.String; | |
fixed (char* pointer = value) | |
{ | |
String = pointer; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string configPath = "C:\settings.conf"; | |
Config.AddVarsFromFile(configPath); | |
Debug.WriteLine(Config.GetVar("MyString").ToString()); | |
Config.AddVar("NewVar", -2, ConfigVarFlags.WriteToFile); | |
Config.SetVar("ResolutionX", 1768); | |
Config.WriteVarsToFile(configPath); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DisplayMode:uint8=0 | |
ResolutionX:int32=1768 | |
ResolutionY:int32=1080 | |
VerticalSync:bool=False | |
RefreshRate:int32=144 | |
Gamma:float=1 | |
TestVar:float=0.75 | |
MyString:string=blablabla | |
NewVar:int=-2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment