mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-30 03:32:47 +00:00
tweak
This commit is contained in:
parent
fcef4d749d
commit
1756c90c17
@ -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());
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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})";
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user