[DatFile] Make the file dictionary transparent

This commit is contained in:
Matt Nadareski
2016-11-08 15:29:52 -08:00
parent a13f0f1635
commit 82a31ed470
10 changed files with 276 additions and 380 deletions

View File

@@ -443,9 +443,9 @@ namespace SabreTools
// Loop through the Dictionary and add all data
_logger.User("Adding new DAT information");
start = DateTime.Now;
foreach (string key in datroot.Files.Keys)
foreach (string key in datroot.Keys())
{
foreach (Rom value in datroot.Files[key])
foreach (Rom value in datroot[key])
{
AddDatToDatabase(value, dbc);
}
@@ -481,7 +481,7 @@ namespace SabreTools
// If the Dat wasn't empty, add the information
SqliteCommand slc = new SqliteCommand();
if (tempdat.Files.Count != 0)
if (tempdat.Count != 0)
{
string crcquery = "INSERT OR IGNORE INTO crc (crc) VALUES";
string md5query = "INSERT OR IGNORE INTO md5 (md5) VALUES";

View File

@@ -106,16 +106,7 @@ namespace SabreTools
}
// Add to the Dat
if (need.Files.ContainsKey(key))
{
need.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
need.Files.Add(key, temp);
}
need.Add(key, rom);
}
}
// Otherwise, just add the file to the list
@@ -145,16 +136,7 @@ namespace SabreTools
}
// Add to the Dat
if (need.Files.ContainsKey(key))
{
need.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
need.Files.Add(key, temp);
}
need.Add(key, rom);
}
}
}

View File

@@ -159,7 +159,7 @@ namespace SabreTools.Helper.Dats
get { return _mergeRoms; }
set { _mergeRoms = value; }
}
public SortedDictionary<string, List<DatItem>> Files
protected SortedDictionary<string, List<DatItem>> Files
{
get
{
@@ -273,6 +273,209 @@ namespace SabreTools.Helper.Dats
#region Instance Methods
#region Accessors
/// <summary>
/// Passthrough to access the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to reference</param>
public List<DatItem> this[string key]
{
get
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
_files.Add(key, new List<DatItem>());
}
// Now return the value
return _files[key];
}
set
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
_files.Add(key, new List<DatItem>());
}
// Now set the value
_files[key] = value;
}
}
/// <summary>
/// Add a new key to the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to add to</param>
public void Add(string key)
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
_files.Add(key, new List<DatItem>());
}
}
}
/// <summary>
/// Add a value to the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param>
public void Add(string key, DatItem value)
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
_files.Add(key, new List<DatItem>());
}
// Now add the value
_files[key].Add(value);
}
}
/// <summary>
/// Add a range of values to the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param>
public void AddRange(string key, List<DatItem> value)
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
_files.Add(key, new List<DatItem>());
}
// Now add the value
_files[key].AddRange(value);
}
}
/// <summary>
/// Get if the file dictionary contains the key
/// </summary>
/// <param name="key">Key in the dictionary to check</param>
/// <returns>True if the key exists, false otherwise</returns>
public bool ContainsKey(string key)
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
return _files.ContainsKey(key);
}
}
/// <summary>
/// Get the number of keys in the file dictionary
/// </summary>
/// <returns>Number of keys in the file dictionary</returns>
public long Count
{
get
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
return _files.Count;
}
}
}
/// <summary>
/// Get the keys from the file dictionary
/// </summary>
/// <returns>IEnumerable of the keys</returns>
public IEnumerable<string> Keys
{
get
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
return _files.Keys;
}
}
}
/// <summary>
/// Remove a key from the file dictionary
/// </summary>
/// <param name="key"></param>
public void Remove(string key)
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
{
_files = new SortedDictionary<string, List<DatItem>>();
}
// If the key is in the dictionary, remove it
if (_files.ContainsKey(key))
{
_files.Remove(key);
}
}
}
#endregion
#region Cloning Methods [MODULAR DONE]
public object Clone()

