From 9750bf7b3f26807034fa1968489fc6050b59a22e Mon Sep 17 00:00:00 2001 From: Olavi Mustanoja Date: Mon, 14 Jun 2021 02:36:57 +0300 Subject: [PATCH] Fix online multiplayer --- GameModes/GameModeManager.cs | 11 ++++++- Networking/NetworkEventCallbacks.cs | 6 +--- Patches/LoadingScreen.cs | 47 +++++++++++++++++++++++++++++ Patches/NetworkConnectionHandler.cs | 32 ++++++++++++++++++++ Unbound.cs | 45 ++++++++++++++++++--------- 5 files changed, 121 insertions(+), 20 deletions(-) create mode 100644 Patches/LoadingScreen.cs create mode 100644 Patches/NetworkConnectionHandler.cs diff --git a/GameModes/GameModeManager.cs b/GameModes/GameModeManager.cs index e2c85db..747c1f3 100644 --- a/GameModes/GameModeManager.cs +++ b/GameModes/GameModeManager.cs @@ -103,6 +103,11 @@ public static T GetGameMode(string gameModeId) where T : Component } public static void SetGameMode(string id) + { + GameModeManager.SetGameMode(id, true); + } + + public static void SetGameMode(string id, bool setActive) { if (id != null && !GameModeManager.handlers.ContainsKey(id)) { @@ -134,7 +139,11 @@ public static void SetGameMode(string id) { PlayerAssigner.instance.InvokeMethod("SetPlayersCanJoin", true); - GameModeManager.CurrentHandler.SetActive(true); + if (setActive) + { + GameModeManager.CurrentHandler.SetActive(true); + } + var pmPlayerDied = (Action) pm.GetFieldValue("PlayerDiedAction"); pm.SetPropertyValue("PlayerJoinedAction", Delegate.Combine(pm.PlayerJoinedAction, new Action(GameModeManager.CurrentHandler.PlayerJoined))); pm.SetFieldValue("PlayerDiedAction", Delegate.Combine(pmPlayerDied, new Action(GameModeManager.CurrentHandler.PlayerDied))); diff --git a/Networking/NetworkEventCallbacks.cs b/Networking/NetworkEventCallbacks.cs index 8b22290..268b24b 100644 --- a/Networking/NetworkEventCallbacks.cs +++ b/Networking/NetworkEventCallbacks.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using UnityEngine; -using Photon.Pun; +using Photon.Pun; namespace UnboundLib { diff --git a/Patches/LoadingScreen.cs b/Patches/LoadingScreen.cs new file mode 100644 index 0000000..a29913b --- /dev/null +++ b/Patches/LoadingScreen.cs @@ -0,0 +1,47 @@ +using HarmonyLib; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Reflection.Emit; +using UnboundLib.GameModes; +using UnityEngine; + +namespace UnboundLib.Patches +{ + [HarmonyPatch(typeof(LoadingScreen), "IDoLoading")] + class LoadingScreen_Patch_IDoLoading + { + static void ActivateGameMode() + { + GameModeManager.CurrentHandler.SetActive(true); + } + + static IEnumerable Transpiler(IEnumerable instructions) + { + // Remove the default player joined and died -hooks. We'll add them back through the GameMode abstraction layer. + var list = instructions.ToList(); + var newInstructions = new List(); + + var f_gameMode = ExtensionMethods.GetFieldInfo(typeof(LoadingScreen), "gameMode"); + var m_objectSetActive = ExtensionMethods.GetMethodInfo(typeof(GameObject), "SetActive"); + var m_gmSetActive = ExtensionMethods.GetMethodInfo(typeof(LoadingScreen_Patch_IDoLoading), "ActivateGameMode"); + + for (int i = 0; i < list.Count; i++) + { + if (list[i].LoadsField(f_gameMode) && list[i + 2].Calls(m_objectSetActive)) + { + newInstructions.Add(new CodeInstruction(OpCodes.Call, m_gmSetActive)); + i += 2; + } + else + { + newInstructions.Add(list[i]); + } + } + + return newInstructions; + } + } +} diff --git a/Patches/NetworkConnectionHandler.cs b/Patches/NetworkConnectionHandler.cs new file mode 100644 index 0000000..545a858 --- /dev/null +++ b/Patches/NetworkConnectionHandler.cs @@ -0,0 +1,32 @@ +using HarmonyLib; +using UnboundLib.GameModes; + +namespace UnboundLib.Patches +{ + [HarmonyPatch(typeof(NetworkConnectionHandler), "QuickMatch")] + class NetworkConnectionHandler_Patch_QuickMatch + { + static void Prefix() + { + GameModeManager.SetGameMode("ArmsRace", false); + } + } + + [HarmonyPatch(typeof(NetworkConnectionHandler), "TwitchJoin")] + class NetworkConnectionHandler_Patch_TwitchJoin + { + static void Prefix() + { + GameModeManager.SetGameMode("ArmsRace", false); + } + } + + [HarmonyPatch(typeof(NetworkConnectionHandler), "HostPrivateAndInviteFriend")] + class NetworkConnectionHandler_Patch_HostPrivateAndInviteFriend + { + static void Prefix() + { + GameModeManager.SetGameMode("ArmsRace", false); + } + } +} diff --git a/Unbound.cs b/Unbound.cs index 3a3b7d6..16691bc 100644 --- a/Unbound.cs +++ b/Unbound.cs @@ -1,19 +1,19 @@ -using System; -using System.Collections; -using UnityEngine; -using BepInEx; +using BepInEx; using HarmonyLib; using Photon.Pun; +using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using TMPro; -using UnboundLib.Networking; using UnboundLib.GameModes; +using UnboundLib.Networking; +using UnityEngine; using UnityEngine.UI; namespace UnboundLib { - [BepInPlugin(ModId, ModName, "1.1.0")] + [BepInPlugin(ModId, ModName, "1.1.1")] [BepInProcess("Rounds.exe")] public class Unbound : BaseUnityPlugin { @@ -82,7 +82,7 @@ public Unbound() text.gameObject.AddComponent().ignoreLayout = true; text.fontSize = 30; text.color = (Color.yellow + Color.red) / 2; - text.font = ((TextMeshProUGUI)FindObjectOfType().GetFieldValue("text")).font; + text.font = ((TextMeshProUGUI) FindObjectOfType().GetFieldValue("text")).font; text.transform.SetParent(MainMenuHandler.instance.transform.Find("Canvas/ListSelector/Main/Group"), true); text.transform.SetAsFirstSibling(); text.rectTransform.localScale = Vector3.one; @@ -93,7 +93,7 @@ public Unbound() orig(self); }; - + On.MainMenuHandler.Close += (orig, self) => { canCreate = false; @@ -102,7 +102,7 @@ public Unbound() orig(self); }; } - + void Awake() { if (Instance == null) @@ -126,18 +126,35 @@ void Awake() void Start() { // store default cards - defaultCards = (CardInfo[])CardChoice.instance.cards.Clone(); + defaultCards = (CardInfo[]) CardChoice.instance.cards.Clone(); // request mod handshake NetworkingManager.RegisterEvent(NetworkEventType.StartHandshake, (data) => { - NetworkingManager.RaiseEvent(NetworkEventType.FinishHandshake); + if (PhotonNetwork.IsMasterClient) + { + NetworkingManager.RaiseEvent(NetworkEventType.FinishHandshake, + GameModeManager.CurrentHandlerID, + GameModeManager.CurrentHandler?.Settings); + } + else + { + NetworkingManager.RaiseEvent(NetworkEventType.FinishHandshake); + } + CardChoice.instance.cards = defaultCards; }); + // recieve mod handshake NetworkingManager.RegisterEvent(NetworkEventType.FinishHandshake, (data) => { CardChoice.instance.cards = activeCards.ToArray(); + + if (data.Length > 0) + { + GameModeManager.SetGameMode((string) data[0], false); + GameModeManager.CurrentHandler.SetSettings((GameSettings) data[1]); + } }); // fetch card to use as a template for all custom cards @@ -158,7 +175,7 @@ where c.cardName.ToLower() == "huge" networkEvents.OnJoinedRoomEvent += OnJoinedRoomAction; networkEvents.OnLeftRoomEvent += OnLeftRoomAction; } - + void Update() { if (GameManager.instance.isPlaying && PhotonNetwork.OfflineMode) @@ -173,14 +190,14 @@ void Update() GameManager.lockInput = showModUi || DevConsole.isTyping; } - + void OnGUI() { if (!showModUi) return; Vector2 center = new Vector2(Screen.width / 2, Screen.height / 2); Vector2 size = new Vector2(400, 100 * GUIListeners.Count); - + GUILayout.BeginVertical(); bool showingSpecificMod = false;