mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[SabreTools] Merge TrimMerge into Update
This commit is contained in:
@@ -124,6 +124,10 @@ namespace SabreTools
|
||||
/// <param name="md5">MD5 of the rom to match (can use asterisk-partials)</param>
|
||||
/// <param name="sha1">SHA-1 of the rom to match (can use asterisk-partials)</param>
|
||||
/// <param name="nodump">Select roms with nodump status as follows: null (match all), true (match Nodump only), false (exclude Nodump)</param>
|
||||
/// /* Trimming info */
|
||||
/// <param name="trim">True if we are supposed to trim names to NTFS length, false otherwise</param>
|
||||
/// <param name="single">True if all games should be replaced by '!', false otherwise</param>
|
||||
/// <param name="root">String representing root directory to compare against for length calculation</param>
|
||||
/// /* Output DAT info */
|
||||
/// <param name="outdir">Optional param for output directory</param>
|
||||
/// <param name="clean">True to clean the game names to WoD standard, false otherwise (default)</param>
|
||||
@@ -182,6 +186,11 @@ namespace SabreTools
|
||||
string sha1,
|
||||
bool? nodump,
|
||||
|
||||
/* Trimming info */
|
||||
bool trim,
|
||||
bool single,
|
||||
string root,
|
||||
|
||||
/* Output DAT info */
|
||||
string outdir,
|
||||
bool clean,
|
||||
@@ -302,36 +311,36 @@ namespace SabreTools
|
||||
{
|
||||
userInputDat.OutputFormat = OutputFormat.ClrMamePro;
|
||||
DatTools.Update(inputs, userInputDat, outdir, merge, diff, cascade, inplace, bare, clean,
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, _logger);
|
||||
}
|
||||
if (outputMiss)
|
||||
{
|
||||
userInputDat.OutputFormat = OutputFormat.MissFile;
|
||||
DatTools.Update(inputs, userInputDat, outdir, merge, diff, cascade, inplace, bare, clean,
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, _logger);
|
||||
}
|
||||
if (outputRC)
|
||||
{
|
||||
userInputDat.OutputFormat = OutputFormat.RomCenter;
|
||||
DatTools.Update(inputs, userInputDat, outdir, merge, diff, cascade, inplace, bare, clean,
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, _logger);
|
||||
}
|
||||
if (outputSD)
|
||||
{
|
||||
userInputDat.OutputFormat = OutputFormat.SabreDat;
|
||||
DatTools.Update(inputs, userInputDat, outdir, merge, diff, cascade, inplace, bare, clean,
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, _logger);
|
||||
}
|
||||
if (outputXML)
|
||||
{
|
||||
userInputDat.OutputFormat = OutputFormat.Xml;
|
||||
DatTools.Update(inputs, userInputDat, outdir, merge, diff, cascade, inplace, bare, clean,
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, _logger);
|
||||
}
|
||||
if (!outputCMP && !outputMiss && !outputRC && !outputSD && !outputXML)
|
||||
{
|
||||
DatTools.Update(inputs, userInputDat, outdir, merge, diff, cascade, inplace, bare, clean,
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
|
||||
gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, _logger);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,23 +409,6 @@ namespace SabreTools
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wrap trimming and merging a single DAT
|
||||
/// </summary>
|
||||
/// <param name="input">Input file or folder to be converted</param>
|
||||
/// <param name="root">Root directory to base path lengths on</param>
|
||||
/// <param name="rename">True is games should not be renamed</param>
|
||||
/// <param name="force">True if forcepacking="unzip" should be included</param>
|
||||
private static void InitTrimMerge(string input, string root, bool rename, bool force)
|
||||
{
|
||||
if (input != "" && (File.Exists(input) || Directory.Exists(input)))
|
||||
{
|
||||
TrimMerge sg = new TrimMerge(input, root, rename, force, _logger);
|
||||
sg.Process();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wrap splitting a DAT by 2 extensions
|
||||
/// </summary>
|
||||
|
||||
@@ -361,8 +361,7 @@ namespace SabreTools
|
||||
case "--skip":
|
||||
skip = true;
|
||||
break;
|
||||
case "-tm":
|
||||
case "--trim-merge":
|
||||
case "-trim":
|
||||
trim = true;
|
||||
break;
|
||||
case "-tsv":
|
||||
@@ -585,8 +584,8 @@ namespace SabreTools
|
||||
|
||||
// If more than one switch is enabled, show the help screen
|
||||
if (!(add ^ datfromdir ^ extsplit ^ generate ^ genall ^ hashsplit ^ import ^ listsrc ^ listsys ^
|
||||
(merge || diff) ^ (update || outputCMP || outputRC || outputSD || outputXML || outputMiss) ^
|
||||
offlineMerge ^ rem ^ stats ^ trim))
|
||||
(merge || diff || update || outputCMP || outputRC || outputSD || outputXML || outputMiss || trim) ^
|
||||
offlineMerge ^ rem ^ stats))
|
||||
{
|
||||
_logger.Error("Only one feature switch is allowed at a time");
|
||||
Build.Help();
|
||||
@@ -644,7 +643,7 @@ namespace SabreTools
|
||||
InitUpdate(inputs, filename, name, description, category, version, date, author, email, homepage, url, comment, header,
|
||||
superdat, forcemerge, forcend, forcepack, outputCMP, outputMiss, outputRC, outputSD, outputXML, usegame, prefix,
|
||||
postfix, quotes, repext, addext, datprefix, romba, tsv, merge, diff, cascade, inplace, bare, gamename, romname,
|
||||
romtype, sgt, slt, seq, crc, md5, sha1, nodump, outdir, clean, dedup);
|
||||
romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, outdir, clean, dedup);
|
||||
}
|
||||
|
||||
// Add a source or system
|
||||
@@ -681,15 +680,6 @@ namespace SabreTools
|
||||
}
|
||||
}
|
||||
|
||||
// Consolodate and trim DAT
|
||||
else if (trim)
|
||||
{
|
||||
foreach (string input in inputs)
|
||||
{
|
||||
InitTrimMerge(input, root, !norename, !disableForce);
|
||||
}
|
||||
}
|
||||
|
||||
// Split a DAT by extension
|
||||
else if (extsplit)
|
||||
{
|
||||
|
||||
@@ -110,7 +110,6 @@
|
||||
<Compile Include="Partials\SabreTools_Inits.cs" />
|
||||
<Compile Include="Partials\SabreTools_Menus.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TrimMerge.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
using SabreTools.Helper;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace SabreTools
|
||||
{
|
||||
public class TrimMerge
|
||||
{
|
||||
// Instance variables
|
||||
private string _filename;
|
||||
private string _path;
|
||||
private bool _rename;
|
||||
private bool _forceunpack;
|
||||
private Logger _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new TrimMerge object
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file or folder to be processed</param>
|
||||
/// <param name="path">Root path to use for trimming</param>
|
||||
/// <param name="rename">True if games should be renamed into a uniform string, false otherwise</param>
|
||||
/// <param name="forceunpack">True if forcepacking="unzip" should be set on the output, false otherwise</param>
|
||||
/// <param name="logger">Logger object for console and file output</param>
|
||||
public TrimMerge(string filename, string path, bool rename, bool forceunpack, Logger logger)
|
||||
{
|
||||
_filename = filename;
|
||||
_path = path;
|
||||
_rename = rename;
|
||||
_forceunpack = forceunpack;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trim and process the given DAT or folder of DATs
|
||||
/// </summary>
|
||||
/// <returns>True if the DAT could be updated, false otherwise</returns>
|
||||
public bool Process()
|
||||
{
|
||||
// If file doesn't exist, error and return
|
||||
if (!File.Exists(_filename) && !Directory.Exists(_filename))
|
||||
{
|
||||
_logger.Error("File or folder '" + _filename + "' doesn't exist");
|
||||
return false;
|
||||
}
|
||||
|
||||
// We want the full path of the file, just in case
|
||||
_filename = Path.GetFullPath(_filename);
|
||||
|
||||
// If it's a single file, handle it as such
|
||||
if (!Directory.Exists(_filename) && File.Exists(_filename))
|
||||
{
|
||||
_logger.Log("File found: " + _filename);
|
||||
ProcessDAT(_filename, _path, _rename);
|
||||
}
|
||||
// If it's a directory, loop through the files and see if any are DATs
|
||||
else if (Directory.Exists(_filename))
|
||||
{
|
||||
// Make sure the path ends with the proper character
|
||||
if (!_filename.EndsWith(Path.DirectorySeparatorChar.ToString()))
|
||||
{
|
||||
_filename += Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
_logger.Log("Directory found: " + _filename);
|
||||
foreach (string file in Directory.EnumerateFiles(_filename, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
_logger.Log("File found: " + file);
|
||||
ProcessDAT(file, _path, _rename);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Import the existing DAT(s)
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to be processed</param>
|
||||
/// <param name="path">The base path to be used for comparison</param>
|
||||
/// <param name="rename">True if roms are to be renamed</param>
|
||||
private void ProcessDAT(string filename, string path, bool rename)
|
||||
{
|
||||
Dat datdata = new Dat
|
||||
{
|
||||
ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None),
|
||||
OutputFormat = DatTools.GetOutputFormat(filename),
|
||||
};
|
||||
datdata = DatTools.Parse(filename, 0, 0, datdata, _logger);
|
||||
|
||||
// Trim all file names according to the path that's set
|
||||
List<string> keys = datdata.Roms.Keys.ToList();
|
||||
foreach (string key in keys)
|
||||
{
|
||||
List<Rom> newroms = new List<Rom>();
|
||||
foreach (Rom rom in datdata.Roms[key])
|
||||
{
|
||||
Rom newrom = rom;
|
||||
|
||||
// If we are in single game mode, rename all games
|
||||
if (rename)
|
||||
{
|
||||
newrom.Game = "!";
|
||||
}
|
||||
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - newrom.Game.Length - _path.Length;
|
||||
if (newrom.Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(newrom.Name);
|
||||
newrom.Name = newrom.Name.Substring(0, usableLength - ext.Length);
|
||||
newrom.Name += ext;
|
||||
}
|
||||
|
||||
newroms.Add(newrom);
|
||||
}
|
||||
datdata.Roms[key] = newroms;
|
||||
}
|
||||
|
||||
// Now write the file out accordingly
|
||||
Output.WriteDatfile(datdata, Path.GetDirectoryName(filename), _logger);
|
||||
|
||||
// Remove the original file if different and inform the user
|
||||
if (filename != Style.CreateOutfileName(" ", datdata).Remove(0, 1))
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(filename);
|
||||
_logger.Log("Original file \"" + filename + "\" deleted");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user