add option to receive data message locally, and give "from" data

This commit is contained in:
_nebula 2023-07-30 11:23:11 +01:00
parent 056f2d75d0
commit 52e31d994b
4 changed files with 22 additions and 14 deletions

View File

@ -6,22 +6,22 @@ namespace QSB.API;
public static class AddonDataManager
{
private static readonly Dictionary<string, Action<object>> _handlers = new();
private static readonly Dictionary<string, Action<uint, object>> _handlers = new();
public static void OnReceiveDataMessage(string messageType, object data)
public static void OnReceiveDataMessage(string messageType, object data, uint from)
{
DebugLog.DebugWrite($"Received data message of message type \"{messageType}\"!");
DebugLog.DebugWrite($"Received data message of message type \"{messageType}\" from {from}!");
if (!_handlers.TryGetValue(messageType, out var handler))
{
return;
}
handler(data);
handler(from, data);
}
public static void RegisterHandler<T>(string messageType, Action<T> handler)
public static void RegisterHandler<T>(string messageType, Action<uint, T> handler)
{
DebugLog.DebugWrite($"Registering handler for \"{messageType}\" with type of {typeof(T).Name}");
_handlers.Add(messageType, data => handler((T)data));
_handlers.Add(messageType, (from, data) => handler(from, (T)data));
}
}

View File

@ -10,6 +10,6 @@ public interface IQSBAPI
void SetCustomData<T>(uint playerId, string key, T data);
T GetCustomData<T>(uint playerId, string key);
void SendMessage<T>(string messageType, T data);
void RegisterHandler<T>(string messageType, Action<T> handler);
void SendMessage<T>(string messageType, T data, bool receiveLocally = false);
void RegisterHandler<T>(string messageType, Action<uint, T> handler);
}

View File

@ -4,9 +4,9 @@ using QSB.Messaging;
namespace QSB.API.Messages;
public class AddonDataMessage : QSBMessage<(string messageType, byte[] data)>
public class AddonDataMessage : QSBMessage<(string messageType, byte[] data, bool receiveLocally)>
{
public AddonDataMessage(string messageType, object data) : base((messageType, Obj2Bytes(data))) { }
public AddonDataMessage(string messageType, object data, bool receiveLocally) : base((messageType, Obj2Bytes(data), receiveLocally)) { }
private static byte[] Obj2Bytes(object obj)
{
@ -25,9 +25,17 @@ public class AddonDataMessage : QSBMessage<(string messageType, byte[] data)>
return obj;
}
public override void OnReceiveLocal()
{
if (Data.receiveLocally)
{
OnReceiveRemote();
}
}
public override void OnReceiveRemote()
{
var obj = Bytes2Obj(Data.data);
AddonDataManager.OnReceiveDataMessage(Data.messageType, obj);
AddonDataManager.OnReceiveDataMessage(Data.messageType, obj, From);
}
}

View File

@ -12,12 +12,12 @@ public class QSBAPI : IQSBAPI
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)
public void SendMessage<T>(string messageType, T data, bool receiveLocally = false)
{
new AddonDataMessage(messageType, data).Send();
new AddonDataMessage(messageType, data, receiveLocally).Send();
}
public void RegisterHandler<T>(string messageType, Action<T> handler)
public void RegisterHandler<T>(string messageType, Action<uint, T> handler)
{
AddonDataManager.RegisterHandler(messageType, handler);
}