Make inputs explicit for OfflineMerge

This commit is contained in:
Matt Nadareski
2016-05-04 14:43:40 -07:00
parent 57e57151cf
commit 231b576490
2 changed files with 364 additions and 252 deletions

View File

@@ -36,7 +36,7 @@ namespace SabreTools
{ {
_currentAllMerged = currentAllMerged.Replace("\"", ""); _currentAllMerged = currentAllMerged.Replace("\"", "");
_currentMissingMerged = currentMissingMerged.Replace("\"", ""); _currentMissingMerged = currentMissingMerged.Replace("\"", "");
_currentNewMerged = currentNewMerged; _currentNewMerged = currentNewMerged.Replace("\"", "");
_fake = fake; _fake = fake;
_logger = logger; _logger = logger;
} }
@@ -83,48 +83,18 @@ namespace SabreTools
fake = true; fake = true;
break; break;
default: default:
if (File.Exists(arg.Replace("\"", ""))) string temparg = arg.Replace("\"", "");
if (temparg.StartsWith("com="))
{ {
logger.Log("File found: " + arg); currentAllMerged = temparg.Split('=')[1];
if (currentAllMerged == "")
{
currentAllMerged = arg.Replace("\"", "");
}
else if (currentMissingMerged == "")
{
currentMissingMerged = arg.Replace("\"", "");
}
else if (currentNewMerged == "")
{
currentNewMerged = arg.Replace("\"", "");
}
else
{
logger.Warning("Only 3 input files are required; ignoring " + arg);
}
} }
else if (Directory.Exists(arg.Replace("\"", ""))) else if (temparg.StartsWith("fix="))
{ {
logger.Log("Directory found: " + arg); currentMissingMerged = temparg.Split('=')[1];
foreach (string file in Directory.GetFiles(arg.Replace("\"", ""), "*", SearchOption.AllDirectories)) }
{ else if (temparg.StartsWith("new="))
if (currentAllMerged == "") {
{ currentNewMerged = temparg.Split('=')[1];
currentAllMerged = file.Replace("\"", "");
}
else if (currentMissingMerged == "")
{
currentMissingMerged = file.Replace("\"", "");
}
else if (currentNewMerged == "")
{
currentNewMerged = file.Replace("\"", "");
}
else
{
logger.Warning("Only 3 input files are required; ignoring " + arg);
}
}
} }
else else
{ {
@@ -138,8 +108,8 @@ namespace SabreTools
} }
} }
// If help is set or any of the inputs are empty, show help // If help is set or all of the inputs are empty, show help
if (help || currentAllMerged == "" || currentMissingMerged == "" || currentNewMerged == "") if (help || (currentAllMerged == "" && currentMissingMerged == "" && currentNewMerged == ""))
{ {
Build.Help(); Build.Help();
logger.Close(); logger.Close();
@@ -148,265 +118,401 @@ namespace SabreTools
// Otherwise, run the program // Otherwise, run the program
OfflineMerge om = new OfflineMerge(currentAllMerged, currentMissingMerged, currentNewMerged, fake, logger); OfflineMerge om = new OfflineMerge(currentAllMerged, currentMissingMerged, currentNewMerged, fake, logger);
om.Process(); bool success = om.Process();
if (!success)
{
logger.Warning("At least one complete DAT and the fixdat is needed to run!");
}
} }
/// <summary> /// <summary>
/// Process the supplied inputs and create the three required outputs: /// Process the supplied inputs and create the four outputs
/// (a) Net New - (currentNewMerged)-(currentAllMerged)
/// (b) Unneeded - (currentAllMerged)-(currentNewMerged)
/// (c) New Missing - (a)+(currentMissingMerged-(b))
/// (d) Have - (currentNewMerged)-(c)
/// </summary> /// </summary>
/// <returns>True if the files were created properly, false otherwise</returns> /// <returns>True if the files were created properly, false otherwise</returns>
public bool Process() public bool Process()
{ {
// First get the combination Dictionary of currentAllMerged and currentNewMerged // Check all of the files for validity and break if one doesn't exist
_logger.Log("Adding Current and New Merged DATs to the dictionary"); if (_currentAllMerged != "" && !File.Exists(_currentAllMerged))
Dictionary<string, List<RomData>> completeDats = new Dictionary<string, List<RomData>>();
completeDats = RomManipulation.ParseDict(_currentAllMerged, 0, 0, completeDats, _logger);
completeDats = RomManipulation.ParseDict(_currentNewMerged, 0, 0, completeDats, _logger);
// Now get Net New output dictionary [(currentNewMerged)-(currentAllMerged)]
_logger.Log("Creating and populating Net New dictionary");
Dictionary<string, List<RomData>> netNew = new Dictionary<string, List<RomData>>();
foreach (string key in completeDats.Keys)
{ {
List<RomData> templist = RomManipulation.Merge(completeDats[key]); return false;
foreach (RomData rom in templist) }
if (_currentMissingMerged != "" && !File.Exists(_currentMissingMerged))
{
return false;
}
if (_currentNewMerged != "" && !File.Exists(_currentNewMerged))
{
return false;
}
Console.WriteLine(_currentAllMerged + " " + _currentMissingMerged + " " + _currentNewMerged);
// If we have all three DATs, then generate everything
if (_currentAllMerged != "" && _currentMissingMerged != "" && _currentNewMerged != "")
{
// First get the combination Dictionary of currentAllMerged and currentNewMerged
_logger.Log("Adding Current and New Merged DATs to the dictionary");
Dictionary<string, List<RomData>> completeDats = new Dictionary<string, List<RomData>>();
completeDats = RomManipulation.ParseDict(_currentAllMerged, 0, 0, completeDats, _logger);
completeDats = RomManipulation.ParseDict(_currentNewMerged, 0, 0, completeDats, _logger);
// Now get Net New output dictionary [(currentNewMerged)-(currentAllMerged)]
_logger.Log("Creating and populating Net New dictionary");
Dictionary<string, List<RomData>> netNew = new Dictionary<string, List<RomData>>();
foreach (string key in completeDats.Keys)
{ {
if (!rom.Dupe && rom.System == _currentNewMerged) List<RomData> templist = RomManipulation.Merge(completeDats[key]);
foreach (RomData rom in templist)
{ {
if (netNew.ContainsKey(key)) if (!rom.Dupe && rom.System == _currentNewMerged)
{ {
netNew[key].Add(rom); if (netNew.ContainsKey(key))
{
netNew[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
netNew.Add(key, temp);
}
} }
else }
}
// Now create the Unneeded dictionary [(currentAllMerged)-(currentNewMerged)]
_logger.Log("Creating and populating Uneeded dictionary");
Dictionary<string, List<RomData>> unneeded = new Dictionary<string, List<RomData>>();
foreach (string key in completeDats.Keys)
{
List<RomData> templist = RomManipulation.Merge(completeDats[key]);
foreach (RomData rom in templist)
{
if (!rom.Dupe && rom.System == _currentAllMerged)
{ {
List<RomData> temp = new List<RomData>(); if (unneeded.ContainsKey(key))
{
unneeded[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
unneeded.Add(key, temp);
}
}
}
}
// Now create the New Missing dictionary [(Net New)+(currentMissingMerged-(Unneeded))]
_logger.Log("Creating and populating New Missing dictionary");
Dictionary<string, List<RomData>> midMissing = new Dictionary<string, List<RomData>>();
midMissing = RomManipulation.ParseDict(_currentMissingMerged, 0, 0, midMissing, _logger);
foreach (string key in unneeded.Keys)
{
if (midMissing.ContainsKey(key))
{
midMissing[key].AddRange(unneeded[key]);
}
else
{
midMissing.Add(key, unneeded[key]);
}
}
Dictionary<string, List<RomData>> newMissing = new Dictionary<string, List<RomData>>();
foreach (string key in midMissing.Keys)
{
List<RomData> templist = RomManipulation.Merge(midMissing[key]);
foreach (RomData rom in templist)
{
if (!rom.Dupe && rom.System == _currentMissingMerged)
{
if (newMissing.ContainsKey(key))
{
newMissing[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
newMissing.Add(key, temp);
}
}
}
}
foreach (string key in netNew.Keys)
{
if (newMissing.ContainsKey(key))
{
newMissing[key].AddRange(netNew[key]);
}
else
{
newMissing.Add(key, netNew[key]);
}
}
// Now create the Have dictionary [(currentNewMerged)-(c)]
_logger.Log("Creating and populating Have dictionary");
Dictionary<string, List<RomData>> midHave = new Dictionary<string, List<RomData>>();
foreach (string key in newMissing.Keys)
{
if (midHave.ContainsKey(key))
{
midHave[key].AddRange(newMissing[key]);
}
else
{
midHave.Add(key, newMissing[key]);
}
}
foreach (string key in completeDats.Keys)
{
if (midHave.ContainsKey(key))
{
foreach (RomData rom in completeDats[key])
{
if (rom.System == _currentNewMerged)
{
midHave[key].Add(rom);
}
}
}
else
{
List<RomData> roms = new List<RomData>();
foreach (RomData rom in completeDats[key])
{
if (rom.System == _currentNewMerged)
{
roms.Add(rom);
}
}
midHave.Add(key, roms);
}
}
Dictionary<string, List<RomData>> have = new Dictionary<string, List<RomData>>();
foreach (string key in midHave.Keys)
{
List<RomData> templist = RomManipulation.Merge(midHave[key]);
foreach (RomData rom in templist)
{
if (!rom.Dupe && rom.System == _currentNewMerged)
{
if (have.ContainsKey(key))
{
have[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
have.Add(key, temp);
}
}
}
}
// If we are supposed to replace everything in the output with default values, do so
if (_fake)
{
_logger.Log("Replacing all hashes in Net New with 0-byte values");
List<string> keys = netNew.Keys.ToList();
foreach (string key in keys)
{
List<RomData> temp = new List<RomData>();
List<RomData> roms = netNew[key];
for (int i = 0; i < roms.Count; i++)
{
RomData rom = roms[i];
rom.Size = sizezero;
rom.CRC = crczero;
rom.MD5 = md5zero;
rom.SHA1 = sha1zero;
temp.Add(rom); temp.Add(rom);
netNew.Add(key, temp);
} }
netNew[key] = temp;
} }
}
}
// Now create the Unneeded dictionary [(currentAllMerged)-(currentNewMerged)] _logger.Log("Replacing all hashes in Unneeded with 0-byte values");
_logger.Log("Creating and populating Uneeded dictionary"); keys = unneeded.Keys.ToList();
Dictionary<string, List<RomData>> unneeded = new Dictionary<string, List<RomData>>(); foreach (string key in keys)
foreach (string key in completeDats.Keys)
{
List<RomData> templist = RomManipulation.Merge(completeDats[key]);
foreach (RomData rom in templist)
{
if (!rom.Dupe && rom.System == _currentAllMerged)
{ {
if (unneeded.ContainsKey(key)) List<RomData> temp = new List<RomData>();
List<RomData> roms = unneeded[key];
for (int i = 0; i < roms.Count; i++)
{ {
unneeded[key].Add(rom); RomData rom = roms[i];
} rom.Size = sizezero;
else rom.CRC = crczero;
{ rom.MD5 = md5zero;
List<RomData> temp = new List<RomData>(); rom.SHA1 = sha1zero;
temp.Add(rom); temp.Add(rom);
unneeded.Add(key, temp);
} }
unneeded[key] = temp;
} }
}
}
// Now create the New Missing dictionary [(Net New)+(currentMissingMerged-(Unneeded))] _logger.Log("Replacing all hashes in New Missing with 0-byte values");
_logger.Log("Creating and populating New Missing dictionary"); keys = newMissing.Keys.ToList();
Dictionary<string, List<RomData>> midMissing = new Dictionary<string, List<RomData>>(); foreach (string key in keys)
midMissing = RomManipulation.ParseDict(_currentMissingMerged, 0, 0, midMissing, _logger);
foreach (string key in unneeded.Keys)
{
if (midMissing.ContainsKey(key))
{
midMissing[key].AddRange(unneeded[key]);
}
else
{
midMissing.Add(key, unneeded[key]);
}
}
Dictionary<string, List<RomData>> newMissing = new Dictionary<string, List<RomData>>();
foreach (string key in midMissing.Keys)
{
List<RomData> templist = RomManipulation.Merge(midMissing[key]);
foreach (RomData rom in templist)
{
if (!rom.Dupe && rom.System == _currentMissingMerged)
{ {
if (newMissing.ContainsKey(key)) List<RomData> temp = new List<RomData>();
List<RomData> roms = newMissing[key];
for (int i = 0; i < roms.Count; i++)
{ {
newMissing[key].Add(rom); RomData rom = roms[i];
} rom.Size = sizezero;
else rom.CRC = crczero;
{ rom.MD5 = md5zero;
List<RomData> temp = new List<RomData>(); rom.SHA1 = sha1zero;
temp.Add(rom); temp.Add(rom);
newMissing.Add(key, temp);
} }
newMissing[key] = temp;
} }
}
}
foreach (string key in netNew.Keys)
{
if (newMissing.ContainsKey(key))
{
newMissing[key].AddRange(netNew[key]);
}
else
{
newMissing.Add(key, netNew[key]);
}
}
// Now create the Have dictionary [(currentNewMerged)-(c)] _logger.Log("Replacing all hashes in Have with 0-byte values");
_logger.Log("Creating and populating Have dictionary"); keys = have.Keys.ToList();
Dictionary<string, List<RomData>> midHave = new Dictionary<string, List<RomData>>(); foreach (string key in keys)
foreach (string key in newMissing.Keys)
{
if (midHave.ContainsKey(key))
{
midHave[key].AddRange(newMissing[key]);
}
else
{
midHave.Add(key, newMissing[key]);
}
}
foreach (string key in completeDats.Keys)
{
if (midHave.ContainsKey(key))
{
foreach (RomData rom in completeDats[key])
{ {
if (rom.System == _currentNewMerged) List<RomData> temp = new List<RomData>();
List<RomData> roms = have[key];
for (int i = 0; i < roms.Count; i++)
{ {
midHave[key].Add(rom); RomData rom = roms[i];
} rom.Size = sizezero;
} rom.CRC = crczero;
} rom.MD5 = md5zero;
else rom.SHA1 = sha1zero;
{
List<RomData> roms = new List<RomData>();
foreach (RomData rom in completeDats[key])
{
if (rom.System == _currentNewMerged)
{
roms.Add(rom);
}
}
midHave.Add(key, roms);
}
}
Dictionary<string, List<RomData>> have = new Dictionary<string, List<RomData>>();
foreach (string key in midHave.Keys)
{
List<RomData> templist = RomManipulation.Merge(midHave[key]);
foreach (RomData rom in templist)
{
if (!rom.Dupe && rom.System == _currentNewMerged)
{
if (have.ContainsKey(key))
{
have[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom); temp.Add(rom);
have.Add(key, temp); }
have[key] = temp;
}
}
// Finally, output all of the files
Output.WriteToDatFromDict("Net New", "Net New", "", DateTime.Now.ToString("yyyy-MM-dd"), "", "SabreTools", false, false, true, "", netNew, _logger);
Output.WriteToDatFromDict("Unneeded", "Unneeded", "", DateTime.Now.ToString("yyyy-MM-dd"), "", "SabreTools", false, false, true, "", unneeded, _logger);
Output.WriteToDatFromDict("New Missing", "New Missing", "", DateTime.Now.ToString("yyyy-MM-dd"), "", "SabreTools", false, false, true, "", newMissing, _logger);
Output.WriteToDatFromDict("Have", "Have", "", DateTime.Now.ToString("yyyy-MM-dd"), "", "SabreTools", false, false, true, "", have, _logger);
return true;
}
// If we only have the old merged and missing, only generate Have
else if (_currentAllMerged != "" && _currentMissingMerged != "")
{
// Now create the Have dictionary [(currentAllMerged)-(currentMissingMerged)]
_logger.Log("Creating and populating Have dictionary");
Dictionary<string, List<RomData>> midHave = new Dictionary<string, List<RomData>>();
midHave = RomManipulation.ParseDict(_currentMissingMerged, 0, 0, midHave, _logger);
midHave = RomManipulation.ParseDict(_currentAllMerged, 0, 0, midHave, _logger);
Dictionary<string, List<RomData>> have = new Dictionary<string, List<RomData>>();
foreach (string key in midHave.Keys)
{
List<RomData> templist = RomManipulation.Merge(midHave[key]);
foreach (RomData rom in templist)
{
if (!rom.Dupe && rom.System == _currentAllMerged)
{
if (have.ContainsKey(key))
{
have[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
have.Add(key, temp);
}
} }
} }
} }
// If we are supposed to replace everything in the output with default values, do so
if (_fake)
{
_logger.Log("Replacing all hashes in Have with 0-byte values");
List<string> keys = have.Keys.ToList();
foreach (string key in keys)
{
List<RomData> temp = new List<RomData>();
List<RomData> roms = have[key];
for (int i = 0; i < roms.Count; i++)
{
RomData rom = roms[i];
rom.Size = sizezero;
rom.CRC = crczero;
rom.MD5 = md5zero;
rom.SHA1 = sha1zero;
temp.Add(rom);
}
have[key] = temp;
}
}
Output.WriteToDatFromDict("Have", "Have", "", DateTime.Now.ToString("yyyy-MM-dd"), "", "SabreTools", false, false, true, "", have, _logger);
return true;
} }
// If we are supposed to replace everything in the output with default values, do so // If we only have the new merged and missing, only generate Have
if (_fake) else if (_currentNewMerged != "" && _currentMissingMerged != "")
{ {
_logger.Log("Replacing all hashes in Net New with 0-byte values"); // Now create the Have dictionary [(currentNewMerged)-(currentMissingMerged)]
List<string> keys = netNew.Keys.ToList(); _logger.Log("Creating and populating Have dictionary");
foreach (string key in keys) Dictionary<string, List<RomData>> midHave = new Dictionary<string, List<RomData>>();
midHave = RomManipulation.ParseDict(_currentMissingMerged, 0, 0, midHave, _logger);
midHave = RomManipulation.ParseDict(_currentNewMerged, 0, 0, midHave, _logger);
Dictionary<string, List<RomData>> have = new Dictionary<string, List<RomData>>();
foreach (string key in midHave.Keys)
{ {
List<RomData> temp = new List<RomData>(); List<RomData> templist = RomManipulation.Merge(midHave[key]);
List<RomData> roms = netNew[key]; foreach (RomData rom in templist)
for (int i = 0; i < roms.Count; i++)
{ {
RomData rom = roms[i]; if (!rom.Dupe && rom.System == _currentNewMerged)
rom.Size = sizezero; {
rom.CRC = crczero; if (have.ContainsKey(key))
rom.MD5 = md5zero; {
rom.SHA1 = sha1zero; have[key].Add(rom);
temp.Add(rom); }
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
have.Add(key, temp);
}
}
} }
netNew[key] = temp;
} }
_logger.Log("Replacing all hashes in Unneeded with 0-byte values"); // If we are supposed to replace everything in the output with default values, do so
keys = unneeded.Keys.ToList(); if (_fake)
foreach (string key in keys)
{ {
List<RomData> temp = new List<RomData>(); _logger.Log("Replacing all hashes in Have with 0-byte values");
List<RomData> roms = unneeded[key]; List<string> keys = have.Keys.ToList();
for (int i = 0; i < roms.Count; i++) foreach (string key in keys)
{ {
RomData rom = roms[i]; List<RomData> temp = new List<RomData>();
rom.Size = sizezero; List<RomData> roms = have[key];
rom.CRC = crczero; for (int i = 0; i < roms.Count; i++)
rom.MD5 = md5zero; {
rom.SHA1 = sha1zero; RomData rom = roms[i];
temp.Add(rom); rom.Size = sizezero;
rom.CRC = crczero;
rom.MD5 = md5zero;
rom.SHA1 = sha1zero;
temp.Add(rom);
}
have[key] = temp;
} }
unneeded[key] = temp;
} }
_logger.Log("Replacing all hashes in New Missing with 0-byte values"); Output.WriteToDatFromDict("Have", "Have", "", DateTime.Now.ToString("yyyy-MM-dd"), "", "SabreTools", false, false, true, "", have, _logger);
keys = newMissing.Keys.ToList();
foreach (string key in keys)
{
List<RomData> temp = new List<RomData>();
List<RomData> roms = newMissing[key];
for (int i = 0; i < roms.Count; i++)
{
RomData rom = roms[i];
rom.Size = sizezero;
rom.CRC = crczero;
rom.MD5 = md5zero;
rom.SHA1 = sha1zero;
temp.Add(rom);
}
newMissing[key] = temp;
}
_logger.Log("Replacing all hashes in Have with 0-byte values"); return true;
keys = have.Keys.ToList();
foreach (string key in keys)
{
List<RomData> temp = new List<RomData>();
List<RomData> roms = have[key];
for (int i = 0; i < roms.Count; i++)
{
RomData rom = roms[i];
rom.Size = sizezero;
rom.CRC = crczero;
rom.MD5 = md5zero;
rom.SHA1 = sha1zero;
temp.Add(rom);
}
have[key] = temp;
}
} }
// Finally, output all of the files return false;
Output.WriteToDatFromDict("Net New", "Net New", "", DateTime.Now.ToString("yyyy-MM-dd"), "", "SabreTools", false, false, true, "", netNew, _logger);
Output.WriteToDatFromDict("Unneeded", "Unneeded", "", DateTime.Now.ToString("yyyy-MM-dd"), "", "SabreTools", false, false, true, "", unneeded, _logger);
Output.WriteToDatFromDict("New Missing", "New Missing", "", DateTime.Now.ToString("yyyy-MM-dd"), "", "SabreTools", false, false, true, "", newMissing, _logger);
Output.WriteToDatFromDict("Have", "Have", "", DateTime.Now.ToString("yyyy-MM-dd"), "", "SabreTools", false, false, true, "", have, _logger);
return true;
} }
} }
} }

