using SabreTools.Helper; using System; using System.Collections.Generic; using System.IO; namespace SabreTools { public class SimpleSortApp { /// /// Main entry point for the program /// /// List of arguments to be parsed public static void Main(string[] args) { // If output is being redirected, don't allow clear screens if (!Console.IsOutputRedirected) { Console.Clear(); } // Perform initial setup and verification Logger logger = new Logger(true, "simplesort.log"); logger.Start(); // Credits take precidence over all if ((new List(args)).Contains("--credits")) { Build.Credits(); return; } // If there's no arguments, show help if (args.Length == 0) { Build.Help(); logger.Close(); return; } // Output the title Build.Start("SimpleSort"); // Set all default values bool help = false, convert = false, delete = false, quickScan = false, romba = false, simpleSort = true, toFolder = false, tzip = false, updateDat = false, verify = false; bool? torrentX = null; int sevenzip = 0, gz = 2, rar = 2, zip = 0; string outDir = "", tempDir = ""; List inputs = new List(); List datfiles = new List(); // Determine which switches are enabled (with values if necessary) foreach (string arg in args) { switch (arg) { case "-?": case "-h": case "--help": help = true; break; case "-c": case "--convert": convert = true; break; case "-d": case "--delete": delete = true; break; case "-do": case "--directory": toFolder = true; break; case "-qs": case "--quick": quickScan = true; break; case "-r": case "--romba": romba = true; break; case "-tgz": case "--tgz": torrentX = false; break; case "-tzip": case "--tzip": torrentX = true; break; case "-ud": case "--updated-dat": updateDat = true; break; case "-v": case "--verify": verify = true; break; default: string temparg = arg.Replace("\"", "").Replace("file://", ""); if (temparg.StartsWith("-7z=") || temparg.StartsWith("--7z=")) { if (!Int32.TryParse(temparg.Split('=')[1], out sevenzip)) { sevenzip = 0; } } else if (temparg.StartsWith("-dat=") || temparg.StartsWith("--dat=")) { string datfile = temparg.Split('=')[1]; if (!File.Exists(datfile)) { logger.Error("DAT must be a valid file: " + datfile); Console.WriteLine(); Build.Help(); logger.Close(); return; } datfiles.Add(datfile); } else if (temparg.StartsWith("-gz=") || temparg.StartsWith("--gz=")) { if (!Int32.TryParse(temparg.Split('=')[1], out gz)) { gz = 2; } } else if (temparg.StartsWith("-out=") || temparg.StartsWith("--out=")) { outDir = temparg.Split('=')[1]; } else if (temparg.StartsWith("-rar=") || temparg.StartsWith("--rar=")) { if (!Int32.TryParse(temparg.Split('=')[1], out rar)) { rar = 2; } } else if (temparg.StartsWith("-t=") || temparg.StartsWith("--temp=")) { tempDir = temparg.Split('=')[1]; } else if (temparg.StartsWith("-zip=") || temparg.StartsWith("--zip=")) { if (!Int32.TryParse(temparg.Split('=')[1], out zip)) { zip = 0; } } else if (File.Exists(temparg) || Directory.Exists(temparg)) { inputs.Add(temparg); } else { logger.Error("Invalid input detected: " + arg); Console.WriteLine(); Build.Help(); Console.WriteLine(); logger.Error("Invalid input detected: " + arg); logger.Close(); return; } break; } } // If help is set, show the help screen if (help) { Build.Help(); logger.Close(); return; } // If a switch that requires a filename is set and no file is, show the help screen if (inputs.Count == 0 && ((simpleSort && !verify) || convert || tzip)) { logger.Error("This feature requires at least one input"); Build.Help(); logger.Close(); return; } // If we are converting the folder to TGZ else if (convert) { InitConvertFolderTGZ(inputs, outDir, tempDir, delete, romba, sevenzip, gz, rar, zip, logger); } // If we are doing a simple sort else if (simpleSort) { if (datfiles.Count > 0) { InitSortVerify(datfiles, inputs, outDir, tempDir, quickScan, toFolder, verify, delete, torrentX, romba, sevenzip, gz, rar, zip, updateDat, logger); } else { logger.Error("A datfile is required to use this feature"); Build.Help(); logger.Close(); return; } } // If nothing is set, show the help else { Build.Help(); } logger.Close(); return; } /// /// Wrap sorting files using an input DAT /// /// Names of the DATs to compare against /// List of input files/folders to check /// Output directory to use to build to /// Temporary directory for archive extraction /// True to enable external scanning of archives, false otherwise /// Integer representing the archive handling level for 7z /// True if files should be output to folder, false otherwise /// True if output directory should be checked instead of rebuilt to, false otherwise /// True if input files should be deleted, false otherwise /// True is for TorrentZip, False is for TorrentGZ, Null is for standard zip /// True if files should be output in Romba depot folders, false otherwise /// Integer representing the archive handling level for GZip /// Integer representing the archive handling level for RAR /// Integer representing the archive handling level for Zip /// True if the updated DAT should be output, false otherwise /// Logger object for file and console output private static void InitSortVerify(List datfiles, List inputs, string outDir, string tempDir, bool quickScan, bool toFolder, bool verify, bool delete, bool? torrentX, bool romba, int sevenzip, int gz, int rar, int zip, bool updateDat, Logger logger) { // Add all of the input DATs into one huge internal DAT Dat datdata = new Dat(); foreach (string datfile in datfiles) { datdata = DatTools.Parse(datfile, 99, 99, datdata, logger, keep: true, softlist: true); } SimpleSort ss = new SimpleSort(datdata, inputs, outDir, tempDir, quickScan, toFolder, verify, delete, torrentX, romba, sevenzip, gz, rar, zip, updateDat, logger); ss.StartProcessing(); } /// /// Wrap sorting files using an input DAT /// /// List of all inputted files and folders /// Output directory (empty for default directory) /// Temporary directory for archive extraction /// True if input files should be deleted, false otherwise /// True if files should be output in Romba depot folders, false otherwise /// Integer representing the archive handling level for 7z /// Integer representing the archive handling level for GZip /// Integer representing the archive handling level for RAR /// Integer representing the archive handling level for Zip /// Logger object for file and console output public static bool InitConvertFolderTGZ(List inputs, string outDir, string tempDir, bool delete, bool romba, int sevenzip, int gz, int rar, int zip, Logger logger) { // Get all individual files from the inputs List newinputs = new List(); foreach (string input in inputs) { if (File.Exists(input)) { newinputs.Add(Path.GetFullPath(input)); } else if (Directory.Exists(input)) { foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories)) { newinputs.Add(Path.GetFullPath(file)); } } } SimpleSort ss = new SimpleSort(new Dat(), newinputs, outDir, tempDir, false, false, false, delete, false, romba, sevenzip, gz, rar, zip, false, logger); return ss.Convert(); } } }