Browse Source

Fix Emoji UnicodeAndNames throwing exception because of duplicit keys (#76)

* Fix Emoji UnicodeAndNames throwing exception because of multiple same keys

* Use ToImmutableDictionary instead of new ReadOnlyDictionary for UnicodesAndNames

* Use HashSet for Emoji Unicodes parse matching
pull/1923/head
František Boháček GitHub 3 years ago
parent
commit
4610dd193c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 4 deletions
  1. +24
    -4
      src/Discord.Net.Core/Entities/Emotes/Emoji.cs

+ 24
- 4
src/Discord.Net.Core/Entities/Emotes/Emoji.cs View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Linq;

namespace Discord
@@ -56,7 +58,7 @@ namespace Discord
if (NamesAndUnicodes.ContainsKey(text))
result = new Emoji(NamesAndUnicodes[text]);

if (UnicodesAndNames.ContainsKey(text))
if (Unicodes.Contains(text))
result = new Emoji(text);

return result != null;
@@ -5942,12 +5944,30 @@ namespace Discord
["♡"] = "❤️"
};

private static IReadOnlyDictionary<string, string> _unicodesAndNames;
private static IReadOnlyDictionary<string, string> UnicodesAndNames
private static IReadOnlyCollection<string> _unicodes;
private static IReadOnlyCollection<string> Unicodes
{
get
{
_unicodesAndNames ??= NamesAndUnicodes.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
_unicodes ??= NamesAndUnicodes.Select(kvp => kvp.Value).ToImmutableHashSet();
return _unicodes;
}
}

private static IReadOnlyDictionary<string, ReadOnlyCollection<string>> _unicodesAndNames;
private static IReadOnlyDictionary<string, ReadOnlyCollection<string>> UnicodesAndNames
{
get
{
_unicodesAndNames ??=
NamesAndUnicodes
.GroupBy(kvp => kvp.Value)
.ToImmutableDictionary(
grouping => grouping.Key,
grouping => grouping.Select(kvp => kvp.Key)
.ToList()
.AsReadOnly()
);
return _unicodesAndNames;
}
}


Loading…
Cancel
Save