View File

@@ -241,7 +241,7 @@ namespace SabreTools.Helper.Dats
public bool HasDuplicates(DatFile datdata, Logger logger)
{
// Check for an empty rom list first
if (datdata.Files == null || datdata.Files.Count == 0)
if (datdata.Count == 0)
{
return false;
}
@@ -285,13 +285,13 @@ namespace SabreTools.Helper.Dats
}
// If the key doesn't exist, return the empty list
if (!datdata.Files.ContainsKey(key))
if (!datdata.ContainsKey(key))
{
return false;
}
// Try to find duplicates
List<DatItem> roms = datdata.Files[key];
List<DatItem> roms = datdata[key];
foreach (DatItem rom in roms)
{
@@ -316,7 +316,7 @@ namespace SabreTools.Helper.Dats
List<DatItem> output = new List<DatItem>();
// Check for an empty rom list first
if (datdata.Files == null || datdata.Files.Count == 0)
if (datdata.Count == 0)
{
return output;
}
@@ -360,13 +360,13 @@ namespace SabreTools.Helper.Dats
}
// If the key doesn't exist, return the empty list
if (!datdata.Files.ContainsKey(key))
if (!datdata.ContainsKey(key))
{
return output;
}
// Try to find duplicates
List<DatItem> roms = datdata.Files[key];
List<DatItem> roms = datdata[key];
List<DatItem> left = new List<DatItem>();
foreach (DatItem rom in roms)
@@ -384,7 +384,7 @@ namespace SabreTools.Helper.Dats
// If we're in removal mode, replace the list with the new one
if (remove)
{
datdata.Files[key] = left;
datdata[key] = left;
}
return output;

View File

@@ -58,16 +58,12 @@ namespace SabreTools.Helper.Dats
{
count++;
string newkey = (rom.Type == ItemType.Rom ? ((Rom)rom).CRC : Constants.CRCZero);
if (sortable.ContainsKey(newkey))
if (!sortable.ContainsKey(newkey))
{
sortable[newkey].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
sortable.Add(newkey, temp);
sortable.Add(newkey, new List<DatItem>());
}
sortable[newkey].Add(rom);
}
}
@@ -147,17 +143,14 @@ namespace SabreTools.Helper.Dats
{
newkey = newkey.ToLowerInvariant();
}
newkey = HttpUtility.HtmlEncode(newkey);
if (sortable.ContainsKey(newkey))
if (!sortable.ContainsKey(newkey))
{
sortable[newkey].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
sortable.Add(newkey, temp);
sortable.Add(newkey, new List<DatItem>());
}
sortable[newkey].Add(rom);
}
}
@@ -229,16 +222,12 @@ namespace SabreTools.Helper.Dats
: (rom.Type == ItemType.Disk
? ((Disk)rom).MD5
: Constants.MD5Zero));
if (sortable.ContainsKey(newkey))
if (!sortable.ContainsKey(newkey))
{
sortable[newkey].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
sortable.Add(newkey, temp);
sortable.Add(newkey, new List<DatItem>());
}
sortable[newkey].Add(rom);
}
}
@@ -310,16 +299,12 @@ namespace SabreTools.Helper.Dats
: (rom.Type == ItemType.Disk
? ((Disk)rom).SHA1
: Constants.MD5Zero));
if (sortable.ContainsKey(newkey))
if (!sortable.ContainsKey(newkey))
{
sortable[newkey].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
sortable.Add(newkey, temp);
sortable.Add(newkey, new List<DatItem>());
}
sortable[newkey].Add(rom);
}
}
@@ -387,16 +372,12 @@ namespace SabreTools.Helper.Dats
{
count++;
string newkey = (rom.Type == ItemType.Rom ? ((Rom)rom).Size.ToString() : "-1");
if (sortable.ContainsKey(newkey))
if (!sortable.ContainsKey(newkey))
{
sortable[newkey].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
sortable.Add(newkey, temp);
sortable.Add(newkey, new List<DatItem>());
}
sortable[newkey].Add(rom);
}
}
@@ -472,16 +453,12 @@ namespace SabreTools.Helper.Dats
? "Default"
: rom.Machine.Name.ToLowerInvariant());
newkey = HttpUtility.HtmlEncode(newkey);
if (sortable.ContainsKey(newkey))
if (!sortable.ContainsKey(newkey))
{
sortable[newkey].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
sortable.Add(newkey, temp);
sortable.Add(newkey, new List<DatItem>());
}
sortable[newkey].Add(rom);
}
return sortable;

