[DatFile] Make Machine a struct for memory usage

This commit is contained in:
Matt Nadareski
2017-06-16 16:24:26 -07:00
parent 1500875822
commit b59bcd7c20
17 changed files with 519 additions and 203 deletions

View File

@@ -251,8 +251,8 @@ namespace SabreTools.Library.Dats
intDat.WriteToFile(interOutDir);
}
// Due to possible memory requirements, each DAT, after written, will be nulled out
intDat = null;
// Due to possible memory requirements, we force a garbage collection
GC.Collect();
});
}
@@ -467,7 +467,7 @@ namespace SabreTools.Library.Dats
if ((diff & DiffMode.NoDupes) != 0)
{
DatItem newrom = item.Clone() as DatItem;
newrom.Machine.Name += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")";
newrom.Machine.AppendName(" (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")");
outerDiffData.Add(key, newrom);
}
@@ -480,7 +480,7 @@ namespace SabreTools.Library.Dats
if ((item.Dupe & DupeType.External) != 0)
{
DatItem newrom = item.Clone() as DatItem;
newrom.Machine.Name += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")";
newrom.Machine.AppendName(" (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")");
dupeData.Add(key, newrom);
}
@@ -549,9 +549,9 @@ namespace SabreTools.Library.Dats
rootpath += (rootpath == "" ? "" : Path.DirectorySeparatorChar.ToString());
filename = filename.Remove(0, rootpath.Length);
newItem.Machine.Name = Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar
newItem.Machine.UpdateName(Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar
+ Path.GetFileNameWithoutExtension(filename) + Path.DirectorySeparatorChar
+ newItem.Machine.Name;
+ newItem.Machine.Name);
newItems.Add(newItem);
}

View File

