[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.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -226,20 +227,17 @@ namespace SabreTools.Library.Dats
try
{
// 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();
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{
List<DatItem> items = this[key];
Parallel.ForEach(items, Globals.ParallelOptions, item =>
{
lock (mapping)
{
// If the key mapping doesn't exist, add it
if (!mapping.ContainsKey(item.Machine.Name))
{
mapping.Add(item.Machine.Name, item.Machine.Description.Replace('/', '_').Replace("\"", "''"));
}
mapping.TryAdd(item.Machine.Name, item.Machine.Description.Replace('/', '_').Replace("\"", "''"));
}
});
});
@@ -251,6 +249,8 @@ namespace SabreTools.Library.Dats
List<DatItem> items = this[key];
List<DatItem> newItems = new List<DatItem>();
Parallel.ForEach(items, Globals.ParallelOptions, item =>
{
try
{
// Update machine name
if (item.Machine.Name != null && mapping.ContainsKey(item.Machine.Name))
@@ -281,6 +281,11 @@ namespace SabreTools.Library.Dats
{
newItems.Add(item);
}
}
catch (Exception ex)
{
Globals.Logger.Warning(ex.ToString());
}
});
// Replace the old list of roms with the new one