Skip to content

Commit

Permalink
Improved UI states when hosting a dedicated client + fixed thread issues
Browse files Browse the repository at this point in the history
  • Loading branch information
trippyone committed Jul 8, 2024
1 parent b96b327 commit 366771f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 39 deletions.
3 changes: 3 additions & 0 deletions Fika.Core/FikaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ protected void Awake()
new AbstractGame_InRaid_Patch().Enable();
new DisconnectButton_Patch().Enable();
new ChangeGameModeButton_Patch().Enable();

gameObject.AddComponent<MainThreadDispatcher>();

#if GOLDMASTER
new TOS_Patch().Enable();
#endif
Expand Down
49 changes: 21 additions & 28 deletions Fika.Core/Networking/Websocket/DedicatedRaidWebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -91,43 +94,33 @@ private void WebSocket_OnMessage(object sender, MessageEventArgs e)
return;
}

string type = jsonObject["type"].ToString();
string type = jsonObject.Value<string>("type");

switch (type)
{
case "fikaDedicatedJoinMatch":
MatchMakerUI matchmakerUI = FindObjectOfType<MatchMakerUI>();

string matchId = jsonObject.Value<string>("matchId");
MatchMakerAcceptScreen matchMakerAcceptScreen = FindObjectOfType<MatchMakerAcceptScreen>();
TMP_Text matchmakerUiHostRaidText = matchmakerUI.RaidGroupHostButton.GetComponentInChildren<TMP_Text>();
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<ClientApplication<ISession>>.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();
Expand Down
32 changes: 22 additions & 10 deletions Fika.Core/UI/Custom/MatchMakerUIScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions Fika.Core/Utils/MainThreadDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using UnityEngine;

public class MainThreadDispatcher : MonoBehaviour
{
private static MainThreadDispatcher _instance;
private readonly Queue<Action> _actions = new Queue<Action>();

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);
}
}
}

0 comments on commit 366771f

Please sign in to comment.