From c03ce96d46963b1c71bb49ba10d587e174f69365 Mon Sep 17 00:00:00 2001 From: Kittenji <41535779+ChrisFeline@users.noreply.github.com> Date: Fri, 4 Oct 2024 15:53:20 -0400 Subject: [PATCH 1/2] Disable Chatbox if not in terrors. --- Utils/API/WebSocketAPI.cs | 1 + Utils/LilOSC.cs | 2 +- Utils/LogParser/LogWatcher.cs | 2 +- Utils/LogParser/ToNLogContext.cs | 3 ++- Windows/StatsWindow.cs | 3 ++- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Utils/API/WebSocketAPI.cs b/Utils/API/WebSocketAPI.cs index 5e94690..1138b44 100644 --- a/Utils/API/WebSocketAPI.cs +++ b/Utils/API/WebSocketAPI.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; diff --git a/Utils/LilOSC.cs b/Utils/LilOSC.cs index 6cb97f1..b6dea25 100644 --- a/Utils/LilOSC.cs +++ b/Utils/LilOSC.cs @@ -246,7 +246,7 @@ internal static void SendData(bool force = false) { if (LastStarted != IsRoundActive || force) SendParam(ParamStarted, LastStarted = IsRoundActive); } - if (ChatboxClear || (Settings.Get.OSCSendChatbox && MainWindow.Started && !force && !string.IsNullOrEmpty(ChatboxMessage))) { + if (ChatboxClear || (ToNLogContext.CanSendChatbox && MainWindow.Started && !force && !string.IsNullOrEmpty(ChatboxMessage))) { ChatboxCountdown -= MainWindow.LogWatcher.Interval; if (ChatboxCountdown < 0) { ChatboxCountdown = ChatboxInterval; diff --git a/Utils/LogParser/LogWatcher.cs b/Utils/LogParser/LogWatcher.cs index 0c2a60d..867717a 100644 --- a/Utils/LogParser/LogWatcher.cs +++ b/Utils/LogParser/LogWatcher.cs @@ -305,7 +305,7 @@ private bool ParseLocation(string line, DateTime lineDate, T logContext) if (index >= line.Length) return false; var instanceId = line.Substring(index).Trim('\n', '\r'); - logContext.Enter(instanceId, instanceId.StartsWith(HomeWorldID)); + logContext.Enter(instanceId, instanceId.Contains(HomeWorldID)); return true; } diff --git a/Utils/LogParser/ToNLogContext.cs b/Utils/LogParser/ToNLogContext.cs index bf9b2f9..e9c6655 100644 --- a/Utils/LogParser/ToNLogContext.cs +++ b/Utils/LogParser/ToNLogContext.cs @@ -14,6 +14,7 @@ namespace ToNSaveManager.Utils.LogParser { internal class ToNLogContext : LogContext { internal static ToNLogContext? Instance { get; private set; } + internal static bool CanSendChatbox => Instance != null && Instance.IsHomeWorld && Settings.Get.OSCSendChatbox; public bool HasLoadedSave { get; set; } @@ -66,7 +67,7 @@ public override void OnAwake() { if (IsRecent) { Instance = this; ToNGameState.ClearStates(); - Logger.Debug("Log Context Path: " + this.FilePath); + LilOSC.SetChatboxMessage(string.Empty); } ClearSummary(); diff --git a/Windows/StatsWindow.cs b/Windows/StatsWindow.cs index 0939bd3..2a83f46 100644 --- a/Windows/StatsWindow.cs +++ b/Windows/StatsWindow.cs @@ -10,6 +10,7 @@ using ToNSaveManager.Models.Index; using ToNSaveManager.Models.Stats; using ToNSaveManager.Utils; +using ToNSaveManager.Utils.LogParser; using static System.Windows.Forms.AxHost; namespace ToNSaveManager @@ -229,7 +230,7 @@ public static string NormalizeLabelText(string text) { */ internal static void UpdateChatboxContent() { - if (IsRoundActive || !MainWindow.Started || !Settings.Get.OSCSendChatbox || string.IsNullOrEmpty(Settings.Get.OSCMessageInfoTemplate.Template)) return; + if (IsRoundActive || !MainWindow.Started || !ToNLogContext.CanSendChatbox || string.IsNullOrEmpty(Settings.Get.OSCMessageInfoTemplate.Template)) return; string template = TemplateManager.ReplaceTemplate(Settings.Get.OSCMessageInfoTemplate.Template); LilOSC.SetChatboxMessage(template); } From c7cd419d7d74e0dacab5c1df67b4910744894caf Mon Sep 17 00:00:00 2001 From: Kittenji <41535779+ChrisFeline@users.noreply.github.com> Date: Fri, 4 Oct 2024 16:36:28 -0400 Subject: [PATCH 2/2] New WebSocket Library --- ToNSaveManager.csproj | 2 +- Utils/API/WebSocketAPI.cs | 50 ++++++++++++++++++++++++--------------- Windows/MainWindow.cs | 5 +++- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/ToNSaveManager.csproj b/ToNSaveManager.csproj index 054e032..6793f82 100644 --- a/ToNSaveManager.csproj +++ b/ToNSaveManager.csproj @@ -48,7 +48,7 @@ - + diff --git a/Utils/API/WebSocketAPI.cs b/Utils/API/WebSocketAPI.cs index 1138b44..d3012bd 100644 --- a/Utils/API/WebSocketAPI.cs +++ b/Utils/API/WebSocketAPI.cs @@ -12,32 +12,29 @@ using ToNSaveManager.Models.Index; using ToNSaveManager.Models.Stats; using ToNSaveManager.Utils.LogParser; -using WebSocketSharp; -using WebSocketSharp.Server; +// using WebSocketSharp; +// using WebSocketSharp.Server; +using WebSocketServer = WatsonWebsocket.WatsonWsServer; namespace ToNSaveManager.Utils.API { - internal class WebSocketAPI : WebSocketBehavior { + internal class WebSocketAPI { static readonly LoggerSource Logger = new LoggerSource(nameof(WebSocketAPI)); internal static WebSocketServer? Server; - protected override void OnMessage(MessageEventArgs e) { - Logger.Log("Received: " + e.Data); - } - - protected override void OnOpen() { - Logger.Log("WebSocket client connected."); + static void OnOpen(WatsonWebsocket.ClientMetadata metadata) { + Logger.Log("WebSocket client connected: " + metadata.Guid + " | " + metadata.Ip); SendEvent(new EventConnected() { Args = EventBuffer.ToArray(), DisplayName = ToNLogContext.Instance?.DisplayName ?? string.Empty, UserID = ToNLogContext.Instance?.UserID ?? string.Empty - }, this); + }, metadata); - SendEvent(new EventValue("INSTANCE", ToNLogContext.Instance?.InstanceID), this); + SendEvent(new EventValue("INSTANCE", ToNLogContext.Instance?.InstanceID), metadata); // Send All Stats foreach (string key in ToNStats.PropertyKeys) { - SendEvent(new EventStats() { Name = key, Value = ToNStats.Get(key) }, this); + SendEvent(new EventStats() { Name = key, Value = ToNStats.Get(key) }, metadata); } } @@ -45,8 +42,11 @@ protected override void OnOpen() { internal static void Initialize() { if (Settings.Get.WebSocketEnabled && Server == null) { int port = Settings.Get.WebSocketPort > 0 ? Settings.Get.WebSocketPort : DEFAULT_PORT; - Server = new WebSocketServer(port); - Server.AddWebSocketService("/"); + Server = new WebSocketServer(new List() { IPAddress.Loopback.ToString(), "localhost" }, port); + //Server.AddWebSocketService("/"); + + Server.ClientConnected += Server_ClientConnected; + Server.ClientDisconnected += Server_ClientDisconnected; } if (Settings.Get.WebSocketEnabled && Server != null && !Server.IsListening) { @@ -55,23 +55,35 @@ internal static void Initialize() { } else if (!Settings.Get.WebSocketEnabled && Server != null && Server.IsListening) { Logger.Debug("Stopping Server..."); Server.Stop(); - Server.RemoveWebSocketService("/"); + Server.Dispose(); Server = null; } } + private static void Server_ClientDisconnected(object? sender, WatsonWebsocket.DisconnectionEventArgs e) { + Logger.Log("WebSocket client disconnected" + e.Client.Guid); + } + + private static void Server_ClientConnected(object? sender, WatsonWebsocket.ConnectionEventArgs e) { + OnOpen(e.Client); + } + internal static void Broadcast(string data) { + if (Server == null) return; Logger.Debug("Broadcasting: " + data); - Server?.WebSocketServices?.Broadcast(data); + + foreach (var metadata in Server.ListClients()) { + _ = Server.SendAsync(metadata.Guid, data).Result; + } } - internal static void SendEvent(T value, WebSocketAPI? connection) where T : IEvent { + internal static void SendEvent(T value, WatsonWebsocket.ClientMetadata? metadata) where T : IEvent { if (!Settings.Get.WebSocketEnabled) return; string jsonData = JsonConvert.SerializeObject(value); try { - if (connection != null) { + if (metadata != null) { Logger.Debug("Sending: " + jsonData); - connection.Send(jsonData); + _ = Server?.SendAsync(metadata.Guid, jsonData).Result; return; } diff --git a/Windows/MainWindow.cs b/Windows/MainWindow.cs index c89f948..5afd925 100644 --- a/Windows/MainWindow.cs +++ b/Windows/MainWindow.cs @@ -72,7 +72,10 @@ private void mainWindow_Loaded(object sender, EventArgs e) { TooltipUtil.Set(linkSupport, "Buy Me A Coffee ♥"); // TODO: Implement this after a week or whatever. - // if (Program.ContainsArg("--emulator") || Program.ContainsArg("--emu") || Program.ContainsArg("-e")) EmulatorWindow.Open(this); + // if (Program.ContainsArg("--emulator") || Program.ContainsArg("--emu") || Program.ContainsArg("-e")) +#if DEBUG + // EmulatorWindow.Open(this); +#endif } private void mainWindow_Shown(object sender, EventArgs e) {