2021-07-07 13:36:25 +02:00
|
|
|
|
using System;
|
2021-12-17 19:50:47 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2021-07-07 13:36:25 +02:00
|
|
|
|
using Lidgren.Network;
|
|
|
|
|
|
|
|
|
|
namespace CoopServer
|
|
|
|
|
{
|
2021-12-03 20:41:44 +01:00
|
|
|
|
internal class Server
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2022-04-10 14:34:55 +02:00
|
|
|
|
private static readonly string _compatibleVersion = "V1_4";
|
|
|
|
|
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");
|
2022-04-10 14:34:55 +02:00
|
|
|
|
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;
|
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
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}");
|
2022-04-10 14:34:55 +02:00
|
|
|
|
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
|
|
|
|
{
|
2021-12-11 14:32:06 +01:00
|
|
|
|
Port = MainSettings.Port,
|
2022-04-12 01:55:33 +02:00
|
|
|
|
MaximumConnections = MainSettings.MaxPlayers,
|
2021-12-11 15:23:29 +01:00
|
|
|
|
EnableUPnP = MainSettings.UPnP
|
2021-07-07 13:36:25 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
|
2021-08-16 12:31:49 +02:00
|
|
|
|
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)
|
|
|
|
|
{
|
2021-12-11 14:32:06 +01:00
|
|
|
|
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
|
|
|
|
|
{
|
2021-12-17 19:50:47 +01:00
|
|
|
|
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 03:38:48 +02:00
|
|
|
|
while (!Program.ReadyToStop)
|
|
|
|
|
{
|
|
|
|
|
string msg =
|
|
|
|
|
"{ " +
|
|
|
|
|
"\"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
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(MainSettings.Resource))
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
try
|
2021-12-17 21:32:57 +01:00
|
|
|
|
{
|
2021-12-28 13:51:36 +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
|
|
|
|
|
2021-12-28 13:51:36 +01: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
|
|
|
|
|
2021-12-28 13:51:36 +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
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
RunningResource = new(script);
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
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
|
|
|
|
}
|
2021-12-28 13:51:36 +01: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
|
|
|
|
|
2021-12-08 13:45:00 +01:00
|
|
|
|
while (!Program.ReadyToStop)
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (RunningResource != null)
|
2021-12-26 03:25:00 +01:00
|
|
|
|
{
|
2022-04-10 14:34:55 +02:00
|
|
|
|
RunningResource.InvokeTick(++_currentTick);
|
2021-12-26 03:25:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
DownloadManager.InsertClient(client.NetHandle);
|
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:
|
2021-12-08 09:57:19 +01:00
|
|
|
|
Logging.Info($"New incoming connection from: [{message.SenderConnection.RemoteEndPoint}]");
|
2022-01-01 03:07:18 +01:00
|
|
|
|
if (message.ReadByte() != (byte)PacketTypes.Handshake)
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-12-08 09:57:19 +01:00
|
|
|
|
Logging.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: Wrong packet!");
|
2021-07-07 13:36:25 +02:00
|
|
|
|
message.SenderConnection.Deny("Wrong packet!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-07-07 18:05:08 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.Handshake packet = new();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
|
|
|
|
|
|
|
|
|
GetHandshake(message.SenderConnection, packet);
|
2021-07-07 18:05:08 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2021-12-08 09:57:19 +01:00
|
|
|
|
Logging.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: {e.Message}");
|
2021-07-07 18:05:08 +02:00
|
|
|
|
message.SenderConnection.Deny(e.Message);
|
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NetIncomingMessageType.StatusChanged:
|
|
|
|
|
NetConnectionStatus status = (NetConnectionStatus)message.ReadByte();
|
|
|
|
|
|
2021-12-08 09:57:19 +01:00
|
|
|
|
if (status == NetConnectionStatus.Disconnected)
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2022-04-03 02:27:30 +02:00
|
|
|
|
long nethandle = message.SenderConnection.RemoteUniqueIdentifier;
|
|
|
|
|
|
2022-04-06 06:52:02 +02:00
|
|
|
|
DownloadManager.RemoveClient(nethandle);
|
2022-04-03 02:27:30 +02:00
|
|
|
|
|
|
|
|
|
SendPlayerDisconnectPacket(nethandle);
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
2021-12-17 23:02:53 +01:00
|
|
|
|
else if (status == NetConnectionStatus.Connected)
|
|
|
|
|
{
|
|
|
|
|
SendPlayerConnectPacket(message.SenderConnection);
|
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
break;
|
|
|
|
|
case NetIncomingMessageType.Data:
|
|
|
|
|
// Get packet type
|
|
|
|
|
byte type = message.ReadByte();
|
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
2022-01-01 03:07:18 +01:00
|
|
|
|
case (byte)PacketTypes.FullSyncPlayer:
|
2021-07-07 18:05:08 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.FullSyncPlayer packet = new();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
|
|
|
|
|
|
|
|
|
FullSyncPlayer(packet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-02-06 09:59:18 -07:00
|
|
|
|
DisconnectAndLog(message.SenderConnection, type, e);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
}
|
2021-07-07 18:05:08 +02:00
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
break;
|
2022-01-01 03:07:18 +01:00
|
|
|
|
case (byte)PacketTypes.FullSyncPlayerVeh:
|
2021-07-12 05:00:48 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.FullSyncPlayerVeh packet = new();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
|
|
|
|
|
|
|
|
|
FullSyncPlayerVeh(packet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-02-06 09:59:18 -07:00
|
|
|
|
DisconnectAndLog(message.SenderConnection, type, e);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
}
|
2021-07-12 05:00:48 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2022-01-01 03:07:18 +01:00
|
|
|
|
case (byte)PacketTypes.LightSyncPlayer:
|
2021-07-12 04:33:23 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.LightSyncPlayer packet = new();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
|
|
|
|
|
|
|
|
|
LightSyncPlayer(packet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-02-06 09:59:18 -07:00
|
|
|
|
DisconnectAndLog(message.SenderConnection, type, e);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
}
|
2021-07-12 04:33:23 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2022-01-01 03:07:18 +01:00
|
|
|
|
case (byte)PacketTypes.LightSyncPlayerVeh:
|
2021-07-12 05:00:48 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.LightSyncPlayerVeh packet = new();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
|
|
|
|
|
|
|
|
|
LightSyncPlayerVeh(packet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-02-06 09:59:18 -07:00
|
|
|
|
DisconnectAndLog(message.SenderConnection, type, e);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
}
|
2021-07-12 05:00:48 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2022-01-01 03:07:18 +01:00
|
|
|
|
case (byte)PacketTypes.ChatMessage:
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-07-07 18:05:08 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.ChatMessage packet = new();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
|
|
|
|
|
|
|
|
|
SendChatMessage(packet);
|
2021-07-07 18:05:08 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-02-06 09:59:18 -07:00
|
|
|
|
DisconnectAndLog(message.SenderConnection, type, e);
|
2021-07-07 18:05:08 +02:00
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
break;
|
2022-01-01 03:07:18 +01:00
|
|
|
|
case (byte)PacketTypes.FullSyncNpc:
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
if (MainSettings.NpcsAllowed)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.FullSyncNpc packet = new();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
|
|
|
|
|
|
|
|
|
FullSyncNpc(message.SenderConnection, packet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-02-06 09:59:18 -07:00
|
|
|
|
DisconnectAndLog(message.SenderConnection, type, e);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message.SenderConnection.Disconnect("Npcs are not allowed!");
|
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2022-01-01 03:07:18 +01:00
|
|
|
|
case (byte)PacketTypes.FullSyncNpcVeh:
|
2021-07-10 09:41:17 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
if (MainSettings.NpcsAllowed)
|
2021-07-10 09:41:17 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.FullSyncNpcVeh packet = new();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
|
|
|
|
|
|
|
|
|
FullSyncNpcVeh(message.SenderConnection, packet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-02-06 09:59:18 -07:00
|
|
|
|
DisconnectAndLog(message.SenderConnection, type, e);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
}
|
2021-07-10 09:41:17 +02:00
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
else
|
2021-07-10 09:41:17 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
message.SenderConnection.Disconnect("Npcs are not allowed!");
|
2021-07-10 09:41:17 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-01-01 03:07:18 +01:00
|
|
|
|
case (byte)PacketTypes.NativeResponse:
|
2021-12-10 13:38:03 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.NativeResponse packet = new();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
2021-12-10 13:38:03 +01:00
|
|
|
|
|
2021-12-14 22:39:15 +01:00
|
|
|
|
Client client = Clients.Find(x => x.NetHandle == message.SenderConnection.RemoteUniqueIdentifier);
|
2021-12-10 15:55:45 +01:00
|
|
|
|
if (client != null)
|
2021-12-10 13:38:03 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
if (client.Callbacks.ContainsKey(packet.ID))
|
2021-12-10 13:38:03 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +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)
|
|
|
|
|
{
|
2022-02-06 09:59:18 -07:00
|
|
|
|
DisconnectAndLog(message.SenderConnection, type, e);
|
2021-12-10 13:38:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-01-01 03:07:18 +01:00
|
|
|
|
case (byte)PacketTypes.Mod:
|
2021-09-29 14:34:22 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
if (MainSettings.ModsAllowed)
|
2021-09-29 14:34:22 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
try
|
2021-09-29 14:34:22 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
Packets.Mod packet = new Packets.Mod();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
|
|
|
|
|
|
|
|
|
bool resourceResult = false;
|
2021-12-28 13:51:36 +01:00
|
|
|
|
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
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
resourceResult = true;
|
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
}
|
2021-11-27 22:44:00 +01:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
if (!resourceResult && packet.Target != -1)
|
2021-09-30 23:35:42 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
|
|
|
|
packet.PacketToNetOutGoingMessage(outgoingMessage);
|
|
|
|
|
|
|
|
|
|
if (packet.Target != 0)
|
2021-11-27 22:44:00 +01:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
2021-11-27 22:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
// Send back to all players
|
|
|
|
|
MainNetServer.SendMessage(outgoingMessage, MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Mod);
|
2021-11-27 22:44:00 +01:00
|
|
|
|
}
|
2021-09-30 23:35:42 +02:00
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-02-06 09:59:18 -07:00
|
|
|
|
DisconnectAndLog(message.SenderConnection, type, e);
|
2021-09-30 23:35:42 +02:00
|
|
|
|
}
|
2021-09-29 14:34:22 +02:00
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
else
|
2021-09-29 14:34:22 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
message.SenderConnection.Disconnect("Mods are not allowed!");
|
2021-09-29 14:34:22 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-04-03 02:27:30 +02:00
|
|
|
|
case (byte)PacketTypes.FileTransferComplete:
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (DownloadManager.AnyFileExists)
|
|
|
|
|
{
|
|
|
|
|
int len = message.ReadInt32();
|
|
|
|
|
byte[] data = message.ReadBytes(len);
|
|
|
|
|
|
|
|
|
|
Packets.FileTransferComplete packet = new();
|
|
|
|
|
packet.NetIncomingMessageToPacket(data);
|
|
|
|
|
|
|
|
|
|
Client client = Clients.Find(x => x.NetHandle == message.SenderConnection.RemoteUniqueIdentifier);
|
|
|
|
|
if (client != null && !client.FilesReceived)
|
|
|
|
|
{
|
|
|
|
|
DownloadManager.TryToRemoveClient(client.NetHandle, packet.ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DisconnectAndLog(message.SenderConnection, type, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-04-12 06:04:02 +02:00
|
|
|
|
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;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
default:
|
|
|
|
|
Logging.Error("Unhandled Data / Packet type");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-08-16 12:31:49 +02:00
|
|
|
|
case NetIncomingMessageType.ConnectionLatencyUpdated:
|
2021-08-17 15:04:50 +02:00
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
Client client = Clients.Find(x => x.NetHandle == 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
|
|
|
|
}
|
2021-08-16 12:31:49 +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
|
|
|
|
}
|
2021-12-08 13:45:00 +01:00
|
|
|
|
|
|
|
|
|
Logging.Warning("Server is shutting down!");
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (RunningResource != null)
|
2021-12-08 13:45:00 +01:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
// Waiting for resource...
|
|
|
|
|
while (!RunningResource.ReadyToStop)
|
2021-12-08 13:54:30 +01:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
// 16 milliseconds to sleep to reduce CPU usage
|
|
|
|
|
Thread.Sleep(1000 / 60);
|
|
|
|
|
}
|
2021-12-08 13:45:00 +01:00
|
|
|
|
}
|
2021-12-08 14:46:07 +01:00
|
|
|
|
|
|
|
|
|
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-02-06 09:59:18 -07:00
|
|
|
|
private void DisconnectAndLog(NetConnection senderConnection, byte type, Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logging.Error($"Error receiving a packet of type {type}");
|
|
|
|
|
Logging.Error(e.Message);
|
|
|
|
|
Logging.Error(e.StackTrace);
|
|
|
|
|
senderConnection.Disconnect(e.Message);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 05:00:48 +02:00
|
|
|
|
#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
|
|
|
|
{
|
2021-12-17 23:58:32 +01:00
|
|
|
|
Logging.Debug("New handshake from: [Name: " + packet.Username + " | Address: " + local.RemoteEndPoint.Address.ToString() + "]");
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
2022-04-10 14:34:55 +02:00
|
|
|
|
if (!packet.ModVersion.StartsWith(_compatibleVersion))
|
2021-12-18 00:21:23 +01:00
|
|
|
|
{
|
2022-04-10 14:34:55 +02: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;
|
|
|
|
|
}
|
2022-02-17 14:42:54 -07:00
|
|
|
|
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;
|
|
|
|
|
}
|
2022-04-10 14:34:55 +02:00
|
|
|
|
if (_mainAllowlist.Username.Any() && !_mainAllowlist.Username.Contains(packet.Username.ToLower()))
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-12-17 23:58:32 +01:00
|
|
|
|
local.Deny("This Username is not on the allow list!");
|
|
|
|
|
return;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
2022-04-10 14:34:55 +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;
|
|
|
|
|
}
|
2022-04-10 14:34:55 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
2021-12-17 23:58:32 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 22:39:15 +01:00
|
|
|
|
long localNetHandle = local.RemoteUniqueIdentifier;
|
2021-07-09 00:20:09 +02:00
|
|
|
|
|
2021-11-25 16:32:04 +01:00
|
|
|
|
Client tmpClient;
|
|
|
|
|
|
2021-07-07 13:36:25 +02:00
|
|
|
|
// Add the player to Players
|
2021-11-25 16:32:04 +01:00
|
|
|
|
lock (Clients)
|
|
|
|
|
{
|
|
|
|
|
Clients.Add(
|
|
|
|
|
tmpClient = new Client()
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
NetHandle = localNetHandle,
|
2021-11-25 16:32:04 +01:00
|
|
|
|
Player = new()
|
|
|
|
|
{
|
|
|
|
|
Username = packet.Username
|
|
|
|
|
}
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}
|
2021-11-25 16:32:04 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
NetHandle = localNetHandle,
|
2021-07-07 13:36:25 +02:00
|
|
|
|
Username = string.Empty,
|
|
|
|
|
ModVersion = string.Empty,
|
2021-12-23 22:03:01 +01:00
|
|
|
|
NPCsAllowed = MainSettings.NpcsAllowed
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}.PacketToNetOutGoingMessage(outgoingMessage);
|
|
|
|
|
|
|
|
|
|
// Accept the connection and send back a new handshake packet with the connection ID
|
|
|
|
|
local.Approve(outgoingMessage);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
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
|
2021-12-17 23:02:53 +01:00
|
|
|
|
private static void SendPlayerConnectPacket(NetConnection local)
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-12-17 23:02:53 +01:00
|
|
|
|
Client localClient = Clients.Find(x => x.NetHandle == local.RemoteUniqueIdentifier);
|
2021-12-10 15:55:45 +01:00
|
|
|
|
if (localClient == null)
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-11-25 16:32:04 +01: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;
|
2021-11-25 16:32:04 +01:00
|
|
|
|
if ((clients = Util.FilterAllLocal(local)).Count > 0)
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-11-25 16:32:04 +01:00
|
|
|
|
// Send all players to local
|
|
|
|
|
clients.ForEach(targetPlayer =>
|
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
long targetNetHandle = targetPlayer.RemoteUniqueIdentifier;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
2021-12-14 22:39:15 +01:00
|
|
|
|
Client targetClient = Clients.Find(x => x.NetHandle == targetNetHandle);
|
2021-12-10 20:23:58 +01:00
|
|
|
|
if (targetClient != null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
|
|
|
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
2022-01-01 03:07:18 +01:00
|
|
|
|
new Packets.PlayerConnect()
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
2021-12-14 22:39:15 +01:00
|
|
|
|
NetHandle = targetNetHandle,
|
2021-11-25 16:32:04 +01:00
|
|
|
|
Username = targetClient.Player.Username
|
|
|
|
|
}.PacketToNetOutGoingMessage(outgoingMessage);
|
|
|
|
|
MainNetServer.SendMessage(outgoingMessage, local, NetDeliveryMethod.ReliableOrdered, 0);
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
2021-11-25 16:32:04 +01: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
|
|
|
|
{
|
2021-12-17 23:02:53 +01:00
|
|
|
|
NetHandle = local.RemoteUniqueIdentifier,
|
2021-11-25 16:32:04 +01:00
|
|
|
|
Username = localClient.Player.Username
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.ReliableOrdered, 0);
|
|
|
|
|
}
|
2021-08-26 17:01:32 +02:00
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (RunningResource != null)
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
RunningResource.InvokePlayerConnected(localClient);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}
|
2021-12-08 09:57:19 +01:00
|
|
|
|
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-12-08 09:57:19 +01:00
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send all players a message that someone has left the server
|
2022-04-03 19:48:51 +02:00
|
|
|
|
private static void SendPlayerDisconnectPacket(long nethandle)
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2022-04-03 19:48:51 +02:00
|
|
|
|
List<NetConnection> clients = MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != nethandle);
|
2021-12-08 09:57:19 +01:00
|
|
|
|
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()
|
2021-12-08 09:57:19 +01:00
|
|
|
|
{
|
2022-04-03 19:48:51 +02:00
|
|
|
|
NetHandle = nethandle
|
2021-12-08 09:57:19 +01:00
|
|
|
|
}.PacketToNetOutGoingMessage(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-04-03 19:48:51 +02:00
|
|
|
|
Client localClient = Clients.FirstOrDefault(x => x.NetHandle == nethandle);
|
2021-12-10 15:55:45 +01:00
|
|
|
|
if (localClient == null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 09:57:19 +01:00
|
|
|
|
Clients.Remove(localClient);
|
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (RunningResource != null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
RunningResource.InvokePlayerDisconnected(localClient);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}
|
2021-12-08 09:57:19 +01:00
|
|
|
|
else
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
2021-12-08 09:57:19 +01:00
|
|
|
|
Logging.Info($"Player {localClient.Player.Username} disconnected!");
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
private static void FullSyncPlayer(Packets.FullSyncPlayer packet)
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
Client client = Util.GetClientByNetHandle(packet.NetHandle);
|
2021-12-11 12:35:17 +01:00
|
|
|
|
if (client == null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
// Save the new data
|
2021-12-26 03:25:00 +01:00
|
|
|
|
client.Player.PedHandle = packet.PedHandle;
|
|
|
|
|
client.Player.IsInVehicle = false;
|
2021-12-23 22:03:01 +01:00
|
|
|
|
client.Player.Position = packet.Position;
|
|
|
|
|
client.Player.Health = packet.Health;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
// Override the latency
|
|
|
|
|
packet.Latency = client.Latency;
|
2021-08-16 12:31:49 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != packet.NetHandle).ForEach(x =>
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
|
|
|
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
2021-12-23 22:03:01 +01:00
|
|
|
|
if (Clients.First(y => y.NetHandle == x.RemoteUniqueIdentifier).Player.IsInRangeOf(packet.Position, 550f))
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
|
|
|
|
packet.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
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
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
NetHandle = packet.NetHandle,
|
|
|
|
|
Position = packet.Position,
|
|
|
|
|
Latency = packet.Latency
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerSuperLight);
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-11-25 16:32:04 +01:00
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (RunningResource != null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
RunningResource.InvokePlayerUpdate(client);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
Client client = Util.GetClientByNetHandle(packet.NetHandle);
|
2021-12-11 12:35:17 +01:00
|
|
|
|
if (client == null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
// Save the new data
|
2021-12-26 03:25:00 +01:00
|
|
|
|
client.Player.PedHandle = packet.PedHandle;
|
|
|
|
|
client.Player.VehicleHandle = packet.VehicleHandle;
|
|
|
|
|
client.Player.IsInVehicle = true;
|
2021-12-23 22:03:01 +01:00
|
|
|
|
client.Player.Position = packet.Position;
|
|
|
|
|
client.Player.Health = packet.Health;
|
2021-08-16 12:31:49 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
// Override the latency
|
|
|
|
|
packet.Latency = client.Latency;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != packet.NetHandle).ForEach(x =>
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
|
|
|
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
2021-08-16 12:31:49 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
if (Clients.First(y => y.NetHandle == x.RemoteUniqueIdentifier).Player.IsInRangeOf(packet.Position, 550f))
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
|
|
|
|
packet.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
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
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
NetHandle = packet.NetHandle,
|
|
|
|
|
Position = packet.Position,
|
|
|
|
|
Latency = packet.Latency
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerSuperLight);
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-11-25 16:32:04 +01:00
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (RunningResource != null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
RunningResource.InvokePlayerUpdate(client);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
Client client = Util.GetClientByNetHandle(packet.NetHandle);
|
2021-12-11 12:35:17 +01:00
|
|
|
|
if (client == null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
// Save the new data
|
|
|
|
|
client.Player.Position = packet.Position;
|
|
|
|
|
client.Player.Health = packet.Health;
|
2021-08-16 12:31:49 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
// Override the latency
|
|
|
|
|
packet.Latency = client.Latency;
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != packet.NetHandle).ForEach(x =>
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
|
|
|
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
2021-08-16 12:31:49 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
if (Clients.First(y => y.NetHandle == x.RemoteUniqueIdentifier).Player.IsInRangeOf(packet.Position, 550f))
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
|
|
|
|
packet.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
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
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
NetHandle = packet.NetHandle,
|
|
|
|
|
Position = packet.Position,
|
|
|
|
|
Latency = packet.Latency
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerSuperLight);
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-11-25 16:32:04 +01:00
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (RunningResource != null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
RunningResource.InvokePlayerUpdate(client);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
private static void LightSyncPlayerVeh(Packets.LightSyncPlayerVeh packet)
|
2021-07-10 09:41:17 +02:00
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
Client client = Util.GetClientByNetHandle(packet.NetHandle);
|
2021-12-11 12:35:17 +01:00
|
|
|
|
if (client == null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
// Save the new data
|
|
|
|
|
client.Player.Position = packet.Position;
|
|
|
|
|
client.Player.Health = packet.Health;
|
2021-08-16 12:31:49 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
// Override the latency
|
|
|
|
|
packet.Latency = client.Latency;
|
2021-07-10 09:41:17 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != packet.NetHandle).ForEach(x =>
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
|
|
|
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
2021-08-16 12:31:49 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
if (Clients.First(y => y.NetHandle == x.RemoteUniqueIdentifier).Player.IsInRangeOf(packet.Position, 550f))
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
|
|
|
|
packet.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
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
|
|
|
|
{
|
2021-12-23 22:03:01 +01:00
|
|
|
|
NetHandle = packet.NetHandle,
|
|
|
|
|
Position = packet.Position,
|
|
|
|
|
Latency = packet.Latency
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PlayerSuperLight);
|
2021-08-26 17:01:32 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-11-25 16:32:04 +01:00
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (RunningResource != null)
|
2021-11-25 16:32:04 +01:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
RunningResource.InvokePlayerUpdate(client);
|
2021-11-25 16:32:04 +01:00
|
|
|
|
}
|
2021-07-10 09:41:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (RunningResource != null)
|
2021-08-15 07:54:25 +02:00
|
|
|
|
{
|
2021-12-02 23:05:22 +01: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))
|
|
|
|
|
{
|
2021-12-02 23:05:22 +01:00
|
|
|
|
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),
|
2021-12-02 23:05:22 +01:00
|
|
|
|
Args = argsWithoutCmd
|
2021-08-18 11:47:59 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
KeyValuePair<Command, Action<CommandContext>> command = Commands.First(x => x.Key.Name == cmdName);
|
2021-12-02 23:05:22 +01:00
|
|
|
|
|
|
|
|
|
if (command.Key.Usage != null && command.Key.ArgsLength != argsWithoutCmd.Length)
|
|
|
|
|
{
|
|
|
|
|
NetConnection userConnection = Util.GetConnectionByUsername(packet.Username);
|
|
|
|
|
if (userConnection == default)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
SendChatMessage("Server", command.Key.Usage, userConnection);
|
2021-12-02 23:05:22 +01:00
|
|
|
|
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
|
|
|
|
|
2021-12-23 22:03:01 +01:00
|
|
|
|
SendChatMessage("Server", "Command not found!", userConnection);
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-07 08:18:29 +01:00
|
|
|
|
|
2021-12-28 13:51:36 +01:00
|
|
|
|
if (RunningResource.InvokeChatMessage(packet.Username, packet.Message))
|
2021-08-18 11:47:59 +02:00
|
|
|
|
{
|
2021-12-28 13:51:36 +01:00
|
|
|
|
return;
|
2021-08-18 11:47:59 +02:00
|
|
|
|
}
|
2021-08-15 07:54:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 19:04:09 +02:00
|
|
|
|
packet.Message = packet.Message.Replace("~", "");
|
2021-07-07 13:36:25 +02:00
|
|
|
|
|
2021-12-23 22:03:01 +01: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
|
|
|
|
}
|
2021-12-23 22:03:01 +01:00
|
|
|
|
|
|
|
|
|
internal static void SendChatMessage(string username, string message, List<NetConnection> targets = null)
|
|
|
|
|
{
|
|
|
|
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
new Packets.ChatMessage() { Username = username, Message = message }.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
|
|
|
|
|
MainNetServer.SendMessage(outgoingMessage, targets ?? MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Chat);
|
|
|
|
|
}
|
|
|
|
|
internal static void SendChatMessage(string username, string message, NetConnection target)
|
|
|
|
|
{
|
|
|
|
|
SendChatMessage(username, message, new List<NetConnection>() { target });
|
|
|
|
|
}
|
2021-07-12 05:00:48 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region -- NPC --
|
2022-01-01 03:07:18 +01:00
|
|
|
|
private static void FullSyncNpc(NetConnection local, Packets.FullSyncNpc packet)
|
2021-07-12 05:00:48 +02:00
|
|
|
|
{
|
2021-08-26 17:01:32 +02:00
|
|
|
|
List<NetConnection> clients;
|
2021-09-28 14:44:10 +02:00
|
|
|
|
if ((clients = Util.GetAllInRange(packet.Position, 550f, local)).Count == 0)
|
2021-07-12 05:00:48 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
|
|
|
|
packet.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.NPCFull);
|
2021-07-12 05:00:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-01 03:07:18 +01:00
|
|
|
|
private static void FullSyncNpcVeh(NetConnection local, Packets.FullSyncNpcVeh packet)
|
2021-07-12 05:00:48 +02:00
|
|
|
|
{
|
2021-08-26 17:01:32 +02:00
|
|
|
|
List<NetConnection> clients;
|
2021-09-28 14:44:10 +02:00
|
|
|
|
if ((clients = Util.GetAllInRange(packet.Position, 550f, local)).Count == 0)
|
2021-08-26 17:01:32 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-12 05:00:48 +02:00
|
|
|
|
|
|
|
|
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
|
|
|
|
packet.PacketToNetOutGoingMessage(outgoingMessage);
|
2021-12-23 22:03:01 +01:00
|
|
|
|
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.NPCFull);
|
2021-07-12 05:00:48 +02:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
2021-08-18 11:47:59 +02:00
|
|
|
|
|
2021-12-02 23:05:22 +01: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)
|
|
|
|
|
{
|
2021-12-02 23:05:22 +01:00
|
|
|
|
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>()
|
|
|
|
|
{
|
2021-12-02 23:05:22 +01:00
|
|
|
|
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)
|
|
|
|
|
{
|
2021-12-02 23:05:22 +01:00
|
|
|
|
Command attribute = method.GetCustomAttribute<Command>(true);
|
2021-08-18 11:47:59 +02:00
|
|
|
|
|
2021-12-02 23:05:22 +01: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
|
|
|
|
}
|
|
|
|
|
}
|