View File

@@ -126,15 +126,8 @@ namespace SabreTools.Helper.Dats
List<string> keys = datHeaders[i].Files.Keys.ToList();
foreach (string key in keys)
{
if (Files.ContainsKey(key))
{
Files[key].AddRange(datHeaders[i].Files[key]);
}
else
{
Files.Add(key, datHeaders[i].Files[key]);
}
datHeaders[i].Files.Remove(key);
AddRange(key, datHeaders[i][key]);
datHeaders[i].Remove(key);
}
datHeaders[i].Files = null;
}
@@ -227,16 +220,7 @@ namespace SabreTools.Helper.Dats
// Individual DATs that are output
if ((diff & DiffMode.Individuals) != 0)
{
if (outDats[rom.SystemID].Files.ContainsKey(key))
{
outDats[rom.SystemID].Files[key].Add(rom);
}
else
{
List<DatItem> tl = new List<DatItem>();
tl.Add(rom);
outDats[rom.SystemID].Files.Add(key, tl);
}
outDats[rom.SystemID].Add(key, rom);
}
// Merged no-duplicates DAT
@@ -245,16 +229,7 @@ namespace SabreTools.Helper.Dats
DatItem newrom = rom;
newrom.Machine.Name += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")";
if (outerDiffData.Files.ContainsKey(key))
{
outerDiffData.Files[key].Add(newrom);
}
else
{
List<DatItem> tl = new List<DatItem>();
tl.Add(rom);
outerDiffData.Files.Add(key, tl);
}
outerDiffData.Add(key, newrom);
}
}
}
@@ -267,16 +242,7 @@ namespace SabreTools.Helper.Dats
DatItem newrom = rom;
newrom.Machine.Name += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")";
if (dupeData.Files.ContainsKey(key))
{
dupeData.Files[key].Add(newrom);
}
else
{
List<DatItem> tl = new List<DatItem>();
tl.Add(rom);
dupeData.Files.Add(key, tl);
}
dupeData.Add(key, newrom);
}
}
}
@@ -388,16 +354,7 @@ namespace SabreTools.Helper.Dats
continue;
}
if (outDats[rom.SystemID].Files.ContainsKey(key))
{
outDats[rom.SystemID].Files[key].Add(rom);
}
else
{
List<DatItem> tl = new List<DatItem>();
tl.Add(rom);
outDats[rom.SystemID].Files.Add(key, tl);
}
outDats[rom.SystemID].Add(key, rom);
}
}
}

View File

@@ -220,18 +220,8 @@ namespace SabreTools.Helper.Dats
if (rom != null && rom.Name != null)
{
// Add the list if it doesn't exist already
string key = rom.Size + "-" + rom.CRC;
lock (Files)
{
if (!Files.ContainsKey(key))
{
Files.Add(key, new List<DatItem>());
}
Files[key].Add(rom);
logger.User("File added: " + Path.GetFileNameWithoutExtension(item) + Environment.NewLine);
}
Add(rom.Size + "-" + rom.CRC, rom);
logger.User("File added: " + Path.GetFileNameWithoutExtension(item) + Environment.NewLine);
}
else
{
@@ -379,13 +369,7 @@ namespace SabreTools.Helper.Dats
}
// Add the list if it doesn't exist already
lock (Files)
{
if (!Files.ContainsKey(key))
{
Files.Add(key, new List<DatItem>());
}
}
Add(key);
try
{
@@ -478,19 +462,7 @@ namespace SabreTools.Helper.Dats
}
// Add the file information to the DAT
lock (Files)
{
if (Files.ContainsKey(key))
{
Files[key].Add(datItem);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(datItem);
Files.Add(key, temp);
}
}
Add(key, datItem);
logger.User("File added: " + romname + Environment.NewLine);
}

