918 lines
39 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;
2021-07-07 13:36:25 +02:00
using Lidgren.Network;
2022-06-01 19:05:45 +08:00
using System.Timers;
using System.Security.Cryptography;
2022-06-04 18:09:42 +08:00
using RageCoop.Server.Scripting;
using System.Net.Sockets;
using System.Threading.Tasks;
2021-07-07 13:36:25 +02:00
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-06-04 18:09:42 +08:00
internal class Server
2021-07-07 13:36:25 +02:00
{
2022-06-02 08:53:01 +08:00
private static readonly string _compatibleVersion = "V0_4";
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");
2021-07-07 13:36:25 +02:00
public static NetServer MainNetServer;
2022-04-14 12:33:19 +02:00
public static readonly Dictionary<Command, Action<CommandContext>> Commands = new();
2022-05-30 11:11:40 +08:00
public static readonly Dictionary<long,Client> Clients = new();
2022-06-01 19:05:45 +08:00
private static System.Timers.Timer SendLatencyTimer = new System.Timers.Timer(5000);
private static Dictionary<int,FileTransfer> InProgressFileTransfers=new();
private static Resources Resources = new Resources();
2021-07-07 13:36:25 +02:00
public Server()
{
2022-05-31 19:35:01 -08:00
Program.Logger.Info("================");
Program.Logger.Info($"Server bound to: 0.0.0.0:{MainSettings.Port}");
Program.Logger.Info($"Server version: {Assembly.GetCallingAssembly().GetName().Version}");
Program.Logger.Info($"Compatible RAGECOOP versions: {_compatibleVersion.Replace('_', '.')}.x");
Program.Logger.Info("================");
2021-09-28 16:51:16 +02:00
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 = false,
AutoFlushSendQueue = true,
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();
2022-06-01 19:05:45 +08:00
SendLatencyTimer.Elapsed+=((s,e) => { SendLatency(); });
SendLatencyTimer.AutoReset=true;
SendLatencyTimer.Enabled=true;
2022-05-31 19:35:01 -08:00
Program.Logger.Info(string.Format("Server listening on {0}:{1}", config.LocalAddress.ToString(), config.Port));
2021-12-08 03:59:14 +01:00
if (MainSettings.AnnounceSelf)
2021-12-07 08:18:29 +01:00
{
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);
2022-06-01 17:55:38 +08:00
Program.Logger.Info($"Your public IP is {info.Address}, announcing to master server...");
2022-04-19 05:33:18 +02:00
}
catch (Exception ex)
{
2022-05-31 19:35:01 -08:00
Program.Logger.Error(ex.InnerException?.Message ?? ex.Message);
2022-04-19 05:33:18 +02:00
return;
}
2022-06-03 16:28:02 +08:00
var realMaster = MainSettings.MasterServer=="[AUTO]" ? Util.DownloadString("https://ragecoop.online/stuff/masterserver") : MainSettings.MasterServer;
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 + "\", " +
2022-05-31 23:12:32 -08:00
"\"maxPlayers\": \"" + MainSettings.MaxPlayers + "\"" +
2022-04-19 03:38:48 +02:00
" }";
HttpResponseMessage response = null;
2021-12-07 08:18:29 +01:00
try
{
2022-06-03 16:28:02 +08:00
response = await httpClient.PostAsync(realMaster, 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-05-31 19:35:01 -08:00
Program.Logger.Error($"MasterServer: {ex.Message}");
2022-04-19 03:38:48 +02:00
// 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-05-31 19:35:01 -08:00
Program.Logger.Error("MasterServer: Something went wrong!");
2022-04-19 03:38:48 +02:00
}
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();
2022-05-31 19:35:01 -08:00
Program.Logger.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
{
2022-05-31 19:35:01 -08:00
Program.Logger.Error($"MasterServer: [{(int)response.StatusCode}]");
2022-05-31 23:12:32 -08:00
Program.Logger.Error($"MasterServer: [{await response.Content.ReadAsStringAsync()}]");
2022-04-14 12:18:07 +02:00
}
}
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)
{
2022-05-31 19:35:01 -08:00
Program.Logger.Error($"MasterServer: {ex.InnerException.Message}");
2022-04-19 03:38:48 +02:00
}
catch (Exception ex)
{
2022-05-31 19:35:01 -08:00
Program.Logger.Error($"MasterServer: {ex.Message}");
2022-04-19 03:38:48 +02:00
}
}).Start();
#endregion
2021-12-07 08:18:29 +01:00
}
Resources.LoadAll();
2022-06-01 19:05:45 +08:00
Listen();
2021-07-07 13:36:25 +02:00
}
private void Listen()
{
2022-05-31 19:35:01 -08:00
Program.Logger.Info("Listening for clients");
Program.Logger.Info("Please use CTRL + C if you want to stop the server!");
while (!Program.ReadyToStop)
2022-06-04 14:13:08 +08:00
{
ProcessMessage(MainNetServer.WaitMessage(10));
2022-06-04 14:13:08 +08:00
}
Program.Logger.Warning("Server is shutting down!");
MainNetServer.Shutdown("Server is shutting down!");
Program.Logger.Info("Waiting for resources to stop...");
Resources.StopAll();
2022-06-04 14:13:08 +08:00
Program.Logger.Dispose();
}
private void ProcessMessage(NetIncomingMessage message)
{
if(message == null) { return; }
2022-06-04 14:13:08 +08:00
switch (message.MessageType)
{
case NetIncomingMessageType.ConnectionApproval:
2021-07-07 13:36:25 +02:00
{
2022-06-04 14:13:08 +08:00
Program.Logger.Info($"New incoming connection from: [{message.SenderConnection.RemoteEndPoint}]");
if (message.ReadByte() != (byte)PacketTypes.Handshake)
{
Program.Logger.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: Wrong packet!");
message.SenderConnection.Deny("Wrong packet!");
}
else
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
Packets.Handshake packet = new();
packet.Unpack(data);
GetHandshake(message.SenderConnection, packet);
}
catch (Exception e)
2021-07-07 13:36:25 +02:00
{
2022-06-04 14:13:08 +08:00
Program.Logger.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: {e.Message}");
message.SenderConnection.Deny(e.Message);
}
}
break;
}
case NetIncomingMessageType.StatusChanged:
{
NetConnectionStatus status = (NetConnectionStatus)message.ReadByte();
if (status == NetConnectionStatus.Disconnected)
{
long nethandle = message.SenderConnection.RemoteUniqueIdentifier;
SendPlayerDisconnectPacket(nethandle);
}
else if (status == NetConnectionStatus.Connected)
{
SendPlayerConnectPacket(message.SenderConnection);
Resources.SendTo(Util.GetClientByNetID(message.SenderConnection.RemoteUniqueIdentifier));
2022-06-04 14:13:08 +08:00
}
break;
}
case NetIncomingMessageType.Data:
{
// Get packet type
byte btype = message.ReadByte();
var type = (PacketTypes)btype;
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
try
{
2022-06-04 14:13:08 +08:00
switch (type)
2022-06-04 14:13:08 +08:00
{
#region SyncData
2022-06-04 14:13:08 +08:00
case PacketTypes.PedStateSync:
{
Packets.PedStateSync packet = new();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
PedStateSync(packet, message.SenderConnection.RemoteUniqueIdentifier);
break;
}
case PacketTypes.VehicleStateSync:
{
Packets.VehicleStateSync packet = new();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
VehicleStateSync(packet, message.SenderConnection.RemoteUniqueIdentifier);
2022-04-03 02:27:30 +02:00
break;
}
case PacketTypes.PedSync:
{
2022-05-22 15:55:26 +08:00
Packets.PedSync packet = new();
packet.Unpack(data);
2022-05-22 15:55:26 +08:00
PedSync(packet, message.SenderConnection.RemoteUniqueIdentifier);
2022-05-22 15:55:26 +08:00
}
break;
case PacketTypes.VehicleSync:
{
Packets.VehicleSync packet = new();
packet.Unpack(data);
VehicleSync(packet, message.SenderConnection.RemoteUniqueIdentifier);
}
break;
case PacketTypes.ProjectileSync:
{
Packets.ProjectileSync packet = new();
packet.Unpack(data);
ProjectileSync(packet, message.SenderConnection.RemoteUniqueIdentifier);
}
break;
#endregion
case PacketTypes.ChatMessage:
{
try
{
Packets.ChatMessage packet = new();
packet.Unpack(data);
API.Events.InvokeOnChatMessage(packet, message.SenderConnection);
SendChatMessage(packet);
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
}
}
break;
case PacketTypes.NativeResponse:
{
Packets.NativeResponse packet = new();
2022-06-04 14:13:08 +08:00
packet.Unpack(data);
2022-06-04 18:09:42 +08:00
Client client = Util.GetClientByNetID(message.SenderConnection.RemoteUniqueIdentifier);
if (client != null)
2021-07-07 18:05:08 +02:00
{
if (client.Callbacks.ContainsKey(packet.ID))
{
client.Callbacks[packet.ID].Invoke(packet.Args[0]);
client.Callbacks.Remove(packet.ID);
}
2021-07-07 18:05:08 +02:00
}
2021-07-07 13:36:25 +02:00
}
break;
case PacketTypes.InvokeCustomEvent:
{
var p = new Packets.InvokeCustomEvent();
p.Unpack(data);
API.SendCustomEvent(p.Hash,p.Data,p.Targets);
}
break;
case PacketTypes.FileTransferComplete:
2022-04-03 02:27:30 +02:00
{
Packets.FileTransferComplete packet = new Packets.FileTransferComplete();
packet.Unpack(data);
FileTransfer toRemove;
// Cancel the download if it's in progress
if (InProgressFileTransfers.TryGetValue(packet.ID,out toRemove))
{
toRemove.Cancel=true;
if (toRemove.Name=="Resources.zip")
{
Clients[message.SenderConnection.RemoteUniqueIdentifier].IsReady=true;
}
}
2022-04-03 02:27:30 +02:00
}
break;
default:
if (type.IsSyncEvent())
2022-05-22 15:55:26 +08:00
{
// Sync Events
try
2022-05-22 15:55:26 +08:00
{
var outgoingMessage = MainNetServer.CreateMessage();
outgoingMessage.Write(btype);
outgoingMessage.Write(len);
outgoingMessage.Write(data);
var toSend = MainNetServer.Connections.Exclude(message.SenderConnection);
if (toSend.Count!=0)
2022-05-22 15:55:26 +08:00
{
MainNetServer.SendMessage(outgoingMessage, toSend, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.SyncEvents);
}
2022-05-22 15:55:26 +08:00
}
catch (Exception e)
2022-05-22 15:55:26 +08:00
{
DisconnectAndLog(message.SenderConnection, type, e);
2022-05-22 15:55:26 +08:00
}
}
else
2022-06-04 14:13:08 +08:00
{
Program.Logger.Error("Unhandled Data / Packet type");
}
break;
2022-06-04 14:13:08 +08:00
}
}
catch (Exception e)
{
DisconnectAndLog(message.SenderConnection, type, e);
}
break;
2021-07-07 13:36:25 +02:00
}
2022-06-04 14:13:08 +08:00
case NetIncomingMessageType.ConnectionLatencyUpdated:
{
2022-06-04 18:09:42 +08:00
Client client = Util.GetClientByNetID(message.SenderConnection.RemoteUniqueIdentifier);
2022-06-04 14:13:08 +08:00
if (client != null)
{
2022-06-04 18:09:42 +08:00
client.Player.Latency = message.ReadFloat();
2022-06-04 14:13:08 +08:00
}
}
break;
case NetIncomingMessageType.ErrorMessage:
Program.Logger.Error(message.ReadString());
break;
case NetIncomingMessageType.WarningMessage:
Program.Logger.Warning(message.ReadString());
break;
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.VerboseDebugMessage:
Program.Logger.Debug(message.ReadString());
break;
default:
Program.Logger.Error(string.Format("Unhandled type: {0} {1} bytes {2} | {3}", message.MessageType, message.LengthBytes, message.DeliveryMethod, message.SequenceChannel));
break;
}
2022-06-04 14:13:08 +08:00
MainNetServer.Recycle(message);
2021-07-07 13:36:25 +02:00
}
2022-06-04 14:13:08 +08:00
2022-06-01 19:05:45 +08:00
private void SendLatency()
{
2022-06-01 19:05:45 +08:00
foreach (Client c in Clients.Values)
{
2022-06-01 19:05:45 +08:00
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != c.NetID).ForEach(x =>
{
2022-06-01 19:05:45 +08:00
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
new Packets.PlayerInfoUpdate()
{
2022-06-01 19:05:45 +08:00
PedID=c.Player.PedID,
Username=c.Player.Username,
2022-06-04 18:09:42 +08:00
Latency=c.Player.Latency,
2022-06-01 19:05:45 +08:00
}.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.ReliableSequenced, (byte)ConnectionChannel.Default);
});
}
2022-06-01 19:05:45 +08:00
}
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)
{
2022-05-31 19:35:01 -08:00
Program.Logger.Error($"Error receiving a packet of type {type}");
Program.Logger.Error(e.Message);
Program.Logger.Error(e.StackTrace);
senderConnection.Disconnect(e.Message);
}
#region -- SYNC --
2021-07-07 13:36:25 +02:00
// Before we approve the connection, we must shake hands
2022-06-04 18:09:42 +08:00
private void GetHandshake(NetConnection connection, Packets.Handshake packet)
2021-07-07 13:36:25 +02:00
{
2022-06-04 18:09:42 +08:00
Program.Logger.Debug("New handshake from: [Name: " + packet.Username + " | Address: " + connection.RemoteEndPoint.Address.ToString() + "]");
2021-07-07 13:36:25 +02:00
if (!packet.ModVersion.StartsWith(_compatibleVersion))
2021-12-18 00:21:23 +01:00
{
2022-06-04 18:09:42 +08:00
connection.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))
{
2022-06-04 18:09:42 +08:00
connection.Deny("Username is empty or contains spaces!");
2021-07-07 13:36:25 +02:00
return;
}
if (packet.Username.Any(p => !char.IsLetterOrDigit(p) && !(p == '_') && !(p=='-')))
2021-07-07 13:36:25 +02:00
{
2022-06-04 18:09:42 +08:00
connection.Deny("Username contains special chars!");
2021-07-07 13:36:25 +02:00
return;
}
if (Clients.Values.Any(x => x.Player.Username.ToLower() == packet.Username.ToLower()))
2021-07-07 13:36:25 +02:00
{
connection.Deny("Username is already taken!");
2021-07-07 13:36:25 +02:00
return;
}
var args = new HandshakeEventArgs()
2021-07-07 13:36:25 +02:00
{
EndPoint=connection.RemoteEndPoint,
ID=packet.PedID,
Username=packet.Username
};
API.Events.InvokePlayerHandshake(args);
if (args.Cancel)
2021-08-21 16:52:17 +02:00
{
connection.Deny(args.DenyReason);
2021-08-21 16:52:17 +02:00
return;
2021-07-07 13:36:25 +02: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)
{
2022-06-04 18:09:42 +08:00
Clients.Add(connection.RemoteUniqueIdentifier,
tmpClient = new Client()
2021-08-26 17:01:32 +02:00
{
2022-06-04 18:09:42 +08:00
NetID = connection.RemoteUniqueIdentifier,
Connection=connection,
Player = new()
{
2022-05-22 15:55:26 +08:00
Username = packet.Username,
2022-05-30 11:11:40 +08:00
PedID=packet.PedID,
}
2021-08-26 17:01:32 +02:00
}
2022-05-22 15:55:26 +08:00
);;
}
2022-06-04 18:09:42 +08:00
Program.Logger.Debug($"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,
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
2022-06-04 18:09:42 +08:00
connection.Approve(outgoingMessage);
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-06-04 18:09:42 +08:00
Client localClient = Util.GetClientByNetID(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
}
List<NetConnection> clients=MainNetServer.Connections.Exclude(local);
// Send all players to local
if (clients.Count > 0)
2021-07-07 13:36:25 +02:00
{
clients.ForEach(targetPlayer =>
{
long targetNetHandle = targetPlayer.RemoteUniqueIdentifier;
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
2021-07-07 13:36:25 +02:00
2022-06-04 18:09:42 +08:00
Client targetClient = Util.GetClientByNetID(targetNetHandle);
2021-12-10 20:23:58 +01:00
if (targetClient != null)
{
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
PedID=localClient.Player.PedID,
Username = localClient.Player.Username
2022-05-22 15:55:26 +08:00
}.Pack(outgoingMessage);
if(clients.Count > 0)
{
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.ReliableOrdered, 0);
}
}
2022-06-04 18:09:42 +08:00
API.Events.InvokePlayerConnected(localClient);
Program.Logger.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-30 11:11:40 +08:00
int playerPedID = Clients[nethandle].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-06-04 18:09:42 +08:00
Client localClient = Util.GetClientByNetID( nethandle);
2021-12-10 15:55:45 +01:00
if (localClient == null)
{
return;
}
Clients.Remove(localClient.NetID);
2022-06-04 18:09:42 +08:00
API.Events.InvokePlayerDisconnected(localClient);
Program.Logger.Info($"Player {localClient.Player.Username} disconnected! ID:{playerPedID}");
2022-05-22 15:55:26 +08:00
}
#region SyncEntities
2022-05-30 11:11:40 +08:00
private static void PedStateSync(Packets.PedStateSync packet,long ClientID)
2022-05-22 15:55:26 +08:00
{
2022-06-04 18:09:42 +08:00
Client client = Util.GetClientByNetID(ClientID);
2022-05-22 15:55:26 +08:00
if (client == null)
{
return;
}
2022-05-30 11:11:40 +08:00
foreach (var c in Clients.Values)
2022-05-22 15:55:26 +08:00
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.Pack(outgoingMessage);
if (c.NetID==client.NetID) { continue; }
2022-05-30 11:11:40 +08:00
MainNetServer.SendMessage(outgoingMessage, c.Connection, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PedSync);
}
2022-05-22 15:55:26 +08:00
}
private static void VehicleStateSync(Packets.VehicleStateSync packet, long ClientID)
{
2022-06-04 18:09:42 +08:00
Client client = Util.GetClientByNetID(ClientID);
2022-05-22 15:55:26 +08:00
if (client == null)
{
return;
}
// Save the new data
if (packet.Passengers.ContainsValue(client.Player.PedID))
{
client.Player.VehicleID = packet.ID;
}
2022-05-30 11:11:40 +08:00
foreach (var c in Clients.Values)
2022-05-22 15:55:26 +08:00
{
if (c.NetID==client.NetID) { continue; }
2022-05-22 15:55:26 +08:00
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.Pack(outgoingMessage);
2022-05-30 11:11:40 +08:00
MainNetServer.SendMessage(outgoingMessage, c.Connection, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PedSync);
}
2022-05-22 15:55:26 +08:00
}
2022-05-30 11:11:40 +08:00
private static void PedSync(Packets.PedSync packet, long ClientID)
2022-05-22 15:55:26 +08:00
{
2022-06-04 18:09:42 +08:00
Client client = Util.GetClientByNetID(ClientID);
2022-05-22 15:55:26 +08:00
if (client == null)
{
return;
}
bool isPlayer = packet.ID==client.Player.PedID;
if (isPlayer) { client.Player.Position=packet.Position; }
foreach (var c in Clients.Values)
2022-05-22 15:55:26 +08:00
{
2022-05-30 11:11:40 +08:00
// Don't send data back
if (c.NetID==client.NetID) { continue; }
// Check streaming distance
if (isPlayer)
{
if ((MainSettings.PlayerStreamingDistance!=-1)&&(packet.Position.DistanceTo(c.Player.Position)>MainSettings.PlayerStreamingDistance))
{
continue;
}
}
else if ((MainSettings.NpcStreamingDistance!=-1)&&(packet.Position.DistanceTo(c.Player.Position)>MainSettings.NpcStreamingDistance))
{
continue;
}
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.Pack(outgoingMessage);
2022-05-30 11:11:40 +08:00
MainNetServer.SendMessage(outgoingMessage,c.Connection, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PedSync);
}
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)
{
2022-06-04 18:09:42 +08:00
Client client = Util.GetClientByNetID(ClientID);
2022-05-22 15:55:26 +08:00
if (client == null)
{
return;
}
bool isPlayer = packet.ID==client.Player.VehicleID;
2022-05-30 11:11:40 +08:00
foreach (var c in Clients.Values)
2022-05-22 15:55:26 +08:00
{
if (c.NetID==client.NetID) { continue; }
if (isPlayer)
{
// Player's vehicle
if ((MainSettings.PlayerStreamingDistance!=-1)&&(packet.Position.DistanceTo(c.Player.Position)>MainSettings.PlayerStreamingDistance))
{
continue;
}
}
else if((MainSettings.NpcStreamingDistance!=-1)&&(packet.Position.DistanceTo(c.Player.Position)>MainSettings.NpcStreamingDistance))
{
continue;
}
2022-05-22 15:55:26 +08:00
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.Pack(outgoingMessage);
2022-05-30 11:11:40 +08:00
MainNetServer.SendMessage(outgoingMessage, c.Connection, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PedSync);
}
2022-05-22 15:55:26 +08:00
}
private static void ProjectileSync(Packets.ProjectileSync packet, long ClientID)
2021-07-07 13:36:25 +02:00
{
2022-06-04 18:09:42 +08:00
Client client = Util.GetClientByNetID(ClientID);
2021-12-11 12:35:17 +01:00
if (client == null)
{
return;
}
2021-07-07 13:36:25 +02:00
2022-05-30 11:11:40 +08:00
foreach (var c in Clients.Values)
2021-08-26 17:01:32 +02:00
{
if (c.NetID==client.NetID) { continue; }
2021-08-26 17:01:32 +02:00
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
packet.Pack(outgoingMessage);
2022-05-30 11:11:40 +08:00
MainNetServer.SendMessage(outgoingMessage, c.Connection, NetDeliveryMethod.UnreliableSequenced, (byte)ConnectionChannel.PedSync);
}
2021-07-07 13:36:25 +02:00
}
2022-05-22 15:55:26 +08:00
#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
{
2022-06-04 18:09:42 +08:00
if (packet.Message.StartsWith('/'))
2021-08-15 07:54:25 +02:00
{
2022-06-04 18:09:42 +08:00
string[] cmdArgs = packet.Message.Split(" ");
string cmdName = cmdArgs[0].Remove(0, 1);
if (Commands.Any(x => x.Key.Name == cmdName))
2021-08-18 11:47:59 +02:00
{
2022-06-04 18:09:42 +08:00
string[] argsWithoutCmd = cmdArgs.Skip(1).ToArray();
2021-08-18 11:47:59 +02:00
2022-06-04 18:09:42 +08:00
CommandContext ctx = new()
{
Client = Clients.Values.Where(x => x.Player.Username == packet.Username).FirstOrDefault(),
Args = argsWithoutCmd
};
2022-06-04 18:09:42 +08:00
KeyValuePair<Command, Action<CommandContext>> command = Commands.First(x => x.Key.Name == cmdName);
2022-06-04 18:09:42 +08:00
if (command.Key.Usage != null && command.Key.ArgsLength != argsWithoutCmd.Length)
2021-08-18 11:47:59 +02:00
{
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
2022-06-04 18:09:42 +08:00
SendChatMessage("Server", command.Key.Usage, userConnection);
return;
2021-08-18 11:47:59 +02:00
}
2022-06-04 18:09:42 +08:00
command.Value.Invoke(ctx);
2021-08-18 11:47:59 +02:00
}
2022-06-04 18:09:42 +08:00
else
2021-08-18 11:47:59 +02:00
{
2022-06-04 18:09:42 +08:00
NetConnection userConnection = Util.GetConnectionByUsername(packet.Username);
if (userConnection == default)
{
return;
}
SendChatMessage("Server", "Command not found!", userConnection);
2021-08-18 11:47:59 +02:00
}
2021-08-15 07:54:25 +02:00
2022-06-04 18:09:42 +08:00
return;
}
packet.Message = packet.Message.Replace("~", "");
SendChatMessage(packet.Username, packet.Message, targets);
2021-08-18 11:47:59 +02:00
2022-05-31 19:35:01 -08:00
Program.Logger.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 SendFile(string path,string name,Client client,Action<float> updateCallback=null)
{
int id = RequestFileID();
var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
fs.Seek(0, SeekOrigin.Begin);
var total = fs.Length;
Program.Logger.Debug($"Initiating file transfer:{name}, {total}");
FileTransfer transfer = new()
{
ID=id,
Name = name,
};
InProgressFileTransfers.Add(id,transfer);
Send(
new Packets.FileTransferRequest()
{
FileLength= total,
Name=name,
ID=id,
},
client, ConnectionChannel.File, NetDeliveryMethod.ReliableOrdered
);
int read = 0;
int thisRead = 0;
do
{
// 4 KB chunk
byte[] chunk = new byte[4096];
read += thisRead=fs.Read(chunk, 0, 4096);
if (thisRead!=chunk.Length)
{
if (thisRead==0) { break; }
Program.Logger.Trace($"Purging chunk:{thisRead}");
Array.Resize(ref chunk, thisRead);
}
Send(
new Packets.FileTransferChunk()
{
ID=id,
FileChunk=chunk,
},
client, ConnectionChannel.File, NetDeliveryMethod.ReliableOrdered);
transfer.Progress=read/fs.Length;
if (updateCallback!=null) { updateCallback(transfer.Progress);}
} while (thisRead>0);
Send(
new Packets.FileTransferComplete()
{
ID= id,
}
, client, ConnectionChannel.File, NetDeliveryMethod.ReliableOrdered
);
fs.Close();
fs.Dispose();
Program.Logger.Debug($"All file chunks sent:{name}");
InProgressFileTransfers.Remove(id);
}
private static int RequestFileID()
{
int ID = 0;
while ((ID==0)
|| InProgressFileTransfers.ContainsKey(ID))
{
byte[] rngBytes = new byte[4];
RandomNumberGenerator.Create().GetBytes(rngBytes);
// Convert the bytes into an integer
ID = BitConverter.ToInt32(rngBytes, 0);
}
return ID;
}
/// <summary>
/// Pack the packet then send to server.
/// </summary>
/// <param name="p"></param>
/// <param name="channel"></param>
/// <param name="method"></param>
public static void Send(Packet p,Client client, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
p.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, client.Connection,method,(int)channel);
}
2021-07-07 13:36:25 +02:00
}
}