mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-30 12:32:55 +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 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());
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
@ -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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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})";
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user