using System; using System.Collections.Generic; using System.IO; using SabreTools.Helper; namespace SabreTools { public class MergeDiff { // Listing related variables private List _inputs; // User specified flags private bool _diff; private bool _dedup; private bool _bare; private bool _forceunpack; private bool _old; // User specified strings private string _name; private string _desc; private string _cat; private string _version; private string _author; // Other required variables private string _date = DateTime.Now.ToString("yyyy-MM-dd"); private Logger _logger; /// /// Create a new MergeDAT object /// /// A List of Strings representing the DATs or DAT folders to be merged /// Internal name of the DAT /// Description and external name of the DAT /// Category for the DAT /// Version of the DAT /// Author of the DAT /// True if all diff variants should be outputted, false otherwise /// True if a DiffDat of all inputs is wanted, false otherwise /// True if the outputted file should remove duplicates, false otherwise /// True if the date should be omitted from the DAT, false otherwise /// True if the forcepacking="unzip" tag is to be added, false otherwise /// True if a old-style DAT should be output, false otherwise /// Logger object for console and file output public MergeDiff(List inputs, string name, string desc, string cat, string version, string author, bool diff, bool dedup, bool bare, bool forceunpack, bool old, Logger logger) { _inputs = inputs; _name = name; _desc = desc; _cat = cat; _version = version; _author = author; _diff = diff; _dedup = dedup; _bare = bare; _forceunpack = forceunpack; _old = old; _logger = logger; } /// /// Combine DATs, optionally diffing and deduping them /// /// True if the DATs merged correctly, false otherwise public bool Process() { // Check if there are enough inputs if (_inputs.Count < 1) { _logger.Warning("At least 1 input is required!"); return false; } // Get the values that will be used if (_name == "") { _name = (_diff ? "DiffDAT" : "") + (_dedup ? "-deduped" : ""); } if (_desc == "") { _desc = (_diff ? "DiffDAT" : "MergeDAT") + (_dedup ? " - deduped" : ""); if (!_bare) { _desc += " (" + _date + ")"; } } if (_cat == "" && _diff) { _cat = "DiffDAT"; } if (_author == "") { _author = "SabreTools"; } // Create a dictionary of all ROMs from the input DATs int i = 0; Dictionary> dict = new Dictionary>(); foreach (string input in _inputs) { _logger.User("Adding DAT: " + input); dict = RomManipulation.ParseDict(input, i, 0, dict, _logger); i++; } // Modify the Dictionary if necessary and output the results if (_diff) { 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, Description = _desc + post, Version = _version, Date = _date, Category = _cat, Author = _author, ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None), OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), }; Output.WriteToDatFromDict(outerDiffData, _dedup, "", diffed, _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, Description = _desc + post, Version = _version, Date = _date, Category = _cat, Author = _author, ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None), OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), }; 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) { if (rom.Dupe >= DupeType.ExternalHash) { if (duplicates.ContainsKey(key)) { duplicates[key].Add(rom); } else { List tl = new List(); tl.Add(rom); duplicates.Add(key, tl); } } } } DatData dupeData = new DatData { Name = _name + post, Description = _desc + post, Version = _version, Date = _date, Category = _cat, Author = _author, ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None), OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), }; Output.WriteToDatFromDict(dupeData, _dedup, "", duplicates, _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); } return true; } } }