Files
SabreTools/DATabase/SingleGame.cs

130 lines
3.8 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
{
2016-04-20 01:19:53 -07:00
// Instance variables
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-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-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()
{
_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-20 01:19:53 -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;
}
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-20 01:19:53 -07:00
_logger.Log("File found: " + file);
ProcessDAT(file, _path, _rename);
}
}
2016-04-20 01:19:53 -07:00
_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-20 01:19:53 -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-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
}
}
}
}
}