mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[SabreTools] Make Split non-object; add typesplit
This commit is contained in:
@@ -171,6 +171,8 @@ namespace SabreTools.Helper
|
||||
helptext.Add(" -source= Source ID");
|
||||
helptext.Add(" -st, --stats Get statistics on all input DATs");
|
||||
helptext.Add(" -si, --single Show individual statistics");
|
||||
helptext.Add(" -ts, --type-split Split a DAT or folder by file types (rom/disk)");
|
||||
helptext.Add(" -out= Output directory");
|
||||
helptext.Add(" -ud, --update Update a DAT file");
|
||||
helptext.Add(" -oc, --output-cmp Output in CMP format");
|
||||
helptext.Add(" -om, --output-miss Output in Missfile format");
|
||||
|
||||
@@ -1,455 +0,0 @@
|
||||
using SabreTools.Helper;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
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 = inputs;
|
||||
_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;
|
||||
_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 = inputs;
|
||||
_extA = new List<string>();
|
||||
_extB = new List<string>();
|
||||
_outdir = outdir;
|
||||
_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 = DatTools.GetOutputFormat(filename, _logger);
|
||||
Dat datdata = new Dat();
|
||||
datdata = DatTools.Parse(filename, 0, 0, datdata, _logger, true);
|
||||
|
||||
// Create each of the respective output DATs
|
||||
_logger.User("Creating and populating new DATs");
|
||||
Dat nodump = new Dat
|
||||
{
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
};
|
||||
Dat sha1 = new Dat
|
||||
{
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
};
|
||||
Dat md5 = new Dat
|
||||
{
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
};
|
||||
Dat crc = new Dat
|
||||
{
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
};
|
||||
|
||||
// Now populate each of the DAT objects in turn
|
||||
List<string> keys = datdata.Files.Keys.ToList();
|
||||
foreach (string key in keys)
|
||||
{
|
||||
List<Rom> roms = datdata.Files[key];
|
||||
foreach (Rom rom in roms)
|
||||
{
|
||||
// If the file is a nodump
|
||||
if (rom.Nodump)
|
||||
{
|
||||
if (nodump.Files.ContainsKey(key))
|
||||
{
|
||||
nodump.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
nodump.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
// If the file has a SHA-1
|
||||
else if (rom.HashData.SHA1 != null && rom.HashData.SHA1 != "")
|
||||
{
|
||||
if (sha1.Files.ContainsKey(key))
|
||||
{
|
||||
sha1.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
sha1.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
// If the file has no SHA-1 but has an MD5
|
||||
else if (rom.HashData.MD5 != null && rom.HashData.MD5 != "")
|
||||
{
|
||||
if (md5.Files.ContainsKey(key))
|
||||
{
|
||||
md5.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
md5.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
// All other cases
|
||||
else
|
||||
{
|
||||
if (crc.Files.ContainsKey(key))
|
||||
{
|
||||
crc.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
crc.Files.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.Files.Count > 0)
|
||||
{
|
||||
success &= DatTools.WriteDatfile(nodump, outdir, _logger);
|
||||
}
|
||||
if (sha1.Files.Count > 0)
|
||||
{
|
||||
success &= DatTools.WriteDatfile(sha1, outdir, _logger);
|
||||
}
|
||||
if (md5.Files.Count > 0)
|
||||
{
|
||||
success &= DatTools.WriteDatfile(md5, outdir, _logger);
|
||||
}
|
||||
if (crc.Files.Count > 0)
|
||||
{
|
||||
success &= DatTools.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
|
||||
Dat datdata = new Dat();
|
||||
datdata = DatTools.Parse(filename, 0, 0, datdata, _logger);
|
||||
|
||||
// Set all of the appropriate outputs for each of the subsets
|
||||
OutputFormat outputFormat = DatTools.GetOutputFormat(filename, _logger);
|
||||
Dat datdataA = new Dat
|
||||
{
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
OutputFormat = outputFormat,
|
||||
};
|
||||
Dat datdataB = new Dat
|
||||
{
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
OutputFormat = outputFormat,
|
||||
};
|
||||
|
||||
// If roms is empty, return false
|
||||
if (datdata.Files.Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now separate the roms accordingly
|
||||
foreach (string key in datdata.Files.Keys)
|
||||
{
|
||||
foreach (Rom rom in datdata.Files[key])
|
||||
{
|
||||
if (_extA.Contains(Path.GetExtension(rom.Name.ToUpperInvariant())))
|
||||
{
|
||||
if (datdataA.Files.ContainsKey(key))
|
||||
{
|
||||
datdataA.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
datdataA.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
else if (_extB.Contains(Path.GetExtension(rom.Name.ToUpperInvariant())))
|
||||
{
|
||||
if (datdataB.Files.ContainsKey(key))
|
||||
{
|
||||
datdataB.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
datdataB.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (datdataA.Files.ContainsKey(key))
|
||||
{
|
||||
datdataA.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
datdataA.Files.Add(key, temp);
|
||||
}
|
||||
if (datdataB.Files.ContainsKey(key))
|
||||
{
|
||||
datdataB.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
datdataB.Files.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 = DatTools.WriteDatfile(datdataA, outdir, _logger);
|
||||
success &= DatTools.WriteDatfile(datdataB, outdir, _logger);
|
||||
|
||||
return success;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,7 +93,6 @@
|
||||
<Compile Include="Objects\Import.cs" />
|
||||
<Compile Include="Objects\ImportTwo.cs" />
|
||||
<Compile Include="Objects\OfflineMerge.cs" />
|
||||
<Compile Include="Objects\Split.cs" />
|
||||
<Compile Include="Resources\Resources.de-DE.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTime>True</DesignTime>
|
||||
|
||||
@@ -3007,5 +3007,491 @@ namespace SabreTools.Helper
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DAT Splitting
|
||||
|
||||
/// <summary>
|
||||
/// Split a DAT by input extensions
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to be split</param>
|
||||
/// <param name="outdir">Name of the directory to write the DATs out to</param>
|
||||
/// <param name="basepath">Parent path for replacement</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>
|
||||
/// <returns>True if split succeeded, false otherwise</returns>
|
||||
public static bool SplitByExt(string filename, string outdir, string basepath, List<string> extA, List<string> extB, Logger logger)
|
||||
{
|
||||
// Make sure all of the extensions have a dot at the beginning
|
||||
List<string> newExtA = new List<string>();
|
||||
foreach (string s in extA)
|
||||
{
|
||||
newExtA.Add((s.StartsWith(".") ? s : "." + s).ToUpperInvariant());
|
||||
}
|
||||
string newExtAString = string.Join(",", newExtA);
|
||||
|
||||
List<string> newExtB = new List<string>();
|
||||
foreach (string s in extB)
|
||||
{
|
||||
newExtB.Add((s.StartsWith(".") ? s : "." + s).ToUpperInvariant());
|
||||
}
|
||||
string newExtBString = string.Join(",", newExtB);
|
||||
|
||||
// Load the current DAT to be processed
|
||||
Dat datdata = new Dat();
|
||||
datdata = DatTools.Parse(filename, 0, 0, datdata, logger);
|
||||
|
||||
// Set all of the appropriate outputs for each of the subsets
|
||||
OutputFormat outputFormat = DatTools.GetOutputFormat(filename, logger);
|
||||
Dat datdataA = new Dat
|
||||
{
|
||||
FileName = datdata.FileName + " (" + newExtAString + ")",
|
||||
Name = datdata.Name + " (" + newExtAString + ")",
|
||||
Description = datdata.Description + " (" + newExtAString + ")",
|
||||
Category = datdata.Category,
|
||||
Version = datdata.Version,
|
||||
Date = datdata.Date,
|
||||
Author = datdata.Author,
|
||||
Email = datdata.Email,
|
||||
Homepage = datdata.Homepage,
|
||||
Url = datdata.Url,
|
||||
Comment = datdata.Comment,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
OutputFormat = outputFormat,
|
||||
};
|
||||
Dat datdataB = new Dat
|
||||
{
|
||||
FileName = datdata.FileName + " (" + newExtBString + ")",
|
||||
Name = datdata.Name + " (" + newExtBString + ")",
|
||||
Description = datdata.Description + " (" + newExtBString + ")",
|
||||
Category = datdata.Category,
|
||||
Version = datdata.Version,
|
||||
Date = datdata.Date,
|
||||
Author = datdata.Author,
|
||||
Email = datdata.Email,
|
||||
Homepage = datdata.Homepage,
|
||||
Url = datdata.Url,
|
||||
Comment = datdata.Comment,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
OutputFormat = outputFormat,
|
||||
};
|
||||
|
||||
// If roms is empty, return false
|
||||
if (datdata.Files.Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now separate the roms accordingly
|
||||
foreach (string key in datdata.Files.Keys)
|
||||
{
|
||||
foreach (Rom rom in datdata.Files[key])
|
||||
{
|
||||
if (newExtA.Contains(Path.GetExtension(rom.Name.ToUpperInvariant())))
|
||||
{
|
||||
if (datdataA.Files.ContainsKey(key))
|
||||
{
|
||||
datdataA.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
datdataA.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
else if (newExtB.Contains(Path.GetExtension(rom.Name.ToUpperInvariant())))
|
||||
{
|
||||
if (datdataB.Files.ContainsKey(key))
|
||||
{
|
||||
datdataB.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
datdataB.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (datdataA.Files.ContainsKey(key))
|
||||
{
|
||||
datdataA.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
datdataA.Files.Add(key, temp);
|
||||
}
|
||||
if (datdataB.Files.ContainsKey(key))
|
||||
{
|
||||
datdataB.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
datdataB.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the output directory
|
||||
if (outdir != "")
|
||||
{
|
||||
outdir = outdir + Path.GetDirectoryName(filename).Remove(0, basepath.Length - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
outdir = Path.GetDirectoryName(filename);
|
||||
}
|
||||
|
||||
// Then write out both files
|
||||
bool success = DatTools.WriteDatfile(datdataA, outdir, logger);
|
||||
success &= DatTools.WriteDatfile(datdataB, outdir, logger);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Split a DAT by best available hashes
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to be split</param>
|
||||
/// <param name="outdir">Name of the directory to write the DATs out to</param>
|
||||
/// <param name="basepath">Parent path for replacement</param>
|
||||
/// <param name="logger">Logger object for console and file writing</param>
|
||||
/// <returns>True if split succeeded, false otherwise</returns>
|
||||
public static bool SplitByHash(string filename, string outdir, string basepath, Logger logger)
|
||||
{
|
||||
// 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 = DatTools.GetOutputFormat(filename, logger);
|
||||
Dat datdata = new Dat();
|
||||
datdata = DatTools.Parse(filename, 0, 0, datdata, logger, true);
|
||||
|
||||
// Create each of the respective output DATs
|
||||
logger.User("Creating and populating new DATs");
|
||||
Dat nodump = new Dat
|
||||
{
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
};
|
||||
Dat sha1 = new Dat
|
||||
{
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
};
|
||||
Dat md5 = new Dat
|
||||
{
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
};
|
||||
Dat crc = new Dat
|
||||
{
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
};
|
||||
|
||||
// Now populate each of the DAT objects in turn
|
||||
List<string> keys = datdata.Files.Keys.ToList();
|
||||
foreach (string key in keys)
|
||||
{
|
||||
List<Rom> roms = datdata.Files[key];
|
||||
foreach (Rom rom in roms)
|
||||
{
|
||||
// If the file is a nodump
|
||||
if (rom.Nodump)
|
||||
{
|
||||
if (nodump.Files.ContainsKey(key))
|
||||
{
|
||||
nodump.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
nodump.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
// If the file has a SHA-1
|
||||
else if (rom.HashData.SHA1 != null && rom.HashData.SHA1 != "")
|
||||
{
|
||||
if (sha1.Files.ContainsKey(key))
|
||||
{
|
||||
sha1.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
sha1.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
// If the file has no SHA-1 but has an MD5
|
||||
else if (rom.HashData.MD5 != null && rom.HashData.MD5 != "")
|
||||
{
|
||||
if (md5.Files.ContainsKey(key))
|
||||
{
|
||||
md5.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
md5.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
// All other cases
|
||||
else
|
||||
{
|
||||
if (crc.Files.ContainsKey(key))
|
||||
{
|
||||
crc.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
crc.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the output directory
|
||||
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.Files.Count > 0)
|
||||
{
|
||||
success &= DatTools.WriteDatfile(nodump, outdir, logger);
|
||||
}
|
||||
if (sha1.Files.Count > 0)
|
||||
{
|
||||
success &= DatTools.WriteDatfile(sha1, outdir, logger);
|
||||
}
|
||||
if (md5.Files.Count > 0)
|
||||
{
|
||||
success &= DatTools.WriteDatfile(md5, outdir, logger);
|
||||
}
|
||||
if (crc.Files.Count > 0)
|
||||
{
|
||||
success &= DatTools.WriteDatfile(crc, outdir, logger);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Split a DAT by type of Rom
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to be split</param>
|
||||
/// <param name="outdir">Name of the directory to write the DATs out to</param>
|
||||
/// <param name="basepath">Parent path for replacement</param>
|
||||
/// <param name="logger">Logger object for console and file writing</param>
|
||||
/// <returns>True if split succeeded, false otherwise</returns>
|
||||
public static bool SplitByType(string filename, string outdir, string basepath, Logger logger)
|
||||
{
|
||||
// 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 = DatTools.GetOutputFormat(filename, logger);
|
||||
Dat datdata = new Dat();
|
||||
datdata = DatTools.Parse(filename, 0, 0, datdata, logger, true);
|
||||
|
||||
// Create each of the respective output DATs
|
||||
logger.User("Creating and populating new DATs");
|
||||
Dat romdat = new Dat
|
||||
{
|
||||
FileName = datdata.FileName + " (Rom)",
|
||||
Name = datdata.Name + " (Rom)",
|
||||
Description = datdata.Description + " (Rom)",
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
};
|
||||
Dat diskdat = new Dat
|
||||
{
|
||||
FileName = datdata.FileName + " (Disk)",
|
||||
Name = datdata.Name + " (Disk)",
|
||||
Description = datdata.Description + " (Disk)",
|
||||
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,
|
||||
Files = new Dictionary<string, List<Rom>>(),
|
||||
};
|
||||
|
||||
// Now populate each of the DAT objects in turn
|
||||
List<string> keys = datdata.Files.Keys.ToList();
|
||||
foreach (string key in keys)
|
||||
{
|
||||
List<Rom> roms = datdata.Files[key];
|
||||
foreach (Rom rom in roms)
|
||||
{
|
||||
// If the file is a Rom
|
||||
if (rom.Type == ItemType.Rom)
|
||||
{
|
||||
if (romdat.Files.ContainsKey(key))
|
||||
{
|
||||
romdat.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
romdat.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
// If the file is a Disk
|
||||
else if (rom.Type == ItemType.Disk)
|
||||
{
|
||||
if (diskdat.Files.ContainsKey(key))
|
||||
{
|
||||
diskdat.Files[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Rom> temp = new List<Rom>();
|
||||
temp.Add(rom);
|
||||
diskdat.Files.Add(key, temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the output directory
|
||||
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 (romdat.Files.Count > 0)
|
||||
{
|
||||
success &= DatTools.WriteDatfile(romdat, outdir, logger);
|
||||
}
|
||||
if (diskdat.Files.Count > 0)
|
||||
{
|
||||
success &= DatTools.WriteDatfile(diskdat, outdir, logger);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -507,10 +507,25 @@ namespace SabreTools
|
||||
/// <param name="outdir">Output directory for the split files</param>
|
||||
private static void InitExtSplit(List<string> inputs, string exta, string extb, string outdir)
|
||||
{
|
||||
// Verify the input files
|
||||
// Convert comma-separated strings to list
|
||||
List<string> extaList = exta.Split(',').ToList();
|
||||
List<string> extbList = extb.Split(',').ToList();
|
||||
|
||||
// Loop over the input files
|
||||
foreach (string input in inputs)
|
||||
{
|
||||
if (!File.Exists(input) && !Directory.Exists(input))
|
||||
if (File.Exists(input))
|
||||
{
|
||||
DatTools.SplitByExt(Path.GetFullPath(input), outdir, Path.GetDirectoryName(input), extaList, extbList, _logger);
|
||||
}
|
||||
else if (Directory.Exists(input))
|
||||
{
|
||||
foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
DatTools.SplitByExt(input, outdir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), extaList, extbList, _logger);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Error(input + " is not a valid file or folder!");
|
||||
Console.WriteLine();
|
||||
@@ -518,14 +533,6 @@ namespace SabreTools
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert comma-separated strings to list
|
||||
List<string> extaList = exta.Split(',').ToList();
|
||||
List<string> extbList = extb.Split(',').ToList();
|
||||
|
||||
Split es = new Split(inputs, extaList, extbList, outdir, _logger);
|
||||
es.Process();
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -535,10 +542,21 @@ namespace SabreTools
|
||||
/// <param name="outdir">Output directory for the split files</param>
|
||||
private static void InitHashSplit(List<string> inputs, string outdir)
|
||||
{
|
||||
// Verify the input files
|
||||
// Loop over the input files
|
||||
foreach (string input in inputs)
|
||||
{
|
||||
if (!File.Exists(input) && !Directory.Exists(input))
|
||||
if (File.Exists(input))
|
||||
{
|
||||
DatTools.SplitByHash(Path.GetFullPath(input), outdir, Path.GetDirectoryName(input), _logger);
|
||||
}
|
||||
else if (Directory.Exists(input))
|
||||
{
|
||||
foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
DatTools.SplitByHash(input, outdir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), _logger);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Error(input + " is not a valid file or folder!");
|
||||
Console.WriteLine();
|
||||
@@ -546,10 +564,37 @@ namespace SabreTools
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If so, run the program
|
||||
Split hs = new Split(inputs, outdir, _logger);
|
||||
hs.Process();
|
||||
/// <summary>
|
||||
/// Wrap splitting a DAT by item type
|
||||
/// </summary>
|
||||
/// <param name="inputs">List of inputs to be used</param>
|
||||
/// <param name="outdir">Output directory for the split files</param>
|
||||
private static void InitTypeSplit(List<string> inputs, string outdir)
|
||||
{
|
||||
// Loop over the input files
|
||||
foreach (string input in inputs)
|
||||
{
|
||||
if (File.Exists(input))
|
||||
{
|
||||
DatTools.SplitByType(Path.GetFullPath(input), outdir, Path.GetDirectoryName(input), _logger);
|
||||
}
|
||||
else if (Directory.Exists(input))
|
||||
{
|
||||
foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
DatTools.SplitByType(input, outdir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), _logger);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Error(input + " is not a valid file or folder!");
|
||||
Console.WriteLine();
|
||||
Build.Help();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -118,6 +118,7 @@ namespace SabreTools
|
||||
stats = false,
|
||||
superdat = false,
|
||||
trim = false,
|
||||
typesplit = false,
|
||||
skip = false,
|
||||
update = false,
|
||||
usegame = true;
|
||||
@@ -394,6 +395,10 @@ namespace SabreTools
|
||||
case "-trim":
|
||||
trim = true;
|
||||
break;
|
||||
case "-ts":
|
||||
case "--type-split":
|
||||
typesplit = true;
|
||||
break;
|
||||
case "-tsv":
|
||||
case "--tsv":
|
||||
tsv = true;
|
||||
@@ -606,7 +611,7 @@ namespace SabreTools
|
||||
// If more than one switch is enabled, show the help screen
|
||||
if (!(add ^ datfromdir ^ datfromdirparallel ^ extsplit ^ generate ^ genall ^ hashsplit ^ import ^ listsrc ^ listsys ^
|
||||
(merge || diff != 0 || update || outputCMP || outputRC || outputSD || outputXML || outputMiss || tsv != null|| trim) ^
|
||||
offlineMerge ^ rem ^ stats))
|
||||
offlineMerge ^ rem ^ stats ^ typesplit))
|
||||
{
|
||||
_logger.Error("Only one feature switch is allowed at a time");
|
||||
Build.Help();
|
||||
@@ -616,7 +621,8 @@ namespace SabreTools
|
||||
|
||||
// If a switch that requires a filename is set and no file is, show the help screen
|
||||
if (inputs.Count == 0 && (update || (outputMiss || tsv != null) || outputCMP || outputRC || outputSD
|
||||
|| outputXML || extsplit || hashsplit || datfromdir || datfromdirparallel || (merge || diff != 0) || stats || trim))
|
||||
|| outputXML || extsplit || hashsplit || datfromdir || datfromdirparallel || (merge || diff != 0)
|
||||
|| stats || trim || typesplit))
|
||||
{
|
||||
_logger.Error("This feature requires at least one input");
|
||||
Build.Help();
|
||||
@@ -713,6 +719,12 @@ namespace SabreTools
|
||||
InitHashSplit(inputs, outdir);
|
||||
}
|
||||
|
||||
// Split a DAT by item type
|
||||
else if (typesplit)
|
||||
{
|
||||
InitTypeSplit(inputs, outdir);
|
||||
}
|
||||
|
||||
// Get statistics on input files
|
||||
else if (stats)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user