Skip to content

Commit

Permalink
Fix duplicate keys exception
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Nov 11, 2022
1 parent 2c39db4 commit a8b8150
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog][Keep a Changelog] and this project adh

## [Released]

## [2.2.2] - 2022-11-11

- `FindAllGamesById` will no longer throw an exception for duplicate IDs

## [2.2.1] - 2022-10-21

Small update that changes the equality comparer of the dictionary returned by `EGSHandler.FindAllGamesById` and `OriginHandler.FindAllGamesById` to `StringComparer.OrdinalIgnoreCase`.
Expand Down
33 changes: 32 additions & 1 deletion GameFinder.Common/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;

Expand Down Expand Up @@ -62,4 +63,34 @@ public static (TGame[] games, string[] errors) SplitResults<TGame>(

return (games, errors);
}

/// <summary>
/// Custom <see cref="Enumerable.ToDictionary{TSource,TKey}(System.Collections.Generic.IEnumerable{TSource},System.Func{TSource,TKey})"/>
/// function that skips duplicate keys.
/// </summary>
/// <param name="source"></param>
/// <param name="keySelector"></param>
/// <param name="elementSelector"></param>
/// <param name="comparer"></param>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TElement"></typeparam>
/// <returns></returns>
public static Dictionary<TKey, TElement> CustomToDictionary<TSource, TKey, TElement>(
[InstantHandle] this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer = null)
where TKey: notnull
{
var dictionary = new Dictionary<TKey, TElement>(comparer);

foreach (var element in source)
{
var key = keySelector(element);
if (dictionary.ContainsKey(key)) continue;

dictionary.Add(key, elementSelector(element));
}

return dictionary;
}
}
1 change: 1 addition & 0 deletions GameFinder.RegistryUtils/InMemoryRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ internal InMemoryRegistryKey AddSubKey(RegistryHive hive, string key)
/// <param name="value"></param>
public void AddValue(string valueName, object value)
{
if (_values.ContainsKey(valueName)) return;
_values.Add(valueName, value);
}

Expand Down
2 changes: 1 addition & 1 deletion GameFinder.StoreHandlers.EGS/EGSHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public override Dictionary<string, EGSGame> FindAllGamesById(out string[] errors
var (games, allErrors) = FindAllGames().SplitResults();
errors = allErrors;

return games.ToDictionary(game => game.CatalogItemId, game => game, StringComparer.OrdinalIgnoreCase);
return games.CustomToDictionary(game => game.CatalogItemId, game => game, StringComparer.OrdinalIgnoreCase);
}

private Result DeserializeGame(IFileInfo itemFile)
Expand Down
2 changes: 1 addition & 1 deletion GameFinder.StoreHandlers.GOG/GOGHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override Dictionary<long, GOGGame> FindAllGamesById(out string[] errors)
var (games, allErrors) = FindAllGames().SplitResults();
errors = allErrors;

return games.ToDictionary(game => game.Id, game => game);
return games.CustomToDictionary(game => game.Id, game => game);
}

private static Result ParseSubKey(IRegistryKey gogKey, string subKeyName)
Expand Down
2 changes: 1 addition & 1 deletion GameFinder.StoreHandlers.Origin/OriginHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public override Dictionary<string, OriginGame> FindAllGamesById(out string[] err
var (games, allErrors) = FindAllGames().SplitResults();
errors = allErrors;

return games.ToDictionary(game => game.Id, game => game, StringComparer.OrdinalIgnoreCase);
return games.CustomToDictionary(game => game.Id, game => game, StringComparer.OrdinalIgnoreCase);
}

private static Result ParseMfstFile(IFileInfo fileInfo)
Expand Down
2 changes: 1 addition & 1 deletion GameFinder.StoreHandlers.Steam/SteamHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public override Dictionary<int, SteamGame> FindAllGamesById(out string[] errors)
var (games, allErrors) = FindAllGames().SplitResults();
errors = allErrors;

return games.ToDictionary(game => game.AppId, game => game);
return games.CustomToDictionary(game => game.AppId, game => game);
}

private (IFileInfo? libraryFoldersFile, string? error) FindSteam()
Expand Down

0 comments on commit a8b8150

Please sign in to comment.