|
| 1 | +using HarmonyLib; |
| 2 | +using Il2CppMicrosoft.AspNetCore.SignalR.Client; |
| 3 | +using PolyMod.Multiplayer.ViewModels; |
| 4 | +using Polytopia.Data; |
| 5 | +using PolytopiaBackendBase; |
| 6 | +using PolytopiaBackendBase.Common; |
| 7 | +using PolytopiaBackendBase.Game; |
| 8 | +using PolytopiaBackendBase.Game.BindingModels; |
| 9 | +using UnityEngine; |
| 10 | +using Newtonsoft.Json; |
| 11 | +using PolytopiaBackendBase.Auth; |
| 12 | + |
| 13 | +namespace PolyMod.Multiplayer; |
| 14 | + |
| 15 | +public static class Client |
| 16 | +{ |
| 17 | + internal const string DEFAULT_SERVER_URL = "https://dev.polydystopia.xyz"; |
| 18 | + internal const string LOCAL_SERVER_URL = "http://localhost:5051/"; |
| 19 | + private const string GldMarker = "##GLD:"; |
| 20 | + internal static bool allowGldMods = false; |
| 21 | + |
| 22 | + // Cache parsed GLD by game Seed to handle rewinds/reloads |
| 23 | + private static readonly Dictionary<int, GameLogicData> _gldCache = new(); |
| 24 | + private static readonly Dictionary<int, int> _versionCache = new(); // Seed -> modGldVersion |
| 25 | + |
| 26 | + internal static void Init() |
| 27 | + { |
| 28 | + Harmony.CreateAndPatchAll(typeof(Client)); |
| 29 | + BuildConfig buildConfig = BuildConfigHelper.GetSelectedBuildConfig(); |
| 30 | + buildConfig.buildServerURL = BuildServerURL.Custom; |
| 31 | + buildConfig.customServerURL = Plugin.config.backendUrl; |
| 32 | + |
| 33 | + // Update BackendUri and HttpClient.BaseAddress since PolytopiaBackendAdapter.Instance |
| 34 | + // was statically initialized before plugins load, so it still points to polytopia-prod.net |
| 35 | + var uri = new Il2CppSystem.Uri(Plugin.config.backendUrl); |
| 36 | + PolytopiaBackendAdapter.Instance.UseBackendUri(uri); |
| 37 | + PolytopiaBackendAdapter.Instance.BackendHttpClient.BaseAddress = uri; |
| 38 | + |
| 39 | + Plugin.logger.LogInfo($"Multiplayer> Server URL set to: {Plugin.config.backendUrl}"); |
| 40 | + } |
| 41 | + |
| 42 | + [HarmonyPostfix] |
| 43 | + [HarmonyPatch(typeof(MultiplayerSelectionScreen), nameof(MultiplayerSelectionScreen.Awake))] |
| 44 | + public static void MultiplayerSelectionScreen_Awake(MultiplayerSelectionScreen __instance) |
| 45 | + { |
| 46 | + __instance.TournamentsButton.gameObject.SetActive(false); |
| 47 | + } |
| 48 | + |
| 49 | + [HarmonyPostfix] |
| 50 | + [HarmonyPatch(typeof(StartScreen_UI2), nameof(StartScreen_UI2.Init))] |
| 51 | + private static void StartScreen_UI2_HideButtons(StartScreen_UI2 __instance) |
| 52 | + { |
| 53 | + __instance.highscoreButton.gameObject.SetActive(false); |
| 54 | + __instance.weeklyChallengeButton.gameObject.SetActive(false); |
| 55 | + } |
| 56 | + |
| 57 | + [HarmonyPostfix] |
| 58 | + [HarmonyPatch(typeof(StartScreen_UI2), nameof(StartScreen_UI2.RunLayout))] |
| 59 | + private static void StartScreen_UI2_ReflowRoundButtons(StartScreen_UI2 __instance, ScreenBase_UI2.ScreenSize screenSize) |
| 60 | + { |
| 61 | + // RunLayout adds all four round buttons to a UITable unconditionally, so hiding the highscore button leaves a gap. Re-run the row without it so the rest recenter. |
| 62 | + UITable table = new(); |
| 63 | + table.AddCell(__instance.settingsButton.Cast<IUILayoutable>()); |
| 64 | + table.AddCell(__instance.throneRoomButton.Cast<IUILayoutable>()); |
| 65 | + table.AddCell(__instance.aboutButton.Cast<IUILayoutable>()); |
| 66 | + table.SetBottom(screenSize.safeRect.Bottom + __instance.settingsButton.GetHalfHeight() + 15f); |
| 67 | + table.margin = 20f; |
| 68 | + table.RunLayout(); |
| 69 | + } |
| 70 | + |
| 71 | + [HarmonyPostfix] |
| 72 | + [HarmonyPatch(typeof(SystemInfo), nameof(SystemInfo.deviceUniqueIdentifier), MethodType.Getter)] |
| 73 | + public static void SteamClient_get_SteamId(ref string __result) |
| 74 | + { |
| 75 | + if (Plugin.config.overrideDeviceId != string.Empty) |
| 76 | + { |
| 77 | + __result = Plugin.config.overrideDeviceId; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// After GameState deserialization, check for trailing GLD version ID and set mockedGameLogicData. |
| 83 | + /// The server appends "##GLD:" + modGldVersion (int) after the normal serialized data. |
| 84 | + /// </summary> |
| 85 | + [HarmonyPostfix] |
| 86 | + [HarmonyPatch(typeof(GameState), nameof(GameState.Deserialize))] |
| 87 | + [Obsolete("This will be succeeded by ModMultiplayer in the future.")] |
| 88 | + private static void Deserialize_Postfix(GameState __instance, BinaryReader __0) |
| 89 | + { |
| 90 | + |
| 91 | + Plugin.logger.LogInfo("Multiplayer> ClientBase_SendCommand"); |
| 92 | + Il2CppSystem.Threading.Tasks.Task<ServerResponse<BoolResponseViewModel>> task = new(); |
| 93 | + var taskCompletionSource = new Il2CppSystem.Threading.Tasks.TaskCompletionSource<ServerResponse<BoolResponseViewModel>>(); |
| 94 | + |
| 95 | + _ = ClientBase_SendCommand_Async(taskCompletionSource, __instance, command); |
| 96 | + |
| 97 | + task = taskCompletionSource.Task; |
| 98 | + |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Fetch GLD from server using ModGldVersion ID |
| 104 | + /// </summary> |
| 105 | + [Obsolete("This will be succeeded by ModMultiplayer in the future.")] |
| 106 | + private static string? FetchGldById(int modGldVersion) |
| 107 | + { |
| 108 | + try |
| 109 | + { |
| 110 | + if (!client.CurrentGameId.HasValue) |
| 111 | + { |
| 112 | + Console.Write("Tried to perform and send command but no GameId was set"); |
| 113 | + return; |
| 114 | + } |
| 115 | + if (!ClientActionManager.CanReceiveCommand(command, client.GameState)) |
| 116 | + { |
| 117 | + Console.Write("Tried to send invalid command"); |
| 118 | + return; |
| 119 | + } |
| 120 | + uint currentResetId = client.resets; |
| 121 | + int count = client.GameState.CommandStack.Count; |
| 122 | + var list = new Il2CppSystem.Collections.Generic.List<CommandBase>(); |
| 123 | + list.Add(command); |
| 124 | + client.ActionManager.ExecuteCommands(list); |
| 125 | + await client.SendCommandToServer(command, count); |
| 126 | + |
| 127 | + var serializedGameState = SerializationHelpers.ToByteArray(client.GameState, client.GameState.Version); |
| 128 | + |
| 129 | + var succ = GameStateSummary.FromGameStateByteArray(serializedGameState, |
| 130 | + out GameStateSummary stateSummary, out var gameState); |
| 131 | + |
| 132 | + var serializedGameSummary = SerializationHelpers.ToByteArray(stateSummary, gameState.Version); |
| 133 | + |
| 134 | + |
| 135 | + client.GameState.TryGetPlayer(client.GameState.CurrentPlayer, out PlayerState playerState); |
| 136 | + var currentPlayerId = ""; |
| 137 | + if(playerState.AccountId.HasValue) |
| 138 | + { |
| 139 | + currentPlayerId = playerState.AccountId.Value.ToString(); |
| 140 | + } |
| 141 | + var setupGameDataViewModel = new ModdedGameStateViewModel |
| 142 | + { |
| 143 | + gameId = client.gameId.ToString(), |
| 144 | + serializedGameState = serializedGameState, |
| 145 | + serializedGameSummary = serializedGameSummary, |
| 146 | + gameSettingsJson = "", |
| 147 | + currentPlayerId = currentPlayerId, |
| 148 | + IsEndTurnCommand = command.GetCommandType() == CommandType.EndTurn |
| 149 | + }; |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + var setupData = System.Text.Json.JsonSerializer.Serialize(setupGameDataViewModel); |
| 154 | + |
| 155 | + var serverResponse = await PolytopiaBackendAdapter.Instance.HubConnection.InvokeAsync<ServerResponse<BoolResponseViewModel>>( |
| 156 | + "UpdateGameStateModded", |
| 157 | + setupData, |
| 158 | + Il2CppSystem.Threading.CancellationToken.None |
| 159 | + ); |
| 160 | + tcs.SetResult(serverResponse); |
| 161 | + } |
| 162 | + catch (Exception ex) |
| 163 | + { |
| 164 | + Plugin.logger.LogError("Multiplayer> Error during HandleSendCommandModded: " + ex.Message); |
| 165 | + tcs.SetException(new Il2CppSystem.Exception(ex.Message)); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments