Make TempClientData cached and disposable

This commit is contained in:
D1GQ committed 2026-04-06 18:09:03 -05:00
1 parent e12f1a9764
commit 476441ec71
1 file changed
+40 -1
+40 -1
View File
@@ -7,8 +7,47 @@ namespace BetterAmongUs.Structs;
/// Represents temporary client data for caching or snapshot purposes.
/// </summary>
/// <param name="clientData">The client data to copy values from.</param>
internal readonly struct TempClientData(ClientData clientData)
internal readonly struct TempClientData(ClientData clientData) : IDisposable
{
private static readonly Dictionary<string, TempClientData> friendCodeLookup = [];
private static readonly Dictionary<string, TempClientData> puidLookup = [];
/// <summary>
/// Gets or creates a cached instance of TempClientData for the specified client data.
/// </summary>
/// <param name="clientData">The client data to get or create a cached instance for.</param>
/// <returns>A cached TempClientData instance for the specified client.</returns>
internal static TempClientData Get(ClientData clientData)
{
TempClientData tempClientData;
if (!friendCodeLookup.TryGetValue(clientData.FriendCode, out tempClientData))
{
if (!puidLookup.TryGetValue(clientData.ProductUserId, out tempClientData))
{
tempClientData = new TempClientData(clientData);
if (!string.IsNullOrEmpty(clientData.FriendCode))
{
friendCodeLookup[clientData.FriendCode] = tempClientData;
}
if (!string.IsNullOrEmpty(clientData.ProductUserId))
{
puidLookup[clientData.ProductUserId] = tempClientData;
}
}
}
return tempClientData;
}
/// <summary>
/// Disposes the TempClientData instance by removing it from all lookup caches.
/// </summary>
public void Dispose()
{
friendCodeLookup.Remove(FriendCode);
puidLookup.Remove(Puid);
}
/// <summary>
/// Gets the unique identifier of the client.
/// </summary>