2016-04-05 15:54:58 -07:00
|
|
|
|
using System;
|
2016-04-18 16:32:17 -07:00
|
|
|
|
using System.Collections.Generic;
|
2016-04-05 15:54:58 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
|
|
using SabreTools.Helper;
|
|
|
|
|
|
|
2016-04-20 02:04:10 -07:00
|
|
|
|
namespace SabreTools
|
2016-04-05 15:54:58 -07:00
|
|
|
|
{
|
2016-04-20 11:37:20 -07:00
|
|
|
|
public class ExtSplit
|
2016-04-05 15:54:58 -07:00
|
|
|
|
{
|
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-05 15:54:58 -07:00
|
|
|
|
|
2016-04-20 02:22:44 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Split a DAT based on filtering by 2 extensions
|
|
|
|
|
|
/// </summary>
|
2016-04-20 02:40:01 -07:00
|
|
|
|
/// <returns>True if DAT was split, false otherwise</returns>
|
|
|
|
|
|
public bool Split()
|
2016-04-20 02:15:45 -07:00
|
|
|
|
{
|
2016-04-20 02:40:01 -07:00
|
|
|
|
// If file doesn't exist, error and return
|
|
|
|
|
|
if (!File.Exists(_filename))
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Error("File '" + _filename + "' doesn't exist");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-12 10:24:42 -07:00
|
|
|
|
// If it's empty, use the current folder
|
|
|
|
|
|
if (_outdir.Trim() == "")
|
|
|
|
|
|
{
|
|
|
|
|
|
_outdir = Environment.CurrentDirectory;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-20 11:27:17 -07:00
|
|
|
|
// If the output directory doesn't exist, create it
|
|
|
|
|
|
if (!Directory.Exists(_outdir))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(_outdir);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-16 20:51:05 -07:00
|
|
|
|
// Create the initial DatData object
|
|
|
|
|
|
DatData datdata = new DatData
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = "",
|
|
|
|
|
|
Description = "",
|
|
|
|
|
|
Category = "",
|
|
|
|
|
|
Version = "",
|
|
|
|
|
|
Date = "",
|
|
|
|
|
|
Author = "",
|
|
|
|
|
|
Email = "",
|
|
|
|
|
|
Homepage = "",
|
|
|
|
|
|
Url = "",
|
|
|
|
|
|
Comment = "",
|
|
|
|
|
|
Roms = new Dictionary<string, List<RomData>>(),
|
|
|
|
|
|
};
|
2016-04-05 15:54:58 -07:00
|
|
|
|
|
2016-04-19 01:11:23 -07:00
|
|
|
|
// Load the current DAT to be processed
|
2016-05-16 20:51:05 -07:00
|
|
|
|
datdata = RomManipulation.ParseDict(_filename, 0, 0, datdata, _logger);
|
|
|
|
|
|
|
|
|
|
|
|
// Set all of the appropriate outputs for each of the subsets
|
|
|
|
|
|
OutputFormat outputFormat = RomManipulation.GetOutputFormat(_filename);
|
|
|
|
|
|
DatData datdataA = new DatData
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = datdata.Name + "." + _extA,
|
|
|
|
|
|
Description = datdata.Description + "." + _extA,
|
|
|
|
|
|
Category = datdata.Category,
|
|
|
|
|
|
Version = datdata.Version,
|
|
|
|
|
|
Date = datdata.Date,
|
|
|
|
|
|
Author = datdata.Author,
|
|
|
|
|
|
Email = datdata.Email,
|
|
|
|
|
|
Homepage = datdata.Homepage,
|
|
|
|
|
|
Url = datdata.Url,
|
|
|
|
|
|
Comment = datdata.Comment,
|
|
|
|
|
|
Roms = new Dictionary<string, List<RomData>>(),
|
|
|
|
|
|
OutputFormat = outputFormat,
|
|
|
|
|
|
};
|
|
|
|
|
|
DatData datdataB = new DatData
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = datdata.Name + "." + _extB,
|
|
|
|
|
|
Description = datdata.Description + "." + _extB,
|
|
|
|
|
|
Category = datdata.Category,
|
|
|
|
|
|
Version = datdata.Version,
|
|
|
|
|
|
Date = datdata.Date,
|
|
|
|
|
|
Author = datdata.Author,
|
|
|
|
|
|
Email = datdata.Email,
|
|
|
|
|
|
Homepage = datdata.Homepage,
|
|
|
|
|
|
Url = datdata.Url,
|
|
|
|
|
|
Comment = datdata.Comment,
|
|
|
|
|
|
Roms = new Dictionary<string, List<RomData>>(),
|
|
|
|
|
|
OutputFormat = outputFormat,
|
|
|
|
|
|
};
|
2016-04-05 15:54:58 -07:00
|
|
|
|
|
2016-04-20 02:40:01 -07:00
|
|
|
|
// If roms is empty, return false
|
2016-05-16 20:51:05 -07:00
|
|
|
|
if (datdata.Roms.Count == 0)
|
2016-04-20 02:40:01 -07:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-19 01:11:23 -07:00
|
|
|
|
// Now separate the roms accordingly
|
2016-05-16 20:51:05 -07:00
|
|
|
|
foreach (string key in datdata.Roms.Keys)
|
2016-04-05 15:54:58 -07:00
|
|
|
|
{
|
2016-05-16 20:51:05 -07:00
|
|
|
|
foreach (RomData rom in datdata.Roms[key])
|
2016-04-05 15:54:58 -07:00
|
|
|
|
{
|
2016-05-16 20:51:05 -07:00
|
|
|
|
if (rom.Name.ToUpperInvariant().EndsWith(_extA))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (datdataA.Roms.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
datdataA.Roms[key].Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
List<RomData> temp = new List<RomData>();
|
|
|
|
|
|
temp.Add(rom);
|
|
|
|
|
|
datdataA.Roms.Add(key, temp);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (rom.Name.ToUpperInvariant().EndsWith(_extB))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (datdataB.Roms.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
datdataB.Roms[key].Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
List<RomData> temp = new List<RomData>();
|
|
|
|
|
|
temp.Add(rom);
|
|
|
|
|
|
datdataB.Roms.Add(key, temp);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (datdataA.Roms.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
datdataA.Roms[key].Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
List<RomData> temp = new List<RomData>();
|
|
|
|
|
|
temp.Add(rom);
|
|
|
|
|
|
datdataA.Roms.Add(key, temp);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (datdataB.Roms.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
datdataB.Roms[key].Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
List<RomData> temp = new List<RomData>();
|
|
|
|
|
|
temp.Add(rom);
|
|
|
|
|
|
datdataB.Roms.Add(key, temp);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-04-05 15:54:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-19 01:11:23 -07:00
|
|
|
|
// Then write out both files
|
2016-05-16 20:51:05 -07:00
|
|
|
|
bool success = Output.WriteToDatFromDict(datdataA, _outdir, _logger);
|
|
|
|
|
|
success &= Output.WriteToDatFromDict(datdataB, _outdir, _logger);
|
2016-04-20 02:40:01 -07:00
|
|
|
|
|
|
|
|
|
|
return success;
|
2016-04-05 15:54:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|