2 using System.Collections.Generic;
7 namespace DirectOutput.LedControl.Loader
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;
215 Log.Warning(
"No DOF version information found in file {0}.".Build(LedControlIniFile));
221 Log.Warning(
"No version section found in file {0}.".Build(LedControlIniFile));
224 if (ColorData == null)
226 Log.Warning(
"Could not find color definition section in file {0}.".Build(LedControlIniFile));
229 throw new Exception(
"Could not find color definition section in file {0}.".Build(LedControlIniFile));
233 else if (ColorData.Count < 1)
235 Log.Warning(
"File {0} does not contain data in the color definition section.".Build(LedControlIniFile));
238 throw new Exception(
"File {0} does not contain data in the color definition section.".Build(LedControlIniFile));
245 Log.Warning(
"Could not find table config section in file {0}.".Build(LedControlIniFile));
248 throw new Exception(
"Could not find table config section section in file {1}.".Build(LedControlIniFile));
252 else if (OutData.Count < 1)
254 Log.Warning(
"File {0} does not contain data in the table config section.".Build(LedControlIniFile));
257 throw new Exception(
"File {0} does not contain data in the table config section".Build(LedControlIniFile));
262 if (VariableData != null)
264 ResolveVariables(OutData, VariableData);
267 if (TableVariableData != null)
269 ResolveTableVariables(OutData, TableVariableData);
274 ColorConfigurations.ParseLedControlData(ColorData, ThrowExceptions);
276 TableConfigurations.ParseLedcontrolData(OutData, ThrowExceptions);
281 this.LedControlIniFile = LedControlIniFile;
284 private List<string> GetSection(Dictionary<
string, List<string>> Sections, IEnumerable<string> SectionStartStrings)
286 foreach (
string StartString
in SectionStartStrings)
288 if (Sections.ContainsKey(StartString))
290 return Sections[StartString];
298 private void ResolveTableVariables(List<String> DataToResolve, List<string> VariableData)
301 TableVariablesDictionary VD =
new TableVariablesDictionary(VariableData);
304 for (
int i = 0; i < DataToResolve.Count - 1; i++)
306 string D = DataToResolve[i].Trim();
307 bool Updated =
false;
308 if (!D.IsNullOrWhiteSpace())
310 int TP = D.IndexOf(
",");
313 string TableName = D.Substring(0, TP).Trim();
314 if (VD.ContainsKey(TableName))
316 foreach (KeyValuePair<string, string> KV
in VD[TableName])
319 if (!N.StartsWith(
"@")) N =
"@" + N;
320 if (!N.EndsWith(
"@")) N +=
"@";
321 D = D.Replace(N, KV.Value);
330 DataToResolve[i] = D;
338 private void ResolveVariables(List<String> DataToResolve, List<string> VariableData)
340 VariablesDictionary VD =
new VariablesDictionary(VariableData);
341 foreach (KeyValuePair<string, string> KV
in VD)
344 if (!N.StartsWith(
"@")) N =
"@" + N;
345 if (!N.EndsWith(
"@")) N +=
"@";
347 for (
int i = 0; i < DataToResolve.Count - 1; i++)
349 DataToResolve[i] = DataToResolve[i].Replace(N, KV.Value);
355 private void ResolveRGBColors()
358 foreach (TableConfig TC
in TableConfigurations)
360 foreach (TableConfigColumn C
in TC.Columns)
362 foreach (TableConfigSetting S
in C)
364 if (ColorConfigurations.Contains(S.ColorName))
366 S.ColorConfig = ColorConfigurations[S.ColorName];
451 public LedControlConfig(
string LedControlIniFilename,
int LedWizNumber,
bool ThrowExceptions =
false)
454 ParseLedControlIni(
new FileInfo(LedControlIniFilename), ThrowExceptions);
455 this.LedWizNumber = LedWizNumber;
472 public LedControlConfig(FileInfo LedControlIniFile,
int LedWizNumber,
bool ThrowExceptions =
false)
475 ParseLedControlIni(LedControlIniFile, ThrowExceptions);
476 this.LedWizNumber = LedWizNumber;