Skip to content

Commit

Permalink
Add mobile patcher
Browse files Browse the repository at this point in the history
  • Loading branch information
d4n3436 committed May 18, 2022
1 parent 0e8a52b commit 75ac278
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/Entities/MobilePatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Reflection;
using Discord.WebSocket;
using HarmonyLib;

namespace Fergun;

/// <summary>
/// Represents the mobile patcher.
/// </summary>
public static class MobilePatcher
{
/// <summary>
/// Patches Discord.Net to display the mobile status.
/// </summary>
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<string, string> props
|| !props.TryGetValue("$device", out string? device)
|| device != "Discord.Net")
return;

props["$os"] = "android";
props["$browser"] = "Discord Android";
}
}
5 changes: 5 additions & 0 deletions src/Entities/StartupOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public class StartupOptions
/// Gets or sets the ID of the guild to register owner commands.
/// </summary>
public ulong OwnerCommandsGuildId { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the mobile status should be used.
/// </summary>
public bool MobileStatus { get; set; }
}
3 changes: 2 additions & 1 deletion src/Fergun.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
<Version>2.0-beta</Version>
</PropertyGroup>

<ItemGroup>
<ItemGroup>
<PackageReference Include="Discord.Addons.Hosting" Version="5.1.0" />
<PackageReference Include="Discord.Net.Interactions" Version="3.5.0" />
<PackageReference Include="Fergun.Interactive" Version="1.5.4" />
<PackageReference Include="GScraper" Version="1.0.2" />
<PackageReference Include="GTranslate" Version="2.1.0" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Lib.Harmony" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
5 changes: 5 additions & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
services.Configure<BotListOptions>(context.Configuration.GetSection(BotListOptions.BotList));
services.Configure<FergunOptions>(context.Configuration.GetSection(FergunOptions.Fergun));
services.AddSqlite<FergunContext>(context.Configuration.GetConnectionString("FergunDatabase"));

if (context.Configuration.GetSection(StartupOptions.Startup).Get<StartupOptions>().MobileStatus)
{
MobilePatcher.Patch();
}
})
.ConfigureDiscordShardedHost((context, config) =>
{
Expand Down
3 changes: 2 additions & 1 deletion src/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
{
"Token": "",
"TestingGuildId": 0,
"OwnerCommandsGuildId": 0
"OwnerCommandsGuildId": 0,
"MobileStatus": false
},
"Fergun":
{
Expand Down

0 comments on commit 75ac278

Please sign in to comment.