Skip to content

Commit

Permalink
Handle player join GUID
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisFeline committed Nov 10, 2024
1 parent f3854d3 commit c375acd
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 26 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Latest Changelog

- Date list now sorts using Universal Time. This prevents unwanted behaviour when 'Daylight Saving' event occurs.
- 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.
1 change: 1 addition & 0 deletions Docs/WebSocketAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion Utils/API/WebSocketAPI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Jint.Native;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions Utils/LogParser/LogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public virtual void OnInit() {
public bool IsHomeWorld { get; private set; }

public DateTime RoomDate { get; private set; }
public readonly HashSet<string> Players = new HashSet<string>();
public readonly HashSet<LogPlayer> Players = new HashSet<LogPlayer>();

/*
// Hold temporal data in this log instance
Expand Down Expand Up @@ -78,17 +78,17 @@ public static T CreateContext<T>(string fileName, string filePath, bool isRecent
/// <summary>
/// Called when a player joins the room.
/// </summary>
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);
}

/// <summary>
/// Called when a player leaves the room.
/// </summary>
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);
}

/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions Utils/LogParser/LogPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace ToNSaveManager.Utils.LogParser {
public struct LogPlayer : IEquatable<LogPlayer> {
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();
}
}
}
29 changes: 18 additions & 11 deletions Utils/LogParser/LogWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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: '";
Expand Down
12 changes: 6 additions & 6 deletions Utils/LogParser/ToNLogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit c375acd

Please sign in to comment.