From fc67d62fe587143ea02f127ba3d9768cc753e71e Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 16 May 2016 20:51:05 -0700 Subject: [PATCH] Remove most of the outdated references and methods --- DATabase/ExtSplit.cs | 123 +++++++++++++++--- DATabase/TrimMerge.cs | 62 +++++---- SabreHelper/RomManipulation.cs | 231 +++++---------------------------- 3 files changed, 179 insertions(+), 237 deletions(-) diff --git a/DATabase/ExtSplit.cs b/DATabase/ExtSplit.cs index fab034bd..e0360542 100644 --- a/DATabase/ExtSplit.cs +++ b/DATabase/ExtSplit.cs @@ -56,41 +56,124 @@ namespace SabreTools Directory.CreateDirectory(_outdir); } - List romsA = new List(); - List romsB = new List(); + // Create the initial DatData object + DatData datdata = new DatData + { + Name = "", + Description = "", + Category = "", + Version = "", + Date = "", + Author = "", + Email = "", + Homepage = "", + Url = "", + Comment = "", + Roms = new Dictionary>(), + }; // Load the current DAT to be processed - List roms = RomManipulation.Parse(_filename, 0, 0, _logger); + datdata = RomManipulation.ParseDict(_filename, 0, 0, datdata, _logger); + + // Set all of the appropriate outputs for each of the subsets + OutputFormat outputFormat = RomManipulation.GetOutputFormat(_filename); + DatData datdataA = new DatData + { + Name = datdata.Name + "." + _extA, + Description = datdata.Description + "." + _extA, + Category = datdata.Category, + Version = datdata.Version, + Date = datdata.Date, + Author = datdata.Author, + Email = datdata.Email, + Homepage = datdata.Homepage, + Url = datdata.Url, + Comment = datdata.Comment, + Roms = new Dictionary>(), + OutputFormat = outputFormat, + }; + DatData datdataB = new DatData + { + Name = datdata.Name + "." + _extB, + Description = datdata.Description + "." + _extB, + Category = datdata.Category, + Version = datdata.Version, + Date = datdata.Date, + Author = datdata.Author, + Email = datdata.Email, + Homepage = datdata.Homepage, + Url = datdata.Url, + Comment = datdata.Comment, + Roms = new Dictionary>(), + OutputFormat = outputFormat, + }; // If roms is empty, return false - if (roms.Count == 0) + if (datdata.Roms.Count == 0) { return false; } // Now separate the roms accordingly - foreach (RomData rom in roms) + foreach (string key in datdata.Roms.Keys) { - if (rom.Name.ToUpperInvariant().EndsWith(_extA)) + foreach (RomData rom in datdata.Roms[key]) { - romsA.Add(rom); - } - else if (rom.Name.ToUpperInvariant().EndsWith(_extB)) - { - romsB.Add(rom); - } - else - { - romsA.Add(rom); - romsB.Add(rom); + if (rom.Name.ToUpperInvariant().EndsWith(_extA)) + { + if (datdataA.Roms.ContainsKey(key)) + { + datdataA.Roms[key].Add(rom); + } + else + { + List temp = new List(); + temp.Add(rom); + datdataA.Roms.Add(key, temp); + } + } + else if (rom.Name.ToUpperInvariant().EndsWith(_extB)) + { + if (datdataB.Roms.ContainsKey(key)) + { + datdataB.Roms[key].Add(rom); + } + else + { + List temp = new List(); + temp.Add(rom); + datdataB.Roms.Add(key, temp); + } + } + else + { + if (datdataA.Roms.ContainsKey(key)) + { + datdataA.Roms[key].Add(rom); + } + else + { + List temp = new List(); + temp.Add(rom); + datdataA.Roms.Add(key, temp); + } + if (datdataB.Roms.ContainsKey(key)) + { + datdataB.Roms[key].Add(rom); + } + else + { + List temp = new List(); + temp.Add(rom); + datdataB.Roms.Add(key, temp); + } + } } } // Then write out both files - bool success = Output.WriteToDat(Path.GetFileNameWithoutExtension(_filename) + "." + _extA, Path.GetFileNameWithoutExtension(_filename) + "." + _extA, - "", "", "", "", false, !RomManipulation.IsXmlDat(_filename), _outdir, romsA, _logger); - success &= Output.WriteToDat(Path.GetFileNameWithoutExtension(_filename) + "." + _extB, Path.GetFileNameWithoutExtension(_filename) + "." + _extB, - "", "", "", "", false, !RomManipulation.IsXmlDat(_filename), _outdir, romsB, _logger); + bool success = Output.WriteToDatFromDict(datdataA, _outdir, _logger); + success &= Output.WriteToDatFromDict(datdataB, _outdir, _logger); return success; } diff --git a/DATabase/TrimMerge.cs b/DATabase/TrimMerge.cs index 1c858974..1b0a7ecf 100644 --- a/DATabase/TrimMerge.cs +++ b/DATabase/TrimMerge.cs @@ -82,40 +82,58 @@ namespace SabreTools /// True if roms are to be renamed private void ProcessDAT(string filename, string path, bool rename) { - List roms = RomManipulation.Parse(filename, 0, 0, _logger); + DatData datdata = new DatData + { + Name = "", + Description = "", + Category = "", + Version = "", + Date = "", + Author = "", + Email = "", + Homepage = "", + Url = "", + Comment = "", + Roms = new Dictionary>(), + ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None), + OutputFormat = RomManipulation.GetOutputFormat(filename), + }; + datdata = RomManipulation.ParseDict(filename, 0, 0, datdata, _logger); // Trim all file names according to the path that's set - List outroms = new List(); - while (roms.Count != 0) + foreach (string key in datdata.Roms.Keys) { - RomData rom = roms[0]; - roms.RemoveAt(0); + List outroms = new List(); - // If we are in single game mode, rename all games - if (rename) + foreach (RomData actrom in datdata.Roms[key]) { - rom.Game = "!"; - } + RomData rom = actrom; - // Windows max name length is 260 - int usableLength = 260 - rom.Game.Length - _path.Length; - if (rom.Name.Length > usableLength) - { - string ext = Path.GetExtension(rom.Name); - rom.Name = rom.Name.Substring(0, usableLength - ext.Length); - rom.Name += ext; - } + // If we are in single game mode, rename all games + if (rename) + { + rom.Game = "!"; + } - outroms.Add(rom); + // Windows max name length is 260 + int usableLength = 260 - rom.Game.Length - _path.Length; + if (rom.Name.Length > usableLength) + { + string ext = Path.GetExtension(rom.Name); + rom.Name = rom.Name.Substring(0, usableLength - ext.Length); + rom.Name += ext; + } + + outroms.Add(rom); + } + datdata.Roms[key] = outroms; } // Now write the file out accordingly - string datName = RomManipulation.GetDatName(filename, _logger); - string datDescription = RomManipulation.GetDatDescription(filename, _logger); - Output.WriteToDat(datName, datDescription, "", "", "", "", _forceunpack, !RomManipulation.IsXmlDat(filename), Path.GetDirectoryName(filename), outroms, _logger); + Output.WriteToDatFromDict(datdata, Path.GetDirectoryName(filename), _logger); // Remove the original file if different and inform the user - if (filename != datDescription + (RomManipulation.IsXmlDat(filename) ? ".xml" : ".dat")) + if (filename != datdata.Description + (RomManipulation.GetOutputFormat(filename) == OutputFormat.Xml ? ".xml" : ".dat")) { try { diff --git a/SabreHelper/RomManipulation.cs b/SabreHelper/RomManipulation.cs index 9ed4b8b6..ba5bff52 100644 --- a/SabreHelper/RomManipulation.cs +++ b/SabreHelper/RomManipulation.cs @@ -17,96 +17,36 @@ namespace SabreTools.Helper public static string SHA1Zero = "da39a3ee5e6b4b0d3255bfef95601890afd80709"; /// - /// Return if the file is XML or not + /// Get what type of DAT the input file is /// /// Name of the file to be parsed - /// True if the DAT is probably XML, false otherwise - public static bool IsXmlDat(string filename) + /// The OutputFormat corresponding to the DAT + public static OutputFormat GetOutputFormat(string filename) { try { StreamReader sr = new StreamReader(File.OpenRead(filename)); string first = sr.ReadLine(); sr.Close(); - return first.Contains("<") && first.Contains(">"); + if (first.Contains("<") && first.Contains(">")) + { + return OutputFormat.Xml; + } + else if (first.Contains("[") && first.Contains("]")) + { + return OutputFormat.RomCenter; + } + else + { + return OutputFormat.ClrMamePro; + } } catch (Exception) { - return false; + return OutputFormat.ClrMamePro; } } - /// - /// Return if the file is RomCenter or not - /// - /// Name of the file to be parsed - /// True if the DAT is probably RomCenter, false otherwise - public static bool IsRCDat(string filename) - { - try - { - StreamReader sr = new StreamReader(File.OpenRead(filename)); - string first = sr.ReadLine(); - sr.Close(); - return first.Contains("[") && first.Contains("]"); - } - catch (Exception) - { - return false; - } - } - - /// - /// Get the XmlDocument associated with a file, if possible - /// - /// Name of the file to be parsed - /// Logger object for console and file output - /// The XmlDocument representing the (possibly converted) file, null otherwise - public static XmlDocument GetXmlDocument(string filename, Logger logger) - { - logger.Log("Attempting to read file: " + filename); - - // Check if file exists - if (!File.Exists(filename)) - { - logger.Warning("File '" + filename + "' could not read from!"); - return null; - } - - XmlDocument doc = new XmlDocument(); - try - { - doc.Load(filename); - } - catch (XmlException) - { - try - { - doc.LoadXml(Converters.ClrMameProToXML(File.ReadAllLines(filename)).ToString()); - } - catch (Exception ex) - { - logger.Error(ex.ToString()); - return null; - } - } - catch (IOException) - { - logger.Error("File '" + filename + "' could not be open or read"); - } - catch (OutOfMemoryException) - { - logger.Error("File '" + filename + "' is too large to be processed!"); - } - catch (Exception ex) - { - logger.Error(ex.ToString()); - return null; - } - - return doc; - } - /// /// Get the XmlTextReader associated with a file, if possible /// @@ -124,128 +64,29 @@ namespace SabreTools.Helper return null; } - if (IsXmlDat(filename)) + XmlTextReader xtr; + StringReader sr; + switch (GetOutputFormat(filename)) { - logger.Log("XML DAT detected"); - XmlTextReader xtr = new XmlTextReader(filename); - xtr.WhitespaceHandling = WhitespaceHandling.None; - xtr.DtdProcessing = DtdProcessing.Ignore; - return xtr; - } - else if (IsRCDat(filename)) - { - logger.Log("RomCenter DAT detected"); - StringReader sr = new StringReader(Converters.RomCenterToXML(File.ReadAllLines(filename)).ToString()); - XmlTextReader xtr = new XmlTextReader(sr); - xtr.WhitespaceHandling = WhitespaceHandling.None; - xtr.DtdProcessing = DtdProcessing.Ignore; - return xtr; - } - else - { - logger.Log("ClrMamePro DAT detected"); - StringReader sr = new StringReader(Converters.ClrMameProToXML(File.ReadAllLines(filename)).ToString()); - XmlTextReader xtr = new XmlTextReader(sr); - xtr.WhitespaceHandling = WhitespaceHandling.None; - xtr.DtdProcessing = DtdProcessing.Ignore; - return xtr; - } - } - - /// - /// Get the name of the DAT for external use - /// - /// Name of the file to be parsed - /// Logger object for console and file output - /// The internal name of the DAT on success, empty string otherwise - /// Needs to be upgraded to XmlTextReader - public static string GetDatName(string filename, Logger logger) - { - string name = ""; - XmlDocument doc = GetXmlDocument(filename, logger); - - // If the returned document is null, return the blank string - if (doc == null) - { - return name; + case OutputFormat.Xml: + logger.Log("XML DAT detected"); + xtr = new XmlTextReader(filename); + break; + case OutputFormat.RomCenter: + logger.Log("RomCenter DAT detected"); + sr = new StringReader(Converters.RomCenterToXML(File.ReadAllLines(filename)).ToString()); + xtr = new XmlTextReader(sr); + break; + default: + logger.Log("ClrMamePro DAT detected"); + sr = new StringReader(Converters.ClrMameProToXML(File.ReadAllLines(filename)).ToString()); + xtr = new XmlTextReader(sr); + break; } - // Experimental looping using only XML parsing - XmlNode node = doc.FirstChild; - if (node != null && node.Name == "xml") - { - // Skip over everything that's not an element - while (node.NodeType != XmlNodeType.Element) - { - node = node.NextSibling; - } - } - - // Once we find the main body, enter it - if (node != null && (node.Name == "datafile" || node.Name == "softwarelist")) - { - node = node.FirstChild; - } - - // Get the name from the header - if (node != null && node.Name == "header") - { - XmlNode temp = node.SelectSingleNode("name"); - if (temp != null) - { - name = temp.InnerText; - } - } - - return name; - } - - /// - /// Get the description of the DAT for external use - /// - /// Name of the file to be parsed - /// Logger object for console and file output - /// The internal name of the DAT on success, empty string otherwise - /// Needs to be upgraded to XmlTextReader - public static string GetDatDescription(string filename, Logger logger) - { - string desc = ""; - XmlDocument doc = GetXmlDocument(filename, logger); - - // If the returned document is null, return the blank string - if (doc == null) - { - return desc; - } - - // Experimental looping using only XML parsing - XmlNode node = doc.FirstChild; - if (node != null && node.Name == "xml") - { - // Skip over everything that's not an element - while (node.NodeType != XmlNodeType.Element) - { - node = node.NextSibling; - } - } - - // Once we find the main body, enter it - if (node != null && (node.Name == "datafile" || node.Name == "softwarelist")) - { - node = node.FirstChild; - } - - // Get the name from the header - if (node != null && node.Name == "header") - { - XmlNode temp = node.SelectSingleNode("description"); - if (temp != null) - { - desc = temp.InnerText; - } - } - - return desc; + xtr.WhitespaceHandling = WhitespaceHandling.None; + xtr.DtdProcessing = DtdProcessing.Ignore; + return xtr; } ///