2016-04-19 16:39:17 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2016-04-27 00:53:15 -07:00
|
|
|
|
using Mono.Data.Sqlite;
|
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-22 13:51:37 -07:00
|
|
|
|
private bool _ad;
|
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-04-22 13:51:37 -07:00
|
|
|
|
bool ad, 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-22 13:51:37 -07:00
|
|
|
|
_ad = ad;
|
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>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// TODO: @tractivo -for the A and B and AB output you could let this be determined by comparing the hashes.
|
|
|
|
|
|
/// when a hash is present in both dats then this entry goes to AB, if its only in A then it stay in A if in B then in B.
|
|
|
|
|
|
/// </remarks>
|
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-04-26 13:17:24 -07:00
|
|
|
|
if ((!_ad && _inputs.Count < 1) || (_ad && _inputs.Count < 2))
|
2016-04-20 20:17:57 -07:00
|
|
|
|
{
|
2016-04-26 13:17:24 -07:00
|
|
|
|
_logger.Warning("At least " + (_ad ? "2" : "1") + " input(s) are 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 == "")
|
|
|
|
|
|
{
|
|
|
|
|
|
_name = (_diff ? "diffdat" : "mergedat") + (_dedup ? "-merged" : "");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (_desc == "")
|
|
|
|
|
|
{
|
|
|
|
|
|
_desc = (_diff ? "diffdat" : "mergedat") + (_dedup ? "-merged" : "");
|
|
|
|
|
|
if (!_bare)
|
|
|
|
|
|
{
|
|
|
|
|
|
_desc += " (" + _date + ")";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (_cat == "" && _diff)
|
|
|
|
|
|
{
|
|
|
|
|
|
_cat = "DiffDAT";
|
|
|
|
|
|
}
|
|
|
|
|
|
if (_author == "")
|
|
|
|
|
|
{
|
|
|
|
|
|
_author = "SabreTools";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-19 16:39:17 -07:00
|
|
|
|
List<RomData> A = new List<RomData>();
|
2016-04-22 13:51:37 -07:00
|
|
|
|
|
2016-04-27 00:53:15 -07:00
|
|
|
|
SqliteConnection dbc = DBTools.InMemoryDb();
|
|
|
|
|
|
|
2016-04-20 20:17:57 -07:00
|
|
|
|
foreach (string input in _inputs)
|
2016-04-19 16:39:17 -07:00
|
|
|
|
{
|
2016-04-20 20:17:57 -07:00
|
|
|
|
_logger.Log("Adding DAT: " + input);
|
2016-04-27 00:53:15 -07:00
|
|
|
|
RomManipulation.Parse2(input, 0, 0, _dedup, dbc, _logger);
|
|
|
|
|
|
|
2016-04-27 16:11:29 -07:00
|
|
|
|
/*
|
2016-04-20 20:17:57 -07:00
|
|
|
|
List<RomData> B = RomManipulation.Parse(input, 0, 0, _logger);
|
|
|
|
|
|
if (_diff)
|
2016-04-19 17:31:03 -07:00
|
|
|
|
{
|
|
|
|
|
|
A = RomManipulation.Diff(A, B);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
A.AddRange(B);
|
|
|
|
|
|
}
|
2016-04-27 16:11:29 -07:00
|
|
|
|
*/
|
2016-04-19 16:39:17 -07:00
|
|
|
|
}
|
2016-04-19 16:58:47 -07:00
|
|
|
|
|
2016-04-27 00:53:15 -07:00
|
|
|
|
// Until I find a way to output the roms from the db, here's just a count of the items in it
|
|
|
|
|
|
using (SqliteCommand slc = new SqliteCommand("SELECT count(*) FROM roms", dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Log("Total number of lines in database: " + slc.ExecuteScalar());
|
|
|
|
|
|
}
|
2016-04-27 16:11:29 -07:00
|
|
|
|
Output.WriteToDat2(_name + "-db", _desc + "-db", _version, _date, _cat, _author, _forceunpack, _old, _diff, "", dbc, _logger);
|
|
|
|
|
|
|
2016-04-27 00:53:15 -07:00
|
|
|
|
dbc.Close();
|
|
|
|
|
|
|
2016-04-22 15:09:07 -07:00
|
|
|
|
// If we're in Alldiff mode, we can only use the first 2 inputs
|
|
|
|
|
|
if (_ad)
|
2016-04-19 16:58:47 -07:00
|
|
|
|
{
|
2016-04-22 15:09:07 -07:00
|
|
|
|
List<RomData> AB1 = RomManipulation.Parse(_inputs[0], 0, 0, _logger);
|
|
|
|
|
|
List<RomData> AB2 = RomManipulation.Parse(_inputs[1], 0, 0, _logger);
|
2016-04-19 16:58:47 -07:00
|
|
|
|
|
2016-04-22 15:09:07 -07:00
|
|
|
|
List<RomData> OnlyA = RomManipulation.DiffOnlyInA(AB1, AB2);
|
|
|
|
|
|
List<RomData> OnlyB = RomManipulation.DiffOnlyInA(AB2, AB1);
|
|
|
|
|
|
List<RomData> BothAB = RomManipulation.DiffInAB(AB1, AB2);
|
|
|
|
|
|
|
|
|
|
|
|
string input0 = Path.GetFileNameWithoutExtension(_inputs[0]);
|
|
|
|
|
|
string input1 = Path.GetFileNameWithoutExtension(_inputs[1]);
|
|
|
|
|
|
|
|
|
|
|
|
Output.WriteToDat(_name + "-" + input0 + "-only", _desc + "-" + input0 + "-only", _version, _date, _cat, _author, _forceunpack, _old, "", OnlyA, _logger);
|
|
|
|
|
|
Output.WriteToDat(_name + "-" + input1 + "-only", _desc + "-" + input1 + "-only", _version, _date, _cat, _author, _forceunpack, _old, "", OnlyB, _logger);
|
|
|
|
|
|
Output.WriteToDat(_name + "-inboth", _desc + "-inboth", _version, _date, _cat, _author, _forceunpack, _old, "", BothAB, _logger);
|
2016-04-20 12:31:51 -07:00
|
|
|
|
}
|
2016-04-22 15:09:07 -07:00
|
|
|
|
|
2016-04-27 16:11:29 -07:00
|
|
|
|
/*
|
2016-04-22 15:09:07 -07:00
|
|
|
|
// If we want a merged list, send it for merging before outputting
|
|
|
|
|
|
if (_dedup)
|
2016-04-20 12:31:51 -07:00
|
|
|
|
{
|
2016-04-22 15:09:07 -07:00
|
|
|
|
A = RomManipulation.Merge(A);
|
2016-04-20 12:31:51 -07:00
|
|
|
|
}
|
2016-04-20 20:17:57 -07:00
|
|
|
|
|
2016-04-22 15:27:57 -07:00
|
|
|
|
// Sort the file by names for ease
|
2016-04-26 13:17:24 -07:00
|
|
|
|
RomManipulation.Sort(A, true);
|
2016-04-22 15:27:57 -07:00
|
|
|
|
|
2016-04-20 21:17:23 -07:00
|
|
|
|
// Now write the file out
|
|
|
|
|
|
Output.WriteToDat(_name, _desc, _version, _date, _cat, _author, _forceunpack, _old, "", A, _logger);
|
2016-04-27 16:11:29 -07:00
|
|
|
|
*/
|
2016-04-20 21:17:23 -07:00
|
|
|
|
|
2016-04-20 20:17:57 -07:00
|
|
|
|
return true;
|
2016-04-19 16:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|