mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Make ItemDictionary responsible for safely adding
This commit is contained in:
@@ -178,96 +178,7 @@ namespace SabreTools.DatFiles
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <returns>The key for the item</returns>
|
||||
protected string ParseAddHelper(DatItem item, bool statsOnly)
|
||||
{
|
||||
string key;
|
||||
|
||||
// If we have a Disk, Media, or Rom, clean the hash data
|
||||
if (item is Disk disk)
|
||||
{
|
||||
// If the file has aboslutely no hashes, skip and log
|
||||
if (disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() != ItemStatus.Nodump
|
||||
&& string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key))
|
||||
&& string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key)))
|
||||
{
|
||||
logger.Verbose($"Incomplete entry for '{disk.GetName()}' will be output as nodump");
|
||||
disk.SetFieldValue<string?>(Models.Metadata.Disk.StatusKey, ItemStatus.Nodump.AsStringValue());
|
||||
}
|
||||
|
||||
item = disk;
|
||||
}
|
||||
if (item is Media media)
|
||||
{
|
||||
// If the file has aboslutely no hashes, skip and log
|
||||
if (string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.MD5Key))
|
||||
&& string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key))
|
||||
&& string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA256Key))
|
||||
&& string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SpamSumKey)))
|
||||
{
|
||||
logger.Verbose($"Incomplete entry for '{media.GetName()}' will be output as nodump");
|
||||
}
|
||||
|
||||
item = media;
|
||||
}
|
||||
else if (item is Rom rom)
|
||||
{
|
||||
long? size = rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey);
|
||||
|
||||
// If we have the case where there is SHA-1 and nothing else, we don't fill in any other part of the data
|
||||
if (size == null && !rom.HasHashes())
|
||||
{
|
||||
// No-op, just catch it so it doesn't go further
|
||||
logger.Verbose($"{Header.GetStringFieldValue(DatHeader.FileNameKey)}: Entry with only SHA-1 found - '{rom.GetName()}'");
|
||||
}
|
||||
|
||||
// If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info
|
||||
else if ((size == 0 || size == null)
|
||||
&& (string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.CRCKey)) || rom.HasZeroHash()))
|
||||
{
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SizeKey, Constants.SizeZero.ToString());
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, Constants.CRCZero);
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.MD5Key, Constants.MD5Zero);
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA1Key, Constants.SHA1Zero);
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA256Key, null); // Constants.SHA256Zero;
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA384Key, null); // Constants.SHA384Zero;
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA512Key, null); // Constants.SHA512Zero;
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SpamSumKey, null); // Constants.SpamSumZero;
|
||||
}
|
||||
|
||||
// If the file has no size and it's not the above case, skip and log
|
||||
else if (rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() != ItemStatus.Nodump && (size == 0 || size == null))
|
||||
{
|
||||
logger.Verbose($"{Header.GetStringFieldValue(DatHeader.FileNameKey)}: Incomplete entry for '{rom.GetName()}' will be output as nodump");
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.StatusKey, ItemStatus.Nodump.AsStringValue());
|
||||
}
|
||||
|
||||
// If the file has a size but aboslutely no hashes, skip and log
|
||||
else if (rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() != ItemStatus.Nodump
|
||||
&& size != null && size > 0
|
||||
&& !rom.HasHashes())
|
||||
{
|
||||
logger.Verbose($"{Header.GetStringFieldValue(DatHeader.FileNameKey)}: Incomplete entry for '{rom.GetName()}' will be output as nodump");
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.StatusKey, ItemStatus.Nodump.AsStringValue());
|
||||
}
|
||||
|
||||
item = rom;
|
||||
}
|
||||
|
||||
// Get the key and add the file
|
||||
key = item.GetKey(ItemKey.Machine);
|
||||
|
||||
// If only adding statistics, we add an empty key for games and then just item stats
|
||||
if (statsOnly)
|
||||
{
|
||||
Items.EnsureKey(key);
|
||||
Items.DatStatistics.AddItemStatistics(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
Items.Add(key, item);
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
=> Items.AddItem(item, statsOnly);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ using System.Xml.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Core.Filter;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.DatItems;
|
||||
using SabreTools.DatItems.Formats;
|
||||
using SabreTools.Hashing;
|
||||
@@ -180,6 +181,104 @@ namespace SabreTools.DatFiles
|
||||
AddRange(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a DatItem to the dictionary after checking
|
||||
/// </summary>
|
||||
/// <param name="item">Item data to check against</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <returns>The key for the item</returns>
|
||||
public string AddItem(DatItem item, bool statsOnly)
|
||||
{
|
||||
string key;
|
||||
|
||||
// If we have a Disk, Media, or Rom, clean the hash data
|
||||
if (item is Disk disk)
|
||||
{
|
||||
// If the file has aboslutely no hashes, skip and log
|
||||
if (disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() != ItemStatus.Nodump
|
||||
&& string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key))
|
||||
&& string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key)))
|
||||
{
|
||||
logger.Verbose($"Incomplete entry for '{disk.GetName()}' will be output as nodump");
|
||||
disk.SetFieldValue<string?>(Models.Metadata.Disk.StatusKey, ItemStatus.Nodump.AsStringValue());
|
||||
}
|
||||
|
||||
item = disk;
|
||||
}
|
||||
if (item is Media media)
|
||||
{
|
||||
// If the file has aboslutely no hashes, skip and log
|
||||
if (string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.MD5Key))
|
||||
&& string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key))
|
||||
&& string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA256Key))
|
||||
&& string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SpamSumKey)))
|
||||
{
|
||||
logger.Verbose($"Incomplete entry for '{media.GetName()}' will be output as nodump");
|
||||
}
|
||||
|
||||
item = media;
|
||||
}
|
||||
else if (item is Rom rom)
|
||||
{
|
||||
long? size = rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey);
|
||||
|
||||
// If we have the case where there is SHA-1 and nothing else, we don't fill in any other part of the data
|
||||
if (size == null && !rom.HasHashes())
|
||||
{
|
||||
// No-op, just catch it so it doesn't go further
|
||||
//logger.Verbose($"{Header.GetStringFieldValue(DatHeader.FileNameKey)}: Entry with only SHA-1 found - '{rom.GetName()}'");
|
||||
}
|
||||
|
||||
// If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info
|
||||
else if ((size == 0 || size == null)
|
||||
&& (string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.CRCKey)) || rom.HasZeroHash()))
|
||||
{
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SizeKey, Constants.SizeZero.ToString());
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, Constants.CRCZero);
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.MD5Key, Constants.MD5Zero);
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA1Key, Constants.SHA1Zero);
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA256Key, null); // Constants.SHA256Zero;
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA384Key, null); // Constants.SHA384Zero;
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA512Key, null); // Constants.SHA512Zero;
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SpamSumKey, null); // Constants.SpamSumZero;
|
||||
}
|
||||
|
||||
// If the file has no size and it's not the above case, skip and log
|
||||
else if (rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() != ItemStatus.Nodump && (size == 0 || size == null))
|
||||
{
|
||||
//logger.Verbose($"{Header.GetStringFieldValue(DatHeader.FileNameKey)}: Incomplete entry for '{rom.GetName()}' will be output as nodump");
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.StatusKey, ItemStatus.Nodump.AsStringValue());
|
||||
}
|
||||
|
||||
// If the file has a size but aboslutely no hashes, skip and log
|
||||
else if (rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() != ItemStatus.Nodump
|
||||
&& size != null && size > 0
|
||||
&& !rom.HasHashes())
|
||||
{
|
||||
//logger.Verbose($"{Header.GetStringFieldValue(DatHeader.FileNameKey)}: Incomplete entry for '{rom.GetName()}' will be output as nodump");
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.StatusKey, ItemStatus.Nodump.AsStringValue());
|
||||
}
|
||||
|
||||
item = rom;
|
||||
}
|
||||
|
||||
// Get the key and add the file
|
||||
key = item.GetKey(ItemKey.Machine);
|
||||
|
||||
// If only adding statistics, we add an empty key for games and then just item stats
|
||||
if (statsOnly)
|
||||
{
|
||||
EnsureKey(key);
|
||||
DatStatistics.AddItemStatistics(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
Add(key, item);
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a range of values to the file dictionary
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user