Skip to content

Commit

Permalink
Fix state change on level load
Browse files Browse the repository at this point in the history
Fix custom levels not using image on load
Server picks random built-in map for default when no players connected
  • Loading branch information
silentbaws committed Aug 5, 2020
1 parent 54c4b10 commit 134bdc6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
7 changes: 7 additions & 0 deletions XLMultiplayer/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 31 additions & 11 deletions XLMultiplayer/MultiplayerController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define VALVESOCKETS_SPAN

using GameManagement;
using HarmonyLib;
using Newtonsoft.Json;
using ReplayEditor;
Expand All @@ -19,6 +20,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
using Valve.Sockets;

Expand Down Expand Up @@ -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<LevelSelectionController>();
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<LevelSelectionController>();

GameStateMachine.Instance.RequestTransitionTo(typeof(PauseState));
GameStateMachine.Instance.RequestTransitionTo(typeof(LevelSelectionState));

IndexPath targetIndex = Traverse.Create(levelSelectionController).Method("GetIndexForLevel", target).GetValue<IndexPath>();
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;
}

Expand Down
4 changes: 3 additions & 1 deletion XLMultiplayerServer/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 134bdc6

Please sign in to comment.