2 using System.Collections.Generic;
7 using System.Xml.Serialization;
27 private int _LedWizDefaultMinCommandIntervalMs = 1;
39 public int LedWizDefaultMinCommandIntervalMs
41 get {
return _LedWizDefaultMinCommandIntervalMs; }
42 set { _LedWizDefaultMinCommandIntervalMs = value.Limit(0, 1000); }
46 private int _LedControlMinimumEffectDurationMs = 60;
56 public int LedControlMinimumEffectDurationMs
58 get {
return _LedControlMinimumEffectDurationMs; }
59 set { _LedControlMinimumEffectDurationMs = value; }
62 private int _LedControlMinimumRGBEffectDurationMs = 120;
72 public int LedControlMinimumRGBEffectDurationMs
74 get {
return _LedControlMinimumRGBEffectDurationMs; }
75 set { _LedControlMinimumRGBEffectDurationMs = value; }
80 private string _IniFilesPath=
"";
88 public string IniFilesPath
90 get {
return _IniFilesPath; }
91 set { _IniFilesPath = value; }
104 List<string> LookupPaths =
new List<string>();
106 if (!IniFilesPath.IsNullOrWhiteSpace())
110 DirectoryInfo DI =
new DirectoryInfo(IniFilesPath);
113 LookupPaths.Add(DI.FullName);
115 }
catch (Exception E) {
116 Log.
Exception(
"The specified IniFilesPath {0} could not be used due to a exception.".Build(IniFilesPath),E);
121 if (!TableFilename.IsNullOrWhiteSpace())
125 if (
new FileInfo(TableFilename).Directory.Exists)
127 LookupPaths.Add(
new FileInfo(TableFilename).Directory.FullName);
134 LookupPaths.AddRange(
new string[] { GetGlobalConfigDirectory().FullName, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) });
138 Dictionary<int, FileInfo> IniFiles =
new Dictionary<int, FileInfo>();
140 bool FoundIt =
false;
141 string[] LedControlFilenames = {
"directoutputconfig",
"ledcontrol" };
143 foreach (
string LedControlFilename
in LedControlFilenames)
145 foreach (
string P
in LookupPaths)
147 DirectoryInfo DI =
new DirectoryInfo(P);
149 List<FileInfo> Files =
new List<FileInfo>();
150 foreach (FileInfo FI
in DI.EnumerateFiles())
152 if (FI.Name.ToLower().StartsWith(LedControlFilename.ToLower()) && FI.Name.ToLower().EndsWith(
".ini"))
159 foreach (FileInfo FI
in Files)
161 if (
string.Equals(FI.Name,
"{0}.ini".Build(LedControlFilename), StringComparison.OrdinalIgnoreCase))
163 if (!IniFiles.ContainsKey(1))
170 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.");
175 string F = FI.Name.Substring(LedControlFilename.Length, FI.Name.Length - LedControlFilename.Length - 4);
179 if (
int.TryParse(F, out LedWizNr))
181 if (!IniFiles.ContainsKey(LedWizNr))
183 IniFiles.Add(LedWizNr, FI);
188 Log.
Warning(
"Found more than one ini file with number {0}.".Build(LedWizNr));
215 Dictionary<int,FileInfo> IniFileDict = GetIniFilesDictionary(TableFilename);
217 if (IniFileDict != null && IniFileDict.Count > 0)
219 DirectoryInfo DI = IniFileDict.First().Value.Directory;
221 FileInfo FI = DI.GetFiles(
"tablemappings.*").FirstOrDefault();
238 private FilePattern _ShapeDefinitionFilePattern = null;
248 get {
return _ShapeDefinitionFilePattern; }
249 set { _ShapeDefinitionFilePattern = value; }
258 if (ShapeDefintionFilePattern!=null && !ShapeDefintionFilePattern.Pattern.IsNullOrWhiteSpace() && ShapeDefintionFilePattern.IsValid)
260 return ShapeDefintionFilePattern.GetFirstMatchingFile(GetReplaceValuesDictionary(TableFilename, RomName));
262 Dictionary<int, FileInfo> IniFilesDict = GetIniFilesDictionary(TableFilename);
263 if (IniFilesDict.Count > 0)
265 FileInfo FI=
new FileInfo(Path.Combine(IniFilesDict.Select(KV=>KV.Value).First().Directory.FullName,
"DirectOutputShapes.xml"));
286 #region Cabinet config file
299 get {
return _CabinetConfigFilePattern; }
300 set { _CabinetConfigFilePattern = value; }
312 if (!CabinetConfigFilePattern.Pattern.IsNullOrWhiteSpace() && CabinetConfigFilePattern.IsValid)
314 return CabinetConfigFilePattern.GetFirstMatchingFile(GetReplaceValuesDictionary());
327 FileInfo CC = GetCabinetConfigFile();
358 get {
return _TableConfigFilePatterns; }
359 set { _TableConfigFilePatterns = value; }
371 return TableConfigFilePatterns.GetFirstMatchingFile(GetReplaceValuesDictionary(FullTableFilename));
382 private bool _EnableLog =
true;
391 public bool EnableLogging
393 get {
return _EnableLog; }
394 set { _EnableLog = value; }
397 private bool _ClearLogOnSessionStart=
true;
405 public bool ClearLogOnSessionStart
407 get {
return _ClearLogOnSessionStart; }
408 set { _ClearLogOnSessionStart = value; }
436 get {
return _LogFilePattern; }
437 set { _LogFilePattern = value; }
448 Dictionary<string, string> R = GetReplaceValuesDictionary(TableFilename, RomName);
449 R.Add(
"DateTime", DateTime.Now.ToString(
"yyyyMMdd_hhmmss"));
450 R.Add(
"Date", DateTime.Now.ToString(
"yyyyMMdd"));
451 R.Add(
"Time", DateTime.Now.ToString(
"hhmmss"));
453 return LogFilePattern.ReplacePlaceholders(R);
460 internal Dictionary<string, string> GetReplaceValuesDictionary(
string TableFileName = null,
string RomName =
"")
462 Dictionary<string, string> D =
new Dictionary<string, string>();
463 if (GetGlobalConfigFile() != null)
465 D.Add(
"GlobalConfigDirectory", GetGlobalConfigDirectory().FullName);
466 D.Add(
"GlobalConfigDir", GetGlobalConfigDirectory().FullName);
469 FileInfo FI =
new FileInfo(Assembly.GetExecutingAssembly().Location);
470 D.Add(
"DllDirectory", FI.Directory.FullName);
471 D.Add(
"DllDir", FI.Directory.FullName);
472 D.Add(
"AssemblyDirectory", FI.Directory.FullName);
473 D.Add(
"AssemblyDir", FI.Directory.FullName);
474 if (!TableFileName.IsNullOrWhiteSpace())
476 FI =
new FileInfo(TableFileName);
477 if (FI.Directory.Exists)
479 D.Add(
"TableDirectory", FI.Directory.FullName);
480 D.Add(
"TableDir", FI.Directory.FullName);
481 D.Add(
"TableDirectoryName", FI.Directory.Name);
482 D.Add(
"TableDirName", FI.Directory.Name);
485 D.Add(
"TableName", Path.GetFileNameWithoutExtension(FI.FullName));
487 if (!RomName.IsNullOrWhiteSpace())
489 D.Add(
"RomName", RomName);
499 #region Global config properties
508 DirectoryInfo DI = GetGlobalConfigDirectory();
509 if (DI == null)
return null;
521 FileInfo FI = GetGlobalConfigFile();
522 if (FI == null)
return null;
528 private string _GlobalConfigFilename =
"";
537 public string GlobalConfigFilename
539 get {
return _GlobalConfigFilename; }
540 set { _GlobalConfigFilename = value; }
550 if (GlobalConfigFilename.IsNullOrWhiteSpace()) {
return null; }
551 return new FileInfo(GlobalConfigFilename);
556 #region Serialization
557 public string GetGlobalConfigXml()
564 XmlSerializerNamespaces Namespaces =
new XmlSerializerNamespaces();
565 Namespaces.Add(
string.Empty,
string.Empty);
566 XmlSerializer Serializer =
new XmlSerializer(typeof(
GlobalConfig));
567 MemoryStream Stream =
new MemoryStream();
568 XmlWriterSettings Settings =
new XmlWriterSettings();
569 Settings.Indent =
true;
570 Settings.NewLineOnAttributes =
true;
572 XmlWriter Writer = XmlWriter.Create(Stream, Settings);
573 Writer.WriteStartDocument();
574 Writer.WriteComment(
"Global configuration for the DirectOutput framework.");
575 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()));
576 Serializer.Serialize(Writer,
this, Namespaces);
577 Writer.WriteEndDocument();
581 using (StreamReader sr =
new StreamReader(Stream, Encoding.Default))
583 XML = sr.ReadToEnd();
604 if (File.Exists(GlobalConfigFileName))
608 string Xml = General.FileReader.ReadFileToString(GlobalConfigFileName);
610 GlobalConfig GC = GetGlobalConfigFromGlobalConfigXml(Xml);
642 byte[] xmlBytes = Encoding.Default.GetBytes(ConfigXml);
643 using (MemoryStream ms =
new MemoryStream(xmlBytes))
648 catch {
return null; }
659 string GCFileName = (GlobalConfigFilename.IsNullOrWhiteSpace() ? this.GlobalConfigFilename : GlobalConfigFilename);
660 if (GCFileName.IsNullOrWhiteSpace())
662 ArgumentException Ex =
new ArgumentException(
"No filename for GlobalConfig file has been supplied. Looking up the filename from the property GlobalConfigFilename failed as well");
665 if (File.Exists(GCFileName))
668 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))));
670 DirectoryInfo GCDirectory =
new FileInfo(GCFileName).Directory;
672 GCDirectory.CreateDirectoryPath();
673 GetGlobalConfigXml().WriteToFile(GCFileName,
false);
string GlobalConfigDirectoryName()
Path to the directory where the global config is stored (readonly).
GlobalConfig()
Initializes a new instance of the GlobalConfig class.
void SaveGlobalConfig(string GlobalConfigFilename="")
Saves the GlobalConfig to the file specified in GlobalConfigFilename. Before saving the current glob...
FileInfo GetCabinetConfigFile()
FileInfo object for the file containing the configuration of the cabinet (outputs, toys and so on).
string GlobalConfigFilename
Gets or sets the global config filename.
FileInfo GetShapeDefinitionFile(string TableFilename="", string RomName="")
Gets the FileInfo object for the xml file defining the shapes to be used by DOF.
static void Warning(string Message)
Writes a warning message to the log.
A simple logger used to record important events and exceptions.
FileInfo GetTableConfigFile(string FullTableFilename)
Gets a FileInfo object for the table config file. The file is lookued up using the list of the prope...
FileInfo GetGlobalConfigFile()
Gets a FileInfo object for the global config file.
DirectoryInfo GetCabinetConfigDirectory()
Gets the cabinet config directory.
FileInfo GetFirstMatchingFile(Dictionary< string, string > ReplaceValues=null)
Gets the first file matching the value of the Pattern property.
string GetLogFilename(string TableFilename="", string RomName="")
Gets the log filename based on the LogFilePattern with replaced placeholders.
static GlobalConfig GetGlobalConfigFromGlobalConfigXml(string ConfigXml)
Instanciates a GlobalConfig object from a global configuration in a XML string.
Dictionary< int, FileInfo > GetIniFilesDictionary(string TableFilename="")
Gets the a dictionary containing all ini files (file) and their number (key).
Global configuration for the DirectOutput framework.
DirectoryInfo GetGlobalConfigDirectory()
Gets a DirectoryInfo object for the global config directory.
The namespace DirectOutput.General contains classes for general use.
A file pattern class used to lookup files matching a specified pattern.
FileInfo GetTableMappingFile(string TableFilename="")
Gets a FileInfo object pointing to the table mapping file or null if no table mapping file exists...
static void Exception(string Message, Exception E=null)
Writes a exception message to the log.
static GlobalConfig GetGlobalConfigFromConfigXmlFile(string GlobalConfigFileName)
Instanciates a GlobalConfig object from a global configuration in a XML file. If the global config f...