Files
SabreTools/SingleGame/SingleGame.cs

140 lines
3.4 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-06 00:47:28 -07:00
_filename = args[0];
if (args.Length > 1)
{
for (int i = 1; i < args.Length; i++)
{
2016-04-19 02:09:48 -07:00
switch (args[i])
{
case "-n":
_rename = false;
break;
case "-z":
_forceunpack = false;
break;
2016-04-19 02:09:48 -07:00
default:
if (args[i].StartsWith("-r"))
{
_path = args[i].Split('=')[1];
}
break;
}
}
}
_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);
// 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);
}
}
}