DirectOutputR1
DirectOutput framework R1 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 
9 
10 namespace DirectOutput.GlobalConfiguration
11 {
12 
16  public class GlobalConfig
17  {
18 
19  #region Led Control
20 
21 
22  private int _LedControlMinimumEffectDurationMs = 60;
23 
32  public int LedControlMinimumEffectDurationMs
33  {
34  get { return _LedControlMinimumEffectDurationMs; }
35  set { _LedControlMinimumEffectDurationMs = value; }
36  }
37 
38  private int _LedControlMinimumRGBEffectDurationMs = 120;
39 
48  public int LedControlMinimumRGBEffectDurationMs
49  {
50  get { return _LedControlMinimumRGBEffectDurationMs; }
51  set { _LedControlMinimumRGBEffectDurationMs = value; }
52  }
53 
54 
55  private LedControlIniFileList _LedControlIniFiles = new LedControlIniFileList();
56 
63  public LedControlIniFileList LedControlIniFiles
64  {
65  get { return _LedControlIniFiles; }
66  set { _LedControlIniFiles = value; }
67  }
68 
69  #endregion
70 
71 
72  #region Cabinet
73 
74  #region Cabinet config file
75  private FilePatternList _CabinetConfigFilePatterns = new FilePatternList();
76 
77 
84  public FilePatternList CabinetConfigFilePatterns
85  {
86  get { return _CabinetConfigFilePatterns; }
87  set { _CabinetConfigFilePatterns = value; }
88  }
89 
90 
95  public FileInfo GetCabinetConfigFile()
96  {
97  if (CabinetConfigFilePatterns != null)
98  {
99  return CabinetConfigFilePatterns.GetFirstMatchingFile(GetReplaceValuesDictionary());
100  }
101  return null;
102  }
103 
104 
109  public DirectoryInfo GetCabinetConfigDirectory()
110  {
111  FileInfo CC = GetCabinetConfigFile();
112  if (CC != null)
113  {
114  return CC.Directory;
115  }
116  return null;
117  }
118  #endregion
119 
120 
121  #endregion
122 
123 
124  #region Table
125 
126 
127 
128  #region Table script files
129  private FilePatternList _TableScriptFilePatterns = new FilePatternList();
130 
137  public FilePatternList TableScriptFilePatterns
138  {
139  get { return _TableScriptFilePatterns; }
140  set { _TableScriptFilePatterns = value; }
141  }
142 
150  public List<FileInfo> GetTableScriptFiles(string FullTableFilename)
151  {
152  if (TableScriptFilePatterns != null)
153  {
154  return TableScriptFilePatterns.GetMatchingFiles(GetReplaceValuesDictionary(FullTableFilename));
155  }
156  return new List<FileInfo>();
157  }
158  #endregion
159 
160 
161  #region Table Config
162  private FilePatternList _TableConfigFilePatterns = new FilePatternList();
163 
170  public FilePatternList TableConfigFilePatterns
171  {
172  get { return _TableConfigFilePatterns; }
173  set { _TableConfigFilePatterns = value; }
174  }
175 
183  public FileInfo GetTableConfigFile(string FullTableFilename)
184  {
185  return TableConfigFilePatterns.GetFirstMatchingFile(GetReplaceValuesDictionary(FullTableFilename));
186  }
187 
188  #endregion
189 
190 
191  #endregion
192 
193 
194 
195  #region Logging
196  private bool _EnableLog = false;
197 
198 
205  public bool EnableLogging
206  {
207  get { return _EnableLog; }
208  set { _EnableLog = value; }
209  }
210 
211  private FilePattern _LogFilePattern = new FilePattern(".\\DirectOutput.log");
212 
213 
231  public FilePattern LogFilePattern
232  {
233  get { return _LogFilePattern; }
234  set { _LogFilePattern = value; }
235  }
236 
243  public string GetLogFilename(string TableFilename = "", string RomName = "")
244  {
245  Dictionary<string, string> R = GetReplaceValuesDictionary(TableFilename, RomName);
246  R.Add("DateTime", DateTime.Now.ToString("yyyyMMdd_hhmmss"));
247  R.Add("Date", DateTime.Now.ToString("yyyyMMdd"));
248  R.Add("Time", DateTime.Now.ToString("hhmmss"));
249 
250  return LogFilePattern.ReplacePlaceholders(R);
251  }
252 
253  #endregion
254 
255 
256 
257  private Dictionary<string, string> GetReplaceValuesDictionary(string TableFileName = null, string RomName = "")
258  {
259  Dictionary<string, string> D = new Dictionary<string, string>();
260  if (GetGlobalConfigFile() != null)
261  {
262  D.Add("GlobalConfigDirectory", GetGlobalConfigDirectory().FullName);
263  D.Add("GlobalConfigDir", GetGlobalConfigDirectory().FullName);
264  }
265 
266  FileInfo FI = new FileInfo(Assembly.GetExecutingAssembly().Location);
267  D.Add("DllDirectory", FI.Directory.FullName);
268  D.Add("DllDir", FI.Directory.FullName);
269  D.Add("AssemblyDirectory", FI.Directory.FullName);
270  D.Add("AssemblyDir", FI.Directory.FullName);
271  if (!TableFileName.IsNullOrWhiteSpace())
272  {
273  FI = new FileInfo(TableFileName);
274  if (FI.Directory.Exists)
275  {
276  D.Add("TableDirectory", FI.Directory.FullName);
277  D.Add("TableDir", FI.Directory.FullName);
278  D.Add("TableDirectoryName", FI.Directory.Name);
279  D.Add("TableDirName", FI.Directory.Name);
280  }
281 
282  D.Add("TableName", Path.GetFileNameWithoutExtension(FI.FullName));
283  }
284  if (!RomName.IsNullOrWhiteSpace())
285  {
286  D.Add("RomName", RomName);
287  }
288 
289 
290  return D;
291 
292  }
293 
294 
295 
296  #region Global config properties
297 
298 
299 
300 
301 
302 
303  public List<FileInfo> GetGlobalScriptFiles()
304  {
305  return GlobalScriptFilePatterns.GetMatchingFiles();
306  }
307 
308 
309  private FilePatternList _GlobalScriptFilePatterns = new FilePatternList();
310 
317  public FilePatternList GlobalScriptFilePatterns
318  {
319  get { return _GlobalScriptFilePatterns; }
320  set { _GlobalScriptFilePatterns = value; }
321  }
322 
327  public string GlobalConfigDirectoryName()
328  {
329  DirectoryInfo DI = GetGlobalConfigDirectory();
330  if (DI == null) return null;
331  return DI.FullName;
332  }
333 
340  public DirectoryInfo GetGlobalConfigDirectory()
341  {
342  FileInfo FI = GetGlobalConfigFile();
343  if (FI == null) return null;
344  return FI.Directory;
345 
346  }
347 
348 
349  private string _GlobalConfigFilename = "";
350 
357  [XmlIgnore]
358  public string GlobalConfigFilename
359  {
360  get { return _GlobalConfigFilename; }
361  set { _GlobalConfigFilename = value; }
362  }
363 
364 
369  public FileInfo GetGlobalConfigFile()
370  {
371  if (GlobalConfigFilename.IsNullOrWhiteSpace()) { return null; }
372  return new FileInfo(GlobalConfigFilename);
373  }
374  #endregion
375 
376 
377  #region Serialization
378 
379 
380 
381 
382  public string GetGlobalConfigXml()
383  {
384 
385  XmlSerializerNamespaces Namespaces = new XmlSerializerNamespaces();
386  Namespaces.Add(string.Empty, string.Empty);
387  XmlSerializer Serializer = new XmlSerializer(typeof(GlobalConfig));
388  MemoryStream Stream = new MemoryStream();
389  XmlWriterSettings Settings = new XmlWriterSettings();
390  Settings.Indent = true;
391  Settings.NewLineOnAttributes = true;
392 
393  XmlWriter Writer = XmlWriter.Create(Stream, Settings);
394  Writer.WriteStartDocument();
395  Writer.WriteComment("Global configuration for the DirectOutput framework.");
396  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()));
397  Serializer.Serialize(Writer, this, Namespaces);
398  Writer.WriteEndDocument();
399  Writer.Flush();
400  Stream.Position = 0;
401  string XML;
402  using (StreamReader sr = new StreamReader(Stream, Encoding.Default))
403  {
404  XML = sr.ReadToEnd();
405  sr.Dispose();
406  }
407  Stream.Dispose();
408 
409  return XML;
410  }
411 
412 
413 
420  public static GlobalConfig GetGlobalConfigFromConfigXmlFile(string GlobalConfigFileName)
421  {
422 
423  try
424  {
425  if (File.Exists(GlobalConfigFileName))
426  {
427 
428 
429  string Xml = General.FileReader.ReadFileToString(GlobalConfigFileName);
430 
431  GlobalConfig GC = GetGlobalConfigFromGlobalConfigXml(Xml);
432  if (GC != null)
433  {
434  GC.GlobalConfigFilename = GlobalConfigFileName;
435  }
436  return GC;
437 
438  }
439  else
440  {
441 
442  return null;
443  }
444  }
445  catch
446  {
447 
448  return null;
449  }
450  }
451 
452 
458  public static GlobalConfig GetGlobalConfigFromGlobalConfigXml(string ConfigXml)
459  {
460  try
461  {
462 
463  byte[] xmlBytes = Encoding.Default.GetBytes(ConfigXml);
464  using (MemoryStream ms = new MemoryStream(xmlBytes))
465  {
466  return (GlobalConfig)new XmlSerializer(typeof(GlobalConfig)).Deserialize(ms);
467  }
468  }
469  catch { return null; }
470 
471  }
472 
478  public void SaveGlobalConfig(string GlobalConfigFilename = "")
479  {
480  string GCFileName = (GlobalConfigFilename.IsNullOrWhiteSpace() ? this.GlobalConfigFilename : GlobalConfigFilename);
481  if (GCFileName.IsNullOrWhiteSpace())
482  {
483  ArgumentException Ex = new ArgumentException("No filename for GlobalConfig file has been supplied. Looking up the filename from the property GlobalConfigFilename failed as well");
484  throw Ex;
485  }
486  if (File.Exists(GCFileName))
487  {
488  //Create a backup of the current global config file
489  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))));
490  };
491  DirectoryInfo GCDirectory = new FileInfo(GCFileName).Directory;
492 
493  GCDirectory.CreateDirectoryPath();
494  GetGlobalConfigXml().WriteToFile(GCFileName, false);
495 
496  }
497  #endregion
498 
499 
503  public GlobalConfig()
504  {
505 
506 
507 
508  }
509 
510  }
511 }