2 using System.Collections.Generic;
14 private int _LedWizNumber = 0;
22 public int LedWizNumber
24 get {
return _LedWizNumber; }
25 set { _LedWizNumber = value; }
28 public Version MinDOFVersion {
get; set; }
42 get {
return _TableConfigurations; }
43 set { _TableConfigurations = value; }
56 get {
return _ColorConfigurations; }
57 set { _ColorConfigurations = value; }
61 private FileInfo _LedControlIniFile;
69 public FileInfo LedControlIniFile
71 get {
return _LedControlIniFile; }
72 private set { _LedControlIniFile = value; }
94 private void ParseLedControlIni(FileInfo LedControlIniFile,
bool ThrowExceptions =
false)
96 string[] ColorStartStrings = {
"[Colors DOF]",
"[Colors LedWiz]" };
97 string[] OutStartStrings = {
"[Config DOF]",
"[Config outs]" };
98 string[] VariableStartStrings = {
"[Variables DOF]" };
99 string[] VersionStartStrings = {
"[version]" };
100 string[] TableVariableStartStrings = {
"[TableVariables]" }; ;
101 string FileData =
"";
106 FileData = General.FileReader.ReadFileToString(LedControlIniFile);
110 Log.Exception(
"Could not read file {0}.".Build(LedControlIniFile), E);
114 throw new Exception(
"Could not read file {0}.".Build(LedControlIniFile), E);
117 if (FileData.IsNullOrWhiteSpace())
119 Log.Warning(
"File {0} does not contain data.".Build(LedControlIniFile));
122 throw new Exception(
"File {0} does not contain data.".Build(LedControlIniFile));
127 Dictionary<string, List<string>> Sections =
new Dictionary<string, List<String>>();
129 #region Read sections
131 List<string> SectionData =
new List<string>();
132 String SectionHeader = null;
133 foreach (
string RawIniLine
in FileData.Split(
new[] {
'\r',
'\n' }, StringSplitOptions.RemoveEmptyEntries))
135 string IniLine = RawIniLine.Trim();
136 if (IniLine.Length > 0 && !IniLine.StartsWith(
"#"))
138 if (IniLine.StartsWith(
"[") && IniLine.EndsWith(
"]") && IniLine.Length > 2)
141 if (!SectionHeader.IsNullOrWhiteSpace())
143 if (Sections.ContainsKey(SectionHeader))
146 while (Sections.ContainsKey(
"{0} {1}".Build(SectionHeader, Cnt)))
149 if (Cnt > 999) {
throw new Exception(
"Section header {0} exists to many times.".Build(SectionHeader)); }
151 SectionHeader =
"{0} {1}".Build(SectionHeader, Cnt);
153 Sections.Add(SectionHeader, SectionData);
154 SectionData =
new List<string>();
156 SectionHeader = IniLine;
161 SectionData.Add(IniLine);
166 if (!SectionHeader.IsNullOrWhiteSpace())
168 if (Sections.ContainsKey(SectionHeader))
171 while (Sections.ContainsKey(
"{0} {1}".Build(SectionHeader, Cnt)))
174 if (Cnt > 999) {
throw new Exception(
"Section header {0} exists to many times.".Build(SectionHeader)); }
176 SectionHeader =
"{0} {1}".Build(SectionHeader, Cnt);
178 Sections.Add(SectionHeader, SectionData);
179 SectionData =
new List<string>();
186 List<string> ColorData = GetSection(Sections, ColorStartStrings);
187 List<string> OutData = GetSection(Sections, OutStartStrings);
188 List<string> VariableData = GetSection(Sections, VariableStartStrings);
189 List<string> VersionData = GetSection(Sections, VersionStartStrings);
190 List<string> TableVariableData = GetSection(Sections, TableVariableStartStrings);
192 if (VersionData != null && VersionData.Count > 0)
194 MinDOFVersion = null;
196 string MinDofVersionLine = VersionData.FirstOrDefault(S => S.ToLowerInvariant().StartsWith(
"mindofversion="));
198 if (MinDofVersionLine != null)
200 string MinDofVersionString = MinDofVersionLine.Substring(
"mindofversion=".Length);
204 MinDOFVersion =
new Version(MinDofVersionString);
208 Log.Exception(
"Could not parse line {1} from file {0}".Build(LedControlIniFile, MinDofVersionLine));
209 MinDOFVersion = null;
211 if (MinDOFVersion != null)
213 Log.Write(
"Min DOF Version is {0} for file {1}".Build(MinDOFVersion.ToString(), LedControlIniFile.Name));
219 Log.Warning(
"No DOF version information found in file {0}.".Build(LedControlIniFile));
225 Log.Warning(
"No version section found in file {0}.".Build(LedControlIniFile));
228 if (ColorData == null)
230 Log.Warning(
"Could not find color definition section in file {0}.".Build(LedControlIniFile));
233 throw new Exception(
"Could not find color definition section in file {0}.".Build(LedControlIniFile));
237 else if (ColorData.Count < 1)
239 Log.Warning(
"File {0} does not contain data in the color definition section.".Build(LedControlIniFile));
242 throw new Exception(
"File {0} does not contain data in the color definition section.".Build(LedControlIniFile));
249 Log.Warning(
"Could not find table config section in file {0}.".Build(LedControlIniFile));
252 throw new Exception(
"Could not find table config section section in file {1}.".Build(LedControlIniFile));
256 else if (OutData.Count < 1)
258 Log.Warning(
"File {0} does not contain data in the table config section.".Build(LedControlIniFile));
261 throw new Exception(
"File {0} does not contain data in the table config section".Build(LedControlIniFile));
266 if (VariableData != null)
268 ResolveVariables(OutData, VariableData);
271 if (TableVariableData != null)
273 ResolveTableVariables(OutData, TableVariableData);
278 ColorConfigurations.ParseLedControlData(ColorData, ThrowExceptions);
280 TableConfigurations.ParseLedcontrolData(OutData, ThrowExceptions);
285 this.LedControlIniFile = LedControlIniFile;
288 private List<string> GetSection(Dictionary<
string, List<string>> Sections, IEnumerable<string> SectionStartStrings)
290 foreach (
string StartString
in SectionStartStrings)
292 if (Sections.ContainsKey(StartString))
294 return Sections[StartString];
302 private void ResolveTableVariables(List<String> DataToResolve, List<string> VariableData)
305 TableVariablesDictionary VD =
new TableVariablesDictionary(VariableData);
308 for (
int i = 0; i < DataToResolve.Count ; i++)
310 string D = DataToResolve[i].Trim();
311 bool Updated =
false;
312 if (!D.IsNullOrWhiteSpace())
314 int TP = D.IndexOf(
",");
317 string TableName = D.Substring(0, TP).Trim();
318 if (VD.ContainsKey(TableName))
320 foreach (KeyValuePair<string, string> KV
in VD[TableName])
323 if (!N.StartsWith(
"@")) N =
"@" + N;
324 if (!N.EndsWith(
"@")) N +=
"@";
325 D = D.Replace(N, KV.Value);
334 DataToResolve[i] = D;
342 private void ResolveVariables(List<String> DataToResolve, List<string> VariableData)
344 VariablesDictionary VD =
new VariablesDictionary(VariableData);
345 foreach (KeyValuePair<string, string> KV
in VD)
348 if (!N.StartsWith(
"@")) N =
"@" + N;
349 if (!N.EndsWith(
"@")) N +=
"@";
351 for (
int i = 0; i < DataToResolve.Count; i++)
353 DataToResolve[i] = DataToResolve[i].Replace(N, KV.Value);
359 private void ResolveRGBColors()
362 foreach (TableConfig TC
in TableConfigurations)
364 foreach (TableConfigColumn C
in TC.Columns)
366 foreach (TableConfigSetting S
in C)
368 if (ColorConfigurations.Any(CC=>CC.Name.Equals(S.ColorName,StringComparison.InvariantCultureIgnoreCase)))
370 S.ColorConfig = ColorConfigurations.First(CC=>CC.Name.Equals(S.ColorName,StringComparison.InvariantCultureIgnoreCase));
455 public LedControlConfig(
string LedControlIniFilename,
int LedWizNumber,
bool ThrowExceptions =
false)
458 ParseLedControlIni(
new FileInfo(LedControlIniFilename), ThrowExceptions);
459 this.LedWizNumber = LedWizNumber;
476 public LedControlConfig(FileInfo LedControlIniFile,
int LedWizNumber,
bool ThrowExceptions =
false)
479 ParseLedControlIni(LedControlIniFile, ThrowExceptions);
480 this.LedWizNumber = LedWizNumber;
LedControlConfig()
Initializes a new instance of the LedControlConfig class.
List of color configurations from a ledcontrol.ini file.
LedControlConfig(string LedControlIniFilename, int LedWizNumber, bool ThrowExceptions=false)
Initializes a new instance of the LedControlConfig class. Parses the ledcontrol.ini file...
LedControlConfig(FileInfo LedControlIniFile, int LedWizNumber, bool ThrowExceptions=false)
Initializes a new instance of the LedControlConfig class. Parses the ledcontrol.ini file...
Ledcontrol configuration read from a ledcontrol.ini file.
A list of table configs from a ini file.