Now we can trigger events!
This commit is contained in:
@ -227,7 +227,7 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
|
||||
public void TriggerServerEvent(string eventName, params object[] args)
|
||||
public void SendTriggerEvent(string eventName, params object[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -350,6 +350,7 @@ namespace CoopServer
|
||||
EventName = reader.ReadString(eventNameBytes);
|
||||
|
||||
// Read args
|
||||
Args = new List<object>();
|
||||
int argsCount = reader.ReadInt();
|
||||
for (int i = 0; i < argsCount; i++)
|
||||
{
|
||||
|
@ -33,6 +33,7 @@ namespace CoopServer
|
||||
|
||||
public static Resource RunningResource = null;
|
||||
public static Dictionary<Command, Action<CommandContext>> Commands;
|
||||
public static readonly Dictionary<TriggerEvent, Action<EventContext>> TriggerEvents = new();
|
||||
|
||||
public static readonly List<Client> Clients = new();
|
||||
|
||||
@ -539,6 +540,47 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (byte)PacketTypes.ServerClientEvent:
|
||||
{
|
||||
try
|
||||
{
|
||||
int len = message.ReadInt32();
|
||||
byte[] data = message.ReadBytes(len);
|
||||
|
||||
Packets.ServerClientEvent packet = new Packets.ServerClientEvent();
|
||||
packet.NetIncomingMessageToPacket(data);
|
||||
|
||||
long senderNetHandle = message.SenderConnection.RemoteUniqueIdentifier;
|
||||
Client client = null;
|
||||
lock (Clients)
|
||||
{
|
||||
client = Util.GetClientByNetHandle(senderNetHandle);
|
||||
}
|
||||
|
||||
if (client != null)
|
||||
{
|
||||
if (TriggerEvents.Any(x => x.Key.EventName == packet.EventName))
|
||||
{
|
||||
EventContext ctx = new()
|
||||
{
|
||||
Client = client,
|
||||
Args = packet.Args.ToArray()
|
||||
};
|
||||
|
||||
TriggerEvents.FirstOrDefault(x => x.Key.EventName == packet.EventName).Value?.Invoke(ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning($"Player \"{client.Player.Username}\" attempted to trigger an unknown event! [{packet.EventName}]");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DisconnectAndLog(message.SenderConnection, type, e);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Logging.Error("Unhandled Data / Packet type");
|
||||
break;
|
||||
@ -1078,5 +1120,28 @@ namespace CoopServer
|
||||
RegisterCommand(attribute.Name, attribute.Usage, attribute.ArgsLength, (Action<CommandContext>)Delegate.CreateDelegate(typeof(Action<CommandContext>), method));
|
||||
}
|
||||
}
|
||||
|
||||
public static void RegisterEvent(string eventName, Action<EventContext> callback)
|
||||
{
|
||||
TriggerEvent ev = new(eventName);
|
||||
|
||||
if (TriggerEvents.ContainsKey(ev))
|
||||
{
|
||||
throw new Exception("TriggerEvent \"" + ev.EventName + "\" was already been registered!");
|
||||
}
|
||||
|
||||
TriggerEvents.Add(ev, callback);
|
||||
}
|
||||
public static void RegisterEvents<T>()
|
||||
{
|
||||
IEnumerable<MethodInfo> events = typeof(T).GetMethods().Where(method => method.GetCustomAttributes(typeof(TriggerEvent), false).Any());
|
||||
|
||||
foreach (MethodInfo method in events)
|
||||
{
|
||||
TriggerEvent attribute = method.GetCustomAttribute<TriggerEvent>(true);
|
||||
|
||||
RegisterEvent(attribute.EventName, (Action<EventContext>)Delegate.CreateDelegate(typeof(Action<EventContext>), method));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -480,10 +480,43 @@ namespace CoopServer
|
||||
{
|
||||
Server.RegisterCommands<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a class of events
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The name of your class with functions</typeparam>
|
||||
public static void RegisterEvents<T>()
|
||||
{
|
||||
Server.RegisterEvents<T>();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
|
||||
public class TriggerEvent : Attribute
|
||||
{
|
||||
public string EventName { get; set; }
|
||||
|
||||
public TriggerEvent(string eventName)
|
||||
{
|
||||
EventName = eventName;
|
||||
}
|
||||
}
|
||||
|
||||
public class EventContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the client which executed the command
|
||||
/// </summary>
|
||||
public Client Client { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Arguments (all standard but no string!)
|
||||
/// </summary>
|
||||
public object[] Args { get; internal set; }
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
|
||||
public class Command : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user