From 936128a7f1079109c8e25bb9c493843d723f702c Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sat, 19 Mar 2016 02:16:26 -0700 Subject: [PATCH] Simplified importing DATs into the database by first converting all of them to XML and then having a unified parser. As a consequence of this, there is also the ability to directly convert one RV DAT to an equivalent XML DAT now. --- DATabase/Converters.cs | 90 ++++++++++++++ DATabase/DATabase.csproj | 1 + DATabase/Importer.cs | 249 +++++++++++---------------------------- DATabase/Program.cs | 37 +++++- 4 files changed, 195 insertions(+), 182 deletions(-) create mode 100644 DATabase/Converters.cs diff --git a/DATabase/Converters.cs b/DATabase/Converters.cs new file mode 100644 index 00000000..ac867fea --- /dev/null +++ b/DATabase/Converters.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using System.Xml.Linq; + +namespace DATabase +{ + class Converters + { + private static string _headerPattern = @"(^.*?) \($"; + private static string _romPattern = @"^\s+((?:rom)|(?:disk)) \( (name) ""(.*?)"" (?:(size) (.*?) )?(?:(crc) (.*?))?(?:(md5) (.*?) )?(?:(sha1) (.*?) )?\)"; + private static string _itemPattern = @"^\s+(.*?) ""(.*?)"""; + private static string _endPattern = @"^\s*\)\s*$"; + + public static XElement RomVaultToXML (string[] filecontents) + { + XElement elem = new XElement("datafile"); + + bool block = false; + for (int k = 0; k < filecontents.Length; k++) + { + string line = filecontents[k]; + + // If the line is the header or a game + if (Regex.IsMatch(line, _headerPattern)) + { + GroupCollection gc = Regex.Match(line, _headerPattern).Groups; + + if (gc[1].Value == "clrmamepro" || gc[1].Value == "romvault") + { + elem.Add(new XElement("header")); + elem = elem.Elements("header").Last(); + } + else + { + elem.Add(new XElement(gc[1].Value)); + elem = elem.Elements(gc[1].Value).Last(); + } + + block = true; + } + + // If the line is a rom or disk and we're in a block + else if (Regex.IsMatch(line, _romPattern) && block) + { + GroupCollection gc = Regex.Match(line, _romPattern).Groups; + + XElement temp = new XElement(gc[1].Value); + + // Loop over all attributes and add them if possible + for (int i = 1; i < gc.Count; i++) + { + if (i + 2 < gc.Count) + { + temp.SetAttributeValue(gc[i+1].Value, gc[i+2].Value); + i++; + } + } + + elem.Add(new XElement(temp)); + } + // If the line is anything but a rom or disk and we're in a block + else if (Regex.IsMatch(line, _itemPattern) && block) + { + GroupCollection gc = Regex.Match(line, _itemPattern).Groups; + + if (gc[1].Value == "name" && elem.Name != "header") + { + elem.SetAttributeValue(gc[1].Value, gc[2].Value); + elem.Add(new XElement("description", gc[2].Value)); + } + else + { + elem.Add(new XElement(gc[1].Value, gc[2].Value)); + } + } + + // If we find an end bracket that's not associated with anything else, the block is done + else if (Regex.IsMatch(line, _endPattern) && block) + { + block = false; + elem = elem.Parent; + } + } + + return elem; + } + } +} diff --git a/DATabase/DATabase.csproj b/DATabase/DATabase.csproj index 72accc2c..ab350f22 100644 --- a/DATabase/DATabase.csproj +++ b/DATabase/DATabase.csproj @@ -66,6 +66,7 @@ + diff --git a/DATabase/Importer.cs b/DATabase/Importer.cs index 35f170cf..0d620397 100644 --- a/DATabase/Importer.cs +++ b/DATabase/Importer.cs @@ -258,142 +258,81 @@ namespace DATabase FileStream fs = File.OpenRead(_filepath); StreamReader sr = new StreamReader(fs); - Console.WriteLine("got here"); - - // Set necessary dat values - string format = ""; - bool machinefound = false; - string machinename = ""; - string description = ""; - long gameid = 0; - bool comment = false; - - // Parse the file for its rom information - while (sr.Peek() > 0) + XmlDocument doc = new XmlDocument(); + try { - string line = sr.ReadLine(); + doc.LoadXml(sr.ReadToEnd()); + } + catch (XmlException ex) + { + doc.LoadXml(Converters.RomVaultToXML(File.ReadAllLines(_filepath)).ToString()); + } - // First each string has to be normalized - line = Style.NormalizeChars(line); + // Experimental looping using only XML parsing + XmlNode node = doc.FirstChild.FirstChild; + if (node.Name == "header") + { + node = node.NextSibling; + } - // If the input style hasn't been set, set it according to the header - if (format == "") + while (node != null) + { + if (node.Name == "machine" || node.Name == "game" || node.Name == "software") { - if (line.IndexOf("") != -1) - { - format = "logiqx"; - } - else if (line.IndexOf("clrmamepro (") != -1 || line.IndexOf("romvault (") != -1) + long gameid = AddGame(sysid, node.Attributes["name"].Value, srcid); + + // Get the roms from the machine + if (node.HasChildNodes) { - format = "romvault"; - } - } - else if (line.IndexOf("") != -1) - { - comment = false; - } - else if (line.IndexOf("