Files
SabreTools/SingleGame/SingleGame.cs

158 lines
3.7 KiB
C#
Raw Normal View History

using System;
2016-04-18 16:32:17 -07:00
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)
{
2016-04-18 20:04:38 -07:00
Console.Clear();
2016-04-06 00:35:39 -07:00
2016-04-18 16:32:17 -07:00
// Credits take precidence over all
if ((new List<string>(args)).Contains("--credits"))
{
Build.Credits();
return;
}
if (args.Length == 0)
{
Build.Help();
return;
}
logger = new Logger(false, "singlegame.log");
logger.Start();
2016-04-18 20:04:38 -07:00
// Output the title
Build.Start("SingleGame");
2016-04-19 13:44:16 -07:00
bool tofile = false;
2016-04-19 14:00:09 -07:00
foreach (string arg in args)
{
2016-04-19 14:00:09 -07:00
switch (arg)
{
2016-04-19 14:00:09 -07:00
case "-n":
_rename = false;
break;
case "-z":
_forceunpack = false;
break;
case "-l":
case "--log":
tofile = true;
break;
default:
if (arg.StartsWith("-r"))
{
_path = arg.Split('=')[1];
}
else
{
_filename = arg;
}
break;
}
}
2016-04-19 13:44:16 -07:00
// Set the possibly new value for logger
logger.ToFile = tofile;
_path = (_path == "" ? Environment.CurrentDirectory : _path);
// Drag and drop means quotes; we don't want quotes
_filename = _filename.Replace("\"", "");
2016-04-19 13:01:07 -07:00
// 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))
{
2016-04-19 13:01:07 -07:00
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))
{
2016-04-19 13:01:07 -07:00
// 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))
{
2016-04-19 13:01:07 -07:00
logger.Log("File found: " + file);
ProcessDAT(file, _path, _rename);
}
}
logger.Close();
}
/// <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 static void ProcessDAT(string filename, string path, bool rename)
{
2016-04-19 13:01:07 -07:00
List<RomData> roms = RomManipulation.Parse(filename, 0, 0, logger);
// Remove the original file and inform the user
try
{
File.Delete(filename);
logger.Log("Original file \"" + filename + "\" deleted");
}
catch (Exception ex)
{
logger.Error(ex.ToString());
}
// Trim all file names according to the path that's set
List<RomData> outroms = new List<RomData>();
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
2016-04-19 13:01:07 -07:00
Output.WriteToDat(Path.GetFileNameWithoutExtension(filename),
2016-04-19 13:11:11 -07:00
Path.GetFileNameWithoutExtension(filename), "", "", "", "", _forceunpack, !RomManipulation.IsXmlDat(filename), Path.GetDirectoryName(filename), outroms, logger);
}
}
}