From c375acd5f9ff499b1c2072719db5dd4af823fc65 Mon Sep 17 00:00:00 2001 From: Kittenji <41535779+ChrisFeline@users.noreply.github.com> Date: Sun, 10 Nov 2024 03:19:04 -0500 Subject: [PATCH] Handle player join GUID --- CHANGELOG.md | 3 ++- Docs/WebSocketAPI.md | 1 + Utils/API/WebSocketAPI.cs | 23 ++++++++++++++++++++++- Utils/LogParser/LogContext.cs | 14 +++++++------- Utils/LogParser/LogPlayer.cs | 18 ++++++++++++++++++ Utils/LogParser/LogWatcher.cs | 29 ++++++++++++++++++----------- Utils/LogParser/ToNLogContext.cs | 12 ++++++------ 7 files changed, 74 insertions(+), 26 deletions(-) create mode 100644 Utils/LogParser/LogPlayer.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 6265200..7efb70a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ # Latest Changelog -- Date list now sorts using Universal Time. This prevents unwanted behaviour when 'Daylight Saving' event occurs. \ No newline at end of file +- Date list now sorts using Universal Time. This prevents unwanted behaviour when 'Daylight Saving' event occurs. +- Fixed player names showing their GUID on the tooltips after VRChat's latest update. \ No newline at end of file diff --git a/Docs/WebSocketAPI.md b/Docs/WebSocketAPI.md index 3475a6f..0d18a69 100644 --- a/Docs/WebSocketAPI.md +++ b/Docs/WebSocketAPI.md @@ -120,6 +120,7 @@ The event data will be sent as a json object string. // Sent when a player joins the instance. "TYPE": "PLAYER_JOIN", "Value": "Kittenji" // The name of the player that joined. +"ID": "usr_7ac745b8-e50e-4c9c-95e5-8e7e3bcde682" // The GUID of the player that joined. ``` ```json diff --git a/Utils/API/WebSocketAPI.cs b/Utils/API/WebSocketAPI.cs index 3da3b82..464bffe 100644 --- a/Utils/API/WebSocketAPI.cs +++ b/Utils/API/WebSocketAPI.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using Jint.Native; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; @@ -247,6 +248,26 @@ internal static void Send(ToNIndex.Item item) { QueueEvent(eventItem); } } + + public struct EventPlayerJoin : IEvent { + const string EVENT_PLAYER_JOIN = "PLAYER_JOIN"; + const string EVENT_PLAYER_LEAVE = "PLAYER_LEAVE"; + + public string Type { get; private set; } + [JsonIgnore] public byte Command { get; set; } + + public string Value { get; set; } + public string ID { get; set; } + + internal static void Send(LogPlayer player, bool joined) { + EventPlayerJoin eventPlayer = new EventPlayerJoin() { + Type = joined ? EVENT_PLAYER_JOIN : EVENT_PLAYER_LEAVE, + Value = player.Name, + ID = player.GUID + }; + QueueEvent(eventPlayer); + } + } #endregion #region Save Manager Events diff --git a/Utils/LogParser/LogContext.cs b/Utils/LogParser/LogContext.cs index f8ba1b5..6de748f 100644 --- a/Utils/LogParser/LogContext.cs +++ b/Utils/LogParser/LogContext.cs @@ -32,7 +32,7 @@ public virtual void OnInit() { public bool IsHomeWorld { get; private set; } public DateTime RoomDate { get; private set; } - public readonly HashSet Players = new HashSet(); + public readonly HashSet Players = new HashSet(); /* // Hold temporal data in this log instance @@ -78,17 +78,17 @@ public static T CreateContext(string fileName, string filePath, bool isRecent /// /// Called when a player joins the room. /// - public virtual void Join(string displayName) { - if (!Players.Contains(displayName)) - Players.Add(displayName); + public virtual void Join(LogPlayer player) { + if (!Players.Contains(player)) + Players.Add(player); } /// /// Called when a player leaves the room. /// - public virtual void Leave(string displayName) { - if (Players.Contains(displayName)) - Players.Remove(displayName); + public virtual void Leave(LogPlayer player) { + if (Players.Contains(player)) + Players.Remove(player); } /// diff --git a/Utils/LogParser/LogPlayer.cs b/Utils/LogParser/LogPlayer.cs new file mode 100644 index 0000000..6f7e523 --- /dev/null +++ b/Utils/LogParser/LogPlayer.cs @@ -0,0 +1,18 @@ +namespace ToNSaveManager.Utils.LogParser { + public struct LogPlayer : IEquatable { + public string Name; + public string GUID; + + public bool Equals(LogPlayer other) { + return Name.Equals(other.Name); + } + + public override string ToString() { + return Name; + } + + public override int GetHashCode() { + return Name.GetHashCode(); + } + } +} diff --git a/Utils/LogParser/LogWatcher.cs b/Utils/LogParser/LogWatcher.cs index 21ecf06..dcb85f7 100644 --- a/Utils/LogParser/LogWatcher.cs +++ b/Utils/LogParser/LogWatcher.cs @@ -339,29 +339,36 @@ private bool ParseLocation(string line, DateTime lineDate, T logContext) const string UserLeaveKeyword = "[Behaviour] OnPlayerLeft"; private bool ParsePlayerJoin(string line, DateTime lineDate, T logContext) { - int index; - string displayName; - if (line.Contains(UserJoinKeyword)) { - index = line.IndexOf(UserJoinKeyword, StringComparison.InvariantCulture) + UserJoinKeyword.Length; - displayName = line.Substring(index + 1).Trim(); - - logContext.Join(displayName); + logContext.Join(ParsePlayerData(line, UserJoinKeyword)); return true; } if (line.Contains(UserLeaveKeyword)) { - index = line.IndexOf(UserLeaveKeyword, StringComparison.InvariantCulture) + UserLeaveKeyword.Length; - displayName = line.Substring(index + 1).Trim(); - - logContext.Leave(displayName); + logContext.Leave(ParsePlayerData(line, UserLeaveKeyword)); return true; } return false; } + private static LogPlayer ParsePlayerData(string line, string keyword) { + int start = line.IndexOf(keyword, StringComparison.InvariantCulture) + keyword.Length + 1; + + string guid = string.Empty; + + int id_start = line.LastIndexOf('('); + int id_end = line.LastIndexOf(')'); + int length = line.Length - start; + if (id_start > 0 && id_end > 0 && id_end > id_start) { + guid = line.Substring(id_start + 1, (id_end - id_start) - 1).Trim(); + length = id_start - start; + } + + string name = line.Substring(start, length).Trim(); + return new LogPlayer() { GUID = guid, Name = name }; + } const string PickupGrabKeyword = "[Behaviour] Pickup object: '"; const string PickupDropKeyword = "[Behaviour] Drop object: '"; diff --git a/Utils/LogParser/ToNLogContext.cs b/Utils/LogParser/ToNLogContext.cs index c42dc99..3fec745 100644 --- a/Utils/LogParser/ToNLogContext.cs +++ b/Utils/LogParser/ToNLogContext.cs @@ -59,20 +59,20 @@ public override void Enter(string instanceID, bool isHomeWorld) { } } - public override void Join(string displayName) { - base.Join(displayName); + public override void Join(LogPlayer player) { + base.Join(player); if (IsRecent) { ToNGameState.SetPlayerCount(Players.Count); - WebSocketAPI.SendValue("PLAYER_JOIN", displayName); + WebSocketAPI.EventPlayerJoin.Send(player, true); } } - public override void Leave(string displayName) { - base.Leave(displayName); + public override void Leave(LogPlayer player) { + base.Leave(player); if (IsRecent) { ToNGameState.SetPlayerCount(Players.Count); - WebSocketAPI.SendValue("PLAYER_LEAVE", displayName); + WebSocketAPI.EventPlayerJoin.Send(player, false); } }