i dont think we need ChangeType

This commit is contained in:
JohnCorby 2023-07-29 16:53:02 -07:00
parent 4d545d12f3
commit d48a1e33fe
3 changed files with 7 additions and 7 deletions

View File

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

View File

@ -11,5 +11,5 @@ public interface IQSBAPI
T GetCustomData<T>(uint playerId, string key);
void SendMessage<T>(string messageType, T data);
void RegisterHandler<T>(string messageType, Action<T> action);
void RegisterHandler<T>(string messageType, Action<T> handler);
}

View File

@ -17,8 +17,8 @@ public class QSBAPI : IQSBAPI
new AddonDataMessage(messageType, data).Send();
}
public void RegisterHandler<T>(string messageType, Action<T> action)
public void RegisterHandler<T>(string messageType, Action<T> handler)
{
AddonDataManager.RegisterHandler(messageType, action);
AddonDataManager.RegisterHandler(messageType, handler);
}
}