View File

@@ -2360,16 +2360,7 @@ namespace SabreTools.Helper.Dats
}
// Add the item to the DAT
if (Files.ContainsKey(key))
{
Files[key].Add(item);
}
else
{
List<DatItem> newvalue = new List<DatItem>();
newvalue.Add(item);
Files.Add(key, newvalue);
}
Add(key, item);
}
}
}

View File

@@ -152,17 +152,7 @@ namespace SabreTools.Helper.Dats
lock (Files)
{
string key = rom.Size + "-" + rom.CRC;
if (current.Files.ContainsKey(key))
{
current.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
current.Files.Add(key, temp);
}
current.Add(rom.Size + "-" + rom.CRC, rom);
}
// If we had a header, we want the full file information too
@@ -173,17 +163,7 @@ namespace SabreTools.Helper.Dats
lock (Files)
{
string key = rom.Size + "-" + rom.CRC;
if (current.Files.ContainsKey(key))
{
current.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
current.Files.Add(key, temp);
}
current.Add(rom.Size + "-" + rom.CRC, rom);
}
}
}
@@ -203,17 +183,7 @@ namespace SabreTools.Helper.Dats
lock (Files)
{
string key = rom.Size + "-" + rom.CRC;
if (current.Files.ContainsKey(key))
{
current.Files[key].Add(newrom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(newrom);
current.Files.Add(key, temp);
}
current.Add(rom.Size + "-" + rom.CRC, newrom);
}
}
}
@@ -236,17 +206,7 @@ namespace SabreTools.Helper.Dats
lock (Files)
{
string key = rom.Size + "-" + rom.CRC;
if (current.Files.ContainsKey(key))
{
current.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
current.Files.Add(key, temp);
}
current.Add(rom.Size + "-" + rom.CRC, rom);
}
// If we had a header, we want the full file information too
@@ -257,17 +217,7 @@ namespace SabreTools.Helper.Dats
lock (Files)
{
string key = rom.Size + "-" + rom.CRC;
if (current.Files.ContainsKey(key))
{
current.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
current.Files.Add(key, temp);
}
current.Add(rom.Size + "-" + rom.CRC, rom);
}
}
});
@@ -280,17 +230,7 @@ namespace SabreTools.Helper.Dats
lock (Files)
{
string key = rom.Size + "-" + rom.CRC;
if (current.Files.ContainsKey(key))
{
current.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
current.Files.Add(key, temp);
}
current.Add(rom.Size + "-" + rom.CRC, rom);
}
}
}
@@ -612,17 +552,7 @@ namespace SabreTools.Helper.Dats
if (rom.SourceID == 99)
{
found = true;
string key = rom.Size + "-" + rom.CRC;
if (matched.Files.ContainsKey(key))
{
matched.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
matched.Files.Add(key, temp);
}
matched.Add(rom.Size + "-" + rom.CRC, rom);
}
}
}

View File

@@ -92,52 +92,16 @@ namespace SabreTools.Helper.Dats
{
if (newExtA.Contains(Path.GetExtension(rom.Name.ToUpperInvariant())))
{
if (datdataA.Files.ContainsKey(key))
{
datdataA.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
datdataA.Files.Add(key, temp);
}
datdataA.Add(key, rom);
}
else if (newExtB.Contains(Path.GetExtension(rom.Name.ToUpperInvariant())))
{
if (datdataB.Files.ContainsKey(key))
{
datdataB.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
datdataB.Files.Add(key, temp);
}
datdataB.Add(key, rom);
}
else
{
if (datdataA.Files.ContainsKey(key))
{
datdataA.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
datdataA.Files.Add(key, temp);
}
if (datdataB.Files.ContainsKey(key))
{
datdataB.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
datdataB.Files.Add(key, temp);
}
datdataA.Add(key, rom);
datdataB.Add(key, rom);
}
}
}
@@ -173,7 +137,7 @@ namespace SabreTools.Helper.Dats
// Create each of the respective output DATs
logger.User("Creating and populating new DATs");
DatFile itemStatus = new DatFile
DatFile nodump = new DatFile
{
FileName = this.FileName + " (Nodump)",
Name = this.Name + " (Nodump)",
@@ -298,78 +262,33 @@ namespace SabreTools.Helper.Dats
continue;
}
// If the file is a itemStatus
// If the file is a nodump
if ((rom.Type == ItemType.Rom && ((Rom)rom).ItemStatus == ItemStatus.Nodump)
|| (rom.Type == ItemType.Disk && ((Disk)rom).ItemStatus == ItemStatus.Nodump))
{
if (itemStatus.Files.ContainsKey(key))
{
itemStatus.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
itemStatus.Files.Add(key, temp);
}
nodump.Add(key, rom);
}
// If the file has a SHA-1
else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).SHA1))
|| (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).SHA1)))
{
if (sha1.Files.ContainsKey(key))
{
sha1.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
sha1.Files.Add(key, temp);
}
sha1.Add(key, rom);
}
// If the file has no SHA-1 but has an MD5
else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).MD5))
|| (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).MD5)))
{
if (md5.Files.ContainsKey(key))
{
md5.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
md5.Files.Add(key, temp);
}
md5.Add(key, rom);
}
// If the file has no MD5 but a CRC
else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).SHA1))
|| (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).SHA1)))
{
if (crc.Files.ContainsKey(key))
{
crc.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
crc.Files.Add(key, temp);
}
crc.Add(key, rom);
}
else
{
if (other.Files.ContainsKey(key))
{
other.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
other.Files.Add(key, temp);
}
other.Add(key, rom);
}
}
}
@@ -387,9 +306,9 @@ namespace SabreTools.Helper.Dats
// Now, output all of the files to the output directory
logger.User("DAT information created, outputting new files");
bool success = true;
if (itemStatus.Files.Count > 0)
if (nodump.Files.Count > 0)
{
success &= itemStatus.WriteToFile(outDir, logger);
success &= nodump.WriteToFile(outDir, logger);
}
if (sha1.Files.Count > 0)
{
@@ -452,14 +371,7 @@ namespace SabreTools.Helper.Dats
items.ForEach(item => item.Machine.Description = Style.GetFileName(item.Machine.Description));
// Now add the game to the output DAT
if (tempDat.Files.ContainsKey(key))
{
tempDat.Files[key].AddRange(items);
}
else
{
tempDat.Files.Add(key, items);
}
tempDat.AddRange(key, items);
// Then set the DAT name to be the parent directory name
tempDat.Name = Style.GetDirectoryName(key);
@@ -617,45 +529,17 @@ namespace SabreTools.Helper.Dats
// If the file is a Rom
if (rom.Type == ItemType.Rom)
{
if (romdat.Files.ContainsKey(key))
{
romdat.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
romdat.Files.Add(key, temp);
}
romdat.Add(key, rom);
}
// If the file is a Disk
else if (rom.Type == ItemType.Disk)
{
if (diskdat.Files.ContainsKey(key))
{
diskdat.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
diskdat.Files.Add(key, temp);
}
diskdat.Add(key, rom);
}
// If the file is a Sample
else if (rom.Type == ItemType.Sample)
{
if (sampledat.Files.ContainsKey(key))
{
sampledat.Files[key].Add(rom);
}
else
{
List<DatItem> temp = new List<DatItem>();
temp.Add(rom);
sampledat.Files.Add(key, temp);
}
sampledat.Add(key, rom);
}
}
}