// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Reflection; namespace Spludlow.Mame { public class MameConfiguration { public string DatabaseDirectory; public string ReportDirectory; public string BinariesDirectory; public string ImportDirectory; public string ExportDirectory; public string LinkDirectoryMachineRom; public string LinkDirectoryMachineDisk; public string LinkDirectorySoftwareRom; public string LinkDirectorySoftwareDisk; public string StoreDirectoryMachineRom; public string StoreDirectoryMachineDisk; public string StoreDirectorySoftwareRom; public string StoreDirectorySoftwareDisk; public MameConfiguration() { string directory = Environment.CurrentDirectory; DatabaseDirectory = directory + @"\Data"; ReportDirectory = directory + @"\Reports"; string configFilename = directory + @"\MHS.txt"; using (StreamReader reader = new StreamReader(configFilename)) { string line; while ((line = reader.ReadLine()) != null) { line = line.Trim(); if (line.Length == 0 || line.StartsWith("#") == true) continue; string[] parts = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 2) throw new ApplicationException("Bad config line: " + line); string name = parts[0]; string value = parts[1]; FieldInfo fieldInfo = this.GetType().GetField(name); if (fieldInfo == null) throw new ApplicationException("Bad config name: " + line); fieldInfo.SetValue(this, value); } } } public string[][] GetAllDirectories(string startWith) { List result = new List(); foreach (FieldInfo fieldInfo in this.GetType().GetFields()) { string name = fieldInfo.Name; if (name.StartsWith(startWith) == false) continue; object value = fieldInfo.GetValue(this); result.Add(new string[] { name, Convert.ToString(value) }); } return result.ToArray(); } public string Show() { StringBuilder text = new StringBuilder(); foreach (FieldInfo fieldInfo in this.GetType().GetFields()) { text.AppendLine(); text.Append(fieldInfo.Name); text.Append(":"); object value = fieldInfo.GetValue(this); if (value != null) text.Append(Convert.ToString(value)); text.AppendLine(); } text.AppendLine(); return text.ToString(); } public string Test() { StringBuilder text = new StringBuilder(); try { string testName = "MHS-Test-" + Spludlow.Text.TimeStamp(); text.AppendLine("Store Directories exist"); foreach (string startsWith in new string[] { "StoreDirectory", "LinkDirectory", "ImportDirectory", "ExportDirectory", "DatabaseDirectory", "ReportDirectory", "BinariesDirectory" }) { foreach (string[] dirParts in GetAllDirectories(startsWith)) { string name = dirParts[0]; string dir = dirParts[1]; try { bool performWriteTest = true; if (startsWith == "BinariesDirectory") { if (File.Exists(dir + @"\chdman.exe") == false) throw new ApplicationException("chdman.exe not in BinariesDirectory"); performWriteTest = false; } //if (startsWith == "ImportDirectory") // Clears file attributes // performWriteTest = false; if (performWriteTest == true) { if (Directory.Exists(dir) == false) Directory.CreateDirectory(dir); string filename = dir + @"\" + testName; string linkFilename = filename + ".link"; try { File.WriteAllText(filename, testName); if (File.ReadAllText(filename) != testName) throw new ApplicationException("directory file read"); if (startsWith == "LinkDirectory") { Spludlow.Io.FsLinks.LinkFile(linkFilename, filename); if (File.Exists(linkFilename) == false) throw new ApplicationException("directory link create"); if (File.ReadAllText(linkFilename) != testName) throw new ApplicationException("directory link read"); } } finally { if (File.Exists(linkFilename) == true) File.Delete(linkFilename); if (File.Exists(filename) == true) File.Delete(filename); } } text.AppendLine("Directory OK: " + name + ": " + dir); } catch (Exception ee) { throw new ApplicationException("Directory error: " + name + ", " + dir + ", " + ee.Message); } } } text.AppendLine("TEST PASS !!!"); } catch (Exception ee) { text.AppendLine("FAIL: " + ee.Message); } return text.ToString(); } public string MameRomPath() { StringBuilder text = new StringBuilder(); text.AppendLine("MAME rompath should be:"); text.AppendLine(); text.Append(LinkDirectoryMachineRom); text.Append(";"); text.Append(LinkDirectoryMachineDisk); text.Append(";"); text.Append(LinkDirectorySoftwareRom); text.Append(";"); text.Append(LinkDirectorySoftwareDisk); text.AppendLine(); text.AppendLine(); return text.ToString(); } } }