diff --git a/DATabase/MergeDiff.cs b/DATabase/MergeDiff.cs index 6451b1f0..96f35bac 100644 --- a/DATabase/MergeDiff.cs +++ b/DATabase/MergeDiff.cs @@ -99,11 +99,23 @@ namespace SabreTools // Create a dictionary of all ROMs from the input DATs int i = 0; - Dictionary> dict = new Dictionary>(); + DatData userData = new DatData + { + Name = _name, + Description = _desc, + Version = _version, + Date = _date, + Category = _cat, + Author = _author, + ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None), + OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), + MergeRoms = _dedup, + Roms = new Dictionary>(), + }; foreach (string input in _inputs) { _logger.User("Adding DAT: " + input); - dict = RomManipulation.ParseDict(input, i, 0, dict, _logger); + userData = RomManipulation.ParseDict(input, i, 0, userData, _logger); i++; } @@ -113,33 +125,6 @@ namespace SabreTools string post = ""; // Get all entries that don't have External dupes - Dictionary> diffed = new Dictionary>(); - foreach (string key in dict.Keys) - { - List temp = dict[key]; - temp = RomManipulation.Merge(temp); - - foreach (RomData rom in temp) - { - if (rom.Dupe < DupeType.ExternalHash) - { - if (diffed.ContainsKey(key)) - { - diffed[key].Add(rom); - } - else - { - List tl = new List(); - tl.Add(rom); - diffed.Add(key, tl); - } - } - } - } - - post = " (No Duplicates)"; - - // Output the difflist (a-b)+(b-a) diff DatData outerDiffData = new DatData { Name = _name + post, @@ -150,36 +135,41 @@ namespace SabreTools Author = _author, ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None), OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), + MergeRoms = _dedup, + Roms = new Dictionary>(), }; - Output.WriteToDatFromDict(outerDiffData, _dedup, "", diffed, _logger); + foreach (string key in userData.Roms.Keys) + { + List temp = userData.Roms[key]; + temp = RomManipulation.Merge(temp); + + foreach (RomData rom in temp) + { + if (rom.Dupe < DupeType.ExternalHash) + { + if (outerDiffData.Roms.ContainsKey(key)) + { + outerDiffData.Roms[key].Add(rom); + } + else + { + List tl = new List(); + tl.Add(rom); + outerDiffData.Roms.Add(key, tl); + } + } + } + } + + post = " (No Duplicates)"; + + // Output the difflist (a-b)+(b-a) diff + Output.WriteToDatFromDict(outerDiffData, "", _logger); // For the AB mode-style diffs, get all required dictionaries and output with a new name // Loop through _inputs first and filter from all diffed roms to find the ones that have the same "System" for (int j = 0; j < _inputs.Count; j++) { - Dictionary> sysDict = new Dictionary>(); - foreach (string key in diffed.Keys) - { - foreach (RomData rom in diffed[key]) - { - if (rom.SystemID == j) - { - if (sysDict.ContainsKey(key)) - { - sysDict[key].Add(rom); - } - else - { - List tl = new List(); - tl.Add(rom); - sysDict.Add(key, tl); - } - } - } - } - - post = " (" + Path.GetFileNameWithoutExtension(_inputs[j]) + " Only)"; - DatData diffData = new DatData { Name = _name + post, @@ -190,36 +180,35 @@ namespace SabreTools Author = _author, ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None), OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), + MergeRoms = _dedup, + Roms = new Dictionary>(), }; - Output.WriteToDatFromDict(diffData, _dedup, "", sysDict, _logger); - } - // Get all entries that have External dupes - Dictionary> duplicates = new Dictionary>(); - post = " (Duplicates)"; - foreach (string key in dict.Keys) - { - List temp = dict[key]; - temp = RomManipulation.Merge(temp); - - foreach (RomData rom in temp) + foreach (string key in outerDiffData.Roms.Keys) { - if (rom.Dupe >= DupeType.ExternalHash) + foreach (RomData rom in outerDiffData.Roms[key]) { - if (duplicates.ContainsKey(key)) + if (rom.SystemID == j) { - duplicates[key].Add(rom); - } - else - { - List tl = new List(); - tl.Add(rom); - duplicates.Add(key, tl); + if (diffData.Roms.ContainsKey(key)) + { + diffData.Roms[key].Add(rom); + } + else + { + List tl = new List(); + tl.Add(rom); + diffData.Roms.Add(key, tl); + } } } } + + post = " (" + Path.GetFileNameWithoutExtension(_inputs[j]) + " Only)"; + Output.WriteToDatFromDict(diffData, "", _logger); } + // Get all entries that have External dupes DatData dupeData = new DatData { Name = _name + post, @@ -230,24 +219,39 @@ namespace SabreTools Author = _author, ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None), OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), + MergeRoms = _dedup, + Roms = new Dictionary>(), }; - Output.WriteToDatFromDict(dupeData, _dedup, "", duplicates, _logger); + post = " (Duplicates)"; + foreach (string key in userData.Roms.Keys) + { + List temp = userData.Roms[key]; + temp = RomManipulation.Merge(temp); + + foreach (RomData rom in temp) + { + if (rom.Dupe >= DupeType.ExternalHash) + { + if (dupeData.Roms.ContainsKey(key)) + { + dupeData.Roms[key].Add(rom); + } + else + { + List tl = new List(); + tl.Add(rom); + dupeData.Roms.Add(key, tl); + } + } + } + } + + Output.WriteToDatFromDict(dupeData, "", _logger); } // Output all entries with user-defined merge else { - DatData userData = new DatData - { - Name = _name, - Description = _desc, - Version = _version, - Date = _date, - Category = _cat, - Author = _author, - ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None), - OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), - }; - Output.WriteToDatFromDict(userData, _dedup, "", dict, _logger); + Output.WriteToDatFromDict(userData, "", _logger); } return true; diff --git a/DATabaseTwo/Generate.cs b/DATabaseTwo/Generate.cs index 775d0c68..f8f2c95a 100644 --- a/DATabaseTwo/Generate.cs +++ b/DATabaseTwo/Generate.cs @@ -144,9 +144,23 @@ namespace SabreTools } } + // Create the output DatData object + DatData datdata = new DatData + { + Name = name, + Description = description, + Version = "", + Date = date, + Category = "SabreTools", + Author = "SabreTools", + ForcePacking = ForcePacking.None, + OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), + MergeRoms = true, + Roms = new Dictionary>(), + }; + // Now read in all of the files SHA1 sha1 = SHA1.Create(); - Dictionary> roms = new Dictionary>(); foreach (string file in Directory.GetFiles(path, "*", SearchOption.AllDirectories)) { string hash = ""; @@ -160,11 +174,11 @@ namespace SabreTools { Int32.TryParse(sourcemap[hash], out tempSrcId); } - roms = RomManipulation.ParseDict(file, 0, tempSrcId, roms, _logger); + datdata = RomManipulation.ParseDict(file, 0, tempSrcId, datdata, _logger); } // If the dictionary is empty for any reason, tell the user and exit - if (roms.Keys.Count == 0 || roms.Count == 0) + if (datdata.Roms.Keys.Count == 0 || datdata.Roms.Count == 0) { _logger.Log("No roms found for system ID " + _systemid); return false; @@ -172,11 +186,11 @@ namespace SabreTools // Now process all of the roms _logger.User("Cleaning rom data"); - List keys = roms.Keys.ToList(); + List keys = datdata.Roms.Keys.ToList(); foreach (string key in keys) { List temp = new List(); - List newroms = roms[key]; + List newroms = datdata.Roms[key]; for (int i = 0; i < newroms.Count; i++) { RomData rom = newroms[i]; @@ -221,22 +235,11 @@ namespace SabreTools temp.Add(rom); } - roms[key] = temp; + datdata.Roms[key] = temp; } // Then write out the file - DatData datdata = new DatData - { - Name = name, - Description = description, - Version = "", - Date = date, - Category = "SabreTools", - Author = "SabreTools", - ForcePacking = ForcePacking.None, - OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), - }; - Output.WriteToDatFromDict(datdata, true, _outroot, roms, _logger, _norename); + Output.WriteToDatFromDict(datdata, _outroot, _logger, _norename); return true; } diff --git a/OfflineMerge/OfflineMerge.cs b/OfflineMerge/OfflineMerge.cs index 7e049ab3..f1e24cec 100644 --- a/OfflineMerge/OfflineMerge.cs +++ b/OfflineMerge/OfflineMerge.cs @@ -392,6 +392,8 @@ namespace SabreTools Author = "SabreTools", ForcePacking = ForcePacking.None, OutputFormat = OutputFormat.Xml, + MergeRoms = true, + Roms = netNew, }; DatData unneededData = new DatData { @@ -403,6 +405,8 @@ namespace SabreTools Author = "SabreTools", ForcePacking = ForcePacking.None, OutputFormat = OutputFormat.Xml, + MergeRoms = true, + Roms = unneeded, }; DatData newMissingData = new DatData { @@ -414,6 +418,8 @@ namespace SabreTools Author = "SabreTools", ForcePacking = ForcePacking.None, OutputFormat = OutputFormat.Xml, + MergeRoms = true, + Roms = newMissing, }; DatData haveData = new DatData { @@ -425,12 +431,14 @@ namespace SabreTools Author = "SabreTools", ForcePacking = ForcePacking.None, OutputFormat = OutputFormat.Xml, + MergeRoms = true, + Roms = have, }; - Output.WriteToDatFromDict(netNewData, true, "", netNew, _logger); - Output.WriteToDatFromDict(unneededData, true, "", unneeded, _logger); - Output.WriteToDatFromDict(newMissingData, true, "", newMissing, _logger); - Output.WriteToDatFromDict(haveData, true, "", have, _logger); + Output.WriteToDatFromDict(netNewData, "", _logger); + Output.WriteToDatFromDict(unneededData, "", _logger); + Output.WriteToDatFromDict(newMissingData, "", _logger); + Output.WriteToDatFromDict(haveData, "", _logger); return true; } @@ -497,8 +505,10 @@ namespace SabreTools Author = "SabreTools", ForcePacking = ForcePacking.None, OutputFormat = OutputFormat.Xml, + MergeRoms = true, + Roms = have, }; - Output.WriteToDatFromDict(haveData, true, "", have, _logger); + Output.WriteToDatFromDict(haveData, "", _logger); return true; } @@ -565,8 +575,10 @@ namespace SabreTools Author = "SabreTools", ForcePacking = ForcePacking.None, OutputFormat = OutputFormat.Xml, + MergeRoms = true, + Roms = have, }; - Output.WriteToDatFromDict(haveData, true, "", have, _logger); + Output.WriteToDatFromDict(haveData, "", _logger); return true; } diff --git a/SabreHelper/Output.cs b/SabreHelper/Output.cs index f1d6727d..d6ad391d 100644 --- a/SabreHelper/Output.cs +++ b/SabreHelper/Output.cs @@ -142,9 +142,7 @@ namespace SabreTools.Helper /// Create and open an output file for writing direct from a dictionary /// /// All information for creating the datfile header - /// Enable output in merged mode (one game per hash) /// Set the output directory - /// Dictionary containing all the roms to be written /// Logger object for console and/or file output /// True if games should only be compared on game and file name (default), false if system and source are counted /// True if the DAT was written correctly, false otherwise @@ -153,15 +151,15 @@ namespace SabreTools.Helper /// - Have the ability to strip special (non-ASCII) characters from rom information /// - Add a flag for ignoring roms with blank sizes /// - public static bool WriteToDatFromDict(DatData datdata, bool merge, string outDir, Dictionary> dict, Logger logger, bool norename = true) + public static bool WriteToDatFromDict(DatData datdata, string outDir, Logger logger, bool norename = true) { // Get all values in the dictionary and write out SortedDictionary> sortable = new SortedDictionary>(); long count = 0; - foreach (List roms in dict.Values) + foreach (List roms in datdata.Roms.Values) { List newroms = roms; - if (merge) + if (datdata.MergeRoms) { newroms = RomManipulation.Merge(newroms); } diff --git a/SabreHelper/RomManipulation.cs b/SabreHelper/RomManipulation.cs index b7993efd..f3334a35 100644 --- a/SabreHelper/RomManipulation.cs +++ b/SabreHelper/RomManipulation.cs @@ -365,6 +365,22 @@ namespace SabreTools.Helper return roms; } + /// + /// Parse a DAT and return all found games and roms within + /// + /// Name of the file to be parsed + /// System ID for the DAT + /// Source ID for the DAT + /// The DatData object representing found roms to this point + /// Logger object for console and/or file output + /// DatData object representing the read-in data + public static DatData ParseDict(string filename, int sysid, int srcid, DatData datdata, Logger logger) + { + Dictionary> roms = ParseDict(filename, sysid, srcid, datdata.Roms, logger); + datdata.Roms = roms; + return datdata; + } + /// /// Parse a DAT and return all found games and roms within ///