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
GlobalConfig.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Reflection;
5 using System.Text;
6 using System.Xml;
7 using System.Xml.Serialization;
8 using DirectOutput.General;
9 
10 
11 namespace DirectOutput.GlobalConfiguration
12 {
13 
17  public class GlobalConfig
18  {
19 
20 
21 
22  #region IniFiles
23 
24 
25  private int _LedControlMinimumEffectDurationMs = 60;
26 
35  public int LedControlMinimumEffectDurationMs
36  {
37  get { return _LedControlMinimumEffectDurationMs; }
38  set { _LedControlMinimumEffectDurationMs = value; }
39  }
40 
41  private int _LedControlMinimumRGBEffectDurationMs = 120;
42 
51  public int LedControlMinimumRGBEffectDurationMs
52  {
53  get { return _LedControlMinimumRGBEffectDurationMs; }
54  set { _LedControlMinimumRGBEffectDurationMs = value; }
55  }
56 
57 
58 
59  private string _IniFilesPath="";
60 
67  public string IniFilesPath
68  {
69  get { return _IniFilesPath; }
70  set { _IniFilesPath = value; }
71  }
72 
73 
79  public Dictionary<int, FileInfo> GetIniFilesDictionary(string TableFilename = "")
80  {
81  //Build the array of possible paths for the ini files
82 
83  List<string> LookupPaths = new List<string>();
84 
85  if (!IniFilesPath.IsNullOrWhiteSpace())
86  {
87  try
88  {
89  DirectoryInfo DI = new DirectoryInfo(IniFilesPath);
90  if (DI.Exists)
91  {
92  LookupPaths.Add(DI.FullName);
93  }
94  } catch (Exception E) {
95  Log.Exception("The specified IniFilesPath {0} could not be used due to a exception.".Build(IniFilesPath),E);
96  } ;
97  }
98 
99 
100  if (!TableFilename.IsNullOrWhiteSpace())
101  {
102  try
103  {
104  if (new FileInfo(TableFilename).Directory.Exists)
105  {
106  LookupPaths.Add(new FileInfo(TableFilename).Directory.FullName);
107  }
108  }
109  catch { }
110  }
111 
112 
113  LookupPaths.AddRange(new string[] { GetGlobalConfigDirectory().FullName, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) });
114 
115  //Build the dictionary of ini files
116 
117  Dictionary<int, FileInfo> IniFiles = new Dictionary<int, FileInfo>();
118 
119  bool FoundIt = false;
120  string[] LedControlFilenames = { "directoutputconfig", "ledcontrol" };
121 
122  foreach (string LedControlFilename in LedControlFilenames)
123  {
124  foreach (string P in LookupPaths)
125  {
126  DirectoryInfo DI = new DirectoryInfo(P);
127 
128  List<FileInfo> Files = new List<FileInfo>();
129  foreach (FileInfo FI in DI.EnumerateFiles())
130  {
131  if (FI.Name.ToLower().StartsWith(LedControlFilename.ToLower()) && FI.Name.ToLower().EndsWith(".ini"))
132  {
133  Files.Add(FI);
134  }
135  }
136 
137 
138  foreach (FileInfo FI in Files)
139  {
140  if (string.Equals(FI.Name, "{0}.ini".Build(LedControlFilename), StringComparison.OrdinalIgnoreCase))
141  {
142  if (!IniFiles.ContainsKey(1))
143  {
144  IniFiles.Add(1, FI);
145  FoundIt = true;
146  }
147  else
148  {
149  Log.Warning("Found more than one ini file with for number 1. Likely you have a ini file without a number and and a second one with number 1.");
150  }
151  }
152  else
153  {
154  string F = FI.Name.Substring(LedControlFilename.Length, FI.Name.Length - LedControlFilename.Length - 4);
155  if (F.IsInteger())
156  {
157  int LedWizNr = -1;
158  if (int.TryParse(F, out LedWizNr))
159  {
160  if (!IniFiles.ContainsKey(LedWizNr))
161  {
162  IniFiles.Add(LedWizNr, FI);
163  FoundIt = true;
164  }
165  else
166  {
167  Log.Warning("Found more than one ini file with number {0}.".Build(LedWizNr));
168  }
169 
170  }
171 
172  }
173 
174  }
175  };
176  if (FoundIt) break;
177  }
178  if (FoundIt) break;
179  }
180 
181 
182 
183  return IniFiles;
184 
185  }
186 
187 
188 
189 
190  #endregion
191 
192 
193  #region Cabinet
194 
195  #region Cabinet config file
196 
197 
198  private FilePattern _CabinetConfigFilePattern=new FilePattern();
199 
206  public FilePattern CabinetConfigFilePattern
207  {
208  get { return _CabinetConfigFilePattern; }
209  set { _CabinetConfigFilePattern = value; }
210  }
211 
212 
213 
214 
219  public FileInfo GetCabinetConfigFile()
220  {
221  if (!CabinetConfigFilePattern.Pattern.IsNullOrWhiteSpace() && CabinetConfigFilePattern.IsValid)
222  {
223  return CabinetConfigFilePattern.GetFirstMatchingFile(GetReplaceValuesDictionary());
224  }
225 
226  return null;
227  }
228 
229 
234  public DirectoryInfo GetCabinetConfigDirectory()
235  {
236  FileInfo CC = GetCabinetConfigFile();
237  if (CC != null)
238  {
239  return CC.Directory;
240  }
241  return null;
242  }
243  #endregion
244 
245 
246  #endregion
247 
248 
249  #region Table
250 
251 
252 
253 
254 
255 
256  #region Table Config
257  private FilePatternList _TableConfigFilePatterns = new FilePatternList();
258 
265  public FilePatternList TableConfigFilePatterns
266  {
267  get { return _TableConfigFilePatterns; }
268  set { _TableConfigFilePatterns = value; }
269  }
270 
278  public FileInfo GetTableConfigFile(string FullTableFilename)
279  {
280  return TableConfigFilePatterns.GetFirstMatchingFile(GetReplaceValuesDictionary(FullTableFilename));
281  }
282 
283  #endregion
284 
285 
286  #endregion
287 
288 
289 
290  #region Logging
291  private bool _EnableLog = false;
292 
293 
300  public bool EnableLogging
301  {
302  get { return _EnableLog; }
303  set { _EnableLog = value; }
304  }
305 
306  private bool _ClearLogOnSessionStart=false;
307 
314  public bool ClearLogOnSessionStart
315  {
316  get { return _ClearLogOnSessionStart; }
317  set { _ClearLogOnSessionStart = value; }
318  }
319 
320 
321 
322 
323  private FilePattern _LogFilePattern = new FilePattern(".\\DirectOutput.log");
324 
325 
343  public FilePattern LogFilePattern
344  {
345  get { return _LogFilePattern; }
346  set { _LogFilePattern = value; }
347  }
348 
355  public string GetLogFilename(string TableFilename = "", string RomName = "")
356  {
357  Dictionary<string, string> R = GetReplaceValuesDictionary(TableFilename, RomName);
358  R.Add("DateTime", DateTime.Now.ToString("yyyyMMdd_hhmmss"));
359  R.Add("Date", DateTime.Now.ToString("yyyyMMdd"));
360  R.Add("Time", DateTime.Now.ToString("hhmmss"));
361 
362  return LogFilePattern.ReplacePlaceholders(R);
363  }
364 
365  #endregion
366 
367 
368 
369  private Dictionary<string, string> GetReplaceValuesDictionary(string TableFileName = null, string RomName = "")
370  {
371  Dictionary<string, string> D = new Dictionary<string, string>();
372  if (GetGlobalConfigFile() != null)
373  {
374  D.Add("GlobalConfigDirectory", GetGlobalConfigDirectory().FullName);
375  D.Add("GlobalConfigDir", GetGlobalConfigDirectory().FullName);
376  }
377 
378  FileInfo FI = new FileInfo(Assembly.GetExecutingAssembly().Location);
379  D.Add("DllDirectory", FI.Directory.FullName);
380  D.Add("DllDir", FI.Directory.FullName);
381  D.Add("AssemblyDirectory", FI.Directory.FullName);
382  D.Add("AssemblyDir", FI.Directory.FullName);
383  if (!TableFileName.IsNullOrWhiteSpace())
384  {
385  FI = new FileInfo(TableFileName);
386  if (FI.Directory.Exists)
387  {
388  D.Add("TableDirectory", FI.Directory.FullName);
389  D.Add("TableDir", FI.Directory.FullName);
390  D.Add("TableDirectoryName", FI.Directory.Name);
391  D.Add("TableDirName", FI.Directory.Name);
392  }
393 
394  D.Add("TableName", Path.GetFileNameWithoutExtension(FI.FullName));
395  }
396  if (!RomName.IsNullOrWhiteSpace())
397  {
398  D.Add("RomName", RomName);
399  }
400 
401 
402  return D;
403 
404  }
405 
406 
407 
408  #region Global config properties
409 
410 
415  public string GlobalConfigDirectoryName()
416  {
417  DirectoryInfo DI = GetGlobalConfigDirectory();
418  if (DI == null) return null;
419  return DI.FullName;
420  }
421 
428  public DirectoryInfo GetGlobalConfigDirectory()
429  {
430  FileInfo FI = GetGlobalConfigFile();
431  if (FI == null) return null;
432  return FI.Directory;
433 
434  }
435 
436 
437  private string _GlobalConfigFilename = "";
438 
445  [XmlIgnore]
446  public string GlobalConfigFilename
447  {
448  get { return _GlobalConfigFilename; }
449  set { _GlobalConfigFilename = value; }
450  }
451 
452 
457  public FileInfo GetGlobalConfigFile()
458  {
459  if (GlobalConfigFilename.IsNullOrWhiteSpace()) { return null; }
460  return new FileInfo(GlobalConfigFilename);
461  }
462  #endregion
463 
464 
465  #region Serialization
466 
467 
468 
469 
470  public string GetGlobalConfigXml()
471  {
472 
473  XmlSerializerNamespaces Namespaces = new XmlSerializerNamespaces();
474  Namespaces.Add(string.Empty, string.Empty);
475  XmlSerializer Serializer = new XmlSerializer(typeof(GlobalConfig));
476  MemoryStream Stream = new MemoryStream();
477  XmlWriterSettings Settings = new XmlWriterSettings();
478  Settings.Indent = true;
479  Settings.NewLineOnAttributes = true;
480 
481  XmlWriter Writer = XmlWriter.Create(Stream, Settings);
482  Writer.WriteStartDocument();
483  Writer.WriteComment("Global configuration for the DirectOutput framework.");
484  Writer.WriteComment("Saved by DirectOutput Version {1}: {0}".Build(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"), System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()));
485  Serializer.Serialize(Writer, this, Namespaces);
486  Writer.WriteEndDocument();
487  Writer.Flush();
488  Stream.Position = 0;
489  string XML;
490  using (StreamReader sr = new StreamReader(Stream, Encoding.Default))
491  {
492  XML = sr.ReadToEnd();
493  sr.Dispose();
494  }
495  Stream.Dispose();
496 
497  return XML;
498  }
499 
500 
501 
508  public static GlobalConfig GetGlobalConfigFromConfigXmlFile(string GlobalConfigFileName)
509  {
510 
511  try
512  {
513  if (File.Exists(GlobalConfigFileName))
514  {
515 
516 
517  string Xml = General.FileReader.ReadFileToString(GlobalConfigFileName);
518 
519  GlobalConfig GC = GetGlobalConfigFromGlobalConfigXml(Xml);
520  if (GC != null)
521  {
522  GC.GlobalConfigFilename = GlobalConfigFileName;
523  }
524  return GC;
525 
526  }
527  else
528  {
529 
530  return null;
531  }
532  }
533  catch
534  {
535 
536  return null;
537  }
538  }
539 
540 
546  public static GlobalConfig GetGlobalConfigFromGlobalConfigXml(string ConfigXml)
547  {
548  try
549  {
550 
551  byte[] xmlBytes = Encoding.Default.GetBytes(ConfigXml);
552  using (MemoryStream ms = new MemoryStream(xmlBytes))
553  {
554  return (GlobalConfig)new XmlSerializer(typeof(GlobalConfig)).Deserialize(ms);
555  }
556  }
557  catch { return null; }
558 
559  }
560 
566  public void SaveGlobalConfig(string GlobalConfigFilename = "")
567  {
568  string GCFileName = (GlobalConfigFilename.IsNullOrWhiteSpace() ? this.GlobalConfigFilename : GlobalConfigFilename);
569  if (GCFileName.IsNullOrWhiteSpace())
570  {
571  ArgumentException Ex = new ArgumentException("No filename for GlobalConfig file has been supplied. Looking up the filename from the property GlobalConfigFilename failed as well");
572  throw Ex;
573  }
574  if (File.Exists(GCFileName))
575  {
576  //Create a backup of the current global config file
577  File.Copy(GCFileName, Path.Combine(Path.GetDirectoryName(GCFileName), "{1} old (replaced {0}){2}".Build(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"), Path.GetFileNameWithoutExtension(GCFileName), Path.GetExtension(GCFileName))));
578  };
579  DirectoryInfo GCDirectory = new FileInfo(GCFileName).Directory;
580 
581  GCDirectory.CreateDirectoryPath();
582  GetGlobalConfigXml().WriteToFile(GCFileName, false);
583 
584  }
585  #endregion
586 
587 
591  public GlobalConfig()
592  {
593 
594 
595 
596  }
597 
598  }
599 }