diff --git a/Helper.cs b/Helper.cs new file mode 100644 index 0000000..a3f7160 --- /dev/null +++ b/Helper.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace BigLobby +{ + public static class Helper + { + public static T[] ResizeArray(T[] oldArray, int newSize) { + var newArray = new T[newSize]; + oldArray.CopyTo(newArray, 0); + return newArray; + } + } +} \ No newline at end of file diff --git a/Patches/Game.cs b/Patches/Game.cs new file mode 100644 index 0000000..889e436 --- /dev/null +++ b/Patches/Game.cs @@ -0,0 +1,48 @@ +using GameNetcodeStuff; +using HarmonyLib; +using Unity.Netcode; +using UnityEngine; + +namespace BigLobby.Patches +{ + internal class PlayerObjects + { + [HarmonyPatch(typeof(StartOfRound), "Awake")] + [HarmonyPostfix] + public static void ResizeLists(ref StartOfRound __instance) { + __instance.allPlayerObjects = Helper.ResizeArray(__instance.allPlayerObjects, Plugin.MaxPlayers); + __instance.allPlayerScripts = Helper.ResizeArray(__instance.allPlayerScripts, Plugin.MaxPlayers); + + var playerPrefab = __instance.playerPrefab; + var playerContainer = __instance.allPlayerObjects[1].transform.parent; + if (GameNetworkManager.Instance.isHostingGame) + { // We are the host, spawn extra players + for (int i = 4; i < Plugin.MaxPlayers; i++) + { + var newPlayer = Object.Instantiate(playerPrefab, playerContainer); + var newScript = newPlayer.GetComponent(); + var netObject = newPlayer.GetComponent(); + + __instance.allPlayerObjects[i] = newPlayer; + __instance.allPlayerScripts[i] = newScript; + newPlayer.name = $"ExtraPlayer{i}"; + newScript.playerClientId = i; + netObject.Spawn(); + } + } + else // We are the client, look for extra players + { + var scripts = Object.FindObjectsOfType(); + foreach (var script in scripts) + { + if (script.playerClientId < 4) continue; + var player = script.gameObject; + var index = int.Parse(player.name[-1]); + script.playerClientId = index; + __instance.allPlayerObjects[index] = player; + __instance.allPlayerScripts[index] = script; + } + } + } + } +} \ No newline at end of file