diff --git a/RombaSharp/RombaSharp.Inits.cs b/RombaSharp/RombaSharp.Inits.cs index d02128e1..56c47a55 100644 --- a/RombaSharp/RombaSharp.Inits.cs +++ b/RombaSharp/RombaSharp.Inits.cs @@ -255,7 +255,7 @@ namespace RombaSharp datdata.PopulateFromDir(input, Hash.DeepHashes /* omitFromScan */, true /* bare */, false /* archivesAsFiles */, SkipFileType.None, false /* addBlanks */, false /* addDate */, _tmpdir /* tempDir */, false /* copyFiles */, null /* headerToCheckAgainst */, true /* chdsAsFiles */); - datdata.WriteToFile(""); + datdata.WriteToFile(); } } diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index a02b100c..89729802 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -1625,9 +1625,6 @@ namespace SabreTools.Library.DatFiles public void DetermineUpdateType(List inputPaths, List basePaths, string outDir, bool merge, UpdateMode updateMode, bool inplace, bool skip, bool bare, bool clean, bool remUnicode, bool descAsName, Filter filter, SplitType splitType, bool trim, bool single, string root) { - // First, we want to ensure the output directory - outDir = Utilities.EnsureOutputDirectory(outDir); - // If we're in merging or diffing mode, use the full list of inputs if (merge || (updateMode != UpdateMode.None && (updateMode & UpdateMode.DiffAgainst) == 0) @@ -1833,19 +1830,7 @@ namespace SabreTools.Library.DatFiles }); // Determine the output path for the DAT - string interOutDir = Utilities.EnsureOutputDirectory(outDir); - if (inplace) - { - interOutDir = Path.GetDirectoryName(splitpath[1]); - } - else if (splitpath[0].Length == splitpath[1].Length) - { - interOutDir = Path.GetDirectoryName(Path.Combine(interOutDir, Path.GetFileName(splitpath[0]))); - } - else - { - interOutDir = Path.GetDirectoryName(Path.Combine(interOutDir, splitpath[0].Remove(0, splitpath[1].Length + 1))); - } + string interOutDir = Utilities.GetOutputPath(outDir, path, inplace, splitpath: true); // Once we're done, try writing out intDat.WriteToFile(interOutDir); @@ -1923,19 +1908,7 @@ namespace SabreTools.Library.DatFiles }); // Determine the output path for the DAT - string interOutDir = Utilities.EnsureOutputDirectory(outDir); - if (inplace) - { - interOutDir = Path.GetDirectoryName(splitpath[1]); - } - else if (splitpath[0].Length == splitpath[1].Length) - { - interOutDir = Path.GetDirectoryName(Path.Combine(interOutDir, Path.GetFileName(splitpath[0]))); - } - else - { - interOutDir = Path.GetDirectoryName(Path.Combine(interOutDir, splitpath[0].Remove(0, splitpath[1].Length + 1))); - } + string interOutDir = Utilities.GetOutputPath(outDir, path, inplace, splitpath: true); // Once we're done, try writing out intDat.WriteToFile(interOutDir); @@ -2024,19 +1997,7 @@ namespace SabreTools.Library.DatFiles Parallel.For((skip ? 1 : 0), inputs.Count, Globals.ParallelOptions, j => { - // If we have an output directory set, replace the path - string path = ""; - if (inplace) - { - path = Path.GetDirectoryName(inputs[j].Split('¬')[0]); - } - else if (outDir != Environment.CurrentDirectory) - { - string[] split = inputs[j].Split('¬'); - path = outDir + (split[0] == split[1] - ? Path.GetFileName(split[0]) - : (Path.GetDirectoryName(split[0]).Remove(0, split[1].Length))); ; - } + string path = Utilities.GetOutputPath(outDir, inputs[j], inplace, splitpath: true); // Try to output the file outDats[j].WriteToFile(path); @@ -2195,12 +2156,7 @@ namespace SabreTools.Library.DatFiles { Parallel.For(0, inputs.Count, Globals.ParallelOptions, j => { - // If we have an output directory set, replace the path - string[] split = inputs[j].Split('¬'); - string path = Path.Combine(outDir, - (split[0] == split[1] - ? Path.GetFileName(split[0]) - : (Path.GetDirectoryName(split[0]).Remove(0, split[1].Length)))); + string path = Utilities.GetOutputPath(outDir, inputs[j], false /* inplace */, splitpath: true); // Try to output the file outDats[j].WriteToFile(path); @@ -2274,60 +2230,53 @@ namespace SabreTools.Library.DatFiles for (int i = 0; i < inputFileNames.Count; i++) { // Get the input file name - string inputFileName = inputFileNames[i]; + string inputPath = inputFileNames[i]; // Clean the input string - if (inputFileName != "") + if (inputPath != "") { - inputFileName = Path.GetFullPath(inputFileName); + inputPath = Path.GetFullPath(inputPath); } - if (File.Exists(inputFileName)) + if (File.Exists(inputPath)) { - // If inplace is set, override the output dir - string realOutDir = outDir; - if (inplace) - { - realOutDir = Path.GetDirectoryName(inputFileName); - } - DatFile innerDatdata = new DatFile(this); - Globals.Logger.User("Processing '{0}'", Path.GetFileName(inputFileName)); - innerDatdata.Parse(inputFileName, 0, 0, splitType, keep: true, clean: clean, remUnicode: remUnicode, descAsName: descAsName, + Globals.Logger.User("Processing '{0}'", Path.GetFileName(inputPath)); + innerDatdata.Parse(inputPath, 0, 0, splitType, keep: true, clean: clean, remUnicode: remUnicode, descAsName: descAsName, keepext: ((innerDatdata.DatFormat & DatFormat.TSV) != 0 || (innerDatdata.DatFormat & DatFormat.CSV) != 0)); innerDatdata.Filter(filter, trim, single, root); - // Try to output the file - innerDatdata.WriteToFile((realOutDir == Environment.CurrentDirectory ? Path.GetDirectoryName(inputFileName) : realOutDir), overwrite: (realOutDir != Environment.CurrentDirectory)); + // Get the correct output path + string realOutDir = Utilities.GetOutputPath(outDir, inputPath, inplace, splitpath: false); + + // Try to output the file, overwriting only if it's not in the current directory + // TODO: Figure out if overwriting should always happen of if there should be a user flag + innerDatdata.WriteToFile(realOutDir, overwrite: (realOutDir != Environment.CurrentDirectory)); } - else if (Directory.Exists(inputFileName)) + else if (Directory.Exists(inputPath)) { - inputFileName = Path.GetFullPath(inputFileName) + Path.DirectorySeparatorChar; + inputPath = Path.GetFullPath(inputPath) + Path.DirectorySeparatorChar; - // If inplace is set, override the output dir - string realOutDir = outDir; - if (inplace) - { - realOutDir = Path.GetDirectoryName(inputFileName); - } - - List subFiles = Directory.EnumerateFiles(inputFileName, "*", SearchOption.AllDirectories).ToList(); + List subFiles = Directory.EnumerateFiles(inputPath, "*", SearchOption.AllDirectories).ToList(); Parallel.ForEach(subFiles, Globals.ParallelOptions, file => { - Globals.Logger.User("Processing '{0}'", Path.GetFullPath(file).Remove(0, inputFileName.Length)); + Globals.Logger.User("Processing '{0}'", Path.GetFullPath(file).Remove(0, inputPath.Length)); DatFile innerDatdata = new DatFile(this); innerDatdata.Parse(file, 0, 0, splitType, keep: true, clean: clean, remUnicode: remUnicode, descAsName: descAsName, keepext: ((innerDatdata.DatFormat & DatFormat.TSV) != 0 || (innerDatdata.DatFormat & DatFormat.CSV) != 0)); innerDatdata.Filter(filter, trim, single, root); - // Try to output the file - innerDatdata.WriteToFile((realOutDir == Environment.CurrentDirectory ? Path.GetDirectoryName(file) : realOutDir + Path.GetDirectoryName(file).Remove(0, inputFileName.Length - 1)), - overwrite: (realOutDir != Environment.CurrentDirectory)); + // Get the correct output path + string realOutDir = Utilities.GetOutputPath(outDir, file, inplace, splitpath: false); + + // Try to output the file, overwriting only if it's not in the current directory + // TODO: Figure out if overwriting should always happen of if there should be a user flag + innerDatdata.WriteToFile(realOutDir, overwrite: (realOutDir != Environment.CurrentDirectory)); }); } else { - Globals.Logger.Error("I'm sorry but '{0}' doesn't exist!", inputFileName); + Globals.Logger.Error("I'm sorry but '{0}' doesn't exist!", inputPath); return; } } @@ -4692,7 +4641,7 @@ namespace SabreTools.Library.DatFiles FileName = "fixDAT_" + FileName; Name = "fixDAT_" + Name; Description = "fixDAT_" + Description; - WriteToFile(null); + WriteToFile(); return success; } @@ -4768,7 +4717,7 @@ namespace SabreTools.Library.DatFiles } // Now output the fixdat to the main folder - success &= matched.WriteToFile("", stats: true); + success &= matched.WriteToFile(stats: true); return success; } @@ -4781,11 +4730,11 @@ namespace SabreTools.Library.DatFiles /// Split a DAT by input extensions /// /// Name of the directory to write the DATs out to - /// Parent path for replacement + /// True if files should be written to the source folders, false otherwise /// List of extensions to split on (first DAT) /// List of extensions to split on (second DAT) /// True if split succeeded, false otherwise - public bool SplitByExtension(string outDir, string basepath, List extA, List extB) + public bool SplitByExtension(string outDir, bool inplace, List extA, List extB) { // Make sure all of the extensions have a dot at the beginning List newExtA = new List(); @@ -4864,14 +4813,7 @@ namespace SabreTools.Library.DatFiles }); // Get the output directory - if (outDir != "") - { - outDir = outDir + Path.GetDirectoryName(this.FileName).Remove(0, basepath.Length - 1); - } - else - { - outDir = Path.GetDirectoryName(this.FileName); - } + outDir = Utilities.GetOutputPath(outDir, FileName, inplace); // Then write out both files bool success = datdataA.WriteToFile(outDir); @@ -4884,13 +4826,10 @@ namespace SabreTools.Library.DatFiles /// Split a DAT by best available hashes /// /// Name of the directory to write the DATs out to - /// Parent path for replacement + /// True if files should be written to the source folders, false otherwise /// True if split succeeded, false otherwise - public bool SplitByHash(string outDir, string basepath) + public bool SplitByHash(string outDir, bool inplace) { - // Sanitize the basepath to be more predictable - basepath = (basepath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? basepath : basepath + Path.DirectorySeparatorChar); - // Create each of the respective output DATs Globals.Logger.User("Creating and populating new DATs"); DatFile nodump = new DatFile @@ -5125,14 +5064,7 @@ namespace SabreTools.Library.DatFiles }); // Get the output directory - if (outDir != "") - { - outDir = outDir + Path.GetDirectoryName(this.FileName).Remove(0, basepath.Length - 1); - } - else - { - outDir = Path.GetDirectoryName(this.FileName); - } + outDir = Utilities.GetOutputPath(outDir, FileName, inplace); // Now, output all of the files to the output directory Globals.Logger.User("DAT information created, outputting new files"); @@ -5152,15 +5084,12 @@ namespace SabreTools.Library.DatFiles /// Split a SuperDAT by lowest available directory level /// /// Name of the directory to write the DATs out to - /// Parent path for replacement + /// True if files should be written to the source folders, false otherwise /// True if short names should be used, false otherwise /// True if original filenames should be used as the base for output filename, false otherwise /// True if split succeeded, false otherwise - public bool SplitByLevel(string outDir, string basepath, bool shortname, bool basedat) + public bool SplitByLevel(string outDir, bool inplace, bool shortname, bool basedat) { - // Sanitize the basepath to be more predictable - basepath = (basepath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? basepath : basepath + Path.DirectorySeparatorChar); - // First, organize by games so that we can do the right thing BucketBy(SortedBy.Game, DedupeType.None, lower: false, norename: true); @@ -5181,7 +5110,7 @@ namespace SabreTools.Library.DatFiles if (tempDat.Name != null && tempDat.Name != Path.GetDirectoryName(key)) { // Process and output the DAT - SplitByLevelHelper(tempDat, outDir, shortname, basedat); + SplitByLevelHelper(tempDat, outDir, shortname, basedat, inplace); // Reset the DAT for the next items tempDat = new DatFile(this) @@ -5203,7 +5132,7 @@ namespace SabreTools.Library.DatFiles }); // Then we write the last DAT out since it would be skipped otherwise - SplitByLevelHelper(tempDat, outDir, shortname, basedat); + SplitByLevelHelper(tempDat, outDir, shortname, basedat, inplace); return true; } @@ -5234,16 +5163,15 @@ namespace SabreTools.Library.DatFiles /// Directory to write out to /// True if short naming scheme should be used, false otherwise /// True if original filenames should be used as the base for output filename, false otherwise - private void SplitByLevelHelper(DatFile datFile, string outDir, bool shortname, bool restore) + /// True if files should be written to the source folders, false otherwise + private void SplitByLevelHelper(DatFile datFile, string outDir, bool shortname, bool restore, bool inplace) { // Get the name from the DAT to use separately string name = datFile.Name; string expName = name.Replace("/", " - ").Replace("\\", " - "); - // Get the path that the file will be written out to - string path = HttpUtility.HtmlDecode(String.IsNullOrWhiteSpace(name) - ? outDir - : Path.Combine(outDir, name)); + // Get the output directory + outDir = Utilities.GetOutputPath(outDir, FileName, inplace); // Now set the new output values datFile.FileName = HttpUtility.HtmlDecode(String.IsNullOrWhiteSpace(name) @@ -5259,20 +5187,17 @@ namespace SabreTools.Library.DatFiles datFile.Type = null; // Write out the temporary DAT to the proper directory - datFile.WriteToFile(path); + datFile.WriteToFile(outDir); } /// /// Split a DAT by type of Rom /// /// Name of the directory to write the DATs out to - /// Parent path for replacement + /// True if files should be written to the source folders, false otherwise /// True if split succeeded, false otherwise - public bool SplitByType(string outDir, string basepath) + public bool SplitByType(string outDir, bool inplace) { - // Sanitize the basepath to be more predictable - basepath = (basepath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? basepath : basepath + Path.DirectorySeparatorChar); - // Create each of the respective output DATs Globals.Logger.User("Creating and populating new DATs"); DatFile romdat = new DatFile @@ -5365,14 +5290,7 @@ namespace SabreTools.Library.DatFiles }); // Get the output directory - if (outDir != "") - { - outDir = outDir + Path.GetDirectoryName(this.FileName).Remove(0, basepath.Length - 1); - } - else - { - outDir = Path.GetDirectoryName(this.FileName); - } + outDir = Utilities.GetOutputPath(outDir, FileName, inplace); // Now, output all of the files to the output directory Globals.Logger.User("DAT information created, outputting new files"); @@ -5471,14 +5389,13 @@ namespace SabreTools.Library.DatFiles /// /// Create and open an output file for writing direct from a dictionary /// - /// All information for creating the datfile header - /// Set the output directory + /// Set the output directory (default current directory) /// True if games should only be compared on game and file name (default), false if system and source are counted /// True if DAT statistics should be output on write, false otherwise (default) /// True if blank roms should be skipped on output, false otherwise (default) /// True if files should be overwritten (default), false if they should be renamed instead /// True if the DAT was written correctly, false otherwise - public bool WriteToFile(string outDir, bool norename = true, bool stats = false, bool ignoreblanks = false, bool overwrite = true) + public bool WriteToFile(string outDir = null, bool norename = true, bool stats = false, bool ignoreblanks = false, bool overwrite = true) { // If there's nothing there, abort if (Count == 0) diff --git a/SabreTools.Library/README.1ST b/SabreTools.Library/README.1ST index 3a4ccb3c..4041d9b3 100644 --- a/SabreTools.Library/README.1ST +++ b/SabreTools.Library/README.1ST @@ -384,6 +384,11 @@ Options: This sets an output folder to be used when the files are created. If a path is not defined, the application directory is used instead. + -ip, --inplace Write to the input directories + This will write out the split files to the source folder instead of + writing them out to the runtime folder by default (or the output + folder if overridden). + -ex, --extract Backup and remove copier headers This will detect, store, and remove copier headers from a file or folder of files. The headers are backed up and collated by the hash of the @@ -426,6 +431,11 @@ Options: This sets an output folder to be used when the files are created. If a path is not defined, the application directory is used instead. + -ip, --inplace Write to the input directories + This will write out the split files to the source folder instead of + writing them out to the runtime folder by default (or the output + folder if overridden). + -ls, --lvl-split Split a SuperDAT or folder by lowest available level For a DAT, or set of DATs, allow for splitting based on the lowest available level of game name. That is, if a game name is top/mid/last, @@ -436,6 +446,11 @@ Options: This sets an output folder to be used when the files are created. If a path is not defined, the application directory is used instead. + -ip, --inplace Write to the input directories + This will write out the split files to the source folder instead of + writing them out to the runtime folder by default (or the output + folder if overridden). + -s, --short Use short names for outputted DATs Instead of using ClrMamePro-style long names for DATs, use just the name of the folder as the name of the DAT. This can be used in @@ -711,6 +726,11 @@ Options: -out= Set the name of the output directory This sets an output folder to be used when the files are created. If a path is not defined, the application directory is used instead. + + -ip, --inplace Write to the input directories + This will write out the split files to the source folder instead of + writing them out to the runtime folder by default (or the output + folder if overridden). -ud, --update Update and manipulate DAT(s) This is the multitool part of the program, allowing for almost every diff --git a/SabreTools/SabreTools.Help.cs b/SabreTools/SabreTools.Help.cs index 97633e6a..32d00b9c 100644 --- a/SabreTools/SabreTools.Help.cs +++ b/SabreTools/SabreTools.Help.cs @@ -339,6 +339,11 @@ namespace SabreTools "Output directory", FeatureType.String, null)); + extSplit.AddFeature("inplace", new Feature( + new List() { "-ip", "--inplace" }, + "Write to the input directories", + FeatureType.Flag, + null)); // Create the Hash Split feature Feature hashSplit = new Feature( @@ -351,6 +356,11 @@ namespace SabreTools "Output directory", FeatureType.String, null)); + hashSplit.AddFeature("inplace", new Feature( + new List() { "-ip", "--inplace" }, + "Write to the input directories", + FeatureType.Flag, + null)); // Create the Level Split feature Feature levelSplit = new Feature( @@ -363,6 +373,11 @@ namespace SabreTools "Output directory", FeatureType.String, null)); + levelSplit.AddFeature("inplace", new Feature( + new List() { "-ip", "--inplace" }, + "Write to the input directories", + FeatureType.Flag, + null)); levelSplit.AddFeature("short", new Feature( new List() { "-s", "--short" }, "Use short output names", @@ -635,6 +650,11 @@ namespace SabreTools "Output directory", FeatureType.String, null)); + typeSplit.AddFeature("inplace", new Feature( + new List() { "-ip", "--inplace" }, + "Write to the input directories", + FeatureType.Flag, + null)); // Create the Update feature Feature update = new Feature( diff --git a/SabreTools/SabreTools.Inits.cs b/SabreTools/SabreTools.Inits.cs index 6a28a2da..be9af3b8 100644 --- a/SabreTools/SabreTools.Inits.cs +++ b/SabreTools/SabreTools.Inits.cs @@ -186,34 +186,27 @@ namespace SabreTools /// First extension to split on /// Second extension to split on /// Output directory for the split files - private static void InitExtSplit(List inputs, List exta, List extb, string outDir) + /// True if files should be written to the source folders, false otherwise + private static void InitExtSplit(List inputs, List exta, List extb, string outDir, bool inplace) { + // Get only files from the inputs + List files = Utilities.GetOnlyFilesFromInputs(inputs, appendparent: true); + // Loop over the input files - foreach (string input in inputs) + foreach (string file in files) { - if (File.Exists(input)) - { - DatFile datFile = new DatFile(); - datFile.Parse(Path.GetFullPath(input), 0, 0); - datFile.SplitByExtension(outDir, Path.GetDirectoryName(input), exta, extb); - } - else if (Directory.Exists(input)) - { - foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories)) - { - DatFile datFile = new DatFile(); - datFile.Parse(Path.GetFullPath(file), 0, 0); - datFile.SplitByExtension(outDir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), - exta, extb); - } - } - else - { - Globals.Logger.Error("'{0}' is not a valid file or folder!", input); - Console.WriteLine(); - _help.OutputIndividualFeature("Extension Split"); - return; - } + // Split the input filename + string[] splitpath = file.Split('¬'); + + // Create and fill the new DAT + DatFile datFile = new DatFile(); + datFile.Parse(splitpath[0], 0, 0); + + // Get the output directory + outDir = Utilities.GetOutputPath(outDir, file, inplace, splitpath: true); + + // Split and write the DAT + datFile.SplitByExtension(outDir, inplace, exta, extb); } } @@ -222,33 +215,27 @@ namespace SabreTools /// /// List of inputs to be used /// Output directory for the split files - private static void InitHashSplit(List inputs, string outDir) + /// True if files should be written to the source folders, false otherwise + private static void InitHashSplit(List inputs, string outDir, bool inplace) { + // Get only files from the inputs + List files = Utilities.GetOnlyFilesFromInputs(inputs, appendparent: true); + // Loop over the input files - foreach (string input in inputs) + foreach (string file in files) { - if (File.Exists(input)) - { - DatFile datFile = new DatFile(); - datFile.Parse(Path.GetFullPath(input), 0, 0); - datFile.SplitByHash(outDir, Path.GetDirectoryName(input)); - } - else if (Directory.Exists(input)) - { - foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories)) - { - DatFile datFile = new DatFile(); - datFile.Parse(Path.GetFullPath(file), 0, 0); - datFile.SplitByHash(outDir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar)); - } - } - else - { - Globals.Logger.Error("'{0}' is not a valid file or folder!", input); - Console.WriteLine(); - _help.OutputIndividualFeature("Hash Split"); - return; - } + // Split the input filename + string[] splitpath = file.Split('¬'); + + // Create and fill the new DAT + DatFile datFile = new DatFile(); + datFile.Parse(splitpath[0], 0, 0); + + // Get the output directory + outDir = Utilities.GetOutputPath(outDir, file, inplace, splitpath: true); + + // Split and write the DAT + datFile.SplitByHash(outDir, inplace); } } @@ -280,36 +267,29 @@ namespace SabreTools /// /// List of inputs to be used /// Output directory for the split files + /// True if files should be written to the source folders, false otherwise /// True if short filenames should be used, false otherwise /// True if original filenames should be used as the base for output filename, false otherwise - private static void InitLevelSplit(List inputs, string outDir, bool shortname, bool basedat) + private static void InitLevelSplit(List inputs, string outDir, bool inplace, bool shortname, bool basedat) { + // Get only files from the inputs + List files = Utilities.GetOnlyFilesFromInputs(inputs, appendparent: true); + // Loop over the input files - foreach (string input in inputs) + foreach (string file in files) { - if (File.Exists(input)) - { - DatFile datFile = new DatFile(); - datFile.Parse(Path.GetFullPath(input), 0, 0, keep: true); - datFile.SplitByLevel(outDir, Path.GetDirectoryName(input), shortname, basedat); - } - else if (Directory.Exists(input)) - { - foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories)) - { - DatFile datFile = new DatFile(); - datFile.Parse(Path.GetFullPath(file), 0, 0, keep: true); - datFile.SplitByLevel(outDir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), - shortname, basedat); - } - } - else - { - Globals.Logger.Error("'{0}' is not a valid file or folder!", input); - Console.WriteLine(); - _help.OutputIndividualFeature("Level Split"); - return; - } + // Split the input filename + string[] splitpath = file.Split('¬'); + + // Create and fill the new DAT + DatFile datFile = new DatFile(); + datFile.Parse(splitpath[0], 0, 0); + + // Get the output directory + outDir = Utilities.GetOutputPath(outDir, file, inplace, splitpath: true); + + // Split and write the DAT + datFile.SplitByLevel(outDir, inplace, shortname, basedat); } } @@ -389,33 +369,27 @@ namespace SabreTools /// /// List of inputs to be used /// Output directory for the split files - private static void InitTypeSplit(List inputs, string outDir) + /// True if files should be written to the source folders, false otherwise + private static void InitTypeSplit(List inputs, string outDir, bool inplace) { + // Get only files from the inputs + List files = Utilities.GetOnlyFilesFromInputs(inputs, appendparent: true); + // Loop over the input files - foreach (string input in inputs) + foreach (string file in files) { - if (File.Exists(input)) - { - DatFile datFile = new DatFile(); - datFile.Parse(Path.GetFullPath(input), 0, 0); - datFile.SplitByType(outDir, Path.GetFullPath(Path.GetDirectoryName(input))); - } - else if (Directory.Exists(input)) - { - foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories)) - { - DatFile datFile = new DatFile(); - datFile.Parse(Path.GetFullPath(file), 0, 0); - datFile.SplitByType(outDir, Path.GetFullPath((input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar))); - } - } - else - { - Globals.Logger.Error("{0} is not a valid file or folder!", input); - Console.WriteLine(); - _help.OutputIndividualFeature("Type Split"); - return; - } + // Split the input filename + string[] splitpath = file.Split('¬'); + + // Create and fill the new DAT + DatFile datFile = new DatFile(); + datFile.Parse(splitpath[0], 0, 0); + + // Get the output directory + outDir = Utilities.GetOutputPath(outDir, file, inplace, splitpath: true); + + // Split and write the DAT + datFile.SplitByType(outDir, inplace); } } diff --git a/SabreTools/SabreTools.cs b/SabreTools/SabreTools.cs index af28f555..a4c4afaf 100644 --- a/SabreTools/SabreTools.cs +++ b/SabreTools/SabreTools.cs @@ -1300,25 +1300,25 @@ namespace SabreTools // Split a DAT by extension else if (splitByExt) { - InitExtSplit(inputs, exta, extb, outDir); + InitExtSplit(inputs, exta, extb, outDir, inplace); } // Split a DAT by available hashes else if (splitByHash) { - InitHashSplit(inputs, outDir); + InitHashSplit(inputs, outDir, inplace); } // Split a SuperDAT by lowest available level else if (splitByLevel) { - InitLevelSplit(inputs, outDir, shortname, basedat); + InitLevelSplit(inputs, outDir, inplace, shortname, basedat); } // Split a DAT by item type else if (splitByType) { - InitTypeSplit(inputs, outDir); + InitTypeSplit(inputs, outDir, inplace); } // Get statistics on input files