mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Remove most of the outdated references and methods
This commit is contained in:
@@ -56,41 +56,124 @@ namespace SabreTools
|
||||
Directory.CreateDirectory(_outdir);
|
||||
}
|
||||
|
||||
List<RomData> romsA = new List<RomData>();
|
||||
List<RomData> romsB = new List<RomData>();
|
||||
// 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>>(),
|
||||
};
|
||||
|
||||
// Load the current DAT to be processed
|
||||
List<RomData> roms = RomManipulation.Parse(_filename, 0, 0, _logger);
|
||||
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,
|
||||
};
|
||||
|
||||
// If roms is empty, return false
|
||||
if (roms.Count == 0)
|
||||
if (datdata.Roms.Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now separate the roms accordingly
|
||||
foreach (RomData rom in roms)
|
||||
foreach (string key in datdata.Roms.Keys)
|
||||
{
|
||||
foreach (RomData rom in datdata.Roms[key])
|
||||
{
|
||||
if (rom.Name.ToUpperInvariant().EndsWith(_extA))
|
||||
{
|
||||
romsA.Add(rom);
|
||||
}
|
||||
else if (rom.Name.ToUpperInvariant().EndsWith(_extB))
|
||||
if (datdataA.Roms.ContainsKey(key))
|
||||
{
|
||||
romsB.Add(rom);
|
||||
datdataA.Roms[key].Add(rom);
|
||||
}
|
||||
else
|
||||
{
|
||||
romsA.Add(rom);
|
||||
romsB.Add(rom);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Then write out both files
|
||||
bool success = Output.WriteToDat(Path.GetFileNameWithoutExtension(_filename) + "." + _extA, Path.GetFileNameWithoutExtension(_filename) + "." + _extA,
|
||||
"", "", "", "", false, !RomManipulation.IsXmlDat(_filename), _outdir, romsA, _logger);
|
||||
success &= Output.WriteToDat(Path.GetFileNameWithoutExtension(_filename) + "." + _extB, Path.GetFileNameWithoutExtension(_filename) + "." + _extB,
|
||||
"", "", "", "", false, !RomManipulation.IsXmlDat(_filename), _outdir, romsB, _logger);
|
||||
bool success = Output.WriteToDatFromDict(datdataA, _outdir, _logger);
|
||||
success &= Output.WriteToDatFromDict(datdataB, _outdir, _logger);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -82,14 +82,32 @@ namespace SabreTools
|
||||
/// <param name="rename">True if roms are to be renamed</param>
|
||||
private void ProcessDAT(string filename, string path, bool rename)
|
||||
{
|
||||
List<RomData> roms = RomManipulation.Parse(filename, 0, 0, _logger);
|
||||
DatData datdata = new DatData
|
||||
{
|
||||
Name = "",
|
||||
Description = "",
|
||||
Category = "",
|
||||
Version = "",
|
||||
Date = "",
|
||||
Author = "",
|
||||
Email = "",
|
||||
Homepage = "",
|
||||
Url = "",
|
||||
Comment = "",
|
||||
Roms = new Dictionary<string, List<RomData>>(),
|
||||
ForcePacking = (_forceunpack ? ForcePacking.Unzip : ForcePacking.None),
|
||||
OutputFormat = RomManipulation.GetOutputFormat(filename),
|
||||
};
|
||||
datdata = RomManipulation.ParseDict(filename, 0, 0, datdata, _logger);
|
||||
|
||||
// Trim all file names according to the path that's set
|
||||
List<RomData> outroms = new List<RomData>();
|
||||
while (roms.Count != 0)
|
||||
foreach (string key in datdata.Roms.Keys)
|
||||
{
|
||||
RomData rom = roms[0];
|
||||
roms.RemoveAt(0);
|
||||
List<RomData> outroms = new List<RomData>();
|
||||
|
||||
foreach (RomData actrom in datdata.Roms[key])
|
||||
{
|
||||
RomData rom = actrom;
|
||||
|
||||
// If we are in single game mode, rename all games
|
||||
if (rename)
|
||||
@@ -108,14 +126,14 @@ namespace SabreTools
|
||||
|
||||
outroms.Add(rom);
|
||||
}
|
||||
datdata.Roms[key] = outroms;
|
||||
}
|
||||
|
||||
// Now write the file out accordingly
|
||||
string datName = RomManipulation.GetDatName(filename, _logger);
|
||||
string datDescription = RomManipulation.GetDatDescription(filename, _logger);
|
||||
Output.WriteToDat(datName, datDescription, "", "", "", "", _forceunpack, !RomManipulation.IsXmlDat(filename), Path.GetDirectoryName(filename), outroms, _logger);
|
||||
Output.WriteToDatFromDict(datdata, Path.GetDirectoryName(filename), _logger);
|
||||
|
||||
// Remove the original file if different and inform the user
|
||||
if (filename != datDescription + (RomManipulation.IsXmlDat(filename) ? ".xml" : ".dat"))
|
||||
if (filename != datdata.Description + (RomManipulation.GetOutputFormat(filename) == OutputFormat.Xml ? ".xml" : ".dat"))
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@@ -17,96 +17,36 @@ namespace SabreTools.Helper
|
||||
public static string SHA1Zero = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
|
||||
|
||||
/// <summary>
|
||||
/// Return if the file is XML or not
|
||||
/// Get what type of DAT the input file is
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <returns>True if the DAT is probably XML, false otherwise</returns>
|
||||
public static bool IsXmlDat(string filename)
|
||||
/// <returns>The OutputFormat corresponding to the DAT</returns>
|
||||
public static OutputFormat GetOutputFormat(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
StreamReader sr = new StreamReader(File.OpenRead(filename));
|
||||
string first = sr.ReadLine();
|
||||
sr.Close();
|
||||
return first.Contains("<") && first.Contains(">");
|
||||
if (first.Contains("<") && first.Contains(">"))
|
||||
{
|
||||
return OutputFormat.Xml;
|
||||
}
|
||||
else if (first.Contains("[") && first.Contains("]"))
|
||||
{
|
||||
return OutputFormat.RomCenter;
|
||||
}
|
||||
else
|
||||
{
|
||||
return OutputFormat.ClrMamePro;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
return OutputFormat.ClrMamePro;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return if the file is RomCenter or not
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <returns>True if the DAT is probably RomCenter, false otherwise</returns>
|
||||
public static bool IsRCDat(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
StreamReader sr = new StreamReader(File.OpenRead(filename));
|
||||
string first = sr.ReadLine();
|
||||
sr.Close();
|
||||
return first.Contains("[") && first.Contains("]");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the XmlDocument associated with a file, if possible
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="logger">Logger object for console and file output</param>
|
||||
/// <returns>The XmlDocument representing the (possibly converted) file, null otherwise</returns>
|
||||
public static XmlDocument GetXmlDocument(string filename, Logger logger)
|
||||
{
|
||||
logger.Log("Attempting to read file: " + filename);
|
||||
|
||||
// Check if file exists
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
logger.Warning("File '" + filename + "' could not read from!");
|
||||
return null;
|
||||
}
|
||||
|
||||
XmlDocument doc = new XmlDocument();
|
||||
try
|
||||
{
|
||||
doc.Load(filename);
|
||||
}
|
||||
catch (XmlException)
|
||||
{
|
||||
try
|
||||
{
|
||||
doc.LoadXml(Converters.ClrMameProToXML(File.ReadAllLines(filename)).ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex.ToString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
logger.Error("File '" + filename + "' could not be open or read");
|
||||
}
|
||||
catch (OutOfMemoryException)
|
||||
{
|
||||
logger.Error("File '" + filename + "' is too large to be processed!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex.ToString());
|
||||
return null;
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the XmlTextReader associated with a file, if possible
|
||||
/// </summary>
|
||||
@@ -124,129 +64,30 @@ namespace SabreTools.Helper
|
||||
return null;
|
||||
}
|
||||
|
||||
if (IsXmlDat(filename))
|
||||
XmlTextReader xtr;
|
||||
StringReader sr;
|
||||
switch (GetOutputFormat(filename))
|
||||
{
|
||||
case OutputFormat.Xml:
|
||||
logger.Log("XML DAT detected");
|
||||
XmlTextReader xtr = new XmlTextReader(filename);
|
||||
xtr.WhitespaceHandling = WhitespaceHandling.None;
|
||||
xtr.DtdProcessing = DtdProcessing.Ignore;
|
||||
return xtr;
|
||||
}
|
||||
else if (IsRCDat(filename))
|
||||
{
|
||||
xtr = new XmlTextReader(filename);
|
||||
break;
|
||||
case OutputFormat.RomCenter:
|
||||
logger.Log("RomCenter DAT detected");
|
||||
StringReader sr = new StringReader(Converters.RomCenterToXML(File.ReadAllLines(filename)).ToString());
|
||||
XmlTextReader xtr = new XmlTextReader(sr);
|
||||
xtr.WhitespaceHandling = WhitespaceHandling.None;
|
||||
xtr.DtdProcessing = DtdProcessing.Ignore;
|
||||
return xtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
sr = new StringReader(Converters.RomCenterToXML(File.ReadAllLines(filename)).ToString());
|
||||
xtr = new XmlTextReader(sr);
|
||||
break;
|
||||
default:
|
||||
logger.Log("ClrMamePro DAT detected");
|
||||
StringReader sr = new StringReader(Converters.ClrMameProToXML(File.ReadAllLines(filename)).ToString());
|
||||
XmlTextReader xtr = new XmlTextReader(sr);
|
||||
sr = new StringReader(Converters.ClrMameProToXML(File.ReadAllLines(filename)).ToString());
|
||||
xtr = new XmlTextReader(sr);
|
||||
break;
|
||||
}
|
||||
|
||||
xtr.WhitespaceHandling = WhitespaceHandling.None;
|
||||
xtr.DtdProcessing = DtdProcessing.Ignore;
|
||||
return xtr;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the name of the DAT for external use
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="logger">Logger object for console and file output</param>
|
||||
/// <returns>The internal name of the DAT on success, empty string otherwise</returns>
|
||||
/// <remarks>Needs to be upgraded to XmlTextReader</remarks>
|
||||
public static string GetDatName(string filename, Logger logger)
|
||||
{
|
||||
string name = "";
|
||||
XmlDocument doc = GetXmlDocument(filename, logger);
|
||||
|
||||
// If the returned document is null, return the blank string
|
||||
if (doc == null)
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
// Experimental looping using only XML parsing
|
||||
XmlNode node = doc.FirstChild;
|
||||
if (node != null && node.Name == "xml")
|
||||
{
|
||||
// Skip over everything that's not an element
|
||||
while (node.NodeType != XmlNodeType.Element)
|
||||
{
|
||||
node = node.NextSibling;
|
||||
}
|
||||
}
|
||||
|
||||
// Once we find the main body, enter it
|
||||
if (node != null && (node.Name == "datafile" || node.Name == "softwarelist"))
|
||||
{
|
||||
node = node.FirstChild;
|
||||
}
|
||||
|
||||
// Get the name from the header
|
||||
if (node != null && node.Name == "header")
|
||||
{
|
||||
XmlNode temp = node.SelectSingleNode("name");
|
||||
if (temp != null)
|
||||
{
|
||||
name = temp.InnerText;
|
||||
}
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the description of the DAT for external use
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="logger">Logger object for console and file output</param>
|
||||
/// <returns>The internal name of the DAT on success, empty string otherwise</returns>
|
||||
/// <remarks>Needs to be upgraded to XmlTextReader</remarks>
|
||||
public static string GetDatDescription(string filename, Logger logger)
|
||||
{
|
||||
string desc = "";
|
||||
XmlDocument doc = GetXmlDocument(filename, logger);
|
||||
|
||||
// If the returned document is null, return the blank string
|
||||
if (doc == null)
|
||||
{
|
||||
return desc;
|
||||
}
|
||||
|
||||
// Experimental looping using only XML parsing
|
||||
XmlNode node = doc.FirstChild;
|
||||
if (node != null && node.Name == "xml")
|
||||
{
|
||||
// Skip over everything that's not an element
|
||||
while (node.NodeType != XmlNodeType.Element)
|
||||
{
|
||||
node = node.NextSibling;
|
||||
}
|
||||
}
|
||||
|
||||
// Once we find the main body, enter it
|
||||
if (node != null && (node.Name == "datafile" || node.Name == "softwarelist"))
|
||||
{
|
||||
node = node.FirstChild;
|
||||
}
|
||||
|
||||
// Get the name from the header
|
||||
if (node != null && node.Name == "header")
|
||||
{
|
||||
XmlNode temp = node.SelectSingleNode("description");
|
||||
if (temp != null)
|
||||
{
|
||||
desc = temp.InnerText;
|
||||
}
|
||||
}
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a DAT and return all found games and roms within
|
||||
|
||||
Reference in New Issue
Block a user