diff --git a/Reactor/Networking/ModList.cs b/Reactor/Networking/ModList.cs index 20dcc47..cab2b6a 100644 --- a/Reactor/Networking/ModList.cs +++ b/Reactor/Networking/ModList.cs @@ -21,7 +21,7 @@ namespace Reactor.Networking return _mapByNetId![netId]; } - internal static void Update() + internal static ISet Update() { var i = (uint) 0; @@ -37,6 +37,8 @@ namespace Reactor.Networking _mapById = Current.ToDictionary(mod => mod.Id, mod => mod); _mapByNetId = Current.ToDictionary(mod => mod.NetId, mod => mod); + + return Current; } } } diff --git a/Reactor/Networking/Patches/ClientPatches.cs b/Reactor/Networking/Patches/ClientPatches.cs index 7c5bc74..6f3aa43 100644 --- a/Reactor/Networking/Patches/ClientPatches.cs +++ b/Reactor/Networking/Patches/ClientPatches.cs @@ -165,7 +165,7 @@ namespace Reactor.Networking.Patches public static void Postfix(ref Il2CppStructArray __result) { - ModList.Update(); + var mods = ModList.Update(); var handshake = new MessageWriter(1000); @@ -173,7 +173,7 @@ namespace Reactor.Networking.Patches ModdedHandshakeC2S.Serialize( handshake, - ModList.Current!.Count + mods.Count ); __result = handshake.ToByteArray(true); diff --git a/Reactor/Networking/Patches/HttpPatches.cs b/Reactor/Networking/Patches/HttpPatches.cs new file mode 100644 index 0000000..d7e7ec4 --- /dev/null +++ b/Reactor/Networking/Patches/HttpPatches.cs @@ -0,0 +1,71 @@ +using System; +using System.Linq; +using System.Text; +using HarmonyLib; +using UnhollowerBaseLib; +using UnityEngine.Networking; + +namespace Reactor.Networking.Patches; + +[HarmonyPatch] +internal static class HttpPatches +{ + public const int Version = 1; + + public static string BuildHeader() + { + var stringBuilder = new StringBuilder(); + + stringBuilder.Append(Version); + stringBuilder.Append(";"); + + var mods = ModList.Update().Where(m => m.Side == PluginSide.Both).OrderBy(m => m.Id, StringComparer.Ordinal).ToArray(); + + stringBuilder.Append(mods.Length); + + foreach (var mod in mods) + { + stringBuilder.Append(";"); + stringBuilder.Append(mod.Id); + stringBuilder.Append("="); + stringBuilder.Append(mod.Version); + } + + return stringBuilder.ToString(); + } + + [HarmonyPatch(typeof(UnityWebRequest), nameof(UnityWebRequest.Get), typeof(string))] + [HarmonyPatch(typeof(UnityWebRequest), nameof(UnityWebRequest.Post), typeof(string), typeof(string))] + [HarmonyPatch(typeof(UnityWebRequest), nameof(UnityWebRequest.Put), typeof(string), typeof(Il2CppStructArray))] + [HarmonyPostfix] + public static void UnityWebRequestPatch(UnityWebRequest __result) + { + var path = __result.uri.AbsolutePath; + + if (path == "/api/games") + { + __result.SetRequestHeader("Client-Mods", BuildHeader()); + } + } + + [HarmonyPatch(typeof(HttpMatchmakerManager._CoRequestGameList_d__7), nameof(HttpMatchmakerManager._CoRequestGameList_d__7.MoveNext))] + public static class CoRequestGameListPatch + { + public static void Prefix(HttpMatchmakerManager._CoRequestGameList_d__7 __instance) + { + if (__instance.__1__state == 2) + { + if (HttpUtils.IsSuccess(__instance._request_5__3.responseCode)) + { + var responseHeader = __instance._request_5__3.GetResponseHeader("Client-Mods-Processed"); + if (responseHeader == null) + { + AmongUsClient.Instance.LastDisconnectReason = DisconnectReasons.Custom; + AmongUsClient.Instance.LastCustomDisconnect = "This region doesn't support modded handshake.\nThe lobbies shown may not be compatible with your current mods.\nFor more info see https://reactor.gg/handshake"; + DisconnectPopup.Instance.Show(); + } + } + } + } + } +}