Skip to content

Commit 1e5b85d

Browse files
committed
Use Transpiler on GraphAPIClient.<Post>d__5`1<GetMultiplayerInstanceResponse>.MoveNext
1 parent 3de3338 commit 1e5b85d

3 files changed

Lines changed: 80 additions & 49 deletions

File tree

MultiplayerCore/Installers/MpAppInstaller.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public override void InstallBindings()
3232
Container.Bind<MpBeatmapLevelProvider>().ToSelf().AsSingle();
3333
Container.BindInterfacesAndSelfTo<CustomLevelsPatcher>().AsSingle();
3434
Container.BindInterfacesAndSelfTo<NetworkConfigPatcher>().AsSingle();
35-
Container.BindInterfacesAndSelfTo<GraphAPIClientPatcher>().AsSingle();
3635
Container.BindInterfacesAndSelfTo<ModeSelectionPatcher>().AsSingle();
3736
Container.BindInterfacesAndSelfTo<PlayerCountPatcher>().AsSingle();
3837
Container.Bind<BGNetDebugLogger>().ToSelf().AsSingle();

MultiplayerCore/Patchers/GraphAPIClientPatcher.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Reflection;
6+
using System.Reflection.Emit;
7+
using System.Runtime.CompilerServices;
8+
using System.Text;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using BGNet.Core.GameLift;
12+
using HarmonyLib;
13+
using SiraUtil.Affinity;
14+
15+
namespace MultiplayerCore.Patches
16+
{
17+
[HarmonyPatch]
18+
internal class GraphAPIClientPatch
19+
{
20+
private static readonly HttpClient _client = new HttpClient();
21+
22+
private static readonly MethodInfo _sendAsyncAttacher = SymbolExtensions.GetMethodInfo(() => SendAsync(null!, 0, CancellationToken.None));
23+
private static readonly MethodInfo _sendAsyncMethod = typeof(HttpClient).GetMethod(nameof(HttpClient.SendAsync), new Type[] { typeof(HttpRequestMessage), typeof(HttpCompletionOption), typeof(CancellationToken) })!;
24+
25+
static MethodBase TargetMethod() =>
26+
AccessTools.FirstInner(typeof(GraphAPIClient), t => t.Name.StartsWith("<Post>d__5`1"))?.MakeGenericType(typeof(GetMultiplayerInstanceResponse)).GetMethod("MoveNext", BindingFlags.NonPublic | BindingFlags.Instance)!;
27+
28+
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
29+
{
30+
Plugin.Logger.Trace("Transpiling GraphAPIClient.Post");
31+
var codes = instructions.ToList();
32+
for (int i = 0; i < codes.Count; i++)
33+
{
34+
Plugin.Logger.Trace($"Instruction at index {i}: {codes[i].opcode}");
35+
if (codes[i].opcode == OpCodes.Callvirt)
36+
{
37+
Plugin.Logger.Trace($"Callvirt Method: {codes[i].operand}");
38+
if (codes[i].Calls(_sendAsyncMethod))
39+
{
40+
Plugin.Logger.Trace("Found SendAsync call");
41+
CodeInstruction newCode = new CodeInstruction(OpCodes.Callvirt, _sendAsyncAttacher);
42+
codes[i] = newCode;
43+
}
44+
}
45+
}
46+
47+
return codes.AsEnumerable();
48+
}
49+
50+
private static Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption,
51+
CancellationToken cancellationToken)
52+
{
53+
54+
Plugin.Logger.Debug("SendAsync MasterServer Request called");
55+
var result = _client.SendAsync(request, completionOption, cancellationToken);
56+
57+
result.ContinueWith(async task =>
58+
{
59+
try
60+
{
61+
HttpResponseMessage response = await task;
62+
if (!response.IsSuccessStatusCode)
63+
{
64+
Plugin.Logger.Error(
65+
$"An error occurred while attempting to post to the Graph API: Uri '{request.RequestUri}' StatusCode: {(int)response.StatusCode}: {response.StatusCode}");
66+
Plugin.Logger.Trace($"Response: {response.Content.ReadAsStringAsync()}");
67+
}
68+
69+
}
70+
catch (Exception ex)
71+
{
72+
Plugin.Logger.Error(
73+
$"An error occurred while attempting to post to the Graph API: Uri '{request.RequestUri}' Exception Message: {ex.Message + (ex.InnerException != null ? " --> " + ex.InnerException.Message : "")}");
74+
Plugin.Logger.Trace(ex);
75+
}
76+
});
77+
return result;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)