using System; using System.Collections.Generic; using System.IO; using System.Xml; using SabreTools.Helper; namespace SabreTools { public class SingleGame { private static string _filename = ""; private static string _path = ""; private static bool _rename = true; private static bool _forceunpack = true; private static Logger logger; public static void Main(string[] args) { Console.Clear(); // Credits take precidence over all if ((new List(args)).Contains("--credits")) { Build.Credits(); return; } if (args.Length == 0) { Build.Help(); return; } bool log = false; foreach (string arg in args) { switch (arg) { case "-nr": case "--no-rename": _rename = false; break; case "-df": case "--disable-force": _forceunpack = false; break; case "-l": case "--log": log = true; break; default: if (arg.StartsWith("-rd=") || arg.StartsWith("--root-dir=")) { _path = arg.Split('=')[1]; } else { _filename = arg; } break; } } // If the filename is blank, show the help and exit if (_filename == "") { Build.Help(); return; } logger = new Logger(false, "singlegame.log"); logger.Start(); // Output the title Build.Start("SingleGame"); // Set the possibly new value for logger logger.ToFile = log; _path = (_path == "" ? Environment.CurrentDirectory : _path); // Drag and drop means quotes; we don't want quotes _filename = _filename.Replace("\"", ""); // We also 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); } } logger.Close(); } /// /// 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 static void ProcessDAT(string filename, string path, bool rename) { List roms = RomManipulation.Parse(filename, 0, 0, logger); // Trim all file names according to the path that's set List outroms = new List(); while (roms.Count != 0) { RomData rom = roms[0]; roms.RemoveAt(0); // If we are in single game mode, rename all games if (rename) { rom.Game = "!"; } // Windows max name length is 260 int usableLength = 259 - _path.Length; if (rom.Name.Length > usableLength) { string ext = Path.GetExtension(rom.Name); rom.Name = rom.Name.Substring(0, usableLength - ext.Length); rom.Name += ext; } outroms.Add(rom); } // Now write the file out accordingly Output.WriteToDat(Path.GetFileNameWithoutExtension(filename), Path.GetFileNameWithoutExtension(filename), "", "", "", "", _forceunpack, !RomManipulation.IsXmlDat(filename), Path.GetDirectoryName(filename), outroms, logger); // Remove the original file if different and inform the user if (Path.GetExtension(filename) != (RomManipulation.IsXmlDat(filename) ? ".xml" : ".dat")) { try { File.Delete(filename); logger.Log("Original file \"" + filename + "\" deleted"); } catch (Exception ex) { logger.Error(ex.ToString()); } } } } }