|
| 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