126 lines
4.0 KiB
C#
126 lines
4.0 KiB
C#
using Harmony;
|
|
using I2.Loc;
|
|
using Riptide.Demos.Steam.PlayerHosted;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace KCM.UI
|
|
{
|
|
[HarmonyPatch(typeof(GameState), "SetNewMode")]
|
|
public static class MultiplayerMenuInjector
|
|
{
|
|
private static bool injected;
|
|
|
|
public static void Postfix()
|
|
{
|
|
try
|
|
{
|
|
EnsureInjected();
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Main.Log("MultiplayerMenuInjector failed");
|
|
Main.Log(ex);
|
|
}
|
|
}
|
|
|
|
private static void EnsureInjected()
|
|
{
|
|
if (injected)
|
|
return;
|
|
|
|
if (GameState.inst == null || GameState.inst.mainMenuMode == null)
|
|
return;
|
|
|
|
GameObject mainMenuUi = GameState.inst.mainMenuMode.mainMenuUI;
|
|
if (mainMenuUi == null)
|
|
return;
|
|
|
|
Transform buttonContainer = mainMenuUi.transform.Find("TopLevelUICanvas/TopLevel/Body/ButtonContainer");
|
|
if (buttonContainer == null)
|
|
return;
|
|
|
|
if (buttonContainer.Find("KCM_Multiplayer") != null)
|
|
{
|
|
injected = true;
|
|
return;
|
|
}
|
|
|
|
Button template = mainMenuUi.transform.Find("TopLevelUICanvas/TopLevel/Body/ButtonContainer/New")?.GetComponent<Button>();
|
|
if (template == null)
|
|
return;
|
|
|
|
Button button = Object.Instantiate(template, buttonContainer);
|
|
button.name = "KCM_Multiplayer";
|
|
|
|
foreach (Localize loc in button.GetComponentsInChildren<Localize>())
|
|
Object.Destroy(loc);
|
|
|
|
button.onClick = new Button.ButtonClickedEvent();
|
|
|
|
TextMeshProUGUI label = button.GetComponentInChildren<TextMeshProUGUI>();
|
|
if (label != null)
|
|
label.text = "Multiplayer";
|
|
|
|
int insertIndex = template.transform.GetSiblingIndex() + 1;
|
|
button.transform.SetSiblingIndex(insertIndex);
|
|
|
|
button.onClick.AddListener(OpenServerBrowser);
|
|
|
|
injected = true;
|
|
Main.Log("Injected Multiplayer menu button");
|
|
}
|
|
|
|
private static void OpenServerBrowser()
|
|
{
|
|
try
|
|
{
|
|
// Ensure Steam + lobby manager exist
|
|
_ = KCMSteamManager.Instance;
|
|
|
|
LobbyManager lobbyManager = Object.FindObjectOfType<LobbyManager>();
|
|
if (lobbyManager == null)
|
|
{
|
|
GameObject go = new GameObject("KCM_LobbyManager");
|
|
Object.DontDestroyOnLoad(go);
|
|
lobbyManager = go.AddComponent<LobbyManager>();
|
|
}
|
|
|
|
// Ensure prefabs loaded
|
|
if (PrefabManager.serverBrowserPrefab == null || PrefabManager.serverLobbyPrefab == null)
|
|
{
|
|
if (Main.helper != null)
|
|
new PrefabManager().PreScriptLoad(Main.helper);
|
|
}
|
|
|
|
// Ensure UI initialized
|
|
KCM.ServerBrowser browser = Object.FindObjectOfType<KCM.ServerBrowser>();
|
|
if (browser == null)
|
|
{
|
|
GameObject go = new GameObject("KCM_ServerBrowser");
|
|
Object.DontDestroyOnLoad(go);
|
|
browser = go.AddComponent<KCM.ServerBrowser>();
|
|
}
|
|
|
|
browser.EnsureUi();
|
|
|
|
if (KCM.ServerBrowser.serverLobbyRef != null)
|
|
KCM.ServerBrowser.serverLobbyRef.SetActive(false);
|
|
|
|
if (KCM.ServerBrowser.serverBrowserRef != null)
|
|
KCM.ServerBrowser.serverBrowserRef.SetActive(true);
|
|
|
|
if (GameState.inst != null && GameState.inst.mainMenuMode != null)
|
|
GameState.inst.mainMenuMode.mainMenuUI.SetActive(false);
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Main.Log("Failed opening server browser");
|
|
Main.Log(ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|