2 using System.Collections.Generic;
4 using System.Reflection;
7 using System.Xml.Serialization;
10 namespace DirectOutput.GlobalConfiguration
22 private int _LedControlMinimumEffectDurationMs = 60;
32 public int LedControlMinimumEffectDurationMs
34 get {
return _LedControlMinimumEffectDurationMs; }
35 set { _LedControlMinimumEffectDurationMs = value; }
38 private int _LedControlMinimumRGBEffectDurationMs = 120;
48 public int LedControlMinimumRGBEffectDurationMs
50 get {
return _LedControlMinimumRGBEffectDurationMs; }
51 set { _LedControlMinimumRGBEffectDurationMs = value; }
65 get {
return _LedControlIniFiles; }
66 set { _LedControlIniFiles = value; }
74 #region Cabinet config file
86 get {
return _CabinetConfigFilePatterns; }
87 set { _CabinetConfigFilePatterns = value; }
95 public FileInfo GetCabinetConfigFile()
97 if (CabinetConfigFilePatterns != null)
109 public DirectoryInfo GetCabinetConfigDirectory()
111 FileInfo CC = GetCabinetConfigFile();
128 #region Table script files
139 get {
return _TableScriptFilePatterns; }
140 set { _TableScriptFilePatterns = value; }
150 public List<FileInfo> GetTableScriptFiles(
string FullTableFilename)
152 if (TableScriptFilePatterns != null)
154 return TableScriptFilePatterns.
GetMatchingFiles(GetReplaceValuesDictionary(FullTableFilename));
156 return new List<FileInfo>();
172 get {
return _TableConfigFilePatterns; }
173 set { _TableConfigFilePatterns = value; }
183 public FileInfo GetTableConfigFile(
string FullTableFilename)
185 return TableConfigFilePatterns.
GetFirstMatchingFile(GetReplaceValuesDictionary(FullTableFilename));
196 private bool _EnableLog =
false;
205 public bool EnableLogging
207 get {
return _EnableLog; }
208 set { _EnableLog = value; }
233 get {
return _LogFilePattern; }
234 set { _LogFilePattern = value; }
243 public string GetLogFilename(
string TableFilename =
"",
string RomName =
"")
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"));
250 return LogFilePattern.ReplacePlaceholders(R);
257 private Dictionary<string, string> GetReplaceValuesDictionary(
string TableFileName = null,
string RomName =
"")
259 Dictionary<string, string> D =
new Dictionary<string, string>();
260 if (GetGlobalConfigFile() != null)
262 D.Add(
"GlobalConfigDirectory", GetGlobalConfigDirectory().FullName);
263 D.Add(
"GlobalConfigDir", GetGlobalConfigDirectory().FullName);
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())
273 FI =
new FileInfo(TableFileName);
274 if (FI.Directory.Exists)
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);
282 D.Add(
"TableName", Path.GetFileNameWithoutExtension(FI.FullName));
284 if (!RomName.IsNullOrWhiteSpace())
286 D.Add(
"RomName", RomName);
296 #region Global config properties
303 public List<FileInfo> GetGlobalScriptFiles()
305 return GlobalScriptFilePatterns.GetMatchingFiles();
319 get {
return _GlobalScriptFilePatterns; }
320 set { _GlobalScriptFilePatterns = value; }
327 public string GlobalConfigDirectoryName()
329 DirectoryInfo DI = GetGlobalConfigDirectory();
330 if (DI == null)
return null;
340 public DirectoryInfo GetGlobalConfigDirectory()
342 FileInfo FI = GetGlobalConfigFile();
343 if (FI == null)
return null;
349 private string _GlobalConfigFilename =
"";
358 public string GlobalConfigFilename
360 get {
return _GlobalConfigFilename; }
361 set { _GlobalConfigFilename = value; }
369 public FileInfo GetGlobalConfigFile()
371 if (GlobalConfigFilename.IsNullOrWhiteSpace()) {
return null; }
372 return new FileInfo(GlobalConfigFilename);
377 #region Serialization
382 public string GetGlobalConfigXml()
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;
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();
402 using (StreamReader sr =
new StreamReader(Stream, Encoding.Default))
404 XML = sr.ReadToEnd();
420 public static GlobalConfig GetGlobalConfigFromConfigXmlFile(
string GlobalConfigFileName)
425 if (File.Exists(GlobalConfigFileName))
429 string Xml = General.FileReader.ReadFileToString(GlobalConfigFileName);
431 GlobalConfig GC = GetGlobalConfigFromGlobalConfigXml(Xml);
458 public static GlobalConfig GetGlobalConfigFromGlobalConfigXml(
string ConfigXml)
463 byte[] xmlBytes = Encoding.Default.GetBytes(ConfigXml);
464 using (MemoryStream ms =
new MemoryStream(xmlBytes))
469 catch {
return null; }
478 public void SaveGlobalConfig(
string GlobalConfigFilename =
"")
480 string GCFileName = (GlobalConfigFilename.IsNullOrWhiteSpace() ? this.GlobalConfigFilename : GlobalConfigFilename);
481 if (GCFileName.IsNullOrWhiteSpace())
483 ArgumentException Ex =
new ArgumentException(
"No filename for GlobalConfig file has been supplied. Looking up the filename from the property GlobalConfigFilename failed as well");
486 if (File.Exists(GCFileName))
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))));
491 DirectoryInfo GCDirectory =
new FileInfo(GCFileName).Directory;
493 GCDirectory.CreateDirectoryPath();
494 GetGlobalConfigXml().WriteToFile(GCFileName,
false);