diff --git a/SabreHelper/Converters.cs b/SabreHelper/Converters.cs index ee107654..7cc79bb5 100644 --- a/SabreHelper/Converters.cs +++ b/SabreHelper/Converters.cs @@ -16,9 +16,9 @@ namespace SabreTools.Helper public class Converters { // Regex matching patterns - private static string _headerPattern = @"(^.*?) \($"; - private static string _itemPattern = @"^\s*(\S*?) (.*)"; - private static string _endPattern = @"^\s*\)\s*$"; + private static string _headerPatternCMP = @"(^.*?) \($"; + private static string _itemPatternCMP = @"^\s*(\S*?) (.*)"; + private static string _endPatternCMP = @"^\s*\)\s*$"; /// /// Convert a ClrMamePro style DAT to an XML derived DAT @@ -41,9 +41,9 @@ namespace SabreTools.Helper } // If the line is the header or a game - if (Regex.IsMatch(line, _headerPattern)) + if (Regex.IsMatch(line, _headerPatternCMP)) { - GroupCollection gc = Regex.Match(line, _headerPattern).Groups; + GroupCollection gc = Regex.Match(line, _headerPatternCMP).Groups; if (gc[1].Value == "clrmamepro" || gc[1].Value == "romvault") { @@ -129,9 +129,9 @@ namespace SabreTools.Helper 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) + else if (Regex.IsMatch(line, _itemPatternCMP) && block) { - GroupCollection gc = Regex.Match(line, _itemPattern).Groups; + GroupCollection gc = Regex.Match(line, _itemPatternCMP).Groups; if (gc[1].Value == "name" && elem.Name != "header") { @@ -145,7 +145,7 @@ namespace SabreTools.Helper } // If we find an end bracket that's not associated with anything else, the block is done - else if (Regex.IsMatch(line, _endPattern) && block) + else if (Regex.IsMatch(line, _endPatternCMP) && block) { block = false; elem = elem.Parent; @@ -155,6 +155,162 @@ namespace SabreTools.Helper return elem; } + /// + /// Convert a RomCenter style DAT to an XML derived DAT + /// + /// Array of strings representing the input file + /// XElement representing the output XML DAT file + public static XElement RomCenterToXML(string[] filecontents) + { + XElement elem = new XElement("datafile"); + + string blocktype = ""; + string lastgame = null; + for (int k = 0; k < filecontents.Length; k++) + { + string line = filecontents[k]; + + // If the line is the start of the credits section + if (line.ToLowerInvariant().Contains("[credits]")) + { + blocktype = "credits"; + if (elem.Name != "header") + { + elem.Add(new XElement("header")); + elem = elem.Elements("header").Last(); + } + } + // If the line is the start of the dat section + else if (line.ToLowerInvariant().Contains("[dat]")) + { + blocktype = "dat"; + if (elem.Name != "header") + { + elem.Add(new XElement("header")); + elem = elem.Elements("header").Last(); + } + } + // If the line is the start of the emulator section + else if (line.ToLowerInvariant().Contains("[emulator]")) + { + blocktype = "emulator"; + if (elem.Name != "header") + { + elem.Add(new XElement("header")); + elem = elem.Elements("header").Last(); + } + } + // If the line is the start of the game section + else if (line.ToLowerInvariant().Contains("[games]")) + { + blocktype = "games"; + } + // Otherwise, it's not a section and it's data, so get out all data + else + { + // If we have an author + if (line.StartsWith("author=")) + { + elem.Add(new XElement("author", line.Split('=')[1])); + } + // If we have one of the three version tags + else if (line.StartsWith("version=")) + { + switch (blocktype) + { + case "credits": + elem.Add(new XElement("version", line.Split('=')[1])); + break; + case "emulator": + elem.Add(new XElement("description", line.Split('=')[1])); + break; + } + } + // If we have a comment + else if (line.StartsWith("comment=")) + { + elem.Add(new XElement("comment", line.Split('=')[1])); + } + // If we have the split flag + else if (line.StartsWith("split=")) + { + int split = 0; + if (Int32.TryParse(line.Split('=')[1], out split)) + { + if (split == 1) + { + XElement cmp = new XElement("clrmamepro"); + cmp.Add(new XAttribute("forcemerging", "split")); + elem.Add(cmp); + } + } + } + // If we have the merge tag + else if (line.StartsWith("merge=")) + { + int merge = 0; + if (Int32.TryParse(line.Split('=')[1], out merge)) + { + if (merge == 1) + { + XElement cmp = new XElement("clrmamepro"); + cmp.Add(new XAttribute("forcemerging", "full")); + elem.Add(cmp); + } + } + } + // If we have the refname tag + else if (line.StartsWith("refname=")) + { + elem.Add(new XElement("name", line.Split('=')[1])); + } + // If we have a rom + else if (line.StartsWith("¬")) + { + /* + The rominfo order is as follows: + 0 - parent name + 1 - parent description + 2 - game name + 3 - game description + 4 - rom name + 5 - rom crc + 6 - rom size + 7 - romof name + 8 - merge name + */ + string[] rominfo = line.Split('¬'); + RomData rom = new RomData + { + Game = rominfo[2], + Name = rominfo[4], + CRC = rominfo[5], + Size = Int64.Parse(rominfo[6]), + }; + + if (lastgame != rom.Game) + { + elem = elem.Parent; + XElement current = new XElement("machine"); + current.Add(new XAttribute("name", rom.Game)); + current.Add(new XElement("description", rom.Game)); + elem.Add(current); + } + elem = elem.Elements("machine").Last(); + + XElement romelem = new XElement("rom"); + romelem.Add(new XAttribute("name", rom.Name)); + romelem.Add(new XAttribute("size", rom.Size)); + romelem.Add(new XAttribute("crc", rom.CRC)); + + elem.Add(romelem); + } + } + } + + return elem; + } + /// /// Convert an XML derived DAT to a ClrMamePro style DAT ///