private void Configure(Action <IDCounterProvider> initFunc) { string configFile = RuntimeEnvironment.GetPhysicalApplicationPath() + Path.DirectorySeparatorChar + "DCounter.ini"; try { IniParser.StreamIniDataParser parser = new IniParser.StreamIniDataParser(); string assemblyName = ""; string typeName = ""; Dictionary <string, string> config = new Dictionary <string, string>(); using (StreamReader sr = File.OpenText(configFile)) { var data = parser.ReadData(sr); var globalConfig = data["dcounter"]; string configType = globalConfig["counterType"]; var specificConfig = data[configType]; config = specificConfig.Where(c => c.KeyName != "__class").ToDictionary(c => c.KeyName, c => c.Value); var className = specificConfig["__class"]; int classNameSepIndex = className.LastIndexOf(','); assemblyName = className.Substring(0, classNameSepIndex).Trim(); typeName = className.Substring(classNameSepIndex + 1).Trim(); } ConfigureProvider(assemblyName, typeName, config, initFunc); } catch (Exception ex) { throw new LoadConfigurationException("Load configuration file \"" + configFile + "\" error!", ex); } }
public LogioConfig(Stream stream) { var streamIniParser = new IniParser.StreamIniDataParser(); _stream = stream; _iniData = streamIniParser.ReadData(new StreamReader(_stream)); stream.Position = 0; }
private IniParser.Model.IniData LoadIniFile(string path) { using (var stream = new System.IO.StreamReader(path)) { var parser = new IniParser.StreamIniDataParser(); return(parser.ReadData(stream)); } }
/// <summary> /// Loads the settings from an ini file. /// </summary> public void Load() { var file = $"{Name}.ini"; if (!File.Exists(file)) { Logger.Warn($"No INI file found for {Name} - creating one."); Save(); return; } using (var stream = File.OpenRead(file)) using (var reader = new StreamReader(stream)) { var parser = new IniParser.StreamIniDataParser(new IniDataParser(IniParserConfig)); var data = parser.ReadData(reader); foreach (var section in data.Sections) { var settings = FindFirstChild(section.SectionName); if (settings != null) { var settingsType = settings.GetType(); foreach (var kv in section.Keys) { var property = settingsType.GetProperty(kv.KeyName); if (property == null) { Logger.Warn($"Ignoring INI property \"{kv.KeyName}\" (not defined in {settings.ClassName})"); continue; } if (property.SetMethod?.IsPublic != true) { //Logger.Warn($"Ignoring INI property \"{kv.KeyName}\" (readonly in {settings.ClassName})"); continue; } dynamic value; if (property.PropertyType == typeof(Font)) { var trimmed = kv.Value.Remove(0, 7); trimmed = trimmed.Remove(trimmed.Length - 1); var parts = trimmed.Split(new[] { ", " }, StringSplitOptions.None) .ToDictionary(x => x.Split('=')[0], x => x.Split('=')[1]); value = new Font(parts["Name"], float.Parse(parts["Size"]), (GraphicsUnit)int.Parse(parts["Units"])); } else { var propType = property.PropertyType; if (propType == typeof(int)) { value = int.Parse(kv.Value); } else if (propType == typeof(string)) { value = kv.Value; } else if (propType == typeof(float)) { value = float.Parse(kv.Value); } else if (propType == typeof(double)) { value = double.Parse(kv.Value); } else { value = Dynamitey.Dynamic.CoerceConvert(kv.Value, propType); } } Dynamitey.Dynamic.InvokeSet(settings, kv.KeyName, value); } } else { Logger.Warn($"No settings object found for INI section: {section.SectionName}"); } } } }