This commit is contained in:
JohnCorby 2023-08-02 14:07:44 -07:00
parent fcef4d749d
commit 1756c90c17
5 changed files with 15 additions and 16 deletions

View File

@ -7,6 +7,5 @@ namespace QSB.API.Messages;
public class AddonCustomDataSyncMessage : QSBMessage<(uint playerId, string key, byte[] data)>
{
public AddonCustomDataSyncMessage(uint playerId, string key, object data) : base((playerId, key, data.ToBytes())) { }
public override void OnReceiveRemote() => QSBPlayerManager.GetPlayer(Data.playerId).SetCustomData(Data.key, Data.data.ToObject());
}

View File

@ -1,9 +1,9 @@
using System;
using System.Linq;
using OWML.Common;
using OWML.Common;
using QSB.API.Messages;
using QSB.Messaging;
using QSB.Player;
using System;
using System.Linq;
using UnityEngine.Events;
namespace QSB.API;
@ -26,11 +26,8 @@ public class QSBAPI : IQSBAPI
public UnityEvent<uint> OnPlayerJoin() => QSBAPIEvents.OnPlayerJoinEvent;
public UnityEvent<uint> OnPlayerLeave() => QSBAPIEvents.OnPlayerLeaveEvent;
public void SetCustomData<T>(uint playerId, string key, T data)
=> QSBPlayerManager.GetPlayer(playerId).SetCustomData<T>(key, data);
public T GetCustomData<T>(uint playerId, string key)
=> QSBPlayerManager.GetPlayer(playerId).GetCustomData<T>(key);
public void SetCustomData<T>(uint playerId, string key, T data) => QSBPlayerManager.GetPlayer(playerId).SetCustomData(key, data);
public T GetCustomData<T>(uint playerId, string key) => QSBPlayerManager.GetPlayer(playerId).GetCustomData<T>(key);
public void SendMessage<T>(string messageType, T data, uint to = uint.MaxValue, bool receiveLocally = false)
=> new AddonDataMessage(messageType, data, receiveLocally) { To = to }.Send();

View File

@ -53,10 +53,9 @@ public class RequestStateResyncMessage : QSBMessage
new PlayerInformationMessage { To = From }.Send();
// Initial sync of all custom data from APIs
foreach (var key in QSBPlayerManager.LocalPlayer.GetCustomDataKeys())
foreach (var kvp in QSBPlayerManager.LocalPlayer._customData)
{
var data = QSBPlayerManager.LocalPlayer.GetCustomData<object>(key);
new AddonCustomDataSyncMessage(QSBPlayerManager.LocalPlayerId, key, data) { To = From }.Send();
new AddonCustomDataSyncMessage(QSBPlayerManager.LocalPlayerId, kvp.Key, kvp.Value) { To = From }.Send();
}
}
}

View File

@ -181,7 +181,8 @@ public partial class PlayerInfo
HUDBox.OnRespawn();
}
private Dictionary<string, object> _customData = new();
// internal for message
internal readonly Dictionary<string, object> _customData = new();
public void SetCustomData<T>(string key, T data)
{
@ -193,7 +194,6 @@ public partial class PlayerInfo
}
}
public T GetCustomData<T>(string key)
{
if (!_customData.TryGetValue(key, out var value))
@ -204,7 +204,5 @@ public partial class PlayerInfo
return (T)value;
}
public IEnumerable<string> GetCustomDataKeys() => _customData.Keys;
public override string ToString() => $"{PlayerId}:{GetType().Name} ({Name})";
}

View File

@ -239,6 +239,9 @@ public static class Extensions
return sb.ToString();
}
/// <summary>
/// only works for c# serializable objects
/// </summary>
public static byte[] ToBytes(this object obj)
{
using var ms = new MemoryStream();
@ -248,6 +251,9 @@ public static class Extensions
return bytes;
}
/// <summary>
/// only works for c# serializable objects
/// </summary>
public static object ToObject(this byte[] bytes)
{
using var ms = new MemoryStream(bytes);