2016-04-06 00:01:54 -07:00
|
|
|
|
using System;
|
2016-04-18 16:32:17 -07:00
|
|
|
|
using System.Collections.Generic;
|
2016-04-06 00:01:54 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
|
|
|
|
using SabreTools.Helper;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SabreTools
|
|
|
|
|
|
{
|
|
|
|
|
|
public class SingleGame
|
|
|
|
|
|
{
|
2016-04-20 01:19:53 -07:00
|
|
|
|
// Instance variables
|
2016-04-06 13:03:25 -07:00
|
|
|
|
private static string _filename = "";
|
|
|
|
|
|
private static string _path = "";
|
2016-04-20 01:19:53 -07:00
|
|
|
|
private static bool _rename;
|
|
|
|
|
|
private static bool _forceunpack;
|
|
|
|
|
|
private static Logger _logger;
|
2016-04-06 00:01:54 -07:00
|
|
|
|
|
2016-04-20 01:19:53 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new SingleGame object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filename">Name of the file or folder to be processed</param>
|
|
|
|
|
|
/// <param name="path">Root path to use for trimming</param>
|
|
|
|
|
|
/// <param name="rename">True if games should be renamed into a uniform string, false otherwise</param>
|
|
|
|
|
|
/// <param name="forceunpack">True if forcepacking="unzip" should be set on the output, false otherwise</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for console and file output</param>
|
|
|
|
|
|
public SingleGame(string filename, string path, bool rename, bool forceunpack, Logger logger)
|
2016-04-06 00:01:54 -07:00
|
|
|
|
{
|
2016-04-20 01:19:53 -07:00
|
|
|
|
_filename = filename;
|
|
|
|
|
|
_path = path;
|
|
|
|
|
|
_rename = rename;
|
|
|
|
|
|
_forceunpack = forceunpack;
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
2016-04-19 13:44:16 -07:00
|
|
|
|
|
2016-04-20 01:19:53 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Trim and process the given DAT or folder of DATs
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Process()
|
|
|
|
|
|
{
|
2016-04-06 13:03:25 -07:00
|
|
|
|
_path = (_path == "" ? Environment.CurrentDirectory : _path);
|
|
|
|
|
|
|
2016-04-19 12:10:21 -07:00
|
|
|
|
// Drag and drop means quotes; we don't want quotes
|
|
|
|
|
|
_filename = _filename.Replace("\"", "");
|
2016-04-06 00:01:54 -07:00
|
|
|
|
|
2016-04-19 13:01:07 -07:00
|
|
|
|
// We also want the full path of the file, just in case
|
|
|
|
|
|
_filename = Path.GetFullPath(_filename);
|
|
|
|
|
|
|
2016-04-19 12:10:21 -07:00
|
|
|
|
// If it's a single file, handle it as such
|
|
|
|
|
|
if (!Directory.Exists(_filename) && File.Exists(_filename))
|
2016-04-06 00:01:54 -07:00
|
|
|
|
{
|
2016-04-20 01:19:53 -07:00
|
|
|
|
_logger.Log("File found: " + _filename);
|
2016-04-19 12:10:21 -07:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-20 01:19:53 -07:00
|
|
|
|
_logger.Log("Directory found: " + _filename);
|
2016-04-19 13:01:07 -07:00
|
|
|
|
foreach (string file in Directory.EnumerateFiles(_filename, "*", SearchOption.AllDirectories))
|
2016-04-06 00:01:54 -07:00
|
|
|
|
{
|
2016-04-20 01:19:53 -07:00
|
|
|
|
_logger.Log("File found: " + file);
|
2016-04-19 12:10:21 -07:00
|
|
|
|
ProcessDAT(file, _path, _rename);
|
|
|
|
|
|
}
|
2016-04-06 00:01:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-20 01:19:53 -07:00
|
|
|
|
_logger.Close();
|
2016-04-19 12:10:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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-20 01:19:53 -07:00
|
|
|
|
List<RomData> roms = RomManipulation.Parse(filename, 0, 0, _logger);
|
2016-04-19 12:10:21 -07:00
|
|
|
|
|
2016-04-19 01:11:23 -07:00
|
|
|
|
// Trim all file names according to the path that's set
|
2016-04-19 12:10:21 -07:00
|
|
|
|
List<RomData> outroms = new List<RomData>();
|
|
|
|
|
|
while (roms.Count != 0)
|
2016-04-06 00:01:54 -07:00
|
|
|
|
{
|
2016-04-19 12:10:21 -07:00
|
|
|
|
RomData rom = roms[0];
|
|
|
|
|
|
roms.RemoveAt(0);
|
|
|
|
|
|
|
|
|
|
|
|
// If we are in single game mode, rename all games
|
|
|
|
|
|
if (rename)
|
|
|
|
|
|
{
|
|
|
|
|
|
rom.Game = "!";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-19 01:11:23 -07:00
|
|
|
|
// Windows max name length is 260
|
|
|
|
|
|
int usableLength = 259 - _path.Length;
|
2016-04-19 12:10:21 -07:00
|
|
|
|
if (rom.Name.Length > usableLength)
|
2016-04-06 00:01:54 -07:00
|
|
|
|
{
|
2016-04-19 12:10:21 -07:00
|
|
|
|
string ext = Path.GetExtension(rom.Name);
|
|
|
|
|
|
rom.Name = rom.Name.Substring(0, usableLength - ext.Length);
|
|
|
|
|
|
rom.Name += ext;
|
2016-04-06 00:01:54 -07:00
|
|
|
|
}
|
2016-04-19 12:10:21 -07:00
|
|
|
|
|
|
|
|
|
|
outroms.Add(rom);
|
|
|
|
|
|
}
|
2016-04-19 01:11:23 -07:00
|
|
|
|
|
|
|
|
|
|
// Now write the file out accordingly
|
2016-04-19 13:01:07 -07:00
|
|
|
|
Output.WriteToDat(Path.GetFileNameWithoutExtension(filename),
|
2016-04-20 01:19:53 -07:00
|
|
|
|
Path.GetFileNameWithoutExtension(filename), "", "", "", "", _forceunpack, !RomManipulation.IsXmlDat(filename), Path.GetDirectoryName(filename), outroms, _logger);
|
2016-04-19 14:57:15 -07:00
|
|
|
|
|
|
|
|
|
|
// Remove the original file if different and inform the user
|
|
|
|
|
|
if (Path.GetExtension(filename) != (RomManipulation.IsXmlDat(filename) ? ".xml" : ".dat"))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(filename);
|
2016-04-20 01:19:53 -07:00
|
|
|
|
_logger.Log("Original file \"" + filename + "\" deleted");
|
2016-04-19 14:57:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2016-04-20 01:19:53 -07:00
|
|
|
|
_logger.Error(ex.ToString());
|
2016-04-19 14:57:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-04-06 00:01:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|