Skip to content

Commit

Permalink
Fix online multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
olavim committed Jun 13, 2021
1 parent 9b36f04 commit 9750bf7
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 20 deletions.
11 changes: 10 additions & 1 deletion GameModes/GameModeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public static T GetGameMode<T>(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))
{
Expand Down Expand Up @@ -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<Player, int>) pm.GetFieldValue("PlayerDiedAction");
pm.SetPropertyValue("PlayerJoinedAction", Delegate.Combine(pm.PlayerJoinedAction, new Action<Player>(GameModeManager.CurrentHandler.PlayerJoined)));
pm.SetFieldValue("PlayerDiedAction", Delegate.Combine(pmPlayerDied, new Action<Player, int>(GameModeManager.CurrentHandler.PlayerDied)));
Expand Down
6 changes: 1 addition & 5 deletions Networking/NetworkEventCallbacks.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Photon.Pun;
using Photon.Pun;

namespace UnboundLib
{
Expand Down
47 changes: 47 additions & 0 deletions Patches/LoadingScreen.cs
Original file line number Diff line number Diff line change
@@ -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<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> 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<CodeInstruction>();

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;
}
}
}
32 changes: 32 additions & 0 deletions Patches/NetworkConnectionHandler.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
45 changes: 31 additions & 14 deletions Unbound.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -82,7 +82,7 @@ public Unbound()
text.gameObject.AddComponent<LayoutElement>().ignoreLayout = true;
text.fontSize = 30;
text.color = (Color.yellow + Color.red) / 2;
text.font = ((TextMeshProUGUI)FindObjectOfType<ListMenuButton>().GetFieldValue("text")).font;
text.font = ((TextMeshProUGUI) FindObjectOfType<ListMenuButton>().GetFieldValue("text")).font;
text.transform.SetParent(MainMenuHandler.instance.transform.Find("Canvas/ListSelector/Main/Group"), true);
text.transform.SetAsFirstSibling();
text.rectTransform.localScale = Vector3.one;
Expand All @@ -93,7 +93,7 @@ public Unbound()

orig(self);
};

On.MainMenuHandler.Close += (orig, self) =>
{
canCreate = false;
Expand All @@ -102,7 +102,7 @@ public Unbound()
orig(self);
};
}

void Awake()
{
if (Instance == null)
Expand All @@ -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
Expand All @@ -158,7 +175,7 @@ where c.cardName.ToLower() == "huge"
networkEvents.OnJoinedRoomEvent += OnJoinedRoomAction;
networkEvents.OnLeftRoomEvent += OnLeftRoomAction;
}

void Update()
{
if (GameManager.instance.isPlaying && PhotonNetwork.OfflineMode)
Expand All @@ -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;
Expand Down

0 comments on commit 9750bf7

Please sign in to comment.