From 366771f53a02349f4bdd4a4680003c0540ec3566 Mon Sep 17 00:00:00 2001 From: trippyone <137233897+trippyone@users.noreply.github.com> Date: Mon, 8 Jul 2024 16:04:17 -0400 Subject: [PATCH] Improved UI states when hosting a dedicated client + fixed thread issues --- Fika.Core/FikaPlugin.cs | 3 ++ .../Websocket/DedicatedRaidWebSocketClient.cs | 49 ++++++++----------- Fika.Core/UI/Custom/MatchMakerUIScript.cs | 32 ++++++++---- .../MatchmakerAcceptScreen_Show_Patch.cs | 2 +- Fika.Core/Utils/MainThreadDispatcher.cs | 47 ++++++++++++++++++ 5 files changed, 94 insertions(+), 39 deletions(-) create mode 100644 Fika.Core/Utils/MainThreadDispatcher.cs diff --git a/Fika.Core/FikaPlugin.cs b/Fika.Core/FikaPlugin.cs index b719452c..8634c842 100644 --- a/Fika.Core/FikaPlugin.cs +++ b/Fika.Core/FikaPlugin.cs @@ -224,6 +224,9 @@ protected void Awake() new AbstractGame_InRaid_Patch().Enable(); new DisconnectButton_Patch().Enable(); new ChangeGameModeButton_Patch().Enable(); + + gameObject.AddComponent(); + #if GOLDMASTER new TOS_Patch().Enable(); #endif diff --git a/Fika.Core/Networking/Websocket/DedicatedRaidWebSocketClient.cs b/Fika.Core/Networking/Websocket/DedicatedRaidWebSocketClient.cs index 879e2057..ebac9e7e 100644 --- a/Fika.Core/Networking/Websocket/DedicatedRaidWebSocketClient.cs +++ b/Fika.Core/Networking/Websocket/DedicatedRaidWebSocketClient.cs @@ -3,21 +3,18 @@ using EFT.UI; using EFT; using Fika.Core.UI.Custom; -using LiteNetLib; using SPT.Common.Http; using System; using UnityEngine; using WebSocketSharp; -using HarmonyLib; using Newtonsoft.Json.Linq; using Comfort.Common; -using TMPro; -using UnityEngine.UI; using Fika.Core.Coop.Utils; +using Fika.Core.UI.Patches.MatchmakerAcceptScreen; namespace Fika.Core.Networking.Websocket { - public class DedicatedRaidWebSocketClient : MonoBehaviour + public class DedicatedRaidWebSocketClient { private static ManualLogSource logger = BepInEx.Logging.Logger.CreateLogSource("Fika.DedicatedWebSocket"); @@ -48,13 +45,19 @@ public DedicatedRaidWebSocketClient() _webSocket.OnOpen += WebSocket_OnOpen; _webSocket.OnError += WebSocket_OnError; - _webSocket.OnMessage += WebSocket_OnMessage; - //_webSocket.OnClose += WebSocket_OnClose; + _webSocket.OnMessage += (sender, args) => + { + // Run the OnMessage event on main thread + MainThreadDispatcher.RunOnMainThread(() => + { + WebSocket_OnMessage(sender, args); + }); + }; } private void WebSocket_OnError(object sender, ErrorEventArgs e) { - logger.LogInfo($"websocket err: {e.Message}"); + logger.LogInfo($"WS error: {e.Message}"); } public void Connect() @@ -91,43 +94,33 @@ private void WebSocket_OnMessage(object sender, MessageEventArgs e) return; } - string type = jsonObject["type"].ToString(); + string type = jsonObject.Value("type"); switch (type) { case "fikaDedicatedJoinMatch": - MatchMakerUI matchmakerUI = FindObjectOfType(); - string matchId = jsonObject.Value("matchId"); - MatchMakerAcceptScreen matchMakerAcceptScreen = FindObjectOfType(); - TMP_Text matchmakerUiHostRaidText = matchmakerUI.RaidGroupHostButton.GetComponentInChildren(); - if (matchMakerAcceptScreen == null) - { - PreloaderUI.Instance.ShowErrorScreen("Fika Dedicated Error", "Failed to find MatchMakerAcceptScreen"); - matchmakerUI.RaidGroupHostButton.interactable = true; - matchmakerUiHostRaidText.text = "HOST RAID"; - } + GameObject matchmakerObject = MatchmakerAcceptScreen_Show_Patch.MatchmakerObject; + MatchMakerAcceptScreen matchMakerAcceptScreen = FikaBackendUtils.MatchMakerAcceptScreenInstance; - if (matchId is not null) + if (!string.IsNullOrEmpty(matchId)) { TarkovApplication tarkovApplication = (TarkovApplication)Singleton>.Instance; tarkovApplication.StartCoroutine(MatchMakerUIScript.JoinMatch(tarkovApplication.Session.Profile.Id, matchId, null, () => - { - // MatchmakerAcceptScreen -> next screen (accept) - matchMakerAcceptScreen.method_22(); + { + // Hide matchmaker UI + matchmakerObject.SetActive(false); - Destroy(matchmakerUI.gameObject); - Destroy(matchmakerUI); + // Matchmaker next screen (accept) + matchMakerAcceptScreen.method_22(); })); } else { PreloaderUI.Instance.ShowErrorScreen("Fika Dedicated Error", "Received fikaJoinMatch WS event but there was no matchId"); - - matchmakerUI.RaidGroupHostButton.interactable = true; - matchmakerUiHostRaidText.text = "HOST RAID"; + matchmakerObject.SetActive(true); } FikaPlugin.DedicatedRaidWebSocket.Close(); diff --git a/Fika.Core/UI/Custom/MatchMakerUIScript.cs b/Fika.Core/UI/Custom/MatchMakerUIScript.cs index aebebb3e..9c7a9c1c 100644 --- a/Fika.Core/UI/Custom/MatchMakerUIScript.cs +++ b/Fika.Core/UI/Custom/MatchMakerUIScript.cs @@ -33,14 +33,30 @@ public class MatchMakerUIScript : MonoBehaviour public GameObject NewBackButton { get; internal set; } private string ProfileId => FikaBackendUtils.Profile.ProfileId; - private float _lastRefreshed; + private bool _started; + + protected void OnEnable() + { + if(_started) + { + StopQuery = false; + StartCoroutine(ServerQuery()); + } + } + + protected void OnDisable() + { + StopQuery = true; + StopCoroutine(ServerQuery()); + } protected void Start() { CreateMatchMakerUI(); - StartCoroutine(ServerQuery()); + + _started = true; } protected void Update() @@ -173,9 +189,7 @@ private void CreateMatchMakerUI() } else { - fikaMatchMakerUi.PlayerAmountSelection.SetActive(false); - fikaMatchMakerUi.RaidGroupHostButton.interactable = false; - matchmakerUiHostRaidText.text = "LOADING..."; + gameObject.SetActive(false); if (FikaPlugin.DedicatedRaidWebSocket == null) { @@ -204,12 +218,11 @@ private void CreateMatchMakerUI() var response = await FikaRequestHandler.StartDedicated(request); - if (response.Error is not null) + if (!string.IsNullOrEmpty(response.Error)) { PreloaderUI.Instance.ShowErrorScreen("Fika Dedicated Error", response.Error); - fikaMatchMakerUi.RaidGroupHostButton.interactable = true; - matchmakerUiHostRaidText.text = "HOST RAID"; + gameObject.SetActive(true); } else { @@ -268,8 +281,7 @@ public static IEnumerator JoinMatch(string profileId, string serverId, Button bu button.enabled = false; } - // TODO: crashes when JoinMatch is called from DedicatedRaidWebSocketClient - //NotificationManagerClass.DisplayMessageNotification("Connecting to session...", iconType: EFT.Communications.ENotificationIconType.EntryPoint); + NotificationManagerClass.DisplayMessageNotification("Connecting to session...", iconType: EFT.Communications.ENotificationIconType.EntryPoint); NetManagerUtils.CreatePingingClient(); diff --git a/Fika.Core/UI/Patches/MatchmakerAcceptScreen/MatchmakerAcceptScreen_Show_Patch.cs b/Fika.Core/UI/Patches/MatchmakerAcceptScreen/MatchmakerAcceptScreen_Show_Patch.cs index db6334ec..29ea58e2 100644 --- a/Fika.Core/UI/Patches/MatchmakerAcceptScreen/MatchmakerAcceptScreen_Show_Patch.cs +++ b/Fika.Core/UI/Patches/MatchmakerAcceptScreen/MatchmakerAcceptScreen_Show_Patch.cs @@ -18,7 +18,7 @@ protected override MethodBase GetTargetMethod() .First(x => x.Name == "Show" && x.GetParameters()[0].Name == "session"); } - private static GameObject MatchmakerObject { get; set; } + public static GameObject MatchmakerObject { get; set; } [PatchPrefix] private static void PreFix(ref RaidSettings raidSettings, DefaultUIButton ____acceptButton, DefaultUIButton ____backButton) diff --git a/Fika.Core/Utils/MainThreadDispatcher.cs b/Fika.Core/Utils/MainThreadDispatcher.cs new file mode 100644 index 00000000..51ca6e38 --- /dev/null +++ b/Fika.Core/Utils/MainThreadDispatcher.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +public class MainThreadDispatcher : MonoBehaviour +{ + private static MainThreadDispatcher _instance; + private readonly Queue _actions = new Queue(); + + private void Awake() + { + if (_instance == null) + { + _instance = this; + DontDestroyOnLoad(gameObject); + } + else + { + Destroy(gameObject); + } + } + + private void Update() + { + lock (_actions) + { + while (_actions.Count > 0) + { + _actions.Dequeue()(); + } + } + } + + public static void RunOnMainThread(Action action) + { + if (_instance == null) + { + Debug.LogError("MainThreadDispatcher is not initialized!"); + return; + } + + lock (_instance._actions) + { + _instance._actions.Enqueue(action); + } + } +} \ No newline at end of file