DirectOutputR1
DirectOutput framework R1 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 
5 namespace DirectOutput.LedControl.Loader
6 {
10  public class LedControlConfig
11  {
12  private int _LedWizNumber = 0;
13 
20  public int LedWizNumber
21  {
22  get { return _LedWizNumber; }
23  set { _LedWizNumber = value; }
24  }
25 
26 
27 
28  private TableConfigList _TableConfigurations;
29 
36  public TableConfigList TableConfigurations
37  {
38  get { return _TableConfigurations; }
39  set { _TableConfigurations = value; }
40  }
41 
42  private ColorConfigList _ColorConfigurations;
43 
50  public ColorConfigList ColorConfigurations
51  {
52  get { return _ColorConfigurations; }
53  set { _ColorConfigurations = value; }
54  }
55 
56 
57 
72  private void ParseLedControlIni(FileInfo LedControlIniFile, bool ThrowExceptions = false)
73  {
74  string[] ColorStartStrings = {"[Colors DOF]","[Colors LedWiz]"};
75  string[] OutStartStrings = {"[Config DOF]","[Config outs]"};
76 
77  //Read the file
78  string Data="";
79  try
80  {
81  Data = General.FileReader.ReadFileToString(LedControlIniFile);
82  }
83  catch (Exception E)
84  {
85  Log.Exception("Could not read file {0}.".Build(LedControlIniFile), E);
86  if (ThrowExceptions)
87  {
88 
89  throw new Exception("Could not read file {0}.".Build(LedControlIniFile), E);
90  }
91  }
92  if (Data.IsNullOrWhiteSpace())
93  {
94  Log.Warning("File {0} does not contain data.".Build(LedControlIniFile));
95  if (ThrowExceptions)
96  {
97  throw new Exception("File {0} does not contain data.".Build(LedControlIniFile));
98  }
99  }
100 
101  //Find starting positions of both sections
102  int ColorStart = -1;
103  string ColorStartString="";
104  foreach (string S in ColorStartStrings)
105  {
106  ColorStart = Data.IndexOf(S, StringComparison.InvariantCultureIgnoreCase);
107  ColorStartString=S;
108  if (ColorStart >= 0) break;
109  }
110  int OutStart = -1;
111  string OutStartString = "";
112  foreach (string S in OutStartStrings)
113  {
114  OutStart = Data.IndexOf(S, StringComparison.InvariantCultureIgnoreCase);
115  OutStartString = S;
116  if (OutStart >= 0) break;
117  }
118 
119  if (ColorStart < 0)
120  {
121  Log.Exception("Could not find color definition section in file {0}.".Build(LedControlIniFile));
122  if (ThrowExceptions)
123  {
124  throw new Exception("Could not find color definition section in file {0}.".Build(LedControlIniFile));
125  }
126  return;
127  }
128 
129  if (OutStart < 0)
130  {
131  Log.Exception("Could not find table config section in file {0}.".Build(LedControlIniFile));
132  if (ThrowExceptions)
133  {
134  throw new Exception("Could not find table config section section in file {1}.".Build(LedControlIniFile));
135  }
136  return;
137  }
138 
139  string[] ColorData;
140  string[] OutData;
141 
142  //Extract data of the sections, split into string arrays and remove empty lines
143  if (ColorStart < OutStart)
144  {
145  ColorData = Data.Substring(ColorStart + ColorStartString.Length, OutStart - (ColorStart + ColorStartString.Length)).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
146  OutData = Data.Substring(OutStart + OutStartString.Length).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
147  }
148  else
149  {
150  OutData = Data.Substring(OutStart + OutStartString.Length, ColorStart - (OutStart + OutStartString.Length)).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
151  ColorData = Data.Substring(ColorStart + ColorStartString.Length).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
152  }
153 
154 
155  if (OutData.Length == 0)
156  {
157  Log.Exception("File {1} does not contain data in the {0} section.".Build(OutStartString, LedControlIniFile));
158  if (ThrowExceptions)
159  {
160  throw new Exception("File {1} does not contain data in the {0} section.".Build(OutStartString, LedControlIniFile));
161  }
162  return;
163  }
164 
165  if (ColorData.Length == 0)
166  {
167  Log.Exception("File {1} does not contain data in the {0} section.".Build(ColorStartString, LedControlIniFile));
168  if (ThrowExceptions)
169  {
170  throw new Exception("File {1} does not contain data in the {0} section.".Build(ColorStartString, LedControlIniFile));
171  }
172  return;
173  }
174 
175 
176 
177  ColorConfigurations.ParseLedControlData(ColorData, ThrowExceptions);
178 
179  TableConfigurations.ParseLedcontrolData(OutData, ThrowExceptions);
180 
181  ResolveOutputNumbers();
182  ResolveRGBColors();
183  }
184 
185  private void ResolveRGBColors()
186  {
187 
188  foreach (TableConfig TC in TableConfigurations)
189  {
190  foreach (TableConfigColumn C in TC.Columns)
191  {
192  foreach (TableConfigSetting S in C)
193  {
194  if (ColorConfigurations.Contains(S.ColorName))
195  {
196  ColorConfig CC = ColorConfigurations[S.ColorName];
197  if (CC != null)
198  {
199  S.ColorConfig = CC;
200  }
201  }
202  }
203  }
204  }
205  }
206 
207 
208  private void ResolveOutputNumbers()
209  {
210  //Get the number of required outputs per column
211  Dictionary<int, int> RequiredOutputs = new Dictionary<int, int>();
212  foreach (TableConfig TC in TableConfigurations)
213  {
214  TC.Columns.Sort();
215  foreach (TableConfigColumn C in TC.Columns)
216  {
217  foreach (TableConfigSetting S in C)
218  {
219  int Cnt = (S.OutputType == OutputTypeEnum.RGBOutput ? 3 : 1);
220  if (RequiredOutputs.ContainsKey(C.Number))
221  {
222 
223  if (RequiredOutputs[C.Number] < Cnt)
224  {
225  RequiredOutputs[C.Number] = Cnt;
226  }
227  }
228  else
229  {
230  RequiredOutputs.Add(C.Number, Cnt);
231  }
232  }
233 
234  }
235 
236  }
237 
238 
239  //Dump();
240  }
241 
242 
243  //private void Dump()
244  //{
245  // string A = "";
246  // foreach (TableConfig TC in TableConfigurations)
247  // {
248  // A+="Cols: {0:##}, ".Build( TC.Columns.Count);
249  // foreach (TableConfigColumn C in TC.Columns)
250  // {
251  // A+="({0:00} {1} {2:00})".Build( C.Number, C.RequiredOutputCount, C.FirstOutputNumber);
252  // }
253  // A += "\n";
254  // }
255  // A.WriteToFile("c:\\parseresult.txt");
256 
257  //}
258 
259 
260 
265  {
266  this.TableConfigurations = new TableConfigList();
267  this.ColorConfigurations = new ColorConfigList();
268  }
269 
284  public LedControlConfig(string LedControlIniFilename, int LedWizNumber, bool ThrowExceptions = false)
285  : this()
286  {
287  ParseLedControlIni(new FileInfo(LedControlIniFilename), ThrowExceptions);
288  this.LedWizNumber = LedWizNumber;
289  }
290 
305  public LedControlConfig(FileInfo LedControlIniFile, int LedWizNumber, bool ThrowExceptions = false)
306  : this()
307  {
308  ParseLedControlIni(LedControlIniFile, ThrowExceptions);
309  this.LedWizNumber = LedWizNumber;
310  }
311 
312  }
313 }