Skip to content

Commit

Permalink
Un-mono notif manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed Jan 15, 2025
1 parent a35d116 commit adbeef9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 52 deletions.
1 change: 1 addition & 0 deletions Fika.Core/FikaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public ManualLogSource FikaLogger
public bool LocalesLoaded;

internal InternalBundleLoader BundleLoaderPlugin { get; private set; }
internal FikaNotificationManager NotificationManager { get; set; }

private static readonly Version RequiredServerVersion = new("2.4.0");

Expand Down
99 changes: 48 additions & 51 deletions Fika.Core/Networking/Websocket/FikaNotificationManager.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using BepInEx.Logging;
using Comfort.Common;
using Diz.Utils;
using EFT.UI;
using Fika.Core.Coop.Utils;
using Fika.Core.Networking.Websocket.Notifications;
using Newtonsoft.Json.Linq;
using SPT.Common.Http;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Linq;
using UnityEngine;
using System.Threading.Tasks;
using WebSocketSharp;

namespace Fika.Core.Networking.Websocket
{
public class FikaNotificationManager : MonoBehaviour
internal class FikaNotificationManager
{
private static readonly ManualLogSource logger = BepInEx.Logging.Logger.CreateLogSource("FikaNotificationManager");
public static FikaNotificationManager Instance;
Expand All @@ -25,75 +24,64 @@ public static bool Exists
return Instance != null;
}
}

public string Host { get; set; }
public string Url { get; set; }
public string SessionId { get; set; }
public bool Reconnecting { get; private set; }
public bool Connected
{
get
{
return _webSocket.ReadyState == WebSocketState.Open;
return webSocket.ReadyState == WebSocketState.Open;
}
}

private WebSocket _webSocket;
// Add a queue for incoming notifications, so they can be brought to the main thread in a nice way.
private ConcurrentQueue<NotificationAbstractClass> notificationsQueue = new();
public bool reconnecting;

private WebSocket webSocket;

public void Awake()
public FikaNotificationManager()
{
Instance = this;
Host = RequestHandler.Host.Replace("http", "ws");
SessionId = RequestHandler.SessionId;
Url = $"{Host}/fika/notification/";

_webSocket = new WebSocket(Url)
webSocket = new WebSocket(Url)
{
WaitTime = TimeSpan.FromMinutes(1),
EmitOnPing = true
};

_webSocket.SetCredentials(SessionId, "", true);
webSocket.SetCredentials(SessionId, "", true);

_webSocket.OnError += WebSocket_OnError;
_webSocket.OnMessage += WebSocket_OnMessage;
_webSocket.OnClose += (sender, e) =>
webSocket.OnError += WebSocket_OnError;
webSocket.OnMessage += WebSocket_OnMessage;
webSocket.OnClose += (sender, e) =>
{
// Prevent looping the Coroutine over and over.
if (Reconnecting)
if (reconnecting)
{
return;
}

StartCoroutine(ReconnectWebSocket());
Task.Run(ReconnectWebSocket);
};

Connect();
}

public void Update()
{
while (notificationsQueue.TryDequeue(out NotificationAbstractClass notification))
{
Singleton<PreloaderUI>.Instance.NotifierView.method_3(notification);
}
}

private void WebSocket_OnError(object sender, ErrorEventArgs e)
{
logger.LogInfo($"WS error: {e.Message}");
}

public void Connect()
{
_webSocket.Connect();
webSocket.Connect();
}

public void Close()
{
_webSocket.Close();
webSocket.Close();
}

private void WebSocket_OnMessage(object sender, MessageEventArgs e)
Expand All @@ -120,61 +108,70 @@ private void WebSocket_OnMessage(object sender, MessageEventArgs e)
#if DEBUG
logger.LogDebug($"Received type: {type}");
#endif

NotificationAbstractClass notification = null;
switch (type)
{
case EFikaNotifications.StartedRaid:
StartRaidNotification startRaidNotification = e.Data.ParseJsonTo<StartRaidNotification>([]);
notification = e.Data.ParseJsonTo<StartRaidNotification>([]);

if (FikaGlobals.IsInRaid())
{
return;
}

notificationsQueue.Enqueue(startRaidNotification);
HandleNotification(notification);
break;
case EFikaNotifications.SentItem:
ReceivedSentItemNotification SentItemNotification = e.Data.ParseJsonTo<ReceivedSentItemNotification>([]);

notificationsQueue.Enqueue(SentItemNotification);
notification = e.Data.ParseJsonTo<ReceivedSentItemNotification>([]);
HandleNotification(notification);
break;
case EFikaNotifications.PushNotification:
PushNotification pushNotification = e.Data.ParseJsonTo<PushNotification>([]);

notificationsQueue.Enqueue(pushNotification);
notification = e.Data.ParseJsonTo<PushNotification>([]);
HandleNotification(notification);
break;
}
}
}

private void HandleNotification(NotificationAbstractClass notification)
{
AsyncWorker.RunInMainTread(() =>
{
Singleton<PreloaderUI>.Instance.NotifierView.method_3(notification);
});
}

private IEnumerator ReconnectWebSocket()
private async Task ReconnectWebSocket()
{
Reconnecting = true;
reconnecting = true;

while (true)
while (reconnecting)
{
if (_webSocket.ReadyState == WebSocketState.Open)
if (webSocket.ReadyState == WebSocketState.Open)
{
break;
}

// Don't attempt to reconnect if we're still attempting to connect.
if (_webSocket.ReadyState != WebSocketState.Connecting)
if (webSocket.ReadyState != WebSocketState.Connecting)
{
_webSocket.Connect();
webSocket.Connect();
}
yield return new WaitForSeconds(10f);
}

Reconnecting = false;
await Task.Delay(10 * 1000);

if (webSocket.ReadyState == WebSocketState.Open)
{
break;
}
}

yield return null;
reconnecting = false;
}

#if DEBUG
public static void TestNotification(EFikaNotifications type)
{
// Ugly ass one-liner, who cares. It's for debug purposes
string Username = FikaPlugin.DevelopersList.ToList()[new System.Random().Next(FikaPlugin.DevelopersList.Count)].Key;
string Username = FikaPlugin.DevelopersList.ToList()[new Random().Next(FikaPlugin.DevelopersList.Count)].Key;

switch (type)
{
Expand Down
2 changes: 1 addition & 1 deletion Fika.Core/UI/Patches/TarkovApplication_method_18_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static void Postfix()
{
if (!FikaNotificationManager.Exists)
{
Singleton<PreloaderUI>.Instance.gameObject.AddComponent<FikaNotificationManager>();
FikaPlugin.Instance.NotificationManager = new();
}
}
}
Expand Down

0 comments on commit adbeef9

Please sign in to comment.