View File

@@ -174,17 +174,23 @@ Options:
case "OfflineMerge": case "OfflineMerge":
Console.WriteLine(@"OfflineMerge - Update DATS for offline arrays Console.WriteLine(@"OfflineMerge - Update DATS for offline arrays
----------------------------------------- -----------------------------------------
Usage: OfflineMerge [options] [Complete] [Missing] [NewComplete] Usage: OfflineMerge [options] [inputs]
Options: Options:
-h, -?, --help Show this help dialog -h, -?, --help Show this help dialog
-f, --fake Replace all hashes and sizes by the default -f, --fake Replace all hashes and sizes by the default
Inputs:
com= Complete current DAT
fix= Complete current Missing
new= New Complete DAT
This program will output the following DATs: This program will output the following DATs:
(a) Net New - (NewComplete)-(Complete) (a) Net New - (NewComplete)-(Complete)
(b) Unneeded - (Complete)-(NewComplete) (b) Unneeded - (Complete)-(NewComplete)
(c) New Missing - (Net New)+(Missing-(Unneeded)) (c) New Missing - (Net New)+(Missing-(Unneeded))
(d) Have - (NewComplete)-(New Missing)"); (d) Have - (NewComplete)-(New Missing)
OR (Complete or NewComplete)-(Missing) if one is missing");
break; break;
default: default:
Console.Write("This is the default help output"); Console.Write("This is the default help output");