mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
More code cleanup
This change comprises of multiple smaller changes: - Convert all WriteToDat to WriteToDatFromDict, including MissFile - Change obsolete Import to use updated ParseDict - Remove all references to Parse - Rename all references to "Dict" specific code to shorter names - Remove more unused methods rendered obsolete by other changes
This commit is contained in:
@@ -89,152 +89,6 @@ namespace SabreTools.Helper
|
||||
return xtr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a DAT and return all found games and roms within
|
||||
/// </summary>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="sysid">System ID for the DAT</param>
|
||||
/// <param name="srcid">Source ID for the DAT</param>
|
||||
/// <param name="logger">Logger object for console and/or file output</param>
|
||||
/// <returns>List of RomData objects representing the found data</returns>
|
||||
public static List<RomData> Parse(string filename, int sysid, int srcid, Logger logger)
|
||||
{
|
||||
List<RomData> roms = new List<RomData>();
|
||||
XmlTextReader xtr = GetXmlTextReader(filename, logger);
|
||||
bool superdat = false, shouldbreak = false;
|
||||
string parent = "";
|
||||
if (xtr != null)
|
||||
{
|
||||
xtr.MoveToContent();
|
||||
while (xtr.NodeType != XmlNodeType.None)
|
||||
{
|
||||
// We only want elements
|
||||
if (xtr.NodeType != XmlNodeType.Element)
|
||||
{
|
||||
xtr.Read();
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (xtr.Name)
|
||||
{
|
||||
case "datafile":
|
||||
case "softwarelist":
|
||||
parent = xtr.Name;
|
||||
xtr.Read();
|
||||
break;
|
||||
case "header":
|
||||
xtr.ReadToDescendant("name");
|
||||
string content = xtr.ReadElementContentAsString();
|
||||
superdat = (content != null ? content.Contains(" - SuperDAT") : false);
|
||||
while (xtr.Name != "header")
|
||||
{
|
||||
xtr.Read();
|
||||
}
|
||||
xtr.Read();
|
||||
break;
|
||||
case "machine":
|
||||
case "game":
|
||||
case "software":
|
||||
string temptype = xtr.Name;
|
||||
string tempname = "";
|
||||
|
||||
// We want to process the entire subtree of the game
|
||||
XmlReader subreader = xtr.ReadSubtree();
|
||||
|
||||
if (subreader != null)
|
||||
{
|
||||
if (temptype == "software" && subreader.ReadToFollowing("description"))
|
||||
{
|
||||
tempname = subreader.ReadElementContentAsString();
|
||||
}
|
||||
else
|
||||
{
|
||||
// There are rare cases where a malformed XML will not have the required attributes. We can only skip them.
|
||||
if (xtr.AttributeCount == 0)
|
||||
{
|
||||
logger.Error("No attributes were found");
|
||||
xtr.ReadToNextSibling(xtr.Name);
|
||||
continue;
|
||||
}
|
||||
tempname = xtr.GetAttribute("name");
|
||||
}
|
||||
|
||||
if (superdat)
|
||||
{
|
||||
tempname = Regex.Match(tempname, @".*?\\(.*)").Groups[1].Value;
|
||||
}
|
||||
|
||||
while (subreader.Read())
|
||||
{
|
||||
// We only want elements
|
||||
if (subreader.NodeType != XmlNodeType.Element)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the roms from the machine
|
||||
switch (subreader.Name)
|
||||
{
|
||||
case "rom":
|
||||
case "disk":
|
||||
// Take care of hex-sized files
|
||||
long size = -1;
|
||||
if (xtr.GetAttribute("size") != null && xtr.GetAttribute("size").Contains("0x"))
|
||||
{
|
||||
size = Convert.ToInt64(xtr.GetAttribute("size"), 16);
|
||||
}
|
||||
else if (xtr.GetAttribute("size") != null)
|
||||
{
|
||||
Int64.TryParse(xtr.GetAttribute("size"), out size);
|
||||
}
|
||||
|
||||
// Take care of hex-prefixed hashes
|
||||
string crc = (xtr.GetAttribute("crc") != null ? xtr.GetAttribute("crc").ToLowerInvariant().Trim() : "");
|
||||
crc = (crc.StartsWith("0x") ? crc.Remove(0, 2) : crc);
|
||||
string md5 = (xtr.GetAttribute("md5") != null ? xtr.GetAttribute("md5").ToLowerInvariant().Trim() : "");
|
||||
md5 = (md5.StartsWith("0x") ? md5.Remove(0, 2) : md5);
|
||||
string sha1 = (xtr.GetAttribute("sha1") != null ? xtr.GetAttribute("sha1").ToLowerInvariant().Trim() : "");
|
||||
sha1 = (sha1.StartsWith("0x") ? sha1.Remove(0, 2) : sha1);
|
||||
|
||||
roms.Add(new RomData
|
||||
{
|
||||
Game = tempname,
|
||||
Name = xtr.GetAttribute("name"),
|
||||
Type = xtr.Name,
|
||||
SystemID = sysid,
|
||||
SourceID = srcid,
|
||||
Size = size,
|
||||
CRC = crc,
|
||||
MD5 = md5,
|
||||
SHA1 = sha1,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Read to next game
|
||||
if (!xtr.ReadToNextSibling(temptype))
|
||||
{
|
||||
shouldbreak = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
xtr.Read();
|
||||
break;
|
||||
}
|
||||
|
||||
// If we hit an endpoint, break out of the loop early
|
||||
if (shouldbreak)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return roms;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a DAT and return all found games and roms within
|
||||
/// </summary>
|
||||
@@ -244,7 +98,7 @@ namespace SabreTools.Helper
|
||||
/// <param name="datdata">The DatData object representing found roms to this point</param>
|
||||
/// <param name="logger">Logger object for console and/or file output</param>
|
||||
/// <returns>DatData object representing the read-in data</returns>
|
||||
public static DatData ParseDict(string filename, int sysid, int srcid, DatData datdata, Logger logger)
|
||||
public static DatData Parse(string filename, int sysid, int srcid, DatData datdata, Logger logger)
|
||||
{
|
||||
XmlTextReader xtr = GetXmlTextReader(filename, logger);
|
||||
bool superdat = false, shouldbreak = false;
|
||||
@@ -695,50 +549,5 @@ namespace SabreTools.Helper
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get differences between two lists of RomData objects
|
||||
/// </summary>
|
||||
/// <param name="A">First RomData list</param>
|
||||
/// <param name="B">Second RomData list</param>
|
||||
/// <returns>Any rom that's not in both lists</returns>
|
||||
/// <remarks>Adapted from http://stackoverflow.com/questions/5620266/the-opposite-of-intersect</remarks>
|
||||
public static List<RomData> Diff(List<RomData> A, List<RomData> B)
|
||||
{
|
||||
List<String> AString = Output.RomDataToString(A);
|
||||
List<String> BString = Output.RomDataToString(B);
|
||||
List<String> CString = AString.Except(BString).Union(BString.Except(AString)).ToList();
|
||||
return Output.StringToRomData(CString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all RomData objects that are in A but not in B
|
||||
/// </summary>
|
||||
/// <param name="A">First RomData list</param>
|
||||
/// <param name="B">Second RomData list</param>
|
||||
/// <returns>Any rom that's only in the first list</returns>
|
||||
/// <remarks>Adapted from http://stackoverflow.com/questions/5620266/the-opposite-of-intersect</remarks>
|
||||
public static List<RomData> DiffOnlyInA(List<RomData> A, List<RomData> B)
|
||||
{
|
||||
List<String> AString = Output.RomDataToString(A);
|
||||
List<String> BString = Output.RomDataToString(B);
|
||||
List<String> CString = AString.Except(BString).ToList();
|
||||
return Output.StringToRomData(CString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all RomData objects that are in A and B
|
||||
/// </summary>
|
||||
/// <param name="A">First RomData list</param>
|
||||
/// <param name="B">Second RomData list</param>
|
||||
/// <returns>Any rom that's in both lists</returns>
|
||||
/// <remarks>Adapted from http://stackoverflow.com/questions/5620266/the-opposite-of-intersect</remarks>
|
||||
public static List<RomData> DiffInAB(List<RomData> A, List<RomData> B)
|
||||
{
|
||||
List<String> AString = Output.RomDataToString(A);
|
||||
List<String> BString = Output.RomDataToString(B);
|
||||
List<String> CString = AString.Union(BString).Except(AString.Except(BString).Union(BString.Except(AString))).ToList();
|
||||
return Output.StringToRomData(CString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user