2016-04-19 16:39:17 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2016-04-22 13:51:37 -07:00
|
|
|
|
using System.IO;
|
2016-04-19 16:39:17 -07:00
|
|
|
|
|
|
|
|
|
|
using SabreTools.Helper;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SabreTools
|
|
|
|
|
|
{
|
2016-04-21 14:10:59 -07:00
|
|
|
|
public class MergeDiff
|
2016-04-19 16:39:17 -07:00
|
|
|
|
{
|
2016-04-20 21:17:23 -07:00
|
|
|
|
// Listing related variables
|
|
|
|
|
|
private List<String> _inputs;
|
|
|
|
|
|
|
|
|
|
|
|
// User specified flags
|
2016-04-20 20:17:57 -07:00
|
|
|
|
private bool _diff;
|
|
|
|
|
|
private bool _dedup;
|
2016-04-21 14:38:11 -07:00
|
|
|
|
private bool _bare;
|
2016-04-20 21:17:23 -07:00
|
|
|
|
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");
|
2016-04-20 20:17:57 -07:00
|
|
|
|
private Logger _logger;
|
|
|
|
|
|
|
2016-04-20 21:59:55 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new MergeDAT object
|
|
|
|
|
|
/// </summary>
|
2016-04-20 20:40:43 -07:00
|
|
|
|
/// <param name="inputs">A List of Strings representing the DATs or DAT folders to be merged</param>
|
2016-04-20 21:59:55 -07:00
|
|
|
|
/// <param name="name">Internal name of the DAT</param>
|
|
|
|
|
|
/// <param name="desc">Description and external name of the DAT</param>
|
|
|
|
|
|
/// <param name="cat">Category for the DAT</param>
|
|
|
|
|
|
/// <param name="version">Version of the DAT</param>
|
|
|
|
|
|
/// <param name="author">Author of the DAT</param>
|
2016-04-22 13:51:37 -07:00
|
|
|
|
/// <param name="ad">True if all diff variants should be outputted, false otherwise</param>
|
2016-04-20 20:40:43 -07:00
|
|
|
|
/// <param name="diff">True if a DiffDat of all inputs is wanted, false otherwise</param>
|
|
|
|
|
|
/// <param name="dedup">True if the outputted file should remove duplicates, false otherwise</param>
|
2016-04-21 14:38:11 -07:00
|
|
|
|
/// <param name="bare">True if the date should be omitted from the DAT, false otherwise</param>
|
2016-04-20 21:59:55 -07:00
|
|
|
|
/// <param name="forceunpack">True if the forcepacking="unzip" tag is to be added, false otherwise</param>
|
|
|
|
|
|
/// <param name="old">True if a old-style DAT should be output, false otherwise</param>
|
2016-04-20 20:40:43 -07:00
|
|
|
|
/// <param name="logger">Logger object for console and file output</param>
|
2016-04-21 14:10:59 -07:00
|
|
|
|
public MergeDiff(List<String> inputs, string name, string desc, string cat, string version, string author,
|
2016-05-11 09:16:46 -07:00
|
|
|
|
bool diff, bool dedup, bool bare, bool forceunpack, bool old, Logger logger)
|
2016-04-20 20:17:57 -07:00
|
|
|
|
{
|
|
|
|
|
|
_inputs = inputs;
|
2016-04-20 21:17:23 -07:00
|
|
|
|
_name = name;
|
|
|
|
|
|
_desc = desc;
|
|
|
|
|
|
_cat = cat;
|
|
|
|
|
|
_version = version;
|
|
|
|
|
|
_author = author;
|
2016-04-20 20:17:57 -07:00
|
|
|
|
_diff = diff;
|
|
|
|
|
|
_dedup = dedup;
|
2016-04-21 14:38:11 -07:00
|
|
|
|
_bare = bare;
|
2016-04-20 21:17:23 -07:00
|
|
|
|
_forceunpack = forceunpack;
|
|
|
|
|
|
_old = old;
|
2016-04-20 20:17:57 -07:00
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
2016-04-19 16:39:17 -07:00
|
|
|
|
|
2016-04-20 20:17:57 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Combine DATs, optionally diffing and deduping them
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if the DATs merged correctly, false otherwise</returns>
|
2016-04-21 14:10:59 -07:00
|
|
|
|
public bool Process()
|
2016-04-20 20:17:57 -07:00
|
|
|
|
{
|
2016-04-21 13:32:35 -07:00
|
|
|
|
// Check if there are enough inputs
|
2016-05-11 09:16:46 -07:00
|
|
|
|
if (_inputs.Count < 1)
|
2016-04-20 20:17:57 -07:00
|
|
|
|
{
|
2016-05-11 09:16:46 -07:00
|
|
|
|
_logger.Warning("At least 1 input is required!");
|
2016-04-20 20:17:57 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-22 15:09:07 -07:00
|
|
|
|
// Get the values that will be used
|
|
|
|
|
|
if (_name == "")
|
|
|
|
|
|
{
|
2016-04-27 20:44:17 -07:00
|
|
|
|
_name = (_diff ? "diffdat" : "mergedat") + (_dedup ? "-deduped" : "");
|
2016-04-22 15:09:07 -07:00
|
|
|
|
}
|
|
|
|
|
|
if (_desc == "")
|
|
|
|
|
|
{
|
2016-04-27 20:44:17 -07:00
|
|
|
|
_desc = (_diff ? "diffdat" : "mergedat") + (_dedup ? "-deduped" : "");
|
2016-04-22 15:09:07 -07:00
|
|
|
|
if (!_bare)
|
|
|
|
|
|
{
|
|
|
|
|
|
_desc += " (" + _date + ")";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (_cat == "" && _diff)
|
|
|
|
|
|
{
|
|
|
|
|
|
_cat = "DiffDAT";
|
|
|
|
|
|
}
|
|
|
|
|
|
if (_author == "")
|
|
|
|
|
|
{
|
|
|
|
|
|
_author = "SabreTools";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-28 10:57:32 -07:00
|
|
|
|
// Create a dictionary of all ROMs from the input DATs
|
2016-05-10 20:55:51 -07:00
|
|
|
|
int i = 0;
|
2016-04-28 11:06:27 -07:00
|
|
|
|
Dictionary<string, List<RomData>> dict = new Dictionary<string, List<RomData>>();
|
|
|
|
|
|
foreach (string input in _inputs)
|
|
|
|
|
|
{
|
2016-05-10 15:41:33 -07:00
|
|
|
|
_logger.User("Adding DAT: " + input);
|
2016-05-10 20:55:51 -07:00
|
|
|
|
dict = RomManipulation.ParseDict(input, i, 0, dict, _logger);
|
|
|
|
|
|
i++;
|
2016-04-28 11:06:27 -07:00
|
|
|
|
}
|
2016-04-28 10:57:32 -07:00
|
|
|
|
|
2016-04-28 11:06:27 -07:00
|
|
|
|
// Modify the Dictionary if necessary and output the results
|
2016-05-11 09:16:46 -07:00
|
|
|
|
if (_diff)
|
2016-04-28 11:06:27 -07:00
|
|
|
|
{
|
2016-05-10 20:55:51 -07:00
|
|
|
|
// Get all entries that don't have External dupes
|
2016-04-28 11:31:35 -07:00
|
|
|
|
Dictionary<string, List<RomData>> diffed = new Dictionary<string, List<RomData>>();
|
|
|
|
|
|
foreach (string key in dict.Keys)
|
|
|
|
|
|
{
|
2016-05-10 20:55:51 -07:00
|
|
|
|
List<RomData> temp = dict[key];
|
|
|
|
|
|
temp = RomManipulation.Merge(temp);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (RomData rom in temp)
|
2016-04-28 11:31:35 -07:00
|
|
|
|
{
|
2016-05-11 09:16:46 -07:00
|
|
|
|
if ((_dedup && rom.Dupe != DupeType.ExternalHash) || (!_dedup && rom.Dupe != DupeType.ExternalAll))
|
2016-05-10 20:55:51 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (diffed.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
diffed[key].Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
List<RomData> tl = new List<RomData>();
|
|
|
|
|
|
tl.Add(rom);
|
|
|
|
|
|
diffed.Add(key, tl);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-04-28 11:31:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-11 09:16:46 -07:00
|
|
|
|
// Output the difflist (a-b)+(b-a) diff
|
|
|
|
|
|
Output.WriteToDatFromDict(_name, _desc, _version, _date, _cat, _author, _forceunpack, _old, _dedup, "", diffed, _logger);
|
2016-04-28 11:31:35 -07:00
|
|
|
|
|
2016-05-11 09:16:46 -07:00
|
|
|
|
// 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"
|
|
|
|
|
|
string post = "";
|
2016-05-11 09:33:52 -07:00
|
|
|
|
for (int j = 0; j < _inputs.Count; j++)
|
2016-04-28 11:31:35 -07:00
|
|
|
|
{
|
2016-05-11 09:16:46 -07:00
|
|
|
|
Dictionary<string, List<RomData>> sysDict = new Dictionary<string, List<RomData>>();
|
|
|
|
|
|
foreach (string key in diffed.Keys)
|
2016-04-28 11:31:35 -07:00
|
|
|
|
{
|
2016-05-11 09:16:46 -07:00
|
|
|
|
foreach (RomData rom in diffed[key])
|
2016-04-28 11:31:35 -07:00
|
|
|
|
{
|
2016-05-11 09:16:46 -07:00
|
|
|
|
if (rom.SystemID == j)
|
2016-04-28 11:31:35 -07:00
|
|
|
|
{
|
2016-05-11 09:16:46 -07:00
|
|
|
|
if (sysDict.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
sysDict[key].Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
List<RomData> tl = new List<RomData>();
|
|
|
|
|
|
tl.Add(rom);
|
|
|
|
|
|
sysDict.Add(key, tl);
|
|
|
|
|
|
}
|
2016-04-28 11:31:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-11 09:33:52 -07:00
|
|
|
|
post = " (" + Path.GetFileNameWithoutExtension(_inputs[j]) + ")";
|
2016-05-11 09:16:46 -07:00
|
|
|
|
Output.WriteToDatFromDict(_name + post, _desc + post, _version, _date, _cat, _author, _forceunpack, _old, _dedup, "", sysDict, _logger);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get all entries that have External dupes
|
|
|
|
|
|
Dictionary<string, List<RomData>> duplicates = new Dictionary<string, List<RomData>>();
|
|
|
|
|
|
post = " (dupes)";
|
|
|
|
|
|
foreach (string key in dict.Keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<RomData> temp = dict[key];
|
|
|
|
|
|
temp = RomManipulation.Merge(temp);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (RomData rom in temp)
|
2016-04-28 11:31:35 -07:00
|
|
|
|
{
|
2016-05-11 09:16:46 -07:00
|
|
|
|
if ((_dedup && rom.Dupe == DupeType.ExternalHash) || (!_dedup && rom.Dupe == DupeType.ExternalAll))
|
2016-04-28 11:31:35 -07:00
|
|
|
|
{
|
2016-05-11 09:16:46 -07:00
|
|
|
|
if (duplicates.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
duplicates[key].Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
List<RomData> tl = new List<RomData>();
|
|
|
|
|
|
tl.Add(rom);
|
|
|
|
|
|
duplicates.Add(key, tl);
|
|
|
|
|
|
}
|
2016-04-28 11:31:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-05-11 09:16:46 -07:00
|
|
|
|
Output.WriteToDatFromDict(_name + post, _desc + post, _version, _date, _cat, _author, _forceunpack, _old, _dedup, "", duplicates, _logger);
|
2016-04-28 11:06:27 -07:00
|
|
|
|
}
|
2016-04-28 11:31:35 -07:00
|
|
|
|
// Output all entries with user-defined merge
|
2016-04-28 11:06:27 -07:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-04-28 11:31:35 -07:00
|
|
|
|
Output.WriteToDatFromDict(_name, _desc, _version, _date, _cat, _author, _forceunpack, _old, _dedup, "", dict, _logger);
|
2016-04-28 11:06:27 -07:00
|
|
|
|
}
|
2016-04-28 10:57:32 -07:00
|
|
|
|
|
2016-04-20 20:17:57 -07:00
|
|
|
|
return true;
|
2016-04-19 16:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|