RAGECOOP-V/Server/Server.cs

1243 lines
55 KiB
C#
Raw Normal View History

2021-07-07 13:36:25 +02:00
using System;
using System.Text;
using System.Net;
2021-07-07 13:36:25 +02:00
using System.Linq;
using System.Collections.Generic;
using System.Threading;
2021-08-14 21:49:23 +02:00
using System.Reflection;
using System.IO;
2021-12-07 08:18:29 +01:00
using System.Net.Http;
2022-05-22 15:55:26 +08:00
using RageCoop.Core;
2022-04-19 05:33:18 +02:00
using Newtonsoft.Json;
2022-05-22 15:55:26 +08:00
using System.Threading.Tasks;
2021-07-07 13:36:25 +02:00
using Lidgren.Network;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Server
2021-07-07 13:36:25 +02:00
{
2022-05-22 15:55:26 +08:00
public struct IpInfo
2022-04-19 05:33:18 +02:00
{
[JsonProperty("ip")]
public string Address { get; set; }
}
2022-05-22 15:55:26 +08:00
public class Server
2021-07-07 13:36:25 +02:00
{
2022-05-22 16:56:35 +08:00
private static readonly string _compatibleVersion = "V0_1";
private static long _currentTick = 0;
2021-07-07 13:36:25 +02:00
2022-04-06 02:18:24 +02:00
public static readonly Settings MainSettings = Util.Read<Settings>("Settings.xml");
private readonly Blocklist _mainBlocklist = Util.Read<Blocklist>("Blocklist.xml");
private readonly Allowlist _mainAllowlist = Util.Read<Allowlist>("Allowlist.xml");
2021-07-07 13:36:25 +02:00
public static NetServer MainNetServer;
public static Resource RunningResource = null;
2022-04-14 12:33:19 +02:00
public static readonly Dictionary<Command, Action<CommandContext>> Commands = new();
2022-04-12 06:04:02 +02:00
public static readonly Dictionary<TriggerEvent, Action<EventContext>> TriggerEvents = new();
2021-08-26 17:01:32 +02:00
public static readonly List<Client> Clients = new();
2021-08-14 21:49:23 +02:00
2021-07-07 13:36:25 +02:00
public Server()
{
2021-09-28 16:51:16 +02:00
Logging.Info("================");
2022-04-19 03:38:48 +02:00
Logging.Info($"Server bound to: 0.0.0.0:{MainSettings.Port}");
2021-09-28 16:51:16 +02:00
Logging.Info($"Server version: {Assembly.GetCallingAssembly().GetName().Version}");
Logging.Info($"Compatible RAGECOOP versions: {_compatibleVersion.Replace('_', '.')}.x");
2021-09-28 16:51:16 +02:00
Logging.Info("================");
2022-04-06 02:18:24 +02:00
// 623c92c287cc392406e7aaaac1c0f3b0 = RAGECOOP
NetPeerConfiguration config = new("623c92c287cc392406e7aaaac1c0f3b0")
2021-07-07 13:36:25 +02:00
{
Port = MainSettings.Port,
MaximumConnections = MainSettings.MaxPlayers,
EnableUPnP = MainSettings.UPnP
2021-07-07 13:36:25 +02:00
};
config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
config.EnableMessageType(NetIncomingMessageType.ConnectionLatencyUpdated);
2021-07-07 13:36:25 +02:00
MainNetServer = new NetServer(config);
MainNetServer.Start();
Logging.Info(string.Format("Server listening on {0}:{1}", config.LocalAddress.ToString(), config.Port));
2021-07-12 01:31:11 +02:00
if (MainSettings.UPnP)
{
Logging.Info(string.Format("Attempting to forward port {0}", MainSettings.Port));
2021-07-12 01:31:11 +02:00
2022-04-06 02:18:24 +02:00
if (MainNetServer.UPnP.ForwardPort(MainSettings.Port, "RAGECOOP server"))
2021-07-12 01:31:11 +02:00
{
Logging.Info(string.Format("Server available on {0}:{1}", MainNetServer.UPnP.GetExternalIP().ToString(), config.Port));
}
else
{
Logging.Error("Port forwarding failed! Your router may not support UPnP.");
2022-04-06 02:18:24 +02:00
Logging.Warning("If you and your friends can join this server, please ignore this error or set UPnP in Settings.xml to false!");
2021-07-12 01:31:11 +02:00
}
}
2021-12-08 03:59:14 +01:00
if (MainSettings.AnnounceSelf)
2021-12-07 08:18:29 +01:00
{
2021-12-08 03:59:14 +01:00
Logging.Info("Announcing to master server...");
2022-04-19 03:38:48 +02:00
#region -- MASTERSERVER --
new Thread(async () =>
2021-12-07 08:18:29 +01:00
{
2022-04-19 03:38:48 +02:00
try
2021-12-07 08:18:29 +01:00
{
2022-04-19 03:38:48 +02:00
// TLS only
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
2021-12-07 08:18:29 +01:00
2022-04-19 03:38:48 +02:00
HttpClient httpClient = new();
2021-12-07 08:18:29 +01:00
2022-04-19 05:33:18 +02:00
IpInfo info;
try
{
HttpResponseMessage response = await httpClient.GetAsync("https://ipinfo.io/json");
2022-04-19 07:41:39 +02:00
if (response.StatusCode != HttpStatusCode.OK)
2022-04-19 05:33:18 +02:00
{
2022-04-19 07:41:39 +02:00
throw new Exception($"IPv4 request failed! [{(int)response.StatusCode}/{response.ReasonPhrase}]");
2022-04-19 05:33:18 +02:00
}
string content = await response.Content.ReadAsStringAsync();
info = JsonConvert.DeserializeObject<IpInfo>(content);
}
catch (Exception ex)
{
Logging.Error(ex.InnerException?.Message ?? ex.Message);
return;
}
2022-04-19 03:38:48 +02:00
while (!Program.ReadyToStop)
{
string msg =
"{ " +
2022-04-19 05:33:18 +02:00
"\"address\": \"" + info.Address + "\", " +
2022-04-19 03:38:48 +02:00
"\"port\": \"" + MainSettings.Port + "\", " +
"\"name\": \"" + MainSettings.Name + "\", " +
"\"version\": \"" + _compatibleVersion.Replace("_", ".") + "\", " +
"\"players\": \"" + MainNetServer.ConnectionsCount + "\", " +
"\"maxPlayers\": \"" + MainSettings.MaxPlayers + "\", " +
"\"allowlist\": \"" + _mainAllowlist.Username.Any() + "\", " +
"\"mods\": \"" + MainSettings.ModsAllowed + "\", " +
"\"npcs\": \"" + MainSettings.NpcsAllowed + "\"" +
" }";
HttpResponseMessage response = null;
2021-12-07 08:18:29 +01:00
try
{
2022-04-19 03:38:48 +02:00
response = await httpClient.PostAsync(MainSettings.MasterServer, new StringContent(msg, Encoding.UTF8, "application/json"));
2021-12-07 08:18:29 +01:00
}
2022-04-19 03:38:48 +02:00
catch (Exception ex)
2021-12-07 08:18:29 +01:00
{
2022-04-19 03:38:48 +02:00
Logging.Error($"MasterServer: {ex.Message}");
// Sleep for 5s
Thread.Sleep(5000);
continue;
2021-12-07 08:18:29 +01:00
}
2022-04-19 03:38:48 +02:00
if (response == null)
2021-12-07 08:18:29 +01:00
{
2022-04-19 03:38:48 +02:00
Logging.Error("MasterServer: Something went wrong!");
}
else if (response.StatusCode != HttpStatusCode.OK)
{
if (response.StatusCode == HttpStatusCode.BadRequest)
2022-04-14 12:18:07 +02:00
{
2022-04-19 03:38:48 +02:00
string requestContent = await response.Content.ReadAsStringAsync();
Logging.Error($"MasterServer: [{(int)response.StatusCode}], {requestContent}");
2022-04-14 12:18:07 +02:00
}
2022-04-19 03:38:48 +02:00
else
2022-04-14 12:18:07 +02:00
{
Logging.Error($"MasterServer: [{(int)response.StatusCode}]");
}
}
2022-04-19 03:38:48 +02:00
// Sleep for 10s
Thread.Sleep(10000);
2021-12-07 08:18:29 +01:00
}
2022-04-19 03:38:48 +02:00
}
catch (HttpRequestException ex)
{
Logging.Error($"MasterServer: {ex.InnerException.Message}");
}
catch (Exception ex)
{
Logging.Error($"MasterServer: {ex.Message}");
}
}).Start();
#endregion
2021-12-07 08:18:29 +01:00
}
if (!string.IsNullOrEmpty(MainSettings.Resource))
2021-08-15 07:54:25 +02:00
{
try
2021-12-17 21:32:57 +01:00
{
string resourcepath = AppDomain.CurrentDomain.BaseDirectory + "resources" + Path.DirectorySeparatorChar + MainSettings.Resource + ".dll";
Logging.Info($"Loading resource \"{MainSettings.Resource}.dll\"...");
2021-08-26 17:01:32 +02:00
Assembly asm = Assembly.LoadFrom(resourcepath);
Type[] types = asm.GetExportedTypes();
IEnumerable<Type> validTypes = types.Where(t => !t.IsInterface && !t.IsAbstract).Where(t => typeof(ServerScript).IsAssignableFrom(t));
Type[] enumerable = validTypes as Type[] ?? validTypes.ToArray();
2021-12-17 21:32:57 +01:00
if (!enumerable.Any())
{
Logging.Error("ERROR: No classes that inherit from ServerScript have been found in the assembly. Starting freeroam.");
}
else
{
if (Activator.CreateInstance(enumerable.ToArray()[0]) is ServerScript script)
2021-08-15 07:54:25 +02:00
{
RunningResource = new(script);
2021-08-15 07:54:25 +02:00
}
else
{
Logging.Warning("Could not create resource: it is null.");
2021-08-15 07:54:25 +02:00
}
2021-08-14 21:49:23 +02:00
}
}
catch (Exception e)
{
Logging.Error(e.InnerException.Message);
}
2021-08-14 21:49:23 +02:00
}
2022-04-03 18:46:57 +02:00
Logging.Info("Searching for client-side files...");
2022-04-02 23:02:49 +02:00
DownloadManager.CheckForDirectoryAndFiles();
2021-07-07 13:36:25 +02:00
Listen();
}
private void Listen()
{
Logging.Info("Listening for clients");
2021-12-10 16:25:59 +01:00
Logging.Info("Please use CTRL + C if you want to stop the server!");
2021-07-07 13:36:25 +02:00
while (!Program.ReadyToStop)
2021-07-07 13:36:25 +02:00
{
if (RunningResource != null)
{
RunningResource.InvokeTick(++_currentTick);
}
2022-04-02 23:02:49 +02:00
// Only new clients that did not receive files on connection will receive the current files in "clientside"
if (DownloadManager.AnyFileExists)
{
lock (Clients)
{
Clients.ForEach(client =>
{
if (!client.FilesSent)
{
2022-05-22 15:55:26 +08:00
DownloadManager.InsertClient(client.ClientID);
2022-04-03 02:27:30 +02:00
client.FilesSent = true;
2022-04-02 23:02:49 +02:00
}
});
}
DownloadManager.Tick();
}
2021-07-07 13:36:25 +02:00
NetIncomingMessage message;
while ((message = MainNetServer.ReadMessage()) != null)
{
switch (message.MessageType)
{
case NetIncomingMessageType.ConnectionApproval:
{
2022-05-22 15:55:26 +08:00
Logging.Info($"New incoming connection from: [{message.SenderConnection.RemoteEndPoint}]");
if (message.ReadByte() != (byte)PacketTypes.Handshake)
2021-07-07 18:05:08 +02:00
{
2022-05-22 15:55:26 +08:00
Logging.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: Wrong packet!");
message.SenderConnection.Deny("Wrong packet!");
2021-07-07 18:05:08 +02:00
}
2022-05-22 15:55:26 +08:00
else
2021-07-07 18:05:08 +02:00
{
2022-05-22 15:55:26 +08:00
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
Packets.Handshake packet = new();
packet.Unpack(data);
GetHandshake(message.SenderConnection, packet);
}
catch (Exception e)
{
Logging.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: {e.Message}");
message.SenderConnection.Deny(e.Message);
}
2021-07-07 18:05:08 +02:00
}
2022-05-22 15:55:26 +08:00
break;
2021-07-07 13:36:25 +02:00
}
case NetIncomingMessageType.StatusChanged:
{
2022-05-22 15:55:26 +08:00
NetConnectionStatus status = (NetConnectionStatus)message.ReadByte();
2022-04-03 02:27:30 +02:00
2022-05-22 15:55:26 +08:00
if (status == NetConnectionStatus.Disconnected)
{
long nethandle = message.SenderConnection.RemoteUniqueIdentifier;
2022-04-03 02:27:30 +02:00
2022-05-22 15:55:26 +08:00
DownloadManager.RemoveClient(nethandle);
SendPlayerDisconnectPacket(nethandle);
}
else if (status == NetConnectionStatus.Connected)
{
SendPlayerConnectPacket(message.SenderConnection);
}
break;
}
2021-07-07 13:36:25 +02:00
case NetIncomingMessageType.Data:
// Get packet type
2022-05-22 15:55:26 +08:00
byte btype= message.ReadByte();
var type = (PacketTypes)btype;
2021-07-07 13:36:25 +02:00
switch (type)
{
2022-05-22 15:55:26 +08:00
#region SyncData
case PacketTypes.CharacterStateSync:
2021-07-07 18:05:08 +02:00
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
2022-05-22 15:55:26 +08:00
Packets.PedStateSync packet = new();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
CharacterStateSync(packet, message.SenderConnection.RemoteUniqueIdentifier);
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
}
2021-07-07 18:05:08 +02:00
}
2021-07-07 13:36:25 +02:00
break;
2022-05-22 15:55:26 +08:00
case PacketTypes.VehicleStateSync:
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
2022-05-22 15:55:26 +08:00
Packets.VehicleStateSync packet = new();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
VehicleStateSync(packet, message.SenderConnection.RemoteUniqueIdentifier);
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
}
}
break;
2022-05-22 15:55:26 +08:00
case PacketTypes.CharacterSync:
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
2022-05-22 15:55:26 +08:00
Packets.PedSync packet = new();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
CharacterSync(packet, message.SenderConnection.RemoteUniqueIdentifier);
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
}
}
break;
2022-05-22 15:55:26 +08:00
case PacketTypes.VehicleSync:
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
2022-05-22 15:55:26 +08:00
Packets.VehicleSync packet = new();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
VehicleSync(packet,message.SenderConnection.RemoteUniqueIdentifier);
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
}
}
break;
2022-05-22 15:55:26 +08:00
#endregion
case PacketTypes.ChatMessage:
2021-07-07 13:36:25 +02:00
{
2021-07-07 18:05:08 +02:00
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
2022-01-01 03:07:18 +01:00
Packets.ChatMessage packet = new();
2022-05-22 15:55:26 +08:00
packet.Unpack(data);
SendChatMessage(packet);
2021-07-07 18:05:08 +02:00
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
2021-07-07 18:05:08 +02:00
}
2021-07-07 13:36:25 +02:00
}
break;
2022-05-22 15:55:26 +08:00
case PacketTypes.NativeResponse:
2021-12-10 13:38:03 +01:00
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
2022-01-01 03:07:18 +01:00
Packets.NativeResponse packet = new();
2022-05-22 15:55:26 +08:00
packet.Unpack(data);
2021-12-10 13:38:03 +01:00
2022-05-22 15:55:26 +08:00
Client client = Clients.Find(x => x.ClientID == message.SenderConnection.RemoteUniqueIdentifier);
2021-12-10 15:55:45 +01:00
if (client != null)
2021-12-10 13:38:03 +01:00
{
if (client.Callbacks.ContainsKey(packet.ID))
2021-12-10 13:38:03 +01:00
{
client.Callbacks[packet.ID].Invoke(packet.Args[0]);
client.Callbacks.Remove(packet.ID);
2021-12-10 13:38:03 +01:00
}
}
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
2021-12-10 13:38:03 +01:00
}
}
break;
2022-05-22 15:55:26 +08:00
case PacketTypes.Mod:
2021-09-29 14:34:22 +02:00
{
if (MainSettings.ModsAllowed)
2021-09-29 14:34:22 +02:00
{
try
2021-09-29 14:34:22 +02:00
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
2022-01-01 03:07:18 +01:00
Packets.Mod packet = new Packets.Mod();
2022-05-22 15:55:26 +08:00
packet.Unpack(data);
bool resourceResult = false;
if (RunningResource != null)
2021-12-17 22:16:39 +01:00
{
2022-01-01 03:07:18 +01:00
if (RunningResource.InvokeModPacketReceived(packet.NetHandle, packet.Target, packet.Name, packet.CustomPacketID, packet.Bytes))
2021-12-17 22:16:39 +01:00
{
resourceResult = true;
}
}
if (!resourceResult && packet.Target != -1)
2021-09-30 23:35:42 +02:00
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
2022-05-22 15:55:26 +08:00
packet.Pack(outgoingMessage);
if (packet.Target != 0)
{
NetConnection target = MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == packet.Target);
if (target == null)
{
Logging.Error($"[ModPacket] target \"{packet.Target}\" not found!");
}
else
{
// Send back to target
MainNetServer.SendMessage(outgoingMessage, target, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Mod);
}
}
else
{
// Send back to all players
MainNetServer.SendMessage(outgoingMessage, MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Mod);
}
2021-09-30 23:35:42 +02:00
}
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
2021-09-30 23:35:42 +02:00
}
2021-09-29 14:34:22 +02:00
}
else
2021-09-29 14:34:22 +02:00
{
message.SenderConnection.Disconnect("Mods are not allowed!");
2021-09-29 14:34:22 +02:00
}
}
break;
2022-05-22 15:55:26 +08:00
case PacketTypes.FileTransferComplete:
2022-04-03 02:27:30 +02:00
{
try
{
if (DownloadManager.AnyFileExists)
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
Packets.FileTransferComplete packet = new();
2022-05-22 15:55:26 +08:00
packet.Unpack(data);
2022-04-03 02:27:30 +02:00
2022-05-22 15:55:26 +08:00
Client client = Clients.Find(x => x.ClientID == message.SenderConnection.RemoteUniqueIdentifier);
2022-04-03 02:27:30 +02:00
if (client != null && !client.FilesReceived)
{
2022-05-22 15:55:26 +08:00
DownloadManager.TryToRemoveClient(client.ClientID, packet.ID);
2022-04-03 02:27:30 +02:00
}
}
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
}
}
break;
2022-05-22 15:55:26 +08:00
case PacketTypes.ServerClientEvent:
2022-04-12 06:04:02 +02:00
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
Packets.ServerClientEvent packet = new Packets.ServerClientEvent();
2022-05-22 15:55:26 +08:00
packet.Unpack(data);
2022-04-12 06:04:02 +02:00
long senderNetHandle = message.SenderConnection.RemoteUniqueIdentifier;
Client client = null;
lock (Clients)
{
2022-05-22 15:55:26 +08:00
client = Util.GetClientByID(senderNetHandle);
2022-04-12 06:04:02 +02:00
}
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;
2021-07-07 13:36:25 +02:00
default:
2022-05-22 15:55:26 +08:00
if (type.IsSyncEvent())
{
// Sync Events
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
outgoingMessage.Write(btype);
outgoingMessage.Write(len);
outgoingMessage.Write(data);
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != message.SenderConnection.RemoteUniqueIdentifier).ForEach(x =>
{
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.CharacterSync);
});
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
}
}
else
{
Logging.Error("Unhandled Data / Packet type");
}
2021-07-07 13:36:25 +02:00
break;
}
break;
case NetIncomingMessageType.ConnectionLatencyUpdated:
2021-08-17 15:04:50 +02:00
{
2022-05-22 15:55:26 +08:00
Client client = Clients.Find(x => x.ClientID == message.SenderConnection.RemoteUniqueIdentifier);
2021-12-10 15:55:45 +01:00
if (client != null)
2021-12-10 13:38:03 +01:00
{
client.Latency = message.ReadFloat();
}
2021-08-17 15:04:50 +02:00
}
break;
2021-07-07 13:36:25 +02:00
case NetIncomingMessageType.ErrorMessage:
Logging.Error(message.ReadString());
break;
case NetIncomingMessageType.WarningMessage:
Logging.Warning(message.ReadString());
break;
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.VerboseDebugMessage:
Logging.Debug(message.ReadString());
break;
default:
Logging.Error(string.Format("Unhandled type: {0} {1} bytes {2} | {3}", message.MessageType, message.LengthBytes, message.DeliveryMethod, message.SequenceChannel));
break;
}
MainNetServer.Recycle(message);
}
2021-11-23 23:02:41 +01:00
// 16 milliseconds to sleep to reduce CPU usage
Thread.Sleep(1000 / 60);
2021-07-07 13:36:25 +02:00
}
Logging.Warning("Server is shutting down!");
if (RunningResource != null)
{
// Waiting for resource...
while (!RunningResource.ReadyToStop)
{
// 16 milliseconds to sleep to reduce CPU usage
Thread.Sleep(1000 / 60);
}
}
if (MainNetServer.Connections.Count > 0)
{
MainNetServer.Connections.ForEach(x => x.Disconnect("Server is shutting down!"));
// We have to wait some time for all Disconnect() messages to be sent successfully
// Sleep for 1 second
Thread.Sleep(1000);
}
2021-07-07 13:36:25 +02:00
}
2022-05-22 15:55:26 +08:00
private void DisconnectAndLog(NetConnection senderConnection,PacketTypes type, Exception e)
{
Logging.Error($"Error receiving a packet of type {type}");
Logging.Error(e.Message);
Logging.Error(e.StackTrace);
senderConnection.Disconnect(e.Message);
}
#region -- PLAYER --
2021-07-07 13:36:25 +02:00
// Before we approve the connection, we must shake hands
2022-01-01 03:07:18 +01:00
private void GetHandshake(NetConnection local, Packets.Handshake packet)
2021-07-07 13:36:25 +02:00
{
Logging.Debug("New handshake from: [Name: " + packet.Username + " | Address: " + local.RemoteEndPoint.Address.ToString() + "]");
2021-07-07 13:36:25 +02:00
if (!packet.ModVersion.StartsWith(_compatibleVersion))
2021-12-18 00:21:23 +01:00
{
local.Deny($"RAGECOOP version {_compatibleVersion.Replace('_', '.')}.x required!");
2021-12-18 00:21:23 +01:00
return;
}
2021-07-07 13:36:25 +02:00
if (string.IsNullOrWhiteSpace(packet.Username))
{
local.Deny("Username is empty or contains spaces!");
return;
}
if (packet.Username.Any(p => !char.IsLetterOrDigit(p) && !(p == '_') && !(p=='-')))
2021-07-07 13:36:25 +02:00
{
local.Deny("Username contains special chars!");
return;
}
if (_mainAllowlist.Username.Any() && !_mainAllowlist.Username.Contains(packet.Username.ToLower()))
2021-07-07 13:36:25 +02:00
{
local.Deny("This Username is not on the allow list!");
return;
2021-07-07 13:36:25 +02:00
}
if (_mainBlocklist.Username.Contains(packet.Username.ToLower()))
2021-07-07 13:36:25 +02:00
{
local.Deny("This Username has been blocked by this server!");
return;
}
if (_mainBlocklist.IP.Contains(local.RemoteEndPoint.ToString().Split(":")[0]))
2021-07-07 13:36:25 +02:00
{
local.Deny("This IP was blocked by this server!");
return;
}
if (Clients.Any(x => x.Player.Username.ToLower() == packet.Username.ToLower()))
2021-08-21 16:52:17 +02:00
{
local.Deny("Username is already taken!");
return;
2021-07-07 13:36:25 +02:00
}
2022-05-22 15:55:26 +08:00
2021-07-09 00:20:09 +02:00
Client tmpClient;
2021-07-07 13:36:25 +02:00
// Add the player to Players
lock (Clients)
{
Clients.Add(
tmpClient = new Client()
2021-08-26 17:01:32 +02:00
{
2022-05-22 15:55:26 +08:00
ClientID = local.RemoteUniqueIdentifier,
Player = new()
{
2022-05-22 15:55:26 +08:00
Username = packet.Username,
PedID=packet.PedID
}
2021-08-26 17:01:32 +02:00
}
2022-05-22 15:55:26 +08:00
);;
}
2022-05-22 15:55:26 +08:00
Logging.Info($"HandShake sucess, Player:{packet.Username} PedID:{packet.PedID}");
2021-07-07 13:36:25 +02:00
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
// Create a new handshake packet
2022-01-01 03:07:18 +01:00
new Packets.Handshake()
2021-07-07 13:36:25 +02:00
{
2022-05-22 15:55:26 +08:00
PedID = packet.PedID,
2021-07-07 13:36:25 +02:00
Username = string.Empty,
ModVersion = string.Empty,
NPCsAllowed = MainSettings.NpcsAllowed
2022-05-22 15:55:26 +08:00
}.Pack(outgoingMessage);
2021-07-07 13:36:25 +02:00
// Accept the connection and send back a new handshake packet with the connection ID
local.Approve(outgoingMessage);
if (RunningResource != null)
{
RunningResource.InvokePlayerHandshake(tmpClient);
}
2021-07-07 13:36:25 +02:00
}
// The connection has been approved, now we need to send all other players to the new player and the new player to all players
private static void SendPlayerConnectPacket(NetConnection local)
2021-07-07 13:36:25 +02:00
{
2022-05-22 15:55:26 +08:00
Client localClient = Clients.Find(x => x.ClientID == local.RemoteUniqueIdentifier);
2021-12-10 15:55:45 +01:00
if (localClient == null)
2021-07-07 13:36:25 +02:00
{
local.Disconnect("No data found!");
return;
2021-07-07 13:36:25 +02:00
}
2021-08-26 17:01:32 +02:00
List<NetConnection> clients;
if ((clients = Util.FilterAllLocal(local)).Count > 0)
2021-07-07 13:36:25 +02:00
{
// Send all players to local
clients.ForEach(targetPlayer =>
{
long targetNetHandle = targetPlayer.RemoteUniqueIdentifier;
2021-07-07 13:36:25 +02:00
2022-05-22 15:55:26 +08:00
Client targetClient = Clients.Find(x => x.ClientID == targetNetHandle);
2021-12-10 20:23:58 +01:00
if (targetClient != null)
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
2022-01-01 03:07:18 +01:00
new Packets.PlayerConnect()
{
2022-05-22 15:55:26 +08:00
// NetHandle = targetNetHandle,
Username = targetClient.Player.Username,
PedID=targetClient.Player.PedID,
}.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, local, NetDeliveryMethod.ReliableOrdered, 0);
}
});
2021-07-07 13:36:25 +02:00
// Send local to all players
2021-07-07 13:36:25 +02:00
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
2022-01-01 03:07:18 +01:00
new Packets.PlayerConnect()
2021-07-07 13:36:25 +02:00
{
2022-05-22 15:55:26 +08:00
// NetHandle = local.RemoteUniqueIdentifier,
PedID=localClient.Player.PedID,
Username = localClient.Player.Username
2022-05-22 15:55:26 +08:00
}.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.ReliableOrdered, 0);
}
2021-08-26 17:01:32 +02:00
if (RunningResource != null)
2021-07-07 13:36:25 +02:00
{
RunningResource.InvokePlayerConnected(localClient);
}
else
{
Logging.Info($"Player {localClient.Player.Username} connected!");
}
if (!string.IsNullOrEmpty(MainSettings.WelcomeMessage))
{
2022-01-01 03:07:18 +01:00
SendChatMessage(new Packets.ChatMessage() { Username = "Server", Message = MainSettings.WelcomeMessage }, new List<NetConnection>() { local });
}
2021-07-07 13:36:25 +02:00
}
// Send all players a message that someone has left the server
private static void SendPlayerDisconnectPacket(long nethandle)
2021-07-07 13:36:25 +02:00
{
List<NetConnection> clients = MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != nethandle);
2022-05-22 15:55:26 +08:00
int playerPedID = Clients.Where(x => x.ClientID==nethandle).First().Player.PedID;
if (clients.Count > 0)
2021-07-07 13:36:25 +02:00
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
2022-01-01 03:07:18 +01:00
new Packets.PlayerDisconnect()
{
2022-05-22 15:55:26 +08:00
PedID=playerPedID,
}.Pack(outgoingMessage);
2021-08-26 17:01:32 +02:00
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.ReliableOrdered, 0);
2021-07-07 13:36:25 +02:00
}
2022-05-22 15:55:26 +08:00
Client localClient = Clients.FirstOrDefault(x => x.ClientID == nethandle);
2021-12-10 15:55:45 +01:00
if (localClient == null)
{
return;
}
Clients.Remove(localClient);
if (RunningResource != null)
{
RunningResource.InvokePlayerDisconnected(localClient);
}
else
{
2022-05-22 15:55:26 +08:00
Logging.Info($"Player {localClient.Player.Username} disconnected! ID:{playerPedID}");
}
}
#region SyncEntities
private static void CharacterStateSync(Packets.PedStateSync packet,long ClientID)
{
Client client = Util.GetClientByID(ClientID);
if (client == null)
{
return;
}
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != ClientID).ForEach(x =>
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.CharacterSync);
});
if (RunningResource != null && packet.ID==client.Player.PedID)
{
RunningResource.InvokePlayerUpdate(client);
}
}
private static void VehicleStateSync(Packets.VehicleStateSync packet, long ClientID)
{
Client client = Util.GetClientByID(ClientID);
if (client == null)
{
return;
}
// Save the new data
if (packet.Passengers.ContainsValue(client.Player.PedID))
{
client.Player.VehicleID = packet.ID;
}
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != ClientID).ForEach(x =>
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.VehicleSync);
});
}
private static void CharacterSync(Packets.PedSync packet, long ClientID)
{
Client client = Util.GetClientByID(ClientID);
if (client == null)
{
return;
}
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != ClientID).ForEach(x =>
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.CharacterSync);
});
if (RunningResource != null && packet.ID==client.Player.PedID)
{
RunningResource.InvokePlayerUpdate(client);
}
2021-07-07 13:36:25 +02:00
}
2022-05-22 15:55:26 +08:00
private static void VehicleSync(Packets.VehicleSync packet, long ClientID)
{
Client client = Util.GetClientByID(ClientID);
if (client == null)
{
return;
}
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != ClientID).ForEach(x =>
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.VehicleSync);
});
/*
Client client = Util.GetClientByID(ClientID);
if (client == null)
{
return;
}
// Save the new data
client.Player.PedHandle = packet.PedHandle;
client.Player.VehicleHandle = packet.VehicleHandle;
client.Player.IsInVehicle = true;
client.Player.Position = packet.Position;
client.Player.Health = packet.Health;
// Override the latency
packet.Latency = client.Latency;
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != ClientID).ForEach(x =>
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerFull);
});
if (RunningResource != null)
{
RunningResource.InvokePlayerUpdate(client);
}
*/
}
#region Obsolete
/*
2022-01-01 03:07:18 +01:00
private static void FullSyncPlayer(Packets.FullSyncPlayer packet)
2021-07-07 13:36:25 +02:00
{
Client client = Util.GetClientByNetHandle(packet.NetHandle);
2021-12-11 12:35:17 +01:00
if (client == null)
{
return;
}
// Save the new data
client.Player.PedHandle = packet.PedHandle;
client.Player.IsInVehicle = false;
client.Player.Position = packet.Position;
client.Player.Health = packet.Health;
2021-07-07 13:36:25 +02:00
// Override the latency
packet.Latency = client.Latency;
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != packet.NetHandle).ForEach(x =>
2021-08-26 17:01:32 +02:00
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
if (Clients.First(y => y.NetHandle == x.RemoteUniqueIdentifier).Player.IsInRangeOf(packet.Position, 550f))
2021-08-26 17:01:32 +02:00
{
packet.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerFull);
2021-08-26 17:01:32 +02:00
}
else
{
2022-01-01 03:07:18 +01:00
new Packets.SuperLightSync()
2021-08-26 17:01:32 +02:00
{
NetHandle = packet.NetHandle,
Position = packet.Position,
Latency = packet.Latency
2021-08-26 17:01:32 +02:00
}.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerSuperLight);
2021-08-26 17:01:32 +02:00
}
});
if (RunningResource != null)
{
RunningResource.InvokePlayerUpdate(client);
}
2021-07-07 13:36:25 +02:00
}
2022-01-01 03:07:18 +01:00
private static void FullSyncPlayerVeh(Packets.FullSyncPlayerVeh packet)
2021-07-07 13:36:25 +02:00
{
Client client = Util.GetClientByNetHandle(packet.NetHandle);
2021-12-11 12:35:17 +01:00
if (client == null)
{
return;
}
// Save the new data
client.Player.PedHandle = packet.PedHandle;
client.Player.VehicleHandle = packet.VehicleHandle;
client.Player.IsInVehicle = true;
client.Player.Position = packet.Position;
client.Player.Health = packet.Health;
// Override the latency
packet.Latency = client.Latency;
2021-07-07 13:36:25 +02:00
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != packet.NetHandle).ForEach(x =>
2021-08-26 17:01:32 +02:00
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
if (Clients.First(y => y.NetHandle == x.RemoteUniqueIdentifier).Player.IsInRangeOf(packet.Position, 550f))
2021-08-26 17:01:32 +02:00
{
packet.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerFull);
2021-08-26 17:01:32 +02:00
}
else
{
2022-01-01 03:07:18 +01:00
new Packets.SuperLightSync()
2021-08-26 17:01:32 +02:00
{
NetHandle = packet.NetHandle,
Position = packet.Position,
Latency = packet.Latency
2021-08-26 17:01:32 +02:00
}.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerSuperLight);
2021-08-26 17:01:32 +02:00
}
});
if (RunningResource != null)
{
RunningResource.InvokePlayerUpdate(client);
}
2021-07-07 13:36:25 +02:00
}
2022-01-01 03:07:18 +01:00
private static void LightSyncPlayer(Packets.LightSyncPlayer packet)
2021-07-07 13:36:25 +02:00
{
Client client = Util.GetClientByNetHandle(packet.NetHandle);
2021-12-11 12:35:17 +01:00
if (client == null)
{
return;
}
// Save the new data
client.Player.Position = packet.Position;
client.Player.Health = packet.Health;
// Override the latency
packet.Latency = client.Latency;
2021-07-07 13:36:25 +02:00
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != packet.NetHandle).ForEach(x =>
2021-08-26 17:01:32 +02:00
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
if (Clients.First(y => y.NetHandle == x.RemoteUniqueIdentifier).Player.IsInRangeOf(packet.Position, 550f))
2021-08-26 17:01:32 +02:00
{
packet.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerLight);
2021-08-26 17:01:32 +02:00
}
else
{
2022-01-01 03:07:18 +01:00
new Packets.SuperLightSync()
2021-08-26 17:01:32 +02:00
{
NetHandle = packet.NetHandle,
Position = packet.Position,
Latency = packet.Latency
2021-08-26 17:01:32 +02:00
}.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerSuperLight);
2021-08-26 17:01:32 +02:00
}
});
if (RunningResource != null)
{
RunningResource.InvokePlayerUpdate(client);
}
2021-07-07 13:36:25 +02:00
}
2022-01-01 03:07:18 +01:00
private static void LightSyncPlayerVeh(Packets.LightSyncPlayerVeh packet)
{
Client client = Util.GetClientByNetHandle(packet.NetHandle);
2021-12-11 12:35:17 +01:00
if (client == null)
{
return;
}
// Save the new data
client.Player.Position = packet.Position;
client.Player.Health = packet.Health;
// Override the latency
packet.Latency = client.Latency;
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != packet.NetHandle).ForEach(x =>
2021-08-26 17:01:32 +02:00
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
if (Clients.First(y => y.NetHandle == x.RemoteUniqueIdentifier).Player.IsInRangeOf(packet.Position, 550f))
2021-08-26 17:01:32 +02:00
{
packet.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerLight);
2021-08-26 17:01:32 +02:00
}
else
{
2022-01-01 03:07:18 +01:00
new Packets.SuperLightSync()
2021-08-26 17:01:32 +02:00
{
NetHandle = packet.NetHandle,
Position = packet.Position,
Latency = packet.Latency
2021-08-26 17:01:32 +02:00
}.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerSuperLight);
2021-08-26 17:01:32 +02:00
}
});
if (RunningResource != null)
{
RunningResource.InvokePlayerUpdate(client);
}
}
2022-05-22 15:55:26 +08:00
*/
#endregion
#endregion
2021-07-07 13:36:25 +02:00
// Send a message to targets or all players
2022-01-01 03:07:18 +01:00
private static void SendChatMessage(Packets.ChatMessage packet, List<NetConnection> targets = null)
2021-07-07 13:36:25 +02:00
{
if (RunningResource != null)
2021-08-15 07:54:25 +02:00
{
if (packet.Message.StartsWith('/'))
2021-08-18 11:47:59 +02:00
{
string[] cmdArgs = packet.Message.Split(" ");
string cmdName = cmdArgs[0].Remove(0, 1);
if (Commands.Any(x => x.Key.Name == cmdName))
{
string[] argsWithoutCmd = cmdArgs.Skip(1).ToArray();
2021-08-18 11:47:59 +02:00
CommandContext ctx = new()
{
2021-12-11 12:35:17 +01:00
Client = Clients.Find(x => x.Player.Username == packet.Username),
Args = argsWithoutCmd
2021-08-18 11:47:59 +02:00
};
KeyValuePair<Command, Action<CommandContext>> command = Commands.First(x => x.Key.Name == cmdName);
if (command.Key.Usage != null && command.Key.ArgsLength != argsWithoutCmd.Length)
{
NetConnection userConnection = Util.GetConnectionByUsername(packet.Username);
if (userConnection == default)
{
return;
}
SendChatMessage("Server", command.Key.Usage, userConnection);
return;
}
2021-08-18 11:47:59 +02:00
command.Value.Invoke(ctx);
}
else
{
2021-08-18 12:47:36 +02:00
NetConnection userConnection = Util.GetConnectionByUsername(packet.Username);
2021-08-21 16:52:17 +02:00
if (userConnection == default)
2021-08-18 12:47:36 +02:00
{
return;
}
2021-08-18 11:47:59 +02:00
SendChatMessage("Server", "Command not found!", userConnection);
2021-08-18 11:47:59 +02:00
}
return;
}
2021-12-07 08:18:29 +01:00
if (RunningResource.InvokeChatMessage(packet.Username, packet.Message))
2021-08-18 11:47:59 +02:00
{
return;
2021-08-18 11:47:59 +02:00
}
2021-08-15 07:54:25 +02:00
}
packet.Message = packet.Message.Replace("~", "");
2021-07-07 13:36:25 +02:00
SendChatMessage(packet.Username, packet.Message, targets);
2021-08-18 11:47:59 +02:00
Logging.Info(packet.Username + ": " + packet.Message);
2021-07-07 13:36:25 +02:00
}
2022-05-22 15:55:26 +08:00
public static void SendChatMessage(string username, string message, List<NetConnection> targets = null)
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
2022-05-22 15:55:26 +08:00
new Packets.ChatMessage() { Username = username, Message = message }.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, targets ?? MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Chat);
}
2022-05-22 15:55:26 +08:00
public static void SendChatMessage(string username, string message, NetConnection target)
{
SendChatMessage(username, message, new List<NetConnection>() { target });
}
#endregion
2021-08-18 11:47:59 +02:00
public static void RegisterCommand(string name, string usage, short argsLength, Action<CommandContext> callback)
{
Command command = new(name) { Usage = usage, ArgsLength = argsLength };
if (Commands.ContainsKey(command))
{
throw new Exception("Command \"" + command.Name + "\" was already been registered!");
}
Commands.Add(command, callback);
}
2021-08-18 11:47:59 +02:00
public static void RegisterCommand(string name, Action<CommandContext> callback)
{
Command command = new(name);
2021-08-18 11:47:59 +02:00
if (Commands.ContainsKey(command))
{
throw new Exception("Command \"" + command.Name + "\" was already been registered!");
}
Commands.Add(command, callback);
}
public static void RegisterCommands<T>()
{
IEnumerable<MethodInfo> commands = typeof(T).GetMethods().Where(method => method.GetCustomAttributes(typeof(Command), false).Any());
2021-08-18 11:47:59 +02:00
foreach (MethodInfo method in commands)
{
Command attribute = method.GetCustomAttribute<Command>(true);
2021-08-18 11:47:59 +02:00
RegisterCommand(attribute.Name, attribute.Usage, attribute.ArgsLength, (Action<CommandContext>)Delegate.CreateDelegate(typeof(Action<CommandContext>), method));
2021-08-18 11:47:59 +02:00
}
}
2022-04-12 06:04:02 +02:00
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));
}
}
2021-07-07 13:36:25 +02:00
}
}