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 class AddonCustomDataSyncMessage : QSBMessage<(uint playerId, string key, byte[] data)>
{ {
public AddonCustomDataSyncMessage(uint playerId, string key, object data) : base((playerId, key, data.ToBytes())) { } 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()); public override void OnReceiveRemote() => QSBPlayerManager.GetPlayer(Data.playerId).SetCustomData(Data.key, Data.data.ToObject());
} }

View File

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

View File

@ -53,10 +53,9 @@ public class RequestStateResyncMessage : QSBMessage
new PlayerInformationMessage { To = From }.Send(); new PlayerInformationMessage { To = From }.Send();
// Initial sync of all custom data from APIs // 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, kvp.Key, kvp.Value) { To = From }.Send();
new AddonCustomDataSyncMessage(QSBPlayerManager.LocalPlayerId, key, data) { To = From }.Send();
} }
} }
} }

View File

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

View File

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