[ALL] More work on conversion

This commit is contained in:
Matt Nadareski
2016-08-29 16:48:36 -07:00
parent a1a7e411d5
commit 4e82b63ea6
4 changed files with 151 additions and 74 deletions

View File

@@ -25,13 +25,40 @@ namespace SabreTools.Helper
/// <summary>
/// Intermediate struct for holding and processing Hash data (NEW SYSTEM)
/// </summary>
public struct HashData
public struct HashData : IEquatable<HashData>
{
public long Size;
public byte[] CRC;
public byte[] MD5;
public byte[] SHA1;
public List<RomData> Roms;
public bool Equals(HashData other)
{
return this.Equals(other, false);
}
public bool Equals(HashData other, bool IsDisk)
{
bool equals = false;
if (!IsDisk &&
((this.MD5 == null || other.MD5 == null) || this.MD5 == other.MD5) &&
((this.SHA1 == null || other.SHA1 == null) || this.SHA1 == other.SHA1))
{
equals = true;
}
else if (!IsDisk &&
(this.Size == other.Size) &&
((this.CRC == null || other.CRC != null) || this.CRC == other.CRC) &&
((this.MD5 == null || other.MD5 == null) || this.MD5 == other.MD5) &&
((this.SHA1 == null || other.SHA1 == null) || this.SHA1 == other.SHA1))
{
equals = true;
}
return equals;
}
}
/// <summary>

View File

