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 5e94690..d3012bd 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; @@ -11,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); } } @@ -44,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) { @@ -54,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/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/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) { 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); }