[DatFile] Safer code to ensure better description-to-name remapping

This commit is contained in:
Matt Nadareski
2017-05-14 20:03:31 -07:00
parent d9850ac8d1
commit bad61d1ed1

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -226,20 +227,17 @@ namespace SabreTools.Library.Dats
try try
{ {
// First we want to get a mapping for all games to description // First we want to get a mapping for all games to description
Dictionary<string, string> mapping = new Dictionary<string, string>(); ConcurrentDictionary<string, string> mapping = new ConcurrentDictionary<string, string>();
List<string> keys = Keys.ToList(); List<string> keys = Keys.ToList();
Parallel.ForEach(keys, Globals.ParallelOptions, key => Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{ {
List<DatItem> items = this[key]; List<DatItem> items = this[key];
Parallel.ForEach(items, Globals.ParallelOptions, item => Parallel.ForEach(items, Globals.ParallelOptions, item =>
{ {
lock (mapping) // If the key mapping doesn't exist, add it
if (!mapping.ContainsKey(item.Machine.Name))
{ {
// If the key mapping doesn't exist, add it mapping.TryAdd(item.Machine.Name, item.Machine.Description.Replace('/', '_').Replace("\"", "''"));
if (!mapping.ContainsKey(item.Machine.Name))
{
mapping.Add(item.Machine.Name, item.Machine.Description.Replace('/', '_').Replace("\"", "''"));
}
} }
}); });
}); });
@@ -252,34 +250,41 @@ namespace SabreTools.Library.Dats
List<DatItem> newItems = new List<DatItem>(); List<DatItem> newItems = new List<DatItem>();
Parallel.ForEach(items, Globals.ParallelOptions, item => Parallel.ForEach(items, Globals.ParallelOptions, item =>
{ {
// Update machine name try
if (item.Machine.Name != null && mapping.ContainsKey(item.Machine.Name))
{ {
item.Machine.Name = mapping[item.Machine.Name]; // Update machine name
} if (item.Machine.Name != null && mapping.ContainsKey(item.Machine.Name))
{
item.Machine.Name = mapping[item.Machine.Name];
}
// Update cloneof // Update cloneof
if (item.Machine.CloneOf != null && mapping.ContainsKey(item.Machine.CloneOf)) if (item.Machine.CloneOf != null && mapping.ContainsKey(item.Machine.CloneOf))
{ {
item.Machine.CloneOf = mapping[item.Machine.CloneOf]; item.Machine.CloneOf = mapping[item.Machine.CloneOf];
} }
// Update romof // Update romof
if (item.Machine.RomOf != null && mapping.ContainsKey(item.Machine.RomOf)) if (item.Machine.RomOf != null && mapping.ContainsKey(item.Machine.RomOf))
{ {
item.Machine.RomOf = mapping[item.Machine.RomOf]; item.Machine.RomOf = mapping[item.Machine.RomOf];
} }
// Update sampleof // Update sampleof
if (item.Machine.SampleOf != null && mapping.ContainsKey(item.Machine.SampleOf)) if (item.Machine.SampleOf != null && mapping.ContainsKey(item.Machine.SampleOf))
{ {
item.Machine.SampleOf = mapping[item.Machine.SampleOf]; item.Machine.SampleOf = mapping[item.Machine.SampleOf];
} }
// Add the new item to the output list // Add the new item to the output list
lock (newItems) lock (newItems)
{
newItems.Add(item);
}
}
catch (Exception ex)
{ {
newItems.Add(item); Globals.Logger.Warning(ex.ToString());
} }
}); });