diff --git a/SabreTools.Helper/Data/Build.cs b/SabreTools.Helper/Data/Build.cs index e2f72fe3..76d9ad0e 100644 --- a/SabreTools.Helper/Data/Build.cs +++ b/SabreTools.Helper/Data/Build.cs @@ -125,10 +125,6 @@ Options: -source= Source ID -st, --stats Get statistics on all input DATs -si, --single Show individual statistics - -tm, --trim-merge Consolidate DAT into a single game and trim entries - -rd=, --root-dir= Set the root directory for trimming calculation - -nr, --no-rename Keep game names instead of using '!' - -df, --disable-force Disable forceunzipping -ud, --update Update a DAT file -oc, --output-cmp Output in CMP format -om, --output-miss Output in Missfile format @@ -167,6 +163,9 @@ Options: Supported values are: None, Zip, Unzip -clean Clean game names according to WoD standards + -trim Trim file names to fit NTFS length + -rd=, --root-dir= Set the root directory for calc + -si, --single All game names replaced by '!' -dd, --dedup Enable deduping in the created DAT -m, --merge Merge the input DATs -b, --bare Don't include date in automatic name diff --git a/SabreTools.Helper/Tools/DatTools.cs b/SabreTools.Helper/Tools/DatTools.cs index 6f13c8e3..5a038c2a 100644 --- a/SabreTools.Helper/Tools/DatTools.cs +++ b/SabreTools.Helper/Tools/DatTools.cs @@ -1521,10 +1521,13 @@ namespace SabreTools.Helper /// MD5 of the rom to match (can use asterisk-partials) /// SHA-1 of the rom to match (can use asterisk-partials) /// Select roms with nodump status as follows: null (match all), true (match Nodump only), false (exclude Nodump) + /// True if we are supposed to trim names to NTFS length, false otherwise + /// True if all games should be replaced by '!', false otherwise + /// String representing root directory to compare against for length calculation /// Logging object for console and file output public static void Update(List inputFileNames, Dat datdata, string outputDirectory, bool merge, bool diff, bool cascade, bool inplace, bool bare, bool clean, string gamename, string romname, string romtype, long sgt, long slt, long seq, string crc, string md5, - string sha1, bool? nodump, Logger logger) + string sha1, bool? nodump, bool trim, bool single, string root, Logger logger) { // If we're in merging or diffing mode, use the full list of inputs if (merge || diff) @@ -1535,7 +1538,13 @@ namespace SabreTools.Helper List datHeaders = PopulateUserData(inputFileNames, inplace, clean, outputDirectory, datdata, out userData, logger); // If we want to filter, apply it to the userData now - userData = Filter(userData, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, logger); + userData = Filter(userData, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, logger); + + // If we're trimming, apply it to the userData now + if (trim) + { + + } // Modify the Dictionary if necessary and output the results if (diff && !cascade) @@ -1570,7 +1579,7 @@ namespace SabreTools.Helper { logger.User("Processing \"" + Path.GetFileName(inputFileName) + "\""); datdata = Parse(inputFileName, 0, 0, datdata, logger, true, clean); - datdata = Filter(datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, logger); + datdata = Filter(datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, logger); // If the extension matches, append ".new" to the filename string extension = (datdata.OutputFormat == OutputFormat.Xml || datdata.OutputFormat == OutputFormat.SabreDat ? ".xml" : ".dat"); @@ -1591,7 +1600,7 @@ namespace SabreTools.Helper Dat innerDatdata = (Dat)datdata.Clone(); innerDatdata.Roms = null; innerDatdata = Parse(file, 0, 0, innerDatdata, logger, true, clean); - innerDatdata = Filter(innerDatdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, logger); + innerDatdata = Filter(innerDatdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, logger); // If the extension matches, append ".new" to the filename string extension = (innerDatdata.OutputFormat == OutputFormat.Xml || innerDatdata.OutputFormat == OutputFormat.SabreDat ? ".xml" : ".dat"); @@ -1672,10 +1681,13 @@ namespace SabreTools.Helper /// MD5 of the rom to match (can use asterisk-partials) /// SHA-1 of the rom to match (can use asterisk-partials) /// Select roms with nodump status as follows: null (match all), true (match Nodump only), false (exclude Nodump) + /// True if we are supposed to trim names to NTFS length, false otherwise + /// True if all games should be replaced by '!', false otherwise + /// String representing root directory to compare against for length calculation /// Logging object for console and file output /// Returns filtered DatData object public static Dat Filter(Dat datdata, string gamename, string romname, string romtype, long sgt, - long slt, long seq, string crc, string md5, string sha1, bool? nodump, Logger logger) + long slt, long seq, string crc, string md5, string sha1, bool? nodump, bool trim, bool single, string root, Logger logger) { // Now loop through and create a new Rom dictionary using filtered values Dictionary> dict = new Dictionary>(); @@ -1683,8 +1695,10 @@ namespace SabreTools.Helper foreach (string key in keys) { List roms = datdata.Roms[key]; - foreach (Rom rom in roms) + for (int i = 0; i < roms.Count; i++) { + Rom rom = roms[i]; + // Filter on nodump status if (nodump == true && !rom.Nodump) { @@ -1803,6 +1817,25 @@ namespace SabreTools.Helper } } + // If we are in single game mode, rename all games + if (single) + { + rom.Game = "!"; + } + + // If we are in NTFS trim mode, trim the game name + if (trim) + { + // Windows max name length is 260 + int usableLength = 260 - rom.Game.Length - root.Length; + if (rom.Name.Length > usableLength) + { + string ext = Path.GetExtension(rom.Name); + rom.Name = rom.Name.Substring(0, usableLength - ext.Length); + rom.Name += ext; + } + } + // If it made it this far, add the rom to the output dictionary if (dict.ContainsKey(key)) { diff --git a/SabreTools/Partials/SabreTools_Inits.cs b/SabreTools/Partials/SabreTools_Inits.cs index 53da1685..dc30465e 100644 --- a/SabreTools/Partials/SabreTools_Inits.cs +++ b/SabreTools/Partials/SabreTools_Inits.cs @@ -124,6 +124,10 @@ namespace SabreTools /// MD5 of the rom to match (can use asterisk-partials) /// SHA-1 of the rom to match (can use asterisk-partials) /// Select roms with nodump status as follows: null (match all), true (match Nodump only), false (exclude Nodump) + /// /* Trimming info */ + /// True if we are supposed to trim names to NTFS length, false otherwise + /// True if all games should be replaced by '!', false otherwise + /// String representing root directory to compare against for length calculation /// /* Output DAT info */ /// Optional param for output directory /// True to clean the game names to WoD standard, false otherwise (default) @@ -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 } } - /// - /// Wrap trimming and merging a single DAT - /// - /// Input file or folder to be converted - /// Root directory to base path lengths on - /// True is games should not be renamed - /// True if forcepacking="unzip" should be included - 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; - } - } - /// /// Wrap splitting a DAT by 2 extensions /// diff --git a/SabreTools/SabreTools.cs b/SabreTools/SabreTools.cs index 079a5e60..e8a50b72 100644 --- a/SabreTools/SabreTools.cs +++ b/SabreTools/SabreTools.cs @@ -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) { diff --git a/SabreTools/SabreTools.csproj b/SabreTools/SabreTools.csproj index f9208af4..cfe408cc 100644 --- a/SabreTools/SabreTools.csproj +++ b/SabreTools/SabreTools.csproj @@ -110,7 +110,6 @@ - diff --git a/SabreTools/TrimMerge.cs b/SabreTools/TrimMerge.cs deleted file mode 100644 index 41e92ebf..00000000 --- a/SabreTools/TrimMerge.cs +++ /dev/null @@ -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; - - /// - /// Create a new TrimMerge object - /// - /// Name of the file or folder to be processed - /// Root path to use for trimming - /// True if games should be renamed into a uniform string, false otherwise - /// True if forcepacking="unzip" should be set on the output, false otherwise - /// Logger object for console and file output - public TrimMerge(string filename, string path, bool rename, bool forceunpack, Logger logger) - { - _filename = filename; - _path = path; - _rename = rename; - _forceunpack = forceunpack; - _logger = logger; - } - - /// - /// Trim and process the given DAT or folder of DATs - /// - /// True if the DAT could be updated, false otherwise - 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; - } - - /// - /// Import the existing DAT(s) - /// - /// Name of the file to be processed - /// The base path to be used for comparison - /// True if roms are to be renamed - 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 keys = datdata.Roms.Keys.ToList(); - foreach (string key in keys) - { - List newroms = new List(); - 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()); - } - } - } - } -}