WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
LedControlConfig.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Text;
5 using System.Linq;
6 
7 namespace DirectOutput.LedControl.Loader
8 {
12  public class LedControlConfig
13  {
14  private int _LedWizNumber = 0;
15 
22  public int LedWizNumber
23  {
24  get { return _LedWizNumber; }
25  set { _LedWizNumber = value; }
26  }
27 
28  public Version MinDOFVersion { get; set; }
29 
30 
31 
32  private TableConfigList _TableConfigurations;
33 
40  public TableConfigList TableConfigurations
41  {
42  get { return _TableConfigurations; }
43  set { _TableConfigurations = value; }
44  }
45 
46  private ColorConfigList _ColorConfigurations;
47 
54  public ColorConfigList ColorConfigurations
55  {
56  get { return _ColorConfigurations; }
57  set { _ColorConfigurations = value; }
58  }
59 
60 
61  private FileInfo _LedControlIniFile;
62 
69  public FileInfo LedControlIniFile
70  {
71  get { return _LedControlIniFile; }
72  private set { _LedControlIniFile = value; }
73  }
74 
75 
76 
77 
78 
79 
94  private void ParseLedControlIni(FileInfo LedControlIniFile, bool ThrowExceptions = false)
95  {
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 = "";
102 
103  #region Read file
104  try
105  {
106  FileData = General.FileReader.ReadFileToString(LedControlIniFile);
107  }
108  catch (Exception E)
109  {
110  Log.Exception("Could not read file {0}.".Build(LedControlIniFile), E);
111  if (ThrowExceptions)
112  {
113 
114  throw new Exception("Could not read file {0}.".Build(LedControlIniFile), E);
115  }
116  }
117  if (FileData.IsNullOrWhiteSpace())
118  {
119  Log.Warning("File {0} does not contain data.".Build(LedControlIniFile));
120  if (ThrowExceptions)
121  {
122  throw new Exception("File {0} does not contain data.".Build(LedControlIniFile));
123  }
124  }
125  #endregion
126 
127  Dictionary<string, List<string>> Sections = new Dictionary<string, List<String>>();
128 
129  #region Read sections
130 
131  List<string> SectionData = new List<string>();
132  String SectionHeader = null;
133  foreach (string RawIniLine in FileData.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
134  {
135  string IniLine = RawIniLine.Trim();
136  if (IniLine.Length > 0 && !IniLine.StartsWith("#"))
137  {
138  if (IniLine.StartsWith("[") && IniLine.EndsWith("]") && IniLine.Length > 2)
139  {
140  //This is a section header
141  if (!SectionHeader.IsNullOrWhiteSpace())
142  {
143  if (Sections.ContainsKey(SectionHeader))
144  {
145  int Cnt = 2;
146  while (Sections.ContainsKey("{0} {1}".Build(SectionHeader, Cnt)))
147  {
148  Cnt++;
149  if (Cnt > 999) { throw new Exception("Section header {0} exists to many times.".Build(SectionHeader)); }
150  }
151  SectionHeader = "{0} {1}".Build(SectionHeader, Cnt);
152  }
153  Sections.Add(SectionHeader, SectionData);
154  SectionData = new List<string>();
155  }
156  SectionHeader = IniLine;
157  }
158  else
159  {
160  //Its a data line
161  SectionData.Add(IniLine);
162  }
163  }
164  }
165 
166  if (!SectionHeader.IsNullOrWhiteSpace())
167  {
168  if (Sections.ContainsKey(SectionHeader))
169  {
170  int Cnt = 2;
171  while (Sections.ContainsKey("{0} {1}".Build(SectionHeader, Cnt)))
172  {
173  Cnt++;
174  if (Cnt > 999) { throw new Exception("Section header {0} exists to many times.".Build(SectionHeader)); }
175  }
176  SectionHeader = "{0} {1}".Build(SectionHeader, Cnt);
177  }
178  Sections.Add(SectionHeader, SectionData);
179  SectionData = new List<string>();
180  }
181  SectionData = null;
182  #endregion
183 
184  FileData = null;
185 
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);
191 
192  if (VersionData != null && VersionData.Count > 0)
193  {
194  MinDOFVersion = null;
195 
196  string MinDofVersionLine = VersionData.FirstOrDefault(S => S.ToLowerInvariant().StartsWith("mindofversion="));
197 
198  if (MinDofVersionLine != null)
199  {
200  string MinDofVersionString = MinDofVersionLine.Substring("mindofversion=".Length);
201 
202  try
203  {
204  MinDOFVersion = new Version(MinDofVersionString);
205  }
206  catch (Exception E)
207  {
208  Log.Exception("Could not parse line {1} from file {0}".Build(LedControlIniFile, MinDofVersionLine));
209  MinDOFVersion = null;
210  }
211  if (MinDOFVersion != null)
212  {
213  Log.Write("Min DOF Version is {0} for file {1}".Build(MinDOFVersion.ToString(), LedControlIniFile.Name));
214  }
215 
216  }
217  else
218  {
219  Log.Warning("No DOF version information found in file {0}.".Build(LedControlIniFile));
220  }
221 
222  }
223  else
224  {
225  Log.Warning("No version section found in file {0}.".Build(LedControlIniFile));
226  }
227 
228  if (ColorData == null)
229  {
230  Log.Warning("Could not find color definition section in file {0}.".Build(LedControlIniFile));
231  if (ThrowExceptions)
232  {
233  throw new Exception("Could not find color definition section in file {0}.".Build(LedControlIniFile));
234  }
235  return;
236  }
237  else if (ColorData.Count < 1)
238  {
239  Log.Warning("File {0} does not contain data in the color definition section.".Build(LedControlIniFile));
240  if (ThrowExceptions)
241  {
242  throw new Exception("File {0} does not contain data in the color definition section.".Build(LedControlIniFile));
243  }
244  return;
245  }
246 
247  if (OutData == null)
248  {
249  Log.Warning("Could not find table config section in file {0}.".Build(LedControlIniFile));
250  if (ThrowExceptions)
251  {
252  throw new Exception("Could not find table config section section in file {1}.".Build(LedControlIniFile));
253  }
254  return;
255  }
256  else if (OutData.Count < 1)
257  {
258  Log.Warning("File {0} does not contain data in the table config section.".Build(LedControlIniFile));
259  if (ThrowExceptions)
260  {
261  throw new Exception("File {0} does not contain data in the table config section".Build(LedControlIniFile));
262  }
263  return;
264  }
265 
266  if (VariableData != null)
267  {
268  ResolveVariables(OutData, VariableData);
269  }
270 
271  if (TableVariableData != null)
272  {
273  ResolveTableVariables(OutData, TableVariableData);
274  }
275 
276 
277 
278  ColorConfigurations.ParseLedControlData(ColorData, ThrowExceptions);
279 
280  TableConfigurations.ParseLedcontrolData(OutData, ThrowExceptions);
281 
282  //ResolveOutputNumbers();
283  ResolveRGBColors();
284 
285  this.LedControlIniFile = LedControlIniFile;
286  }
287 
288  private List<string> GetSection(Dictionary<string, List<string>> Sections, IEnumerable<string> SectionStartStrings)
289  {
290  foreach (string StartString in SectionStartStrings)
291  {
292  if (Sections.ContainsKey(StartString))
293  {
294  return Sections[StartString];
295  }
296  }
297 
298  return null;
299  }
300 
301 
302  private void ResolveTableVariables(List<String> DataToResolve, List<string> VariableData)
303  {
304 
305  TableVariablesDictionary VD = new TableVariablesDictionary(VariableData);
306 
307 
308  for (int i = 0; i < DataToResolve.Count ; i++)
309  {
310  string D = DataToResolve[i].Trim();
311  bool Updated = false;
312  if (!D.IsNullOrWhiteSpace())
313  {
314  int TP = D.IndexOf(",");
315  if (TP > 0)
316  {
317  string TableName = D.Substring(0, TP).Trim();
318  if (VD.ContainsKey(TableName))
319  {
320  foreach (KeyValuePair<string, string> KV in VD[TableName])
321  {
322  string N = KV.Key;
323  if (!N.StartsWith("@")) N = "@" + N;
324  if (!N.EndsWith("@")) N += "@";
325  D = D.Replace(N, KV.Value);
326  Updated = true;
327  }
328  }
329  }
330 
331  }
332  if (Updated)
333  {
334  DataToResolve[i] = D;
335 
336  }
337  }
338 
339  }
340 
341 
342  private void ResolveVariables(List<String> DataToResolve, List<string> VariableData)
343  {
344  VariablesDictionary VD = new VariablesDictionary(VariableData);
345  foreach (KeyValuePair<string, string> KV in VD)
346  {
347  string N = KV.Key;
348  if (!N.StartsWith("@")) N = "@" + N;
349  if (!N.EndsWith("@")) N += "@";
350 
351  for (int i = 0; i < DataToResolve.Count; i++)
352  {
353  DataToResolve[i] = DataToResolve[i].Replace(N, KV.Value);
354  }
355  }
356 
357  }
358 
359  private void ResolveRGBColors()
360  {
361 
362  foreach (TableConfig TC in TableConfigurations)
363  {
364  foreach (TableConfigColumn C in TC.Columns)
365  {
366  foreach (TableConfigSetting S in C)
367  {
368  if (ColorConfigurations.Any(CC=>CC.Name.Equals(S.ColorName,StringComparison.InvariantCultureIgnoreCase)))
369  {
370  S.ColorConfig = ColorConfigurations.First(CC=>CC.Name.Equals(S.ColorName,StringComparison.InvariantCultureIgnoreCase));
371 
372  }
373  }
374  }
375  }
376  }
377 
378 
379  //private void ResolveOutputNumbers()
380  //{
381  // //Get the number of required outputs per column
382  // Dictionary<int, int> RequiredOutputs = new Dictionary<int, int>();
383  // foreach (TableConfig TC in TableConfigurations)
384  // {
385  // TC.Columns.Sort();
386  // foreach (TableConfigColumn C in TC.Columns)
387  // {
388  // foreach (TableConfigSetting S in C)
389  // {
390  // int Cnt = (S.OutputType == OutputTypeEnum.RGBOutput ? 3 : 1);
391  // if (RequiredOutputs.ContainsKey(C.Number))
392  // {
393 
394  // if (RequiredOutputs[C.Number] < Cnt)
395  // {
396  // RequiredOutputs[C.Number] = Cnt;
397  // }
398  // }
399  // else
400  // {
401  // RequiredOutputs.Add(C.Number, Cnt);
402  // }
403  // }
404 
405  // }
406 
407  // }
408 
409 
410  // //Dump();
411  //}
412 
413 
414  //private void Dump()
415  //{
416  // string A = "";
417  // foreach (TableConfig TC in TableConfigurations)
418  // {
419  // A+="Cols: {0:##}, ".Build( TC.Columns.Count);
420  // foreach (TableConfigColumn C in TC.Columns)
421  // {
422  // A+="({0:00} {1} {2:00})".Build( C.Number, C.RequiredOutputCount, C.FirstOutputNumber);
423  // }
424  // A += "\n";
425  // }
426  // A.WriteToFile("c:\\parseresult.txt");
427 
428  //}
429 
430 
431 
436  {
437  this.TableConfigurations = new TableConfigList();
438  this.ColorConfigurations = new ColorConfigList();
439  }
440 
455  public LedControlConfig(string LedControlIniFilename, int LedWizNumber, bool ThrowExceptions = false)
456  : this()
457  {
458  ParseLedControlIni(new FileInfo(LedControlIniFilename), ThrowExceptions);
459  this.LedWizNumber = LedWizNumber;
460  }
461 
476  public LedControlConfig(FileInfo LedControlIniFile, int LedWizNumber, bool ThrowExceptions = false)
477  : this()
478  {
479  ParseLedControlIni(LedControlIniFile, ThrowExceptions);
480  this.LedWizNumber = LedWizNumber;
481  }
482 
483  }
484 }
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.