mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Separate MachineDescriptionToName into helpers
This commit is contained in:
@@ -553,7 +553,7 @@ namespace SabreTools.DatFiles
|
|||||||
PerformSorting();
|
PerformSorting();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// List all duplicates found in a DAT based on a DatItem
|
/// List all duplicates found in a DAT based on a DatItem
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -684,41 +684,41 @@ namespace SabreTools.DatFiles
|
|||||||
List<string> oldkeys = [.. Keys];
|
List<string> oldkeys = [.. Keys];
|
||||||
|
|
||||||
#if NET452_OR_GREATER || NETCOREAPP
|
#if NET452_OR_GREATER || NETCOREAPP
|
||||||
Parallel.For(0, oldkeys.Count, Globals.ParallelOptions, k =>
|
Parallel.For(0, oldkeys.Count, Globals.ParallelOptions, k =>
|
||||||
#elif NET40_OR_GREATER
|
#elif NET40_OR_GREATER
|
||||||
Parallel.For(0, oldkeys.Count, k =>
|
Parallel.For(0, oldkeys.Count, k =>
|
||||||
#else
|
#else
|
||||||
for (int k = 0; k < oldkeys.Count; k++)
|
for (int k = 0; k < oldkeys.Count; k++)
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
|
string key = oldkeys[k];
|
||||||
|
if (this[key] == null)
|
||||||
|
Remove(key);
|
||||||
|
|
||||||
|
// Now add each of the roms to their respective keys
|
||||||
|
for (int i = 0; i < this[key]!.Count; i++)
|
||||||
{
|
{
|
||||||
string key = oldkeys[k];
|
DatItem item = this[key]![i];
|
||||||
if (this[key] == null)
|
if (item == null)
|
||||||
Remove(key);
|
continue;
|
||||||
|
|
||||||
// Now add each of the roms to their respective keys
|
// We want to get the key most appropriate for the given sorting type
|
||||||
for (int i = 0; i < this[key]!.Count; i++)
|
string newkey = item.GetKey(bucketBy, lower, norename);
|
||||||
|
|
||||||
|
// If the key is different, move the item to the new key
|
||||||
|
if (newkey != key)
|
||||||
{
|
{
|
||||||
DatItem item = this[key]![i];
|
Add(newkey, item);
|
||||||
if (item == null)
|
Remove(key, item);
|
||||||
continue;
|
i--; // This make sure that the pointer stays on the correct since one was removed
|
||||||
|
|
||||||
// We want to get the key most appropriate for the given sorting type
|
|
||||||
string newkey = item.GetKey(bucketBy, lower, norename);
|
|
||||||
|
|
||||||
// If the key is different, move the item to the new key
|
|
||||||
if (newkey != key)
|
|
||||||
{
|
|
||||||
Add(newkey, item);
|
|
||||||
Remove(key, item);
|
|
||||||
i--; // This make sure that the pointer stays on the correct since one was removed
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If the key is now empty, remove it
|
// If the key is now empty, remove it
|
||||||
if (this[key]!.Count == 0)
|
if (this[key]!.Count == 0)
|
||||||
Remove(key);
|
Remove(key);
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
});
|
});
|
||||||
#else
|
#else
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -736,34 +736,34 @@ namespace SabreTools.DatFiles
|
|||||||
|
|
||||||
List<string> keys = [.. Keys];
|
List<string> keys = [.. Keys];
|
||||||
#if NET452_OR_GREATER || NETCOREAPP
|
#if NET452_OR_GREATER || NETCOREAPP
|
||||||
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
|
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
|
||||||
#elif NET40_OR_GREATER
|
#elif NET40_OR_GREATER
|
||||||
Parallel.ForEach(keys, key =>
|
Parallel.ForEach(keys, key =>
|
||||||
#else
|
#else
|
||||||
foreach (var key in keys)
|
foreach (var key in keys)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
// Get the possibly unsorted list
|
// Get the possibly unsorted list
|
||||||
ConcurrentList<DatItem>? sortedlist = this[key]?.ToConcurrentList();
|
ConcurrentList<DatItem>? sortedlist = this[key]?.ToConcurrentList();
|
||||||
if (sortedlist == null)
|
if (sortedlist == null)
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
return;
|
return;
|
||||||
#else
|
#else
|
||||||
continue;
|
continue;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Sort the list of items to be consistent
|
// Sort the list of items to be consistent
|
||||||
DatItem.Sort(ref sortedlist, false);
|
DatItem.Sort(ref sortedlist, false);
|
||||||
|
|
||||||
// If we're merging the roms, do so
|
// If we're merging the roms, do so
|
||||||
if (dedupeType == DedupeType.Full || (dedupeType == DedupeType.Game && bucketBy == ItemKey.Machine))
|
if (dedupeType == DedupeType.Full || (dedupeType == DedupeType.Game && bucketBy == ItemKey.Machine))
|
||||||
sortedlist = DatItem.Merge(sortedlist);
|
sortedlist = DatItem.Merge(sortedlist);
|
||||||
|
|
||||||
// Add the list back to the dictionary
|
// Add the list back to the dictionary
|
||||||
Reset(key);
|
Reset(key);
|
||||||
AddRange(key, sortedlist);
|
AddRange(key, sortedlist);
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
});
|
});
|
||||||
#else
|
#else
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -776,21 +776,21 @@ namespace SabreTools.DatFiles
|
|||||||
{
|
{
|
||||||
List<string> keys = [.. Keys];
|
List<string> keys = [.. Keys];
|
||||||
#if NET452_OR_GREATER || NETCOREAPP
|
#if NET452_OR_GREATER || NETCOREAPP
|
||||||
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
|
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
|
||||||
#elif NET40_OR_GREATER
|
#elif NET40_OR_GREATER
|
||||||
Parallel.ForEach(keys, key =>
|
Parallel.ForEach(keys, key =>
|
||||||
#else
|
#else
|
||||||
foreach (var key in keys)
|
foreach (var key in keys)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
// Get the possibly unsorted list
|
// Get the possibly unsorted list
|
||||||
ConcurrentList<DatItem>? sortedlist = this[key];
|
ConcurrentList<DatItem>? sortedlist = this[key];
|
||||||
|
|
||||||
// Sort the list of items to be consistent
|
// Sort the list of items to be consistent
|
||||||
if (sortedlist != null)
|
if (sortedlist != null)
|
||||||
DatItem.Sort(ref sortedlist, false);
|
DatItem.Sort(ref sortedlist, false);
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
});
|
});
|
||||||
#else
|
#else
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -867,89 +867,13 @@ namespace SabreTools.DatFiles
|
|||||||
{
|
{
|
||||||
// First we want to get a mapping for all games to description
|
// First we want to get a mapping for all games to description
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
ConcurrentDictionary<string, string> mapping = new();
|
ConcurrentDictionary<string, string> mapping = CreateMachineToDescriptionMapping();
|
||||||
#else
|
#else
|
||||||
Dictionary<string, string> mapping = [];
|
Dictionary<string, string> mapping = CreateMachineToDescriptionMapping();
|
||||||
#endif
|
|
||||||
#if NET452_OR_GREATER || NETCOREAPP
|
|
||||||
Parallel.ForEach(Keys, Globals.ParallelOptions, key =>
|
|
||||||
#elif NET40_OR_GREATER
|
|
||||||
Parallel.ForEach(Keys, key =>
|
|
||||||
#else
|
|
||||||
foreach (var key in Keys)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
var items = this[key];
|
|
||||||
if (items == null)
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
return;
|
|
||||||
#else
|
|
||||||
continue;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
foreach (DatItem item in items)
|
|
||||||
{
|
|
||||||
// If the key mapping doesn't exist, add it
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
mapping.TryAdd(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!, item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!.Replace('/', '_').Replace("\"", "''").Replace(":", " -"));
|
|
||||||
#else
|
|
||||||
mapping[item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!] = item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!.Replace('/', '_').Replace("\"", "''").Replace(":", " -");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
});
|
|
||||||
#else
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Now we loop through every item and update accordingly
|
// Now we loop through every item and update accordingly
|
||||||
#if NET452_OR_GREATER || NETCOREAPP
|
UpdateMachineNamesFromDescriptions(mapping);
|
||||||
Parallel.ForEach(Keys, Globals.ParallelOptions, key =>
|
|
||||||
#elif NET40_OR_GREATER
|
|
||||||
Parallel.ForEach(Keys, key =>
|
|
||||||
#else
|
|
||||||
foreach (var key in Keys)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
var items = this[key];
|
|
||||||
if (items == null)
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
return;
|
|
||||||
#else
|
|
||||||
continue;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ConcurrentList<DatItem> newItems = [];
|
|
||||||
foreach (DatItem item in items)
|
|
||||||
{
|
|
||||||
// Update machine name
|
|
||||||
if (!string.IsNullOrEmpty(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)) && mapping.ContainsKey(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!))
|
|
||||||
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, mapping[item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!]);
|
|
||||||
|
|
||||||
// Update cloneof
|
|
||||||
if (!string.IsNullOrEmpty(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)) && mapping.ContainsKey(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)!))
|
|
||||||
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey, mapping[item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)!]);
|
|
||||||
|
|
||||||
// Update romof
|
|
||||||
if (!string.IsNullOrEmpty(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)) && mapping.ContainsKey(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)!))
|
|
||||||
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, mapping[item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)!]);
|
|
||||||
|
|
||||||
// Update sampleof
|
|
||||||
if (!string.IsNullOrEmpty(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)) && mapping.ContainsKey(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)!))
|
|
||||||
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.SampleOfKey, mapping[item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)!]);
|
|
||||||
|
|
||||||
// Add the new item to the output list
|
|
||||||
newItems.Add(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace the old list of roms with the new one
|
|
||||||
Remove(key);
|
|
||||||
AddRange(key, newItems);
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
});
|
|
||||||
#else
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (!throwOnError)
|
catch (Exception ex) when (!throwOnError)
|
||||||
{
|
{
|
||||||
@@ -1036,6 +960,56 @@ namespace SabreTools.DatFiles
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create machine to description mapping dictionary
|
||||||
|
/// </summary>
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
private ConcurrentDictionary<string, string> CreateMachineToDescriptionMapping()
|
||||||
|
#else
|
||||||
|
private Dictionary<string, string> CreateMachineToDescriptionMapping()
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
ConcurrentDictionary<string, string> mapping = new();
|
||||||
|
#else
|
||||||
|
Dictionary<string, string> mapping = [];
|
||||||
|
#endif
|
||||||
|
#if NET452_OR_GREATER || NETCOREAPP
|
||||||
|
Parallel.ForEach(Keys, Globals.ParallelOptions, key =>
|
||||||
|
#elif NET40_OR_GREATER
|
||||||
|
Parallel.ForEach(Keys, key =>
|
||||||
|
#else
|
||||||
|
foreach (var key in Keys)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
var items = this[key];
|
||||||
|
if (items == null)
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
return;
|
||||||
|
#else
|
||||||
|
continue;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
foreach (DatItem item in items)
|
||||||
|
{
|
||||||
|
// If the key mapping doesn't exist, add it
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
mapping.TryAdd(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!,
|
||||||
|
item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!.Replace('/', '_').Replace("\"", "''").Replace(":", " -"));
|
||||||
|
#else
|
||||||
|
mapping[item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!]
|
||||||
|
= item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!.Replace('/', '_').Replace("\"", "''").Replace(":", " -");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
});
|
||||||
|
#else
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return mapping;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -1054,6 +1028,64 @@ namespace SabreTools.DatFiles
|
|||||||
datItem.SetName(Path.GetFileName(datItem.GetName()));
|
datItem.SetName(Path.GetFileName(datItem.GetName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update machine names from descriptions according to mappings
|
||||||
|
/// </summary>
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
private void UpdateMachineNamesFromDescriptions(ConcurrentDictionary<string, string> mapping)
|
||||||
|
#else
|
||||||
|
private void UpdateMachineNamesFromDescriptions(Dictionary<string, string> mapping)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
#if NET452_OR_GREATER || NETCOREAPP
|
||||||
|
Parallel.ForEach(Keys, Globals.ParallelOptions, key =>
|
||||||
|
#elif NET40_OR_GREATER
|
||||||
|
Parallel.ForEach(Keys, key =>
|
||||||
|
#else
|
||||||
|
foreach (var key in Keys)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
var items = this[key];
|
||||||
|
if (items == null)
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
return;
|
||||||
|
#else
|
||||||
|
continue;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ConcurrentList<DatItem> newItems = [];
|
||||||
|
foreach (DatItem item in items)
|
||||||
|
{
|
||||||
|
// Update machine name
|
||||||
|
if (!string.IsNullOrEmpty(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)) && mapping.ContainsKey(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!))
|
||||||
|
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, mapping[item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!]);
|
||||||
|
|
||||||
|
// Update cloneof
|
||||||
|
if (!string.IsNullOrEmpty(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)) && mapping.ContainsKey(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)!))
|
||||||
|
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey, mapping[item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)!]);
|
||||||
|
|
||||||
|
// Update romof
|
||||||
|
if (!string.IsNullOrEmpty(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)) && mapping.ContainsKey(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)!))
|
||||||
|
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, mapping[item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)!]);
|
||||||
|
|
||||||
|
// Update sampleof
|
||||||
|
if (!string.IsNullOrEmpty(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)) && mapping.ContainsKey(item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)!))
|
||||||
|
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.SampleOfKey, mapping[item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)!]);
|
||||||
|
|
||||||
|
// Add the new item to the output list
|
||||||
|
newItems.Add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace the old list of roms with the new one
|
||||||
|
Remove(key);
|
||||||
|
AddRange(key, newItems);
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
});
|
||||||
|
#else
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Statistics
|
#region Statistics
|
||||||
|
|||||||
@@ -928,90 +928,13 @@ namespace SabreTools.DatFiles
|
|||||||
{
|
{
|
||||||
// First we want to get a mapping for all games to description
|
// First we want to get a mapping for all games to description
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
ConcurrentDictionary<string, string> mapping = new();
|
ConcurrentDictionary<string, string> mapping = CreateMachineToDescriptionMapping();
|
||||||
#else
|
#else
|
||||||
Dictionary<string, string> mapping = [];
|
Dictionary<string, string> mapping = CreateMachineToDescriptionMapping();
|
||||||
#endif
|
|
||||||
#if NET452_OR_GREATER || NETCOREAPP
|
|
||||||
Parallel.ForEach(SortedKeys, Globals.ParallelOptions, key =>
|
|
||||||
#elif NET40_OR_GREATER
|
|
||||||
Parallel.ForEach(SortedKeys, key =>
|
|
||||||
#else
|
|
||||||
foreach (var key in SortedKeys)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
var items = GetDatItemsForBucket(key);
|
|
||||||
if (items == null)
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
return;
|
|
||||||
#else
|
|
||||||
continue;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
foreach ((long, DatItem) item in items)
|
|
||||||
{
|
|
||||||
// If the key mapping doesn't exist, add it
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
mapping.TryAdd(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!,
|
|
||||||
item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!.Replace('/', '_').Replace("\"", "''").Replace(":", " -"));
|
|
||||||
#else
|
|
||||||
mapping[item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!]
|
|
||||||
= item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!.Replace('/', '_').Replace("\"", "''").Replace(":", " -");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
});
|
|
||||||
#else
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Now we loop through every item and update accordingly
|
// Now we loop through every item and update accordingly
|
||||||
#if NET452_OR_GREATER || NETCOREAPP
|
UpdateMachineNamesFromDescriptions(mapping);
|
||||||
Parallel.ForEach(SortedKeys, Globals.ParallelOptions, key =>
|
|
||||||
#elif NET40_OR_GREATER
|
|
||||||
Parallel.ForEach(SortedKeys, key =>
|
|
||||||
#else
|
|
||||||
foreach (var key in SortedKeys)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
var items = GetDatItemsForBucket(key);
|
|
||||||
if (items == null)
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
return;
|
|
||||||
#else
|
|
||||||
continue;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ConcurrentList<(long, DatItem)> newItems = [];
|
|
||||||
foreach ((long, DatItem) item in items)
|
|
||||||
{
|
|
||||||
// Update machine name
|
|
||||||
if (!string.IsNullOrEmpty(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)) && mapping.ContainsKey(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!))
|
|
||||||
item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, mapping[item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!]);
|
|
||||||
|
|
||||||
// Update cloneof
|
|
||||||
if (!string.IsNullOrEmpty(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)) && mapping.ContainsKey(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)!))
|
|
||||||
item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey, mapping[item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)!]);
|
|
||||||
|
|
||||||
// Update romof
|
|
||||||
if (!string.IsNullOrEmpty(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)) && mapping.ContainsKey(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)!))
|
|
||||||
item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, mapping[item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)!]);
|
|
||||||
|
|
||||||
// Update sampleof
|
|
||||||
if (!string.IsNullOrEmpty(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)) && mapping.ContainsKey(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)!))
|
|
||||||
item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.SampleOfKey, mapping[item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)!]);
|
|
||||||
|
|
||||||
// Add the new item to the output list
|
|
||||||
newItems.Add(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace the old list of roms with the new one
|
|
||||||
_buckets[key] = newItems.Select(i => i.Item1).ToConcurrentList();
|
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
|
||||||
});
|
|
||||||
#else
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (!throwOnError)
|
catch (Exception ex) when (!throwOnError)
|
||||||
{
|
{
|
||||||
@@ -1097,6 +1020,56 @@ namespace SabreTools.DatFiles
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create machine to description mapping dictionary
|
||||||
|
/// </summary>
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
private ConcurrentDictionary<string, string> CreateMachineToDescriptionMapping()
|
||||||
|
#else
|
||||||
|
private Dictionary<string, string> CreateMachineToDescriptionMapping()
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
ConcurrentDictionary<string, string> mapping = new();
|
||||||
|
#else
|
||||||
|
Dictionary<string, string> mapping = [];
|
||||||
|
#endif
|
||||||
|
#if NET452_OR_GREATER || NETCOREAPP
|
||||||
|
Parallel.ForEach(SortedKeys, Globals.ParallelOptions, key =>
|
||||||
|
#elif NET40_OR_GREATER
|
||||||
|
Parallel.ForEach(SortedKeys, key =>
|
||||||
|
#else
|
||||||
|
foreach (var key in SortedKeys)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
var items = GetDatItemsForBucket(key);
|
||||||
|
if (items == null)
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
return;
|
||||||
|
#else
|
||||||
|
continue;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
foreach ((long, DatItem) item in items)
|
||||||
|
{
|
||||||
|
// If the key mapping doesn't exist, add it
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
mapping.TryAdd(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!,
|
||||||
|
item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!.Replace('/', '_').Replace("\"", "''").Replace(":", " -"));
|
||||||
|
#else
|
||||||
|
mapping[item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!]
|
||||||
|
= item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!.Replace('/', '_').Replace("\"", "''").Replace(":", " -");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
});
|
||||||
|
#else
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return mapping;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Execute all filters in a filter runner on a single bucket
|
/// Execute all filters in a filter runner on a single bucket
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -1138,6 +1111,63 @@ namespace SabreTools.DatFiles
|
|||||||
datItem.Item2.SetName(Path.GetFileName(datItem.Item2.GetName()));
|
datItem.Item2.SetName(Path.GetFileName(datItem.Item2.GetName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update machine names from descriptions according to mappings
|
||||||
|
/// </summary>
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
private void UpdateMachineNamesFromDescriptions(ConcurrentDictionary<string, string> mapping)
|
||||||
|
#else
|
||||||
|
private void UpdateMachineNamesFromDescriptions(Dictionary<string, string> mapping)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
#if NET452_OR_GREATER || NETCOREAPP
|
||||||
|
Parallel.ForEach(SortedKeys, Globals.ParallelOptions, key =>
|
||||||
|
#elif NET40_OR_GREATER
|
||||||
|
Parallel.ForEach(SortedKeys, key =>
|
||||||
|
#else
|
||||||
|
foreach (var key in SortedKeys)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
var items = GetDatItemsForBucket(key);
|
||||||
|
if (items == null)
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
return;
|
||||||
|
#else
|
||||||
|
continue;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ConcurrentList<(long, DatItem)> newItems = [];
|
||||||
|
foreach ((long, DatItem) item in items)
|
||||||
|
{
|
||||||
|
// Update machine name
|
||||||
|
if (!string.IsNullOrEmpty(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)) && mapping.ContainsKey(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!))
|
||||||
|
item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, mapping[item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)!]);
|
||||||
|
|
||||||
|
// Update cloneof
|
||||||
|
if (!string.IsNullOrEmpty(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)) && mapping.ContainsKey(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)!))
|
||||||
|
item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey, mapping[item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)!]);
|
||||||
|
|
||||||
|
// Update romof
|
||||||
|
if (!string.IsNullOrEmpty(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)) && mapping.ContainsKey(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)!))
|
||||||
|
item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, mapping[item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)!]);
|
||||||
|
|
||||||
|
// Update sampleof
|
||||||
|
if (!string.IsNullOrEmpty(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)) && mapping.ContainsKey(item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)!))
|
||||||
|
item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.SampleOfKey, mapping[item.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)!]);
|
||||||
|
|
||||||
|
// Add the new item to the output list
|
||||||
|
newItems.Add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace the old list of roms with the new one
|
||||||
|
_buckets[key] = newItems.Select(i => i.Item1).ToConcurrentList();
|
||||||
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
|
});
|
||||||
|
#else
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Statistics
|
#region Statistics
|
||||||
|
|||||||
Reference in New Issue
Block a user