From 75ac2784594514d338f28fc3fbca068d2b0b8383 Mon Sep 17 00:00:00 2001 From: d4n Date: Wed, 18 May 2022 18:27:17 -0500 Subject: [PATCH] Add mobile patcher --- src/Entities/MobilePatcher.cs | 46 ++++++++++++++++++++++++++++++++++ src/Entities/StartupOptions.cs | 5 ++++ src/Fergun.csproj | 3 ++- src/Program.cs | 5 ++++ src/appsettings.json | 3 ++- 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/Entities/MobilePatcher.cs diff --git a/src/Entities/MobilePatcher.cs b/src/Entities/MobilePatcher.cs new file mode 100644 index 0000000..43af602 --- /dev/null +++ b/src/Entities/MobilePatcher.cs @@ -0,0 +1,46 @@ +using System.Reflection; +using Discord.WebSocket; +using HarmonyLib; + +namespace Fergun; + +/// +/// Represents the mobile patcher. +/// +public static class MobilePatcher +{ + /// + /// Patches Discord.Net to display the mobile status. + /// + public static void Patch() + { + var harmony = new Harmony(nameof(MobilePatcher)); + + var original = AccessTools.Method("Discord.API.DiscordSocketApiClient:SendGatewayAsync"); + var prefix = typeof(MobilePatcher).GetMethod(nameof(Prefix)); + + harmony.Patch(original, new HarmonyMethod(prefix)); + } + + private static readonly Type _identifyParams = + typeof(BaseSocketClient).Assembly.GetType("Discord.API.Gateway.IdentifyParams", true)!; + + private static readonly PropertyInfo? _property = _identifyParams.GetProperty("Properties"); + + public static void Prefix(in byte opCode, in object payload) + { + if (opCode != 2) // Identify + return; + + if (payload.GetType() != _identifyParams) + return; + + if (_property?.GetValue(payload) is not IDictionary props + || !props.TryGetValue("$device", out string? device) + || device != "Discord.Net") + return; + + props["$os"] = "android"; + props["$browser"] = "Discord Android"; + } +} \ No newline at end of file diff --git a/src/Entities/StartupOptions.cs b/src/Entities/StartupOptions.cs index f7ba1a9..2c70926 100644 --- a/src/Entities/StartupOptions.cs +++ b/src/Entities/StartupOptions.cs @@ -21,4 +21,9 @@ public class StartupOptions /// Gets or sets the ID of the guild to register owner commands. /// public ulong OwnerCommandsGuildId { get; set; } + + /// + /// Gets or sets a value indicating whether the mobile status should be used. + /// + public bool MobileStatus { get; set; } } \ No newline at end of file diff --git a/src/Fergun.csproj b/src/Fergun.csproj index e5d8862..9545145 100644 --- a/src/Fergun.csproj +++ b/src/Fergun.csproj @@ -8,13 +8,14 @@ 2.0-beta - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/Program.cs b/src/Program.cs index 3d0d233..a475d20 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -41,6 +41,11 @@ services.Configure(context.Configuration.GetSection(BotListOptions.BotList)); services.Configure(context.Configuration.GetSection(FergunOptions.Fergun)); services.AddSqlite(context.Configuration.GetConnectionString("FergunDatabase")); + + if (context.Configuration.GetSection(StartupOptions.Startup).Get().MobileStatus) + { + MobilePatcher.Patch(); + } }) .ConfigureDiscordShardedHost((context, config) => { diff --git a/src/appsettings.json b/src/appsettings.json index 6522f78..d7e6d39 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -7,7 +7,8 @@ { "Token": "", "TestingGuildId": 0, - "OwnerCommandsGuildId": 0 + "OwnerCommandsGuildId": 0, + "MobileStatus": false }, "Fergun": {