Files
SabreTools/DATabase/ExtSplit.cs

93 lines
2.6 KiB
C#
Raw Normal View History

using System;
2016-04-18 16:32:17 -07:00
using System.Collections.Generic;
using System.IO;
using SabreTools.Helper;
2016-04-20 02:04:10 -07:00
namespace SabreTools
{
2016-04-20 11:37:20 -07:00
public class ExtSplit
{
2016-04-20 02:15:45 -07:00
// Instance variables
2016-04-20 11:27:17 -07:00
private string _extA;
private string _extB;
private string _filename;
private string _outdir;
2016-04-20 02:15:45 -07:00
private static Logger _logger;
/// <summary>
/// Create a new DatSplit object
/// </summary>
/// <param name="filename">Filename of the DAT to split</param>
/// <param name="extA">First extension to split on</param>
/// <param name="extB">Second extension to split on</param>
/// <param name="logger">Logger object for console and file writing</param>
2016-04-20 11:37:20 -07:00
public ExtSplit(string filename, string extA, string extB, string outdir, Logger logger)
2016-04-20 02:15:45 -07:00
{
_filename = filename.Replace("\"", "");
_extA = (extA.StartsWith(".") ? extA : "." + extA).ToUpperInvariant();
_extB = (extB.StartsWith(".") ? extB : "." + extB).ToUpperInvariant();
2016-04-20 11:27:17 -07:00
_outdir = outdir.Replace("\"", "");
2016-04-20 02:15:45 -07:00
_logger = logger;
}
2016-04-20 02:22:44 -07:00
/// <summary>
/// Split a DAT based on filtering by 2 extensions
/// </summary>
/// <returns>True if DAT was split, false otherwise</returns>
public bool Split()
2016-04-20 02:15:45 -07:00
{
// If file doesn't exist, error and return
if (!File.Exists(_filename))
{
_logger.Error("File '" + _filename + "' doesn't exist");
return false;
}
2016-04-20 11:27:17 -07:00
// If the output directory doesn't exist, create it
if (!Directory.Exists(_outdir))
{
Directory.CreateDirectory(_outdir);
}
List<RomData> romsA = new List<RomData>();
List<RomData> romsB = new List<RomData>();
// Load the current DAT to be processed
2016-04-20 02:20:25 -07:00
List<RomData> roms = RomManipulation.Parse(_filename, 0, 0, _logger);
// If roms is empty, return false
if (roms.Count == 0)
{
return false;
}
// Now separate the roms accordingly
foreach (RomData rom in roms)
{
if (rom.Name.ToUpperInvariant().EndsWith(_extA))
{
romsA.Add(rom);
}
else if (rom.Name.ToUpperInvariant().EndsWith(_extB))
{
romsB.Add(rom);
}
else
{
romsA.Add(rom);
romsB.Add(rom);
}
}
// Then write out both files
bool success = Output.WriteToDat(Path.GetFileNameWithoutExtension(_filename) + "." + _extA, Path.GetFileNameWithoutExtension(_filename) + "." + _extA,
2016-04-20 11:37:20 -07:00
"", "", "", "", false, !RomManipulation.IsXmlDat(_filename), _outdir, romsA, _logger);
success &= Output.WriteToDat(Path.GetFileNameWithoutExtension(_filename) + "." + _extB, Path.GetFileNameWithoutExtension(_filename) + "." + _extB,
2016-04-20 11:37:20 -07:00
"", "", "", "", false, !RomManipulation.IsXmlDat(_filename), _outdir, romsB, _logger);
return success;
}
}
}