Now we can trigger events!
This commit is contained in:
@ -189,7 +189,7 @@ namespace CoopClient
|
|||||||
OnChatMessage?.Invoke(from, message);
|
OnChatMessage?.Invoke(from, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void InvokeServerEvent(string eventName, params object[] args)
|
public void InvokeServerEvent(string eventName, object[] args)
|
||||||
{
|
{
|
||||||
_serverEvents.FirstOrDefault(x => x.Key == eventName).Value?.Invoke(args);
|
_serverEvents.FirstOrDefault(x => x.Key == eventName).Value?.Invoke(args);
|
||||||
}
|
}
|
||||||
@ -464,9 +464,9 @@ namespace CoopClient
|
|||||||
AddServerEvent(eventName, arg => action(arg));
|
AddServerEvent(eventName, arg => action(arg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void TriggerServerEvent(string eventName, params object[] args)
|
public void SendTriggerEvent(string eventName, params object[] args)
|
||||||
{
|
{
|
||||||
// TODO
|
Main.MainNetworking.SendTriggerEvent(eventName, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1242,6 +1242,20 @@ namespace CoopClient
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SendTriggerEvent(string eventName, params object[] args)
|
||||||
|
{
|
||||||
|
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
|
||||||
|
|
||||||
|
new Packets.ServerClientEvent()
|
||||||
|
{
|
||||||
|
EventName = eventName,
|
||||||
|
Args = new List<object>(args)
|
||||||
|
}.PacketToNetOutGoingMessage(outgoingMessage);
|
||||||
|
|
||||||
|
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableUnordered, (byte)ConnectionChannel.Event);
|
||||||
|
Client.FlushSendQueue();
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,7 +227,7 @@ namespace CoopServer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void TriggerServerEvent(string eventName, params object[] args)
|
public void SendTriggerEvent(string eventName, params object[] args)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -350,6 +350,7 @@ namespace CoopServer
|
|||||||
EventName = reader.ReadString(eventNameBytes);
|
EventName = reader.ReadString(eventNameBytes);
|
||||||
|
|
||||||
// Read args
|
// Read args
|
||||||
|
Args = new List<object>();
|
||||||
int argsCount = reader.ReadInt();
|
int argsCount = reader.ReadInt();
|
||||||
for (int i = 0; i < argsCount; i++)
|
for (int i = 0; i < argsCount; i++)
|
||||||
{
|
{
|
||||||
|
@ -33,6 +33,7 @@ namespace CoopServer
|
|||||||
|
|
||||||
public static Resource RunningResource = null;
|
public static Resource RunningResource = null;
|
||||||
public static Dictionary<Command, Action<CommandContext>> Commands;
|
public static Dictionary<Command, Action<CommandContext>> Commands;
|
||||||
|
public static readonly Dictionary<TriggerEvent, Action<EventContext>> TriggerEvents = new();
|
||||||
|
|
||||||
public static readonly List<Client> Clients = new();
|
public static readonly List<Client> Clients = new();
|
||||||
|
|
||||||
@ -539,6 +540,47 @@ namespace CoopServer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
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:
|
default:
|
||||||
Logging.Error("Unhandled Data / Packet type");
|
Logging.Error("Unhandled Data / Packet type");
|
||||||
break;
|
break;
|
||||||
@ -1078,5 +1120,28 @@ namespace CoopServer
|
|||||||
RegisterCommand(attribute.Name, attribute.Usage, attribute.ArgsLength, (Action<CommandContext>)Delegate.CreateDelegate(typeof(Action<CommandContext>), method));
|
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>();
|
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
|
#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
|
public class Command : Attribute
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Reference in New Issue
Block a user