From 134bdc6d7d44b0effb3091e540e112ed9c9c2338 Mon Sep 17 00:00:00 2001 From: Silentbaws Date: Wed, 5 Aug 2020 11:17:57 -0400 Subject: [PATCH] Fix state change on level load Fix custom levels not using image on load Server picks random built-in map for default when no players connected --- XLMultiplayer/Main.cs | 7 +++++ XLMultiplayer/MultiplayerController.cs | 42 +++++++++++++++++++------- XLMultiplayerServer/Server.cs | 4 ++- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/XLMultiplayer/Main.cs b/XLMultiplayer/Main.cs index 1745d68..ab0327f 100644 --- a/XLMultiplayer/Main.cs +++ b/XLMultiplayer/Main.cs @@ -943,6 +943,13 @@ private static bool Prefix(ReplayAudioEventPlayer __instance, int index) { } } + [HarmonyPatch(typeof(LevelSelectionController), "Update")] + static class LevelSelectionControllerUpdatePatch { + static bool Prefix() { + return Main.multiplayerController == null || !Main.multiplayerController.isConnected; + } + } + [HarmonyPatch(typeof(Input), "GetKeyDown", typeof(KeyCode))] static class InputKeyDownPatch { static void Postfix(ref bool __result) { diff --git a/XLMultiplayer/MultiplayerController.cs b/XLMultiplayer/MultiplayerController.cs index ae2f845..89b4701 100644 --- a/XLMultiplayer/MultiplayerController.cs +++ b/XLMultiplayer/MultiplayerController.cs @@ -1,5 +1,6 @@ #define VALVESOCKETS_SPAN +using GameManagement; using HarmonyLib; using Newtonsoft.Json; using ReplayEditor; @@ -19,6 +20,7 @@ using System.Text.RegularExpressions; using System.Threading; using UnityEngine; +using UnityEngine.EventSystems; using UnityEngine.Networking; using Valve.Sockets; @@ -432,23 +434,41 @@ public IEnumerator LoadMap(string path) { while (!sendingUpdates) { yield return new WaitForEndOfFrame(); } - //Load map with path - LevelSelectionController levelSelectionController = GameManagement.GameStateMachine.Instance.LevelSelectionObject.GetComponentInChildren(); - GameManagement.GameStateMachine.Instance.LevelSelectionObject.SetActive(true); + LevelInfo target = LevelManager.Instance.Levels.Find(level => level.path.Equals(path)); if (target == null) { target = LevelManager.Instance.CustomLevels.Find(level => level.path.Equals(path)); } - LevelManager.Instance.LoadLevel(target); - StartCoroutine(CloseAfterLoad()); - yield break; - } - private IEnumerator CloseAfterLoad() { - while (GameManagement.GameStateMachine.Instance.IsLoading) { - yield return new WaitForEndOfFrame(); + yield return new WaitWhile(() => GameManagement.GameStateMachine.Instance.IsLoading); + if (!target.Equals(LevelManager.Instance.currentLevel)) { + //Load map with path + LevelSelectionController levelSelectionController = GameStateMachine.Instance.LevelSelectionObject.GetComponentInChildren(); + + GameStateMachine.Instance.RequestTransitionTo(typeof(PauseState)); + GameStateMachine.Instance.RequestTransitionTo(typeof(LevelSelectionState)); + + IndexPath targetIndex = Traverse.Create(levelSelectionController).Method("GetIndexForLevel", target).GetValue(); + Traverse.Create(levelSelectionController).Method("OnLevelHighlighted", targetIndex).GetValue(); + + string texturePath = Path.ChangeExtension(target.path, "png"); + if (!File.Exists(texturePath)) { + texturePath = Path.ChangeExtension(target.path, "jpg"); + } + if (!File.Exists(texturePath)) { + texturePath = Path.ChangeExtension(target.path, "jpeg"); + } + + if (File.Exists(texturePath)) { + yield return new WaitWhile(() => target.previewImage == null); + } + + levelSelectionController.OnItemSelected(targetIndex); + + yield return new WaitWhile(() => GameStateMachine.Instance.IsLoading); + + PlayerController.Instance.respawn.ForceRespawn(); } - GameManagement.GameStateMachine.Instance.LevelSelectionObject.SetActive(false); yield break; } diff --git a/XLMultiplayerServer/Server.cs b/XLMultiplayerServer/Server.cs index 1b4736e..2e6c9d5 100644 --- a/XLMultiplayerServer/Server.cs +++ b/XLMultiplayerServer/Server.cs @@ -1052,7 +1052,9 @@ public void ServerLoop() { // Handle map voting and map enforcement if (ENFORCE_MAPS) { if (total_players == 0) { - currentMapHash = "2"; + Random newRandom = new Random(); + + currentMapHash = newRandom.Next(0, 8).ToString(); } bool startNewTimer = false;