@@ -402,7 +402,7 @@ namespace SabreTools.Library.Dats
// Update rom information
datItem.Name = romname;
if (datItem.Machine == null)
if (datItem.Machine == default(Machine))
{
datItem.Machine = new Machine
{
@@ -412,8 +412,8 @@ namespace SabreTools.Library.Dats
}
else
{
datItem.Machine.Name = gamename;
datItem.Machine.Description = gamename;
datItem.Machine.UpdateName(gamename);
datItem.Machine.UpdateDescription(gamename);
}
// Add the file information to the DAT

View File

@@ -21,146 +21,152 @@ namespace SabreTools.Library.Dats
#region Bucketing
/// <summary>
/// Take the arbitrarily sorted Files Dictionary and convert to one sorted by a user-defined method
/// </summary>
/// <param name="bucketBy">SortedBy enum representing how to sort the individual items</param>
/// <param name="mergeroms">True if roms should be deduped, false otherwise</param>
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
public void BucketBy(SortedBy bucketBy, bool mergeroms, bool lower = true, bool norename = true)
/// <summary>
/// Take the arbitrarily sorted Files Dictionary and convert to one sorted by a user-defined method
/// </summary>
/// <param name="bucketBy">SortedBy enum representing how to sort the individual items</param>
/// <param name="mergeroms">True if roms should be deduped, false otherwise</param>
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
public void BucketBy(SortedBy bucketBy, bool mergeroms, bool lower = true, bool norename = true)
{
// If we already have the right sorting, trust it
if (_sortedBy == bucketBy)
{
return;
}
// If we have a situation where there's no dictionary or no keys at all, we skip
if (_files == null || _files.Count == 0)
{
return;
}
// Set the sorted type
_sortedBy = bucketBy;
// Create the temporary dictionary to sort into
SortedDictionary<string, List<DatItem>> sortable = new SortedDictionary<string, List<DatItem>>();
Globals.Logger.User("Organizing roms by " + bucketBy +(mergeroms ? " and merging" : ""));
// First do the initial sort of all of the roms
List<string> keys = Keys.ToList();
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{
List<DatItem> roms = this[key];
// Now add each of the roms to their respective games
foreach (DatItem rom in roms)
{
// If we already have the right sorting, trust it
if (_sortedBy == bucketBy)
string newkey = "";
// We want to get the key most appropriate for the given sorting type
switch (bucketBy)
{
return;
}
// Set the sorted type
_sortedBy = bucketBy;
// Create the temporary dictionary to sort into
SortedDictionary<string, List<DatItem>> sortable = new SortedDictionary<string, List<DatItem>>();
Globals.Logger.User("Organizing roms by " + bucketBy +(mergeroms ? " and merging" : ""));
// First do the initial sort of all of the roms
List<string> keys = Keys.ToList();
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{
List<DatItem> roms = this[key];
// Now add each of the roms to their respective games
foreach (DatItem rom in roms)
{
string newkey = "";
// We want to get the key most appropriate for the given sorting type
switch (bucketBy)
case SortedBy.CRC:
newkey = (rom.Type == ItemType.Rom ? ((Rom)rom).CRC : Constants.CRCZero);
break;
case SortedBy.Game:
newkey = (norename ? ""
: rom.SystemID.ToString().PadLeft(10, '0')
+ "-"
+ rom.SourceID.ToString().PadLeft(10, '0') + "-")
+ (String.IsNullOrEmpty(rom.Machine.Name)
? "Default"
: rom.Machine.Name);
if (lower)
{
case SortedBy.CRC:
newkey = (rom.Type == ItemType.Rom ? ((Rom)rom).CRC : Constants.CRCZero);
break;
case SortedBy.Game:
newkey = (norename ? ""
: rom.SystemID.ToString().PadLeft(10, '0')
+ "-"
+ rom.SourceID.ToString().PadLeft(10, '0') + "-")
+ (String.IsNullOrEmpty(rom.Machine.Name)
? "Default"
: rom.Machine.Name);
if (lower)
{
newkey = newkey.ToLowerInvariant();
}
if (newkey == null)
{
newkey = "null";
}
newkey = HttpUtility.HtmlEncode(newkey);
break;
case SortedBy.MD5:
newkey = (rom.Type == ItemType.Rom
? ((Rom)rom).MD5
: (rom.Type == ItemType.Disk
? ((Disk)rom).MD5
: Constants.MD5Zero));
break;
case SortedBy.SHA1:
newkey = (rom.Type == ItemType.Rom
? ((Rom)rom).SHA1
: (rom.Type == ItemType.Disk
? ((Disk)rom).SHA1
: Constants.SHA1Zero));
break;
case SortedBy.SHA256:
newkey = (rom.Type == ItemType.Rom
? ((Rom)rom).SHA256
: (rom.Type == ItemType.Disk
? ((Disk)rom).SHA256
: Constants.SHA256Zero));
break;
case SortedBy.SHA384:
newkey = (rom.Type == ItemType.Rom
? ((Rom)rom).SHA384
: (rom.Type == ItemType.Disk
? ((Disk)rom).SHA384
: Constants.SHA384Zero));
break;
case SortedBy.SHA512:
newkey = (rom.Type == ItemType.Rom
? ((Rom)rom).SHA512
: (rom.Type == ItemType.Disk
? ((Disk)rom).SHA512
: Constants.SHA512Zero));
break;
newkey = newkey.ToLowerInvariant();
}
// Double and triple check the key
if (newkey == null)
{
newkey = "";
newkey = "null";
}
// Add the DatItem to the temp dictionary
lock (sortable)
{
if (!sortable.ContainsKey(newkey))
{
sortable.Add(newkey, new List<DatItem>());
}
sortable[newkey].Add(rom);
}
}
});
newkey = HttpUtility.HtmlEncode(newkey);
break;
case SortedBy.MD5:
newkey = (rom.Type == ItemType.Rom
? ((Rom)rom).MD5
: (rom.Type == ItemType.Disk
? ((Disk)rom).MD5
: Constants.MD5Zero));
break;
case SortedBy.SHA1:
newkey = (rom.Type == ItemType.Rom
? ((Rom)rom).SHA1
: (rom.Type == ItemType.Disk
? ((Disk)rom).SHA1
: Constants.SHA1Zero));
break;
case SortedBy.SHA256:
newkey = (rom.Type == ItemType.Rom
? ((Rom)rom).SHA256
: (rom.Type == ItemType.Disk
? ((Disk)rom).SHA256
: Constants.SHA256Zero));
break;
case SortedBy.SHA384:
newkey = (rom.Type == ItemType.Rom
? ((Rom)rom).SHA384
: (rom.Type == ItemType.Disk
? ((Disk)rom).SHA384
: Constants.SHA384Zero));
break;
case SortedBy.SHA512:
newkey = (rom.Type == ItemType.Rom
? ((Rom)rom).SHA512
: (rom.Type == ItemType.Disk
? ((Disk)rom).SHA512
: Constants.SHA512Zero));
break;
}
// Now go through and sort all of the individual lists
keys = sortable.Keys.ToList();
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
// Double and triple check the key
if (newkey == null)
{
// Get the possibly unsorted list
List<DatItem> sortedlist = sortable[key];
newkey = "";
}
// Sort the list of items to be consistent
DatItem.Sort(ref sortedlist, false);
// If we're merging the roms, do so
if (mergeroms)
// Add the DatItem to the temp dictionary
lock (sortable)
{
if (!sortable.ContainsKey(newkey))
{
sortedlist = DatItem.Merge(sortedlist);
sortable.Add(newkey, new List<DatItem>());
}
// Add the list back to the temp dictionary
lock (sortable)
{
sortable[key] = sortedlist;
}
});
// Now assign the dictionary back
_files = sortable;
sortable[newkey].Add(rom);
}
}
});
// Now go through and sort all of the individual lists
keys = sortable.Keys.ToList();
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{
// Get the possibly unsorted list
List<DatItem> sortedlist = sortable[key];
// Sort the list of items to be consistent
DatItem.Sort(ref sortedlist, false);
// If we're merging the roms, do so
if (mergeroms)
{
sortedlist = DatItem.Merge(sortedlist);
}
// Add the list back to the temp dictionary
lock (sortable)
{
sortable[key] = sortedlist;
}
});
// Now assign the dictionary back
_files = sortable;
}
#endregion
@@ -190,7 +196,7 @@ namespace SabreTools.Library.Dats
// If we are in single game mode, rename all games
if (single)
{
item.Machine.Name = "!";
item.Machine.UpdateName("!");
}
// If we are in NTFS trim mode, trim the game name
@@ -253,25 +259,25 @@ namespace SabreTools.Library.Dats
// Update machine name
if (!String.IsNullOrEmpty(item.Machine.Name) && mapping.ContainsKey(item.Machine.Name))
{
item.Machine.Name = mapping[item.Machine.Name];
item.Machine.UpdateName(mapping[item.Machine.Name]);
}
// Update cloneof
if (!String.IsNullOrEmpty(item.Machine.CloneOf) && mapping.ContainsKey(item.Machine.CloneOf))
{
item.Machine.CloneOf = mapping[item.Machine.CloneOf];
item.Machine.UpdateCloneOf(mapping[item.Machine.CloneOf]);
}
// Update romof
if (!String.IsNullOrEmpty(item.Machine.RomOf) && mapping.ContainsKey(item.Machine.RomOf))
{
item.Machine.RomOf = mapping[item.Machine.RomOf];
item.Machine.UpdateRomOf(mapping[item.Machine.RomOf]);
}
// Update sampleof
if (!String.IsNullOrEmpty(item.Machine.SampleOf) && mapping.ContainsKey(item.Machine.SampleOf))
{
item.Machine.SampleOf = mapping[item.Machine.SampleOf];
item.Machine.UpdateSampleOf(mapping[item.Machine.SampleOf]);
}
// Add the new item to the output list
@@ -759,7 +765,7 @@ namespace SabreTools.Library.Dats
string romof = this[parent][0].Machine.RomOf;
foreach (DatItem item in items)
{
item.Machine.RomOf = romof;
item.Machine.UpdateRomOf(romof);
}
}
}
@@ -1025,7 +1031,7 @@ namespace SabreTools.Library.Dats
string romof = this[parent][0].Machine.RomOf;
foreach (DatItem item in items)
{
item.Machine.RomOf = romof;
item.Machine.UpdateRomOf(romof);
}
}
}
@@ -1041,8 +1047,8 @@ namespace SabreTools.Library.Dats
List<DatItem> items = this[game];
foreach (DatItem item in items)
{
item.Machine.CloneOf = null;
item.Machine.RomOf = null;
item.Machine.UpdateCloneOf(null);
item.Machine.UpdateRomOf(null);
}
}
}