@@ -853,9 +853,9 @@ namespace SabreTools.Helper
/// <param name="output">Output directory to build to</param>
/// <param name="hash">RomData representing the new information</param>
/// <remarks>This uses the new system that is not implemented anywhere yet</remarks>
public static void WriteToArchive(string input, string output, RomData rom, int machineIndex)
public static void WriteToArchive(string input, string output, RomData rom)
{
string archiveFileName = Path.Combine(output, rom.Machines[machineIndex] + ".zip");
string archiveFileName = Path.Combine(output, rom.Machine + ".zip");
ZipArchive outarchive = null;
try
@@ -904,9 +904,9 @@ namespace SabreTools.Helper
/// <param name="output">Output directory to build to</param>
/// <param name="rom">RomData representing the new information</param>
/// <remarks>This uses the new system that is not implemented anywhere yet</remarks>
public static void WriteToManagedArchive(string input, string output, RomData rom, int machineIndex)
public static void WriteToManagedArchive(string input, string output, RomData rom)
{
string archiveFileName = Path.Combine(output, rom.Machines[machineIndex] + ".zip");
string archiveFileName = Path.Combine(output, rom.Machine + ".zip");
// Delete an empty file first
if (File.Exists(archiveFileName) && new FileInfo(archiveFileName).Length == 0)
@@ -1013,18 +1013,16 @@ namespace SabreTools.Helper
+ (size == 0 ? reader.Entry.Size : size) + ", "
+ (crc == null ? BitConverter.GetBytes(reader.Entry.Crc) : crc));
MachineData tempmachine = new MachineData
{
Name = gamename,
Description = gamename,
};
RomData temprom = new RomData
{
Type = ItemType.Rom,
Name = reader.Entry.Key,
Machines = new List<MachineData>(),
Machine = new MachineData
{
Name = gamename,
Description = gamename,
},
};
temprom.Machines.Add(tempmachine);
HashData temphash = new HashData
{
Size = (size == 0 ? reader.Entry.Size : size),
@@ -1105,24 +1103,22 @@ namespace SabreTools.Helper
// Now convert the size and get the right position
long extractedsize = (long)BitConverter.ToUInt64(headersz.Reverse().ToArray(), 0);
MachineData tempmachine = new MachineData
{
Name = sha1,
Description = sha1,
};
RomData temprom = new RomData
{
Type = ItemType.Rom,
Name = sha1,
Machines = new List<MachineData>(),
Machine = new MachineData
{
Name = sha1,
Description = sha1,
},
};
temprom.Machines.Add(tempmachine);
HashData temphash = new HashData
{
Size = extractedsize,
CRC = headercrc,
MD5 = headermd5,
SHA1 = StringToByteArray(sha1),
SHA1 = Style.StringToByteArray(sha1),
Roms = new List<RomData>(),
};
temphash.Roms.Add(temprom);

View File

@@ -2389,19 +2389,17 @@ namespace SabreTools.Helper
// If we're in cleaning mode, sanitize the game name
gamename = (clean ? Style.CleanGameName(gamename) : gamename);
MachineData machineData = new MachineData
RomData romData = new RomData
{
Type = (line.Trim().StartsWith("disk (") ? ItemType.Disk : ItemType.Rom),
Machine = new MachineData
{
Name = gamename,
Description = gamedesc,
SystemID = sysid,
SourceID = srcid,
},
};
RomData romData = new RomData
{
Type = (line.Trim().StartsWith("disk (") ? ItemType.Disk : ItemType.Rom),
Machines = new List<MachineData>(),
};
romData.Machines.Add(machineData);
HashData hashData = new HashData
{
Roms = new List<RomData>(),
@@ -2788,7 +2786,10 @@ namespace SabreTools.Helper
// If we're in cleaning mode, sanitize the game name
rominfo[3] = (clean ? Style.CleanGameName(rominfo[3]) : rominfo[3]);
MachineData machineData = new MachineData
RomData romData = new RomData
{
Name = rominfo[5],
Machine = new MachineData
{
Name = rominfo[3],
Description = rominfo[4],
@@ -2796,13 +2797,8 @@ namespace SabreTools.Helper
RomOf = rominfo[1],
SystemID = sysid,
SourceID = srcid,
},
};
RomData romData = new RomData
{
Name = rominfo[5],
Machines = new List<MachineData>(),
};
romData.Machines.Add(machineData);
HashData hashData = new HashData
{
Size = Int64.Parse(rominfo[7]),
@@ -2900,18 +2896,16 @@ namespace SabreTools.Helper
// If we're in cleaning mode, sanitize the game name
tempgame = (clean ? Style.CleanGameName(tempgame) : tempgame);
MachineData machineData = new MachineData
{
Name = tempgame,
Description = tempgame,
};
RomData romData = new RomData
{
Type = ItemType.Rom,
Name = "null",
Machines = new List<MachineData>(),
Machine = new MachineData
{
Name = tempgame,
Description = tempgame,
},
};
romData.Machines.Add(machineData);
HashData hashData = new HashData
{
Size = -1,
@@ -3341,23 +3335,21 @@ namespace SabreTools.Helper
}
// Get the new values to add
MachineData machineData = new MachineData
{
Name = tempname,
Description = gamedesc,
SystemID = sysid,
System = filename,
SourceID = srcid,
};
RomData romData = new RomData
{
Name = subreader.GetAttribute("name"),
Type = (subreader.Name.ToLowerInvariant() == "disk" ? ItemType.Disk : ItemType.Rom),
Nodump = nodump,
Date = date,
Machines = new List<MachineData>(),
Machine = new MachineData
{
Name = tempname,
Description = gamedesc,
SystemID = sysid,
System = filename,
SourceID = srcid,
},
};
romData.Machines.Add(machineData);
HashData hashData = new HashData
{
Size = size,
@@ -3396,18 +3388,16 @@ namespace SabreTools.Helper
// If we're in cleaning mode, sanitize the game name
tempname = (clean ? Style.CleanGameName(tempname.Split(Path.DirectorySeparatorChar)) : tempname);
MachineData machineData = new MachineData
{
Name = tempname,
Description = tempname,
};
RomData romData = new RomData
{
Type = ItemType.Rom,
Name = "null",
Machines = new List<MachineData>(),
Machine = new MachineData
{
Name = tempname,
Description = tempname,
},
};
romData.Machines.Add(machineData);
HashData hashData = new HashData
{
Size = -1,
@@ -3568,22 +3558,20 @@ namespace SabreTools.Helper
}
// Get the new values to add
MachineData machineData = new MachineData
{
Name = tempname,
SystemID = sysid,
System = filename,
SourceID = srcid,
};
RomData romData = new RomData
{
Name = xtr.GetAttribute("name"),
Type = (xtr.GetAttribute("type").ToLowerInvariant() == "disk" ? ItemType.Disk : ItemType.Rom),
Nodump = nodump,
Date = date,
Machines = new List<MachineData>(),
Machine = new MachineData
{
Name = tempname,
SystemID = sysid,
System = filename,
SourceID = srcid,
},
};
romData.Machines.Add(machineData);
HashData hashData = new HashData
{
Size = size,
@@ -3682,7 +3670,7 @@ namespace SabreTools.Helper
}
else
{
List<Rom> temp = new List<Rom>();
List<HashData> temp = new List<HashData>();
temp.Add(hash);
sortable.Add(newkey, temp);
}

View File

@@ -350,6 +350,26 @@ namespace SabreTools.Helper
return dupefound;
}
/// <summary>
/// Determine if a file is a duplicate using partial matching logic
/// </summary>
/// <param name="hash">Hash to check for duplicate status</param>
/// <param name="lasthash">Hash to use as a baseline</param>
/// <param name="logger">Logger object for console and/or file output</param>
/// <returns>True if the hashes are duplicates, false otherwise</returns>
public static bool IsDuplicate(HashData hash, HashData lasthash, Logger logger)
{
bool dupefound = hash.Equals(lasthash);
// More wonderful SHA-1 logging that has to be done
if (hash.SHA1 == lasthash.SHA1 && hash.Size != lasthash.Size)
{
logger.User("SHA-1 mismatch - Hash: " + hash.SHA1);
}
return dupefound;
}
/// <summary>
/// Return the duplicate status of two roms
/// </summary>
@@ -396,6 +416,52 @@ namespace SabreTools.Helper
return output;
}
/// <summary>
/// Return the duplicate status of two hashes
/// </summary>
/// <param name="hash">Current hash to check</param>
/// <param name="lasthash">Last hash to check against</param>
/// <param name="logger">Logger object for console and/or file output</param>
/// <returns>The DupeType corresponding to the relationship between the two</returns>
public static DupeType GetDuplicateStatus(HashData hash, HashData lasthash, Logger logger)
{
DupeType output = DupeType.None;
// If we don't have a duplicate at all, return none
if (!IsDuplicate(hash, lasthash, logger))
{
return output;
}
// If the duplicate is external already or should be, set it
if (lasthash.Roms[0].DupeType >= DupeType.ExternalHash || lasthash.Roms[0].Machine.SystemID != hash.Roms[0].Machine.SystemID || lasthash.Roms[0].Machine.SourceID != hash.Roms[0].Machine.SourceID)
{
if (lasthash.Roms[0].Machine.Name == hash.Roms[0].Machine.Name && lasthash.Roms[0].Name == hash.Roms[0].Name)
{
output = DupeType.ExternalAll;
}
else
{
output = DupeType.ExternalHash;
}
}
// Otherwise, it's considered an internal dupe
else
{
if (lasthash.Roms[0].Machine.Name == hash.Roms[0].Machine.Name && lasthash.Roms[0].Name == hash.Roms[0].Name)
{
output = DupeType.InternalAll;
}
else
{
output = DupeType.InternalHash;
}
}
return output;
}
/// <summary>
/// Sort a list of RomData objects by SystemID, SourceID, Game, and Name (in order)
/// </summary>