From 75d89a2cded1180dfdc49d2dff7aaa4f43e9dc1c Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 19 Apr 2016 00:07:40 -0700 Subject: [PATCH] Move sorting to DLL --- DATabase/Generate.cs | 91 +++------------------------ SabreHelper/SabreHelper.csproj | 1 + SabreHelper/Sort.cs | 111 +++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 81 deletions(-) create mode 100644 SabreHelper/Sort.cs diff --git a/DATabase/Generate.cs b/DATabase/Generate.cs index d33d5018..244dff23 100644 --- a/DATabase/Generate.cs +++ b/DATabase/Generate.cs @@ -2,10 +2,7 @@ using System.Collections.Generic; using System.Data.SQLite; using System.IO; -using System.Linq; -using System.Text; using System.Text.RegularExpressions; -using System.Web; using SabreTools.Helper; @@ -25,7 +22,6 @@ namespace SabreTools private bool _old; // Private required variables - private Dictionary _headers; private Logger _logger; /// @@ -48,26 +44,13 @@ namespace SabreTools _logger = logger; // Take care of special outfolder cases - _outdir = (outdir == "" ? outdir : - (outdir.Contains("/") && !outdir.EndsWith("/") ? outdir + "/" : - (outdir.Contains("\\") && !outdir.EndsWith("\\") ? outdir + "\\" : - (!outdir.Contains("/") && !outdir.Contains("\\") ? outdir + "\\" : outdir) - ) - ) + _outdir = (outdir == "" ? Environment.CurrentDirectory + Path.DirectorySeparatorChar : + (!outdir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? outdir + Path.DirectorySeparatorChar : outdir) ); if (_outdir != "" && !Directory.Exists(_outdir)) { Directory.CreateDirectory(_outdir); } - - _headers = new Dictionary(); - _headers.Add(25, "a7800.xml"); - _headers.Add(228, "fds.xml"); - _headers.Add(31, "lynx.xml"); - _headers.Add(0, "mega.xml"); // Merged version of all other headers - _headers.Add(234, "n64.xml"); - _headers.Add(238, "nes.xml"); - _headers.Add(241, "snes.xml"); // Self-created to deal with various headers } /// @@ -255,52 +238,12 @@ JOIN checksums SHA1 = sldr.GetString(12), }; - if (merged) + // Rename the game associated if it's still valid and we allow renames + if (merged && !_norename) { - // If it's the first rom in the list, don't touch it - if (roms.Count != 0) - { - // Check if the rom is a duplicate - RomData last = roms[roms.Count - 1]; - - bool shouldcont = false; - if (temp.Type == "rom" && last.Type == "rom") - { - shouldcont = ((temp.Size != -1 && temp.Size == last.Size) && ( - (temp.CRC != "" && last.CRC != "" && temp.CRC == last.CRC) || - (temp.MD5 != "" && last.MD5 != "" && temp.MD5 == last.MD5) || - (temp.SHA1 != "" && last.SHA1 != "" && temp.SHA1 == last.SHA1) - ) - ); - } - else if (temp.Type == "disk" && last.Type == "disk") - { - shouldcont = ((temp.MD5 != "" && last.MD5 != "" && temp.MD5 == last.MD5) || - (temp.SHA1 != "" && last.SHA1 != "" && temp.SHA1 == last.SHA1) - ); - } - - // If it's a duplicate, skip adding it to the output but add any missing information - if (shouldcont) - { - last.CRC = (last.CRC == "" && temp.CRC != "" ? temp.CRC : last.CRC); - last.MD5 = (last.MD5 == "" && temp.MD5 != "" ? temp.MD5 : last.MD5); - last.SHA1 = (last.SHA1 == "" && temp.SHA1 != "" ? temp.SHA1 : last.SHA1); - - roms.RemoveAt(roms.Count - 1); - roms.Insert(roms.Count, last); - - continue; - } - } - - // Rename the game associated if it's still valid and we allow renames - if (!_norename) - { - temp.Game = temp.Game + - (sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") + - (srcmerged ? " [" + temp.Source + "]" : ""); - } + temp.Game = temp.Game + + (sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") + + (srcmerged ? " [" + temp.Source + "]" : ""); } roms.Add(temp); @@ -309,25 +252,11 @@ JOIN checksums } } - // If we're in a merged mode, resort by the correct parameters + // If we're in a merged mode, merge and then resort by the correct parameters if (merged) { - roms.Sort(delegate (RomData x, RomData y) - { - if (x.SystemID == y.SystemID) - { - if (x.SourceID == y.SourceID) - { - if (x.Game == y.Game) - { - return String.Compare(x.Name, y.Name); - } - return String.Compare(x.Game, y.Game); - } - return (_norename ? String.Compare(x.Game, y.Game) : x.SourceID - y.SourceID); - } - return (_norename ? String.Compare(x.Game, y.Game) : x.SystemID - y.SystemID); - }); + roms = Sort.Merge(roms, true); + Sort.RomSort(roms, _norename); } // Now check rename within games diff --git a/SabreHelper/SabreHelper.csproj b/SabreHelper/SabreHelper.csproj index c4dd755b..635c79cb 100644 --- a/SabreHelper/SabreHelper.csproj +++ b/SabreHelper/SabreHelper.csproj @@ -76,6 +76,7 @@ + diff --git a/SabreHelper/Sort.cs b/SabreHelper/Sort.cs new file mode 100644 index 00000000..6c6cbe94 --- /dev/null +++ b/SabreHelper/Sort.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; + +namespace SabreTools.Helper +{ + public class Sort + { + public static List Merge(List inroms, bool presorted = false) + { + List outroms = new List(); + + // First sort the roms by size, crc, sysid, srcid, md5, and sha1 (in order), if not sorted already + if (!presorted) + { + inroms.Sort(delegate (RomData x, RomData y) + { + if (x.Size == y.Size) + { + if (x.CRC == y.CRC) + { + if (x.SystemID == y.SystemID) + { + if (x.SourceID == y.SourceID) + { + if (x.MD5 == y.MD5) + { + return String.Compare(x.SHA1, y.SHA1); + } + return String.Compare(x.MD5, y.MD5); + } + return x.SourceID - y.SourceID; + } + return x.SystemID - y.SystemID; + } + return String.Compare(x.CRC, y.CRC); + } + return (int)(x.Size - y.Size); + }); + } + + // Then, deduplicate them by checking to see if data matches + foreach (RomData rom in inroms) + { + // If it's the first rom in the list, don't touch it + if (outroms.Count != 0) + { + // Check if the rom is a duplicate + RomData last = outroms[outroms.Count - 1]; + + bool shouldcont = false; + if (rom.Type == "rom" && last.Type == "rom") + { + shouldcont = ((rom.Size != -1 && rom.Size == last.Size) && ( + (rom.CRC != "" && last.CRC != "" && rom.CRC == last.CRC) || + (rom.MD5 != "" && last.MD5 != "" && rom.MD5 == last.MD5) || + (rom.SHA1 != "" && last.SHA1 != "" && rom.SHA1 == last.SHA1) + ) + ); + } + else if (rom.Type == "disk" && last.Type == "disk") + { + shouldcont = ((rom.MD5 != "" && last.MD5 != "" && rom.MD5 == last.MD5) || + (rom.SHA1 != "" && last.SHA1 != "" && rom.SHA1 == last.SHA1) + ); + } + + // If it's a duplicate, skip adding it to the output but add any missing information + if (shouldcont) + { + last.CRC = (last.CRC == "" && rom.CRC != "" ? rom.CRC : last.CRC); + last.MD5 = (last.MD5 == "" && rom.MD5 != "" ? rom.MD5 : last.MD5); + last.SHA1 = (last.SHA1 == "" && rom.SHA1 != "" ? rom.SHA1 : last.SHA1); + + outroms.RemoveAt(inroms.Count - 1); + outroms.Insert(inroms.Count, last); + + continue; + } + } + } + + // Then return the result + return outroms; + } + + /// + /// Sort a list of RomData objects by SystemID, SourceID, Game, and Name (in order) + /// + /// List of RomData objects representing the roms to be sorted + /// True if files are not renamed, false otherwise + public static void RomSort(List roms, bool norename) + { + roms.Sort(delegate (RomData x, RomData y) + { + if (x.SystemID == y.SystemID) + { + if (x.SourceID == y.SourceID) + { + if (x.Game == y.Game) + { + return String.Compare(x.Name, y.Name); + } + return String.Compare(x.Game, y.Game); + } + return (norename ? String.Compare(x.Game, y.Game) : x.SourceID - y.SourceID); + } + return (norename ? String.Compare(x.Game, y.Game) : x.SystemID - y.SystemID); + }); + } + } +}