DirectOuput Framework R2
DirectOutput framework R2 for virtual pinball cabinets
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
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 
212  }
213  else
214  {
215  Log.Warning("No DOF version information found in file {0}.".Build(LedControlIniFile));
216  }
217 
218  }
219  else
220  {
221  Log.Warning("No version section found in file {0}.".Build(LedControlIniFile));
222  }
223 
224  if (ColorData == null)
225  {
226  Log.Warning("Could not find color definition section in file {0}.".Build(LedControlIniFile));
227  if (ThrowExceptions)
228  {
229  throw new Exception("Could not find color definition section in file {0}.".Build(LedControlIniFile));
230  }
231  return;
232  }
233  else if (ColorData.Count < 1)
234  {
235  Log.Warning("File {0} does not contain data in the color definition section.".Build(LedControlIniFile));
236  if (ThrowExceptions)
237  {
238  throw new Exception("File {0} does not contain data in the color definition section.".Build(LedControlIniFile));
239  }
240  return;
241  }
242 
243  if (OutData == null)
244  {
245  Log.Warning("Could not find table config section in file {0}.".Build(LedControlIniFile));
246  if (ThrowExceptions)
247  {
248  throw new Exception("Could not find table config section section in file {1}.".Build(LedControlIniFile));
249  }
250  return;
251  }
252  else if (OutData.Count < 1)
253  {
254  Log.Warning("File {0} does not contain data in the table config section.".Build(LedControlIniFile));
255  if (ThrowExceptions)
256  {
257  throw new Exception("File {0} does not contain data in the table config section".Build(LedControlIniFile));
258  }
259  return;
260  }
261 
262  if (VariableData != null)
263  {
264  ResolveVariables(OutData, VariableData);
265  }
266 
267  if (TableVariableData != null)
268  {
269  ResolveTableVariables(OutData, TableVariableData);
270  }
271 
272 
273 
274  ColorConfigurations.ParseLedControlData(ColorData, ThrowExceptions);
275 
276  TableConfigurations.ParseLedcontrolData(OutData, ThrowExceptions);
277 
278  //ResolveOutputNumbers();
279  ResolveRGBColors();
280 
281  this.LedControlIniFile = LedControlIniFile;
282  }
283 
284  private List<string> GetSection(Dictionary<string, List<string>> Sections, IEnumerable<string> SectionStartStrings)
285  {
286  foreach (string StartString in SectionStartStrings)
287  {
288  if (Sections.ContainsKey(StartString))
289  {
290  return Sections[StartString];
291  }
292  }
293 
294  return null;
295  }
296 
297 
298  private void ResolveTableVariables(List<String> DataToResolve, List<string> VariableData)
299  {
300 
301  TableVariablesDictionary VD = new TableVariablesDictionary(VariableData);
302 
303 
304  for (int i = 0; i < DataToResolve.Count - 1; i++)
305  {
306  string D = DataToResolve[i].Trim();
307  bool Updated = false;
308  if (!D.IsNullOrWhiteSpace())
309  {
310  int TP = D.IndexOf(",");
311  if (TP > 0)
312  {
313  string TableName = D.Substring(0, TP).Trim();
314  if (VD.ContainsKey(TableName))
315  {
316  foreach (KeyValuePair<string, string> KV in VD[TableName])
317  {
318  string N = KV.Key;
319  if (!N.StartsWith("@")) N = "@" + N;
320  if (!N.EndsWith("@")) N += "@";
321  D = D.Replace(N, KV.Value);
322  Updated = true;
323  }
324  }
325  }
326 
327  }
328  if (Updated)
329  {
330  DataToResolve[i] = D;
331 
332  }
333  }
334 
335  }
336 
337 
338  private void ResolveVariables(List<String> DataToResolve, List<string> VariableData)
339  {
340  VariablesDictionary VD = new VariablesDictionary(VariableData);
341  foreach (KeyValuePair<string, string> KV in VD)
342  {
343  string N = KV.Key;
344  if (!N.StartsWith("@")) N = "@" + N;
345  if (!N.EndsWith("@")) N += "@";
346 
347  for (int i = 0; i < DataToResolve.Count - 1; i++)
348  {
349  DataToResolve[i] = DataToResolve[i].Replace(N, KV.Value);
350  }
351  }
352 
353  }
354 
355  private void ResolveRGBColors()
356  {
357 
358  foreach (TableConfig TC in TableConfigurations)
359  {
360  foreach (TableConfigColumn C in TC.Columns)
361  {
362  foreach (TableConfigSetting S in C)
363  {
364  if (ColorConfigurations.Contains(S.ColorName))
365  {
366  S.ColorConfig = ColorConfigurations[S.ColorName];
367 
368  }
369  }
370  }
371  }
372  }
373 
374 
375  //private void ResolveOutputNumbers()
376  //{
377  // //Get the number of required outputs per column
378  // Dictionary<int, int> RequiredOutputs = new Dictionary<int, int>();
379  // foreach (TableConfig TC in TableConfigurations)
380  // {
381  // TC.Columns.Sort();
382  // foreach (TableConfigColumn C in TC.Columns)
383  // {
384  // foreach (TableConfigSetting S in C)
385  // {
386  // int Cnt = (S.OutputType == OutputTypeEnum.RGBOutput ? 3 : 1);
387  // if (RequiredOutputs.ContainsKey(C.Number))
388  // {
389 
390  // if (RequiredOutputs[C.Number] < Cnt)
391  // {
392  // RequiredOutputs[C.Number] = Cnt;
393  // }
394  // }
395  // else
396  // {
397  // RequiredOutputs.Add(C.Number, Cnt);
398  // }
399  // }
400 
401  // }
402 
403  // }
404 
405 
406  // //Dump();
407  //}
408 
409 
410  //private void Dump()
411  //{
412  // string A = "";
413  // foreach (TableConfig TC in TableConfigurations)
414  // {
415  // A+="Cols: {0:##}, ".Build( TC.Columns.Count);
416  // foreach (TableConfigColumn C in TC.Columns)
417  // {
418  // A+="({0:00} {1} {2:00})".Build( C.Number, C.RequiredOutputCount, C.FirstOutputNumber);
419  // }
420  // A += "\n";
421  // }
422  // A.WriteToFile("c:\\parseresult.txt");
423 
424  //}
425 
426 
427 
432  {
433  this.TableConfigurations = new TableConfigList();
434  this.ColorConfigurations = new ColorConfigList();
435  }
436 
451  public LedControlConfig(string LedControlIniFilename, int LedWizNumber, bool ThrowExceptions = false)
452  : this()
453  {
454  ParseLedControlIni(new FileInfo(LedControlIniFilename), ThrowExceptions);
455  this.LedWizNumber = LedWizNumber;
456  }
457 
472  public LedControlConfig(FileInfo LedControlIniFile, int LedWizNumber, bool ThrowExceptions = false)
473  : this()
474  {
475  ParseLedControlIni(LedControlIniFile, ThrowExceptions);
476  this.LedWizNumber = LedWizNumber;
477  }
478 
479  }
480 }