diff --git a/Clients/PsnClient/Utils/TmdbHasher.cs b/Clients/PsnClient/Utils/TmdbHasher.cs index a21d98f7..abd7d841 100644 --- a/Clients/PsnClient/Utils/TmdbHasher.cs +++ b/Clients/PsnClient/Utils/TmdbHasher.cs @@ -10,10 +10,7 @@ public static class TmdbHasher private static readonly byte[] HmacKey = "F5DE66D2680E255B2DF79E74F890EBF349262F618BCAE2A9ACCDEE5156CE8DF2CDF2D48C71173CDC2594465B87405D197CF1AED3B7E9671EEB56CA6753C2E6B0".FromHexString(); public static string GetTitleHash(string productId) - { - using var hmacSha1 = new HMACSHA1(HmacKey); - return hmacSha1.ComputeHash(Encoding.ASCII.GetBytes(productId)).ToHexString(); - } + => HMACSHA1.HashData(HmacKey, Encoding.UTF8.GetBytes(productId)).ToHexString(); public static byte[] FromHexString(this string hexString) { diff --git a/CompatBot/Commands/Misc.cs b/CompatBot/Commands/Misc.cs index 0e8c09e1..9bf07e14 100644 --- a/CompatBot/Commands/Misc.cs +++ b/CompatBot/Commands/Misc.cs @@ -1,4 +1,5 @@ -using System.Text.RegularExpressions; +using System.Net.Http; +using System.Text.RegularExpressions; using CompatApiClient.Utils; using CompatBot.Database; using CompatBot.Database.Providers; @@ -11,6 +12,7 @@ namespace CompatBot.Commands; internal static partial class Misc { + private static readonly HttpClient HttpClient = HttpClientFactory.Create(); private static readonly Random rng = new(); private static readonly List EightBallAnswers = @@ -343,14 +345,28 @@ internal static partial class Misc whatever = whatever.ToLowerInvariant().StripInvisibleAndDiacritics(); var originalWhatever = whatever; var matches = Instead().Matches(whatever); - if (matches.Any()) + if (matches is [.., Match last]) { - var insteadWhatever = matches.Last().Groups["instead"].Value.TrimEager(); + var insteadWhatever = last.Groups["instead"].Value.TrimEager(); if (!string.IsNullOrEmpty(insteadWhatever)) whatever = insteadWhatever; } foreach (var attachment in ctx.Message.Attachments) + { + if (attachment is { Width: > 0, Height: > 0, Url: {Length: >0} url }) + { + await using var imgStream = await HttpClient.GetStreamAsync(url).ConfigureAwait(false); + if (await ColorGetter.GetBlurHashAsync(imgStream).ConfigureAwait(false) is {Length: >0} hash) + { +#if DEBUG + Config.Log.Trace($"Got image blur hash {hash}"); +#endif + whatever += $" {hash}"; + continue; + } + } whatever += $" {attachment.FileSize}"; + } var nekoUser = await ctx.Client.GetUserAsync(272032356922032139ul).ConfigureAwait(false); var nekoMember = await ctx.Client.GetMemberAsync(nekoUser).ConfigureAwait(false); @@ -494,7 +510,11 @@ internal static partial class Misc await ctx.Channel.SendMessageAsync("Rating nothing makes _**so much**_ sense, right?").ConfigureAwait(false); else { - var seed = (prefix + whatever).GetHashCode(StringComparison.CurrentCultureIgnoreCase); + var seedMsg = prefix + whatever; + var seed = seedMsg.GetStableHash(); +#if DEBUG + Config.Log.Trace($"Rating seed is {seed:x8} for '{seedMsg}'"); +#endif var seededRng = new Random(seed); var answer = choices[seededRng.Next(choices.Count)]; var msgBuilder = new DiscordMessageBuilder() diff --git a/CompatBot/Utils/ColorGetter.cs b/CompatBot/Utils/ColorGetter.cs index fa31a752..5f5b4ca1 100644 --- a/CompatBot/Utils/ColorGetter.cs +++ b/CompatBot/Utils/ColorGetter.cs @@ -1,4 +1,6 @@ using System.IO; +using Blurhash.ImageSharp; +using SixLabors.ImageSharp; using SixLabors.ImageSharp.ColorSpaces; using SixLabors.ImageSharp.ColorSpaces.Conversion; using SixLabors.ImageSharp.PixelFormats; @@ -8,7 +10,7 @@ namespace CompatBot.Utils; internal static class ColorGetter { - public static SixLabors.ImageSharp.Color GetDominantColor(SixLabors.ImageSharp.Image img) + public static Color GetDominantColor(Image img) { //img.Mutate(x => x.Resize(new ResizeOptions { Sampler = KnownResamplers.NearestNeighbor, Size = new Size(100, 0) })); int r = 0; @@ -42,7 +44,7 @@ internal static class ColorGetter { try { - var img = await SixLabors.ImageSharp.Image.LoadAsync(jpg).ConfigureAwait(false); + var img = await Image.LoadAsync(jpg).ConfigureAwait(false); var quantizerOptions = new QuantizerOptions { Dither = null, MaxColors = 4 }; var sampling = new ExtensivePixelSamplingStrategy(); var quantizer = new WuQuantizer().CreatePixelSpecificQuantizer(new(), quantizerOptions); @@ -69,4 +71,18 @@ internal static class ColorGetter return defaultColor; } } + + public static async ValueTask GetBlurHashAsync(Stream attachment) + { + try + { + var img = await Image.LoadAsync(attachment).ConfigureAwait(false); + return Blurhasher.Encode(img, 4, 3); + } + catch (Exception e) + { + Config.Log.Warn(e, "Failed to get image blur hash"); + return null; + } + } } \ No newline at end of file diff --git a/CompatBot/Utils/Extensions/StringUtils.cs b/CompatBot/Utils/Extensions/StringUtils.cs index 54f38635..b6a2505f 100644 --- a/CompatBot/Utils/Extensions/StringUtils.cs +++ b/CompatBot/Utils/Extensions/StringUtils.cs @@ -1,5 +1,6 @@ using System.Buffers; using System.Globalization; +using System.Security.Cryptography; using System.Text.RegularExpressions; using CompatBot.Utils.Extensions; using HomoglyphConverter; @@ -447,9 +448,8 @@ public static partial class StringUtils internal static int GetStableHash(this string str) { - using var sha256 = System.Security.Cryptography.SHA256.Create(); - var data = Encoding.UTF8.GetBytes(str); - var hash = sha256.ComputeHash(data); + var data = Encoding.UTF8.GetBytes(str.ToLowerInvariant()); + var hash = SHA256.HashData(data); return BitConverter.ToInt32(hash, 0); }