2 using System.Collections.Generic;
4 using System.Reflection;
7 using System.Xml.Serialization;
8 using DirectOutput.General;
11 namespace DirectOutput.GlobalConfiguration
25 private int _LedControlMinimumEffectDurationMs = 60;
35 public int LedControlMinimumEffectDurationMs
37 get {
return _LedControlMinimumEffectDurationMs; }
38 set { _LedControlMinimumEffectDurationMs = value; }
41 private int _LedControlMinimumRGBEffectDurationMs = 120;
51 public int LedControlMinimumRGBEffectDurationMs
53 get {
return _LedControlMinimumRGBEffectDurationMs; }
54 set { _LedControlMinimumRGBEffectDurationMs = value; }
59 private string _IniFilesPath=
"";
67 public string IniFilesPath
69 get {
return _IniFilesPath; }
70 set { _IniFilesPath = value; }
79 public Dictionary<int, FileInfo> GetIniFilesDictionary(
string TableFilename =
"")
83 List<string> LookupPaths =
new List<string>();
85 if (!IniFilesPath.IsNullOrWhiteSpace())
89 DirectoryInfo DI =
new DirectoryInfo(IniFilesPath);
92 LookupPaths.Add(DI.FullName);
94 }
catch (Exception E) {
95 Log.
Exception(
"The specified IniFilesPath {0} could not be used due to a exception.".Build(IniFilesPath),E);
100 if (!TableFilename.IsNullOrWhiteSpace())
104 if (
new FileInfo(TableFilename).Directory.Exists)
106 LookupPaths.Add(
new FileInfo(TableFilename).Directory.FullName);
113 LookupPaths.AddRange(
new string[] { GetGlobalConfigDirectory().FullName, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) });
117 Dictionary<int, FileInfo> IniFiles =
new Dictionary<int, FileInfo>();
119 bool FoundIt =
false;
120 string[] LedControlFilenames = {
"directoutputconfig",
"ledcontrol" };
122 foreach (
string LedControlFilename
in LedControlFilenames)
124 foreach (
string P
in LookupPaths)
126 DirectoryInfo DI =
new DirectoryInfo(P);
128 List<FileInfo> Files =
new List<FileInfo>();
129 foreach (FileInfo FI
in DI.EnumerateFiles())
131 if (FI.Name.ToLower().StartsWith(LedControlFilename.ToLower()) && FI.Name.ToLower().EndsWith(
".ini"))
138 foreach (FileInfo FI
in Files)
140 if (
string.Equals(FI.Name,
"{0}.ini".Build(LedControlFilename), StringComparison.OrdinalIgnoreCase))
142 if (!IniFiles.ContainsKey(1))
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.");
154 string F = FI.Name.Substring(LedControlFilename.Length, FI.Name.Length - LedControlFilename.Length - 4);
158 if (
int.TryParse(F, out LedWizNr))
160 if (!IniFiles.ContainsKey(LedWizNr))
162 IniFiles.Add(LedWizNr, FI);
167 Log.
Warning(
"Found more than one ini file with number {0}.".Build(LedWizNr));
195 #region Cabinet config file
208 get {
return _CabinetConfigFilePattern; }
209 set { _CabinetConfigFilePattern = value; }
219 public FileInfo GetCabinetConfigFile()
221 if (!CabinetConfigFilePattern.Pattern.IsNullOrWhiteSpace() && CabinetConfigFilePattern.IsValid)
234 public DirectoryInfo GetCabinetConfigDirectory()
236 FileInfo CC = GetCabinetConfigFile();
267 get {
return _TableConfigFilePatterns; }
268 set { _TableConfigFilePatterns = value; }
278 public FileInfo GetTableConfigFile(
string FullTableFilename)
280 return TableConfigFilePatterns.
GetFirstMatchingFile(GetReplaceValuesDictionary(FullTableFilename));
291 private bool _EnableLog =
false;
300 public bool EnableLogging
302 get {
return _EnableLog; }
303 set { _EnableLog = value; }
306 private bool _ClearLogOnSessionStart=
false;
314 public bool ClearLogOnSessionStart
316 get {
return _ClearLogOnSessionStart; }
317 set { _ClearLogOnSessionStart = value; }
345 get {
return _LogFilePattern; }
346 set { _LogFilePattern = value; }
355 public string GetLogFilename(
string TableFilename =
"",
string RomName =
"")
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"));
362 return LogFilePattern.ReplacePlaceholders(R);
369 private Dictionary<string, string> GetReplaceValuesDictionary(
string TableFileName = null,
string RomName =
"")
371 Dictionary<string, string> D =
new Dictionary<string, string>();
372 if (GetGlobalConfigFile() != null)
374 D.Add(
"GlobalConfigDirectory", GetGlobalConfigDirectory().FullName);
375 D.Add(
"GlobalConfigDir", GetGlobalConfigDirectory().FullName);
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())
385 FI =
new FileInfo(TableFileName);
386 if (FI.Directory.Exists)
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);
394 D.Add(
"TableName", Path.GetFileNameWithoutExtension(FI.FullName));
396 if (!RomName.IsNullOrWhiteSpace())
398 D.Add(
"RomName", RomName);
408 #region Global config properties
415 public string GlobalConfigDirectoryName()
417 DirectoryInfo DI = GetGlobalConfigDirectory();
418 if (DI == null)
return null;
428 public DirectoryInfo GetGlobalConfigDirectory()
430 FileInfo FI = GetGlobalConfigFile();
431 if (FI == null)
return null;
437 private string _GlobalConfigFilename =
"";
446 public string GlobalConfigFilename
448 get {
return _GlobalConfigFilename; }
449 set { _GlobalConfigFilename = value; }
457 public FileInfo GetGlobalConfigFile()
459 if (GlobalConfigFilename.IsNullOrWhiteSpace()) {
return null; }
460 return new FileInfo(GlobalConfigFilename);
465 #region Serialization
470 public string GetGlobalConfigXml()
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;
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();
490 using (StreamReader sr =
new StreamReader(Stream, Encoding.Default))
492 XML = sr.ReadToEnd();
508 public static GlobalConfig GetGlobalConfigFromConfigXmlFile(
string GlobalConfigFileName)
513 if (File.Exists(GlobalConfigFileName))
517 string Xml = General.FileReader.ReadFileToString(GlobalConfigFileName);
519 GlobalConfig GC = GetGlobalConfigFromGlobalConfigXml(Xml);
546 public static GlobalConfig GetGlobalConfigFromGlobalConfigXml(
string ConfigXml)
551 byte[] xmlBytes = Encoding.Default.GetBytes(ConfigXml);
552 using (MemoryStream ms =
new MemoryStream(xmlBytes))
557 catch {
return null; }
566 public void SaveGlobalConfig(
string GlobalConfigFilename =
"")
568 string GCFileName = (GlobalConfigFilename.IsNullOrWhiteSpace() ? this.GlobalConfigFilename : GlobalConfigFilename);
569 if (GCFileName.IsNullOrWhiteSpace())
571 ArgumentException Ex =
new ArgumentException(
"No filename for GlobalConfig file has been supplied. Looking up the filename from the property GlobalConfigFilename failed as well");
574 if (File.Exists(GCFileName))
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))));
579 DirectoryInfo GCDirectory =
new FileInfo(GCFileName).Directory;
581 GCDirectory.CreateDirectoryPath();
582 GetGlobalConfigXml().WriteToFile(GCFileName,
false);