mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[Split] Merge ExtSplit and HashSplit into one logical entity called Split
This commit is contained in:
@@ -643,10 +643,7 @@ namespace SabreTools
|
|||||||
// Split a DAT by extension
|
// Split a DAT by extension
|
||||||
else if (extsplit)
|
else if (extsplit)
|
||||||
{
|
{
|
||||||
foreach (string input in inputs)
|
InitExtSplit(inputs, exta, extb, outdir);
|
||||||
{
|
|
||||||
InitExtSplit(input, exta, extb, outdir);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge, diff, and dedupe at least 2 DATs
|
// Merge, diff, and dedupe at least 2 DATs
|
||||||
|
|||||||
@@ -104,11 +104,10 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="ExtSplit.cs" />
|
<Compile Include="Split.cs" />
|
||||||
<Compile Include="Filter.cs" />
|
<Compile Include="Filter.cs" />
|
||||||
<Compile Include="ImportExport\Generate.cs" />
|
<Compile Include="ImportExport\Generate.cs" />
|
||||||
<Compile Include="ImportExport\GenerateTwo.cs" />
|
<Compile Include="ImportExport\GenerateTwo.cs" />
|
||||||
<Compile Include="HashSplit.cs" />
|
|
||||||
<Compile Include="ImportExport\Import.cs" />
|
<Compile Include="ImportExport\Import.cs" />
|
||||||
<Compile Include="DATabase.cs" />
|
<Compile Include="DATabase.cs" />
|
||||||
<Compile Include="ImportExport\ImportTwo.cs" />
|
<Compile Include="ImportExport\ImportTwo.cs" />
|
||||||
|
|||||||
@@ -1,176 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
using SabreTools.Helper;
|
|
||||||
|
|
||||||
namespace SabreTools
|
|
||||||
{
|
|
||||||
public class ExtSplit
|
|
||||||
{
|
|
||||||
// Instance variables
|
|
||||||
private List<string> _extA;
|
|
||||||
private List<string> _extB;
|
|
||||||
private string _filename;
|
|
||||||
private string _outdir;
|
|
||||||
private static Logger _logger;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create a new DatSplit object
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="filename">Filename of the DAT to split</param>
|
|
||||||
/// <param name="extA">List of extensions to split on (first DAT)</param>
|
|
||||||
/// <param name="extB">List of extensions to split on (second DAT)</param>
|
|
||||||
/// <param name="logger">Logger object for console and file writing</param>
|
|
||||||
public ExtSplit(string filename, List<string> extA, List<string> extB, string outdir, Logger logger)
|
|
||||||
{
|
|
||||||
_filename = filename.Replace("\"", "");
|
|
||||||
_extA = new List<string>();
|
|
||||||
foreach (string s in extA)
|
|
||||||
{
|
|
||||||
_extA.Add((s.StartsWith(".") ? s : "." + s).ToUpperInvariant());
|
|
||||||
}
|
|
||||||
_extB = new List<string>();
|
|
||||||
foreach (string s in extB)
|
|
||||||
{
|
|
||||||
_extB.Add((s.StartsWith(".") ? s : "." + s).ToUpperInvariant());
|
|
||||||
}
|
|
||||||
_outdir = outdir.Replace("\"", "");
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Split a DAT based on filtering by 2 extensions
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>True if DAT was split, false otherwise</returns>
|
|
||||||
public bool Split()
|
|
||||||
{
|
|
||||||
// If file doesn't exist, error and return
|
|
||||||
if (!File.Exists(_filename))
|
|
||||||
{
|
|
||||||
_logger.Error("File '" + _filename + "' doesn't exist");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If it's empty, use the current folder
|
|
||||||
if (_outdir.Trim() == "")
|
|
||||||
{
|
|
||||||
_outdir = Environment.CurrentDirectory;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the output directory doesn't exist, create it
|
|
||||||
if (!Directory.Exists(_outdir))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(_outdir);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load the current DAT to be processed
|
|
||||||
DatData datdata = new DatData();
|
|
||||||
datdata = RomManipulation.Parse(_filename, 0, 0, datdata, _logger);
|
|
||||||
|
|
||||||
// Set all of the appropriate outputs for each of the subsets
|
|
||||||
OutputFormat outputFormat = RomManipulation.GetOutputFormat(_filename);
|
|
||||||
DatData datdataA = new DatData
|
|
||||||
{
|
|
||||||
FileName = datdata.FileName + " (" + String.Join(",", _extA) + ")",
|
|
||||||
Name = datdata.Name + " (" + String.Join(",", _extA) + ")",
|
|
||||||
Description = datdata.Description + " (" + String.Join(",", _extA) + ")",
|
|
||||||
Category = datdata.Category,
|
|
||||||
Version = datdata.Version,
|
|
||||||
Date = datdata.Date,
|
|
||||||
Author = datdata.Author,
|
|
||||||
Email = datdata.Email,
|
|
||||||
Homepage = datdata.Homepage,
|
|
||||||
Url = datdata.Url,
|
|
||||||
Comment = datdata.Comment,
|
|
||||||
Roms = new Dictionary<string, List<RomData>>(),
|
|
||||||
OutputFormat = outputFormat,
|
|
||||||
};
|
|
||||||
DatData datdataB = new DatData
|
|
||||||
{
|
|
||||||
FileName = datdata.FileName + " (" + String.Join(",", _extB) + ")",
|
|
||||||
Name = datdata.Name + " (" + String.Join(",", _extB) + ")",
|
|
||||||
Description = datdata.Description + " (" + String.Join(",", _extB) + ")",
|
|
||||||
Category = datdata.Category,
|
|
||||||
Version = datdata.Version,
|
|
||||||
Date = datdata.Date,
|
|
||||||
Author = datdata.Author,
|
|
||||||
Email = datdata.Email,
|
|
||||||
Homepage = datdata.Homepage,
|
|
||||||
Url = datdata.Url,
|
|
||||||
Comment = datdata.Comment,
|
|
||||||
Roms = new Dictionary<string, List<RomData>>(),
|
|
||||||
OutputFormat = outputFormat,
|
|
||||||
};
|
|
||||||
|
|
||||||
// If roms is empty, return false
|
|
||||||
if (datdata.Roms.Count == 0)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now separate the roms accordingly
|
|
||||||
foreach (string key in datdata.Roms.Keys)
|
|
||||||
{
|
|
||||||
foreach (RomData rom in datdata.Roms[key])
|
|
||||||
{
|
|
||||||
if (_extA.Contains(Path.GetExtension(rom.Name.ToUpperInvariant())))
|
|
||||||
{
|
|
||||||
if (datdataA.Roms.ContainsKey(key))
|
|
||||||
{
|
|
||||||
datdataA.Roms[key].Add(rom);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<RomData> temp = new List<RomData>();
|
|
||||||
temp.Add(rom);
|
|
||||||
datdataA.Roms.Add(key, temp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (_extB.Contains(Path.GetExtension(rom.Name.ToUpperInvariant())))
|
|
||||||
{
|
|
||||||
if (datdataB.Roms.ContainsKey(key))
|
|
||||||
{
|
|
||||||
datdataB.Roms[key].Add(rom);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<RomData> temp = new List<RomData>();
|
|
||||||
temp.Add(rom);
|
|
||||||
datdataB.Roms.Add(key, temp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (datdataA.Roms.ContainsKey(key))
|
|
||||||
{
|
|
||||||
datdataA.Roms[key].Add(rom);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<RomData> temp = new List<RomData>();
|
|
||||||
temp.Add(rom);
|
|
||||||
datdataA.Roms.Add(key, temp);
|
|
||||||
}
|
|
||||||
if (datdataB.Roms.ContainsKey(key))
|
|
||||||
{
|
|
||||||
datdataB.Roms[key].Add(rom);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<RomData> temp = new List<RomData>();
|
|
||||||
temp.Add(rom);
|
|
||||||
datdataB.Roms.Add(key, temp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then write out both files
|
|
||||||
bool success = Output.WriteDatfile(datdataA, _outdir, _logger);
|
|
||||||
success &= Output.WriteDatfile(datdataB, _outdir, _logger);
|
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,264 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
using SabreTools.Helper;
|
|
||||||
|
|
||||||
namespace SabreTools
|
|
||||||
{
|
|
||||||
public class HashSplit
|
|
||||||
{
|
|
||||||
// Internal variables
|
|
||||||
List<string> _inputs;
|
|
||||||
string _outdir;
|
|
||||||
Logger _logger;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create a HashSplit object
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="filename">The name of the file to be split</param>
|
|
||||||
/// <param name="outdir">Optional output directory, defaults to DAT directory</param>
|
|
||||||
/// <param name="logger">Logger object for file and console output</param>
|
|
||||||
public HashSplit(List<string> inputs, string outdir, Logger logger)
|
|
||||||
{
|
|
||||||
_inputs = inputs;
|
|
||||||
_outdir = (outdir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? outdir : outdir + Path.DirectorySeparatorChar).Replace("\"", "");
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Split the DAT into parts by best-available hash data
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>True if the DATs were output, false otherwise</returns>
|
|
||||||
public bool Split()
|
|
||||||
{
|
|
||||||
// First, clean the original filenames
|
|
||||||
List<string> newinputs = new List<string>();
|
|
||||||
foreach (string input in _inputs)
|
|
||||||
{
|
|
||||||
newinputs.Add(Path.GetFullPath(input.Replace("\"", "")));
|
|
||||||
}
|
|
||||||
_inputs = newinputs;
|
|
||||||
|
|
||||||
// Now, process each file and folder in the input
|
|
||||||
bool finalreturn = true;
|
|
||||||
foreach (string input in _inputs)
|
|
||||||
{
|
|
||||||
if (File.Exists(input))
|
|
||||||
{
|
|
||||||
finalreturn &= SplitHelper(input, Path.GetDirectoryName(input));
|
|
||||||
}
|
|
||||||
if (Directory.Exists(input))
|
|
||||||
{
|
|
||||||
foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories))
|
|
||||||
{
|
|
||||||
finalreturn &= SplitHelper(Path.GetFullPath(file), (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return finalreturn;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool SplitHelper(string filename, string basepath)
|
|
||||||
{
|
|
||||||
// Sanitize the basepath to be more predictable
|
|
||||||
basepath = (basepath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? basepath : basepath + Path.DirectorySeparatorChar);
|
|
||||||
|
|
||||||
// Get the file data to be split
|
|
||||||
OutputFormat outputFormat = RomManipulation.GetOutputFormat(filename);
|
|
||||||
DatData datdata = new DatData();
|
|
||||||
datdata = RomManipulation.Parse(filename, 0, 0, datdata, _logger, true);
|
|
||||||
|
|
||||||
// Create each of the respective output DATs
|
|
||||||
_logger.User("Creating and populating new DATs");
|
|
||||||
DatData nodump = new DatData
|
|
||||||
{
|
|
||||||
FileName = datdata.FileName + " (Nodump)",
|
|
||||||
Name = datdata.Name + " (Nodump)",
|
|
||||||
Description = datdata.Description + " (Nodump)",
|
|
||||||
Category = datdata.Category,
|
|
||||||
Version = datdata.Version,
|
|
||||||
Date = datdata.Date,
|
|
||||||
Author = datdata.Author,
|
|
||||||
Email = datdata.Email,
|
|
||||||
Homepage = datdata.Homepage,
|
|
||||||
Url = datdata.Url,
|
|
||||||
Comment = datdata.Comment,
|
|
||||||
Header = datdata.Header,
|
|
||||||
Type = datdata.Type,
|
|
||||||
ForceMerging = datdata.ForceMerging,
|
|
||||||
ForceNodump = datdata.ForceNodump,
|
|
||||||
ForcePacking = datdata.ForcePacking,
|
|
||||||
OutputFormat = outputFormat,
|
|
||||||
MergeRoms = datdata.MergeRoms,
|
|
||||||
Roms = new Dictionary<string, List<RomData>>(),
|
|
||||||
};
|
|
||||||
DatData sha1 = new DatData
|
|
||||||
{
|
|
||||||
FileName = datdata.FileName + " (SHA-1)",
|
|
||||||
Name = datdata.Name + " (SHA-1)",
|
|
||||||
Description = datdata.Description + " (SHA-1)",
|
|
||||||
Category = datdata.Category,
|
|
||||||
Version = datdata.Version,
|
|
||||||
Date = datdata.Date,
|
|
||||||
Author = datdata.Author,
|
|
||||||
Email = datdata.Email,
|
|
||||||
Homepage = datdata.Homepage,
|
|
||||||
Url = datdata.Url,
|
|
||||||
Comment = datdata.Comment,
|
|
||||||
Header = datdata.Header,
|
|
||||||
Type = datdata.Type,
|
|
||||||
ForceMerging = datdata.ForceMerging,
|
|
||||||
ForceNodump = datdata.ForceNodump,
|
|
||||||
ForcePacking = datdata.ForcePacking,
|
|
||||||
OutputFormat = outputFormat,
|
|
||||||
MergeRoms = datdata.MergeRoms,
|
|
||||||
Roms = new Dictionary<string, List<RomData>>(),
|
|
||||||
};
|
|
||||||
DatData md5 = new DatData
|
|
||||||
{
|
|
||||||
FileName = datdata.FileName + " (MD5)",
|
|
||||||
Name = datdata.Name + " (MD5)",
|
|
||||||
Description = datdata.Description + " (MD5)",
|
|
||||||
Category = datdata.Category,
|
|
||||||
Version = datdata.Version,
|
|
||||||
Date = datdata.Date,
|
|
||||||
Author = datdata.Author,
|
|
||||||
Email = datdata.Email,
|
|
||||||
Homepage = datdata.Homepage,
|
|
||||||
Url = datdata.Url,
|
|
||||||
Comment = datdata.Comment,
|
|
||||||
Header = datdata.Header,
|
|
||||||
Type = datdata.Type,
|
|
||||||
ForceMerging = datdata.ForceMerging,
|
|
||||||
ForceNodump = datdata.ForceNodump,
|
|
||||||
ForcePacking = datdata.ForcePacking,
|
|
||||||
OutputFormat = outputFormat,
|
|
||||||
MergeRoms = datdata.MergeRoms,
|
|
||||||
Roms = new Dictionary<string, List<RomData>>(),
|
|
||||||
};
|
|
||||||
DatData crc = new DatData
|
|
||||||
{
|
|
||||||
FileName = datdata.FileName + " (CRC)",
|
|
||||||
Name = datdata.Name + " (CRC)",
|
|
||||||
Description = datdata.Description + " (CRC)",
|
|
||||||
Category = datdata.Category,
|
|
||||||
Version = datdata.Version,
|
|
||||||
Date = datdata.Date,
|
|
||||||
Author = datdata.Author,
|
|
||||||
Email = datdata.Email,
|
|
||||||
Homepage = datdata.Homepage,
|
|
||||||
Url = datdata.Url,
|
|
||||||
Comment = datdata.Comment,
|
|
||||||
Header = datdata.Header,
|
|
||||||
Type = datdata.Type,
|
|
||||||
ForceMerging = datdata.ForceMerging,
|
|
||||||
ForceNodump = datdata.ForceNodump,
|
|
||||||
ForcePacking = datdata.ForcePacking,
|
|
||||||
OutputFormat = outputFormat,
|
|
||||||
MergeRoms = datdata.MergeRoms,
|
|
||||||
Roms = new Dictionary<string, List<RomData>>(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Now populate each of the DAT objects in turn
|
|
||||||
List<string> keys = datdata.Roms.Keys.ToList();
|
|
||||||
foreach (string key in keys)
|
|
||||||
{
|
|
||||||
List<RomData> roms = datdata.Roms[key];
|
|
||||||
foreach (RomData rom in roms)
|
|
||||||
{
|
|
||||||
// If the file is a nodump
|
|
||||||
if (rom.Nodump)
|
|
||||||
{
|
|
||||||
if (nodump.Roms.ContainsKey(key))
|
|
||||||
{
|
|
||||||
nodump.Roms[key].Add(rom);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<RomData> temp = new List<RomData>();
|
|
||||||
temp.Add(rom);
|
|
||||||
nodump.Roms.Add(key, temp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If the file has a SHA-1
|
|
||||||
else if (rom.SHA1 != null && rom.SHA1 != "")
|
|
||||||
{
|
|
||||||
if (sha1.Roms.ContainsKey(key))
|
|
||||||
{
|
|
||||||
sha1.Roms[key].Add(rom);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<RomData> temp = new List<RomData>();
|
|
||||||
temp.Add(rom);
|
|
||||||
sha1.Roms.Add(key, temp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If the file has no SHA-1 but has an MD5
|
|
||||||
else if (rom.MD5 != null && rom.MD5 != "")
|
|
||||||
{
|
|
||||||
if (md5.Roms.ContainsKey(key))
|
|
||||||
{
|
|
||||||
md5.Roms[key].Add(rom);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<RomData> temp = new List<RomData>();
|
|
||||||
temp.Add(rom);
|
|
||||||
md5.Roms.Add(key, temp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// All other cases
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (crc.Roms.ContainsKey(key))
|
|
||||||
{
|
|
||||||
crc.Roms[key].Add(rom);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<RomData> temp = new List<RomData>();
|
|
||||||
temp.Add(rom);
|
|
||||||
crc.Roms.Add(key, temp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the output directory
|
|
||||||
string outdir = "";
|
|
||||||
if (_outdir != "")
|
|
||||||
{
|
|
||||||
outdir = _outdir + Path.GetDirectoryName(filename).Remove(0, basepath.Length - 1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
outdir = Path.GetDirectoryName(filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now, output all of the files to the output directory
|
|
||||||
_logger.User("DAT information created, outputting new files");
|
|
||||||
bool success = true;
|
|
||||||
if (nodump.Roms.Count > 0)
|
|
||||||
{
|
|
||||||
success &= Output.WriteDatfile(nodump, outdir, _logger);
|
|
||||||
}
|
|
||||||
if (sha1.Roms.Count > 0)
|
|
||||||
{
|
|
||||||
success &= Output.WriteDatfile(sha1, outdir, _logger);
|
|
||||||
}
|
|
||||||
if (md5.Roms.Count > 0)
|
|
||||||
{
|
|
||||||
success &= Output.WriteDatfile(md5, outdir, _logger);
|
|
||||||
}
|
|
||||||
if (crc.Roms.Count > 0)
|
|
||||||
{
|
|
||||||
success &= Output.WriteDatfile(crc, outdir, _logger);
|
|
||||||
}
|
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -405,14 +405,25 @@ namespace SabreTools
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Wrap splitting a DAT by 2 extensions
|
/// Wrap splitting a DAT by 2 extensions
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input">Input file or folder to be split</param>
|
/// <param name="inputs">Input files or folders to be split</param>
|
||||||
/// <param name="exta">First extension to split on</param>
|
/// <param name="exta">First extension to split on</param>
|
||||||
/// <param name="extb">Second extension to split on</param>
|
/// <param name="extb">Second extension to split on</param>
|
||||||
/// <param name="outdir">Output directory for the split files</param>
|
/// <param name="outdir">Output directory for the split files</param>
|
||||||
private static void InitExtSplit(string input, string exta, string extb, string outdir)
|
private static void InitExtSplit(List<string> inputs, string exta, string extb, string outdir)
|
||||||
{
|
{
|
||||||
|
// Verify the input files
|
||||||
|
foreach (string input in inputs)
|
||||||
|
{
|
||||||
|
if (!File.Exists(input.Replace("\"", "")) && !Directory.Exists(input.Replace("\"", "")))
|
||||||
|
{
|
||||||
|
_logger.Error(input + " is not a valid file or folder!");
|
||||||
|
Console.WriteLine();
|
||||||
|
Build.Help();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Strip any quotations from the names
|
// Strip any quotations from the names
|
||||||
input = input.Replace("\"", "");
|
|
||||||
exta = exta.Replace("\"", "");
|
exta = exta.Replace("\"", "");
|
||||||
extb = extb.Replace("\"", "");
|
extb = extb.Replace("\"", "");
|
||||||
outdir = outdir.Replace("\"", "");
|
outdir = outdir.Replace("\"", "");
|
||||||
@@ -421,21 +432,9 @@ namespace SabreTools
|
|||||||
List<string> extaList = exta.Split(',').ToList();
|
List<string> extaList = exta.Split(',').ToList();
|
||||||
List<string> extbList = extb.Split(',').ToList();
|
List<string> extbList = extb.Split(',').ToList();
|
||||||
|
|
||||||
if (input != "" && File.Exists(input))
|
Split es = new Split(inputs, extaList, extbList, outdir, _logger);
|
||||||
{
|
es.Process();
|
||||||
if (exta == "" || extb == "")
|
return;
|
||||||
{
|
|
||||||
_logger.Warning("Two extensions are needed to split a DAT!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ExtSplit es = new ExtSplit(input, extaList, extbList, outdir, _logger);
|
|
||||||
es.Split();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.Log("I'm sorry but " + input + "doesn't exist!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -461,8 +460,8 @@ namespace SabreTools
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If so, run the program
|
// If so, run the program
|
||||||
HashSplit hs = new HashSplit(inputs, outdir, _logger);
|
Split hs = new Split(inputs, outdir, _logger);
|
||||||
hs.Split();
|
hs.Process();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
464
DATabase/Split.cs
Normal file
464
DATabase/Split.cs
Normal file
@@ -0,0 +1,464 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
using SabreTools.Helper;
|
||||||
|
|
||||||
|
namespace SabreTools
|
||||||
|
{
|
||||||
|
public class Split
|
||||||
|
{
|
||||||
|
// Instance variables
|
||||||
|
private bool _hash;
|
||||||
|
private List<string> _extA;
|
||||||
|
private List<string> _extB;
|
||||||
|
private List<string> _inputs;
|
||||||
|
private string _outdir;
|
||||||
|
private static Logger _logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a new Split object (extension split)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">Filename of the DAT to split</param>
|
||||||
|
/// <param name="extA">List of extensions to split on (first DAT)</param>
|
||||||
|
/// <param name="extB">List of extensions to split on (second DAT)</param>
|
||||||
|
/// <param name="logger">Logger object for console and file writing</param>
|
||||||
|
public Split(List<string> inputs, List<string> extA, List<string> extB, string outdir, Logger logger)
|
||||||
|
{
|
||||||
|
_hash = false;
|
||||||
|
_inputs = new List<string>();
|
||||||
|
foreach (string s in inputs)
|
||||||
|
{
|
||||||
|
_inputs.Add(s.Replace("\"", ""));
|
||||||
|
}
|
||||||
|
_extA = new List<string>();
|
||||||
|
foreach (string s in extA)
|
||||||
|
{
|
||||||
|
_extA.Add((s.StartsWith(".") ? s : "." + s).ToUpperInvariant());
|
||||||
|
}
|
||||||
|
_extB = new List<string>();
|
||||||
|
foreach (string s in extB)
|
||||||
|
{
|
||||||
|
_extB.Add((s.StartsWith(".") ? s : "." + s).ToUpperInvariant());
|
||||||
|
}
|
||||||
|
_outdir = outdir.Replace("\"", "");
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a new Split object (hash split)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">Filename of the DAT to split</param>
|
||||||
|
/// <param name="logger">Logger object for console and file writing</param>
|
||||||
|
public Split(List<string> inputs, string outdir, Logger logger)
|
||||||
|
{
|
||||||
|
_hash = true;
|
||||||
|
_inputs = new List<string>();
|
||||||
|
foreach (string s in inputs)
|
||||||
|
{
|
||||||
|
_inputs.Add(s.Replace("\"", ""));
|
||||||
|
}
|
||||||
|
_extA = new List<string>();
|
||||||
|
_extB = new List<string>();
|
||||||
|
_outdir = outdir.Replace("\"", "");
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Split a DAT based on filtering by 2 extensions
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>True if DAT was split, false otherwise</returns>
|
||||||
|
public bool Process()
|
||||||
|
{
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
// If it's empty, use the current folder
|
||||||
|
if (_outdir.Trim() == "")
|
||||||
|
{
|
||||||
|
_outdir = Environment.CurrentDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the output directory doesn't exist, create it
|
||||||
|
if (!Directory.Exists(_outdir))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(_outdir);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop over the inputs
|
||||||
|
foreach (string input in _inputs)
|
||||||
|
{
|
||||||
|
// If it's a file, run the proper split on the file
|
||||||
|
if (File.Exists(input))
|
||||||
|
{
|
||||||
|
if (_hash)
|
||||||
|
{
|
||||||
|
success &= SplitByHash(Path.GetFullPath(input), Path.GetDirectoryName(input));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
success &= SplitByExt(Path.GetFullPath(input), Path.GetDirectoryName(input));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If it's a directory, run the splits over the files within
|
||||||
|
else if (Directory.Exists(input))
|
||||||
|
{
|
||||||
|
foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories))
|
||||||
|
{
|
||||||
|
if (_hash)
|
||||||
|
{
|
||||||
|
success &= SplitByHash(Path.GetFullPath(file), (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
success &= SplitByExt(Path.GetFullPath(file), (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If file doesn't exist, error and return
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.Error("File or folder '" + input + "' doesn't exist");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Split a DAT by best available hashes
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">Name of the file to be split</param>
|
||||||
|
/// <param name="basepath">Parent path for replacement</param>
|
||||||
|
/// <returns>True if split succeeded, false otherwise</returns>
|
||||||
|
private bool SplitByHash(string filename, string basepath)
|
||||||
|
{
|
||||||
|
// Sanitize the basepath to be more predictable
|
||||||
|
basepath = (basepath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? basepath : basepath + Path.DirectorySeparatorChar);
|
||||||
|
|
||||||
|
// Get the file data to be split
|
||||||
|
OutputFormat outputFormat = RomManipulation.GetOutputFormat(filename);
|
||||||
|
DatData datdata = new DatData();
|
||||||
|
datdata = RomManipulation.Parse(filename, 0, 0, datdata, _logger, true);
|
||||||
|
|
||||||
|
// Create each of the respective output DATs
|
||||||
|
_logger.User("Creating and populating new DATs");
|
||||||
|
DatData nodump = new DatData
|
||||||
|
{
|
||||||
|
FileName = datdata.FileName + " (Nodump)",
|
||||||
|
Name = datdata.Name + " (Nodump)",
|
||||||
|
Description = datdata.Description + " (Nodump)",
|
||||||
|
Category = datdata.Category,
|
||||||
|
Version = datdata.Version,
|
||||||
|
Date = datdata.Date,
|
||||||
|
Author = datdata.Author,
|
||||||
|
Email = datdata.Email,
|
||||||
|
Homepage = datdata.Homepage,
|
||||||
|
Url = datdata.Url,
|
||||||
|
Comment = datdata.Comment,
|
||||||
|
Header = datdata.Header,
|
||||||
|
Type = datdata.Type,
|
||||||
|
ForceMerging = datdata.ForceMerging,
|
||||||
|
ForceNodump = datdata.ForceNodump,
|
||||||
|
ForcePacking = datdata.ForcePacking,
|
||||||
|
OutputFormat = outputFormat,
|
||||||
|
MergeRoms = datdata.MergeRoms,
|
||||||
|
Roms = new Dictionary<string, List<RomData>>(),
|
||||||
|
};
|
||||||
|
DatData sha1 = new DatData
|
||||||
|
{
|
||||||
|
FileName = datdata.FileName + " (SHA-1)",
|
||||||
|
Name = datdata.Name + " (SHA-1)",
|
||||||
|
Description = datdata.Description + " (SHA-1)",
|
||||||
|
Category = datdata.Category,
|
||||||
|
Version = datdata.Version,
|
||||||
|
Date = datdata.Date,
|
||||||
|
Author = datdata.Author,
|
||||||
|
Email = datdata.Email,
|
||||||
|
Homepage = datdata.Homepage,
|
||||||
|
Url = datdata.Url,
|
||||||
|
Comment = datdata.Comment,
|
||||||
|
Header = datdata.Header,
|
||||||
|
Type = datdata.Type,
|
||||||
|
ForceMerging = datdata.ForceMerging,
|
||||||
|
ForceNodump = datdata.ForceNodump,
|
||||||
|
ForcePacking = datdata.ForcePacking,
|
||||||
|
OutputFormat = outputFormat,
|
||||||
|
MergeRoms = datdata.MergeRoms,
|
||||||
|
Roms = new Dictionary<string, List<RomData>>(),
|
||||||
|
};
|
||||||
|
DatData md5 = new DatData
|
||||||
|
{
|
||||||
|
FileName = datdata.FileName + " (MD5)",
|
||||||
|
Name = datdata.Name + " (MD5)",
|
||||||
|
Description = datdata.Description + " (MD5)",
|
||||||
|
Category = datdata.Category,
|
||||||
|
Version = datdata.Version,
|
||||||
|
Date = datdata.Date,
|
||||||
|
Author = datdata.Author,
|
||||||
|
Email = datdata.Email,
|
||||||
|
Homepage = datdata.Homepage,
|
||||||
|
Url = datdata.Url,
|
||||||
|
Comment = datdata.Comment,
|
||||||
|
Header = datdata.Header,
|
||||||
|
Type = datdata.Type,
|
||||||
|
ForceMerging = datdata.ForceMerging,
|
||||||
|
ForceNodump = datdata.ForceNodump,
|
||||||
|
ForcePacking = datdata.ForcePacking,
|
||||||
|
OutputFormat = outputFormat,
|
||||||
|
MergeRoms = datdata.MergeRoms,
|
||||||
|
Roms = new Dictionary<string, List<RomData>>(),
|
||||||
|
};
|
||||||
|
DatData crc = new DatData
|
||||||
|
{
|
||||||
|
FileName = datdata.FileName + " (CRC)",
|
||||||
|
Name = datdata.Name + " (CRC)",
|
||||||
|
Description = datdata.Description + " (CRC)",
|
||||||
|
Category = datdata.Category,
|
||||||
|
Version = datdata.Version,
|
||||||
|
Date = datdata.Date,
|
||||||
|
Author = datdata.Author,
|
||||||
|
Email = datdata.Email,
|
||||||
|
Homepage = datdata.Homepage,
|
||||||
|
Url = datdata.Url,
|
||||||
|
Comment = datdata.Comment,
|
||||||
|
Header = datdata.Header,
|
||||||
|
Type = datdata.Type,
|
||||||
|
ForceMerging = datdata.ForceMerging,
|
||||||
|
ForceNodump = datdata.ForceNodump,
|
||||||
|
ForcePacking = datdata.ForcePacking,
|
||||||
|
OutputFormat = outputFormat,
|
||||||
|
MergeRoms = datdata.MergeRoms,
|
||||||
|
Roms = new Dictionary<string, List<RomData>>(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Now populate each of the DAT objects in turn
|
||||||
|
List<string> keys = datdata.Roms.Keys.ToList();
|
||||||
|
foreach (string key in keys)
|
||||||
|
{
|
||||||
|
List<RomData> roms = datdata.Roms[key];
|
||||||
|
foreach (RomData rom in roms)
|
||||||
|
{
|
||||||
|
// If the file is a nodump
|
||||||
|
if (rom.Nodump)
|
||||||
|
{
|
||||||
|
if (nodump.Roms.ContainsKey(key))
|
||||||
|
{
|
||||||
|
nodump.Roms[key].Add(rom);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
List<RomData> temp = new List<RomData>();
|
||||||
|
temp.Add(rom);
|
||||||
|
nodump.Roms.Add(key, temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If the file has a SHA-1
|
||||||
|
else if (rom.SHA1 != null && rom.SHA1 != "")
|
||||||
|
{
|
||||||
|
if (sha1.Roms.ContainsKey(key))
|
||||||
|
{
|
||||||
|
sha1.Roms[key].Add(rom);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
List<RomData> temp = new List<RomData>();
|
||||||
|
temp.Add(rom);
|
||||||
|
sha1.Roms.Add(key, temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If the file has no SHA-1 but has an MD5
|
||||||
|
else if (rom.MD5 != null && rom.MD5 != "")
|
||||||
|
{
|
||||||
|
if (md5.Roms.ContainsKey(key))
|
||||||
|
{
|
||||||
|
md5.Roms[key].Add(rom);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
List<RomData> temp = new List<RomData>();
|
||||||
|
temp.Add(rom);
|
||||||
|
md5.Roms.Add(key, temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// All other cases
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (crc.Roms.ContainsKey(key))
|
||||||
|
{
|
||||||
|
crc.Roms[key].Add(rom);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
List<RomData> temp = new List<RomData>();
|
||||||
|
temp.Add(rom);
|
||||||
|
crc.Roms.Add(key, temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the output directory
|
||||||
|
string outdir = "";
|
||||||
|
if (_outdir != "")
|
||||||
|
{
|
||||||
|
outdir = _outdir + Path.GetDirectoryName(filename).Remove(0, basepath.Length - 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
outdir = Path.GetDirectoryName(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now, output all of the files to the output directory
|
||||||
|
_logger.User("DAT information created, outputting new files");
|
||||||
|
bool success = true;
|
||||||
|
if (nodump.Roms.Count > 0)
|
||||||
|
{
|
||||||
|
success &= Output.WriteDatfile(nodump, outdir, _logger);
|
||||||
|
}
|
||||||
|
if (sha1.Roms.Count > 0)
|
||||||
|
{
|
||||||
|
success &= Output.WriteDatfile(sha1, outdir, _logger);
|
||||||
|
}
|
||||||
|
if (md5.Roms.Count > 0)
|
||||||
|
{
|
||||||
|
success &= Output.WriteDatfile(md5, outdir, _logger);
|
||||||
|
}
|
||||||
|
if (crc.Roms.Count > 0)
|
||||||
|
{
|
||||||
|
success &= Output.WriteDatfile(crc, outdir, _logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Split a DAT by input extensions
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">Name of the file to be split</param>
|
||||||
|
/// <param name="basepath">Parent path for replacement</param>
|
||||||
|
/// <returns>True if split succeeded, false otherwise</returns>
|
||||||
|
private bool SplitByExt(string filename, string basepath)
|
||||||
|
{
|
||||||
|
// Load the current DAT to be processed
|
||||||
|
DatData datdata = new DatData();
|
||||||
|
datdata = RomManipulation.Parse(filename, 0, 0, datdata, _logger);
|
||||||
|
|
||||||
|
// Set all of the appropriate outputs for each of the subsets
|
||||||
|
OutputFormat outputFormat = RomManipulation.GetOutputFormat(filename);
|
||||||
|
DatData datdataA = new DatData
|
||||||
|
{
|
||||||
|
FileName = datdata.FileName + " (" + String.Join(",", _extA) + ")",
|
||||||
|
Name = datdata.Name + " (" + String.Join(",", _extA) + ")",
|
||||||
|
Description = datdata.Description + " (" + String.Join(",", _extA) + ")",
|
||||||
|
Category = datdata.Category,
|
||||||
|
Version = datdata.Version,
|
||||||
|
Date = datdata.Date,
|
||||||
|
Author = datdata.Author,
|
||||||
|
Email = datdata.Email,
|
||||||
|
Homepage = datdata.Homepage,
|
||||||
|
Url = datdata.Url,
|
||||||
|
Comment = datdata.Comment,
|
||||||
|
Roms = new Dictionary<string, List<RomData>>(),
|
||||||
|
OutputFormat = outputFormat,
|
||||||
|
};
|
||||||
|
DatData datdataB = new DatData
|
||||||
|
{
|
||||||
|
FileName = datdata.FileName + " (" + String.Join(",", _extB) + ")",
|
||||||
|
Name = datdata.Name + " (" + String.Join(",", _extB) + ")",
|
||||||
|
Description = datdata.Description + " (" + String.Join(",", _extB) + ")",
|
||||||
|
Category = datdata.Category,
|
||||||
|
Version = datdata.Version,
|
||||||
|
Date = datdata.Date,
|
||||||
|
Author = datdata.Author,
|
||||||
|
Email = datdata.Email,
|
||||||
|
Homepage = datdata.Homepage,
|
||||||
|
Url = datdata.Url,
|
||||||
|
Comment = datdata.Comment,
|
||||||
|
Roms = new Dictionary<string, List<RomData>>(),
|
||||||
|
OutputFormat = outputFormat,
|
||||||
|
};
|
||||||
|
|
||||||
|
// If roms is empty, return false
|
||||||
|
if (datdata.Roms.Count == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now separate the roms accordingly
|
||||||
|
foreach (string key in datdata.Roms.Keys)
|
||||||
|
{
|
||||||
|
foreach (RomData rom in datdata.Roms[key])
|
||||||
|
{
|
||||||
|
if (_extA.Contains(Path.GetExtension(rom.Name.ToUpperInvariant())))
|
||||||
|
{
|
||||||
|
if (datdataA.Roms.ContainsKey(key))
|
||||||
|
{
|
||||||
|
datdataA.Roms[key].Add(rom);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
List<RomData> temp = new List<RomData>();
|
||||||
|
temp.Add(rom);
|
||||||
|
datdataA.Roms.Add(key, temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (_extB.Contains(Path.GetExtension(rom.Name.ToUpperInvariant())))
|
||||||
|
{
|
||||||
|
if (datdataB.Roms.ContainsKey(key))
|
||||||
|
{
|
||||||
|
datdataB.Roms[key].Add(rom);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
List<RomData> temp = new List<RomData>();
|
||||||
|
temp.Add(rom);
|
||||||
|
datdataB.Roms.Add(key, temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (datdataA.Roms.ContainsKey(key))
|
||||||
|
{
|
||||||
|
datdataA.Roms[key].Add(rom);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
List<RomData> temp = new List<RomData>();
|
||||||
|
temp.Add(rom);
|
||||||
|
datdataA.Roms.Add(key, temp);
|
||||||
|
}
|
||||||
|
if (datdataB.Roms.ContainsKey(key))
|
||||||
|
{
|
||||||
|
datdataB.Roms[key].Add(rom);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
List<RomData> temp = new List<RomData>();
|
||||||
|
temp.Add(rom);
|
||||||
|
datdataB.Roms.Add(key, temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the output directory
|
||||||
|
string outdir = "";
|
||||||
|
if (_outdir != "")
|
||||||
|
{
|
||||||
|
outdir = _outdir + Path.GetDirectoryName(filename).Remove(0, basepath.Length - 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
outdir = Path.GetDirectoryName(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then write out both files
|
||||||
|
bool success = Output.WriteDatfile(datdataA, outdir, _logger);
|
||||||
|
success &= Output.WriteDatfile(datdataB, outdir, _logger);
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user