View File

@@ -3018,14 +3018,14 @@ namespace SabreTools.Library.Dats
}
// If we're in cleaning mode, sanitize the game name
item.Machine.Name = (clean ? Style.CleanGameName(item.Machine.Name) : item.Machine.Name);
item.Machine.UpdateName((clean ? Style.CleanGameName(item.Machine.Name) : item.Machine.Name));
// If we're stripping unicode characters, do so from all relevant things
if (remUnicode)
{
item.Name = Style.RemoveUnicodeCharacters(item.Name);
item.Machine.Name = Style.RemoveUnicodeCharacters(item.Machine.Name);
item.Machine.Description = Style.RemoveUnicodeCharacters(item.Machine.Description);
item.Machine.UpdateName(Style.RemoveUnicodeCharacters(item.Machine.Name));
item.Machine.UpdateDescription(Style.RemoveUnicodeCharacters(item.Machine.Description));
}
// If we have a Rom or a Disk, clean the hash data

View File

@@ -645,8 +645,8 @@ namespace SabreTools.Library.Dats
// If we are coming from an archive, set the correct machine name
if (machinename != null)
{
item.Machine.Name = machinename;
item.Machine.Description = machinename;
item.Machine.UpdateName(machinename);
item.Machine.UpdateDescription(machinename);
}
Globals.Logger.User("No matches found for '" + Style.GetFileName(rom.Name) + "', rebuilding accordingly from inverse flag...");

View File

@@ -435,8 +435,8 @@ namespace SabreTools.Library.Dats
// Clean the input list and set all games to be pathless
List<DatItem> items = this[key];
items.ForEach(item => item.Machine.Name = Style.GetFileName(item.Machine.Name));
items.ForEach(item => item.Machine.Description = Style.GetFileName(item.Machine.Description));
items.ForEach(item => item.Machine.UpdateName(Style.GetFileName(item.Machine.Name)));
items.ForEach(item => item.Machine.UpdateDescription(Style.GetFileName(item.Machine.Description)));
// Now add the game to the output DAT
tempDat.AddRange(key, items);

View File

@@ -474,7 +474,7 @@ namespace SabreTools.Library.Dats
// No game should start with a path separator
if (rom.Machine.Name.StartsWith(Path.DirectorySeparatorChar.ToString()))
{
rom.Machine.Name = rom.Machine.Name.Substring(1);
rom.Machine.UpdateName(rom.Machine.Name.Substring(1));
}
string state = "";