better
This commit is contained in:
@ -32,7 +32,7 @@ namespace CoopClient.Entities
|
||||
{
|
||||
long key = localNpcs.ElementAt(i).Key;
|
||||
|
||||
if ((tickCount - localNpcs[key].LastUpdateReceived) > 3500)
|
||||
if ((tickCount - localNpcs[key].LastUpdateReceived) > 3000)
|
||||
{
|
||||
if (localNpcs[key].Character != null && localNpcs[key].Character.Exists() && localNpcs[key].Health > 0)
|
||||
{
|
||||
|
@ -44,7 +44,7 @@ namespace CoopClient
|
||||
|
||||
Tick += OnTick;
|
||||
KeyDown += OnKeyDown;
|
||||
Aborted += OnAbort;
|
||||
Aborted += (object sender, EventArgs e) => CleanUp();
|
||||
|
||||
Util.NativeMemory();
|
||||
}
|
||||
@ -100,12 +100,14 @@ namespace CoopClient
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((Environment.TickCount - LastDataSend) >= (1000 / 60))
|
||||
if ((Environment.TickCount - LastDataSend) < (1000 / 60))
|
||||
{
|
||||
MainNetworking.SendPlayerData();
|
||||
|
||||
LastDataSend = Environment.TickCount;
|
||||
return;
|
||||
}
|
||||
|
||||
MainNetworking.SendPlayerData();
|
||||
|
||||
LastDataSend = Environment.TickCount;
|
||||
}
|
||||
|
||||
private void OnKeyDown(object sender, KeyEventArgs e)
|
||||
@ -168,11 +170,6 @@ namespace CoopClient
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAbort(object sender, EventArgs e)
|
||||
{
|
||||
CleanUp();
|
||||
}
|
||||
|
||||
public static void CleanUp()
|
||||
{
|
||||
MainChat.Clear();
|
||||
@ -201,7 +198,7 @@ namespace CoopClient
|
||||
entity.Delete();
|
||||
}
|
||||
|
||||
foreach (Vehicle veh in World.GetAllVehicles().Where(v => v.Handle != Game.Player.Character.Handle))
|
||||
foreach (Vehicle veh in World.GetAllVehicles().Where(v => v.Handle != Game.Player.Character.CurrentVehicle?.Handle))
|
||||
{
|
||||
veh.Delete();
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using CoopClient.Entities;
|
||||
|
||||
@ -6,7 +7,6 @@ using Lidgren.Network;
|
||||
|
||||
using GTA;
|
||||
using GTA.Native;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CoopClient
|
||||
{
|
||||
@ -270,39 +270,6 @@ namespace CoopClient
|
||||
|
||||
Main.Players.Remove(packet.Player);
|
||||
}
|
||||
|
||||
//if (!Main.NpcsAllowed)
|
||||
//{
|
||||
// return;
|
||||
//}
|
||||
//
|
||||
//lock (Main.Npcs)
|
||||
//{
|
||||
// for (int i = 0; i < Main.Npcs.Count; i++)
|
||||
// {
|
||||
// long key = Main.Npcs.ElementAt(i).Key;
|
||||
//
|
||||
// if (!key.ToString().StartsWith(packet.Player.ToString()))
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// EntitiesNpc npcData = Main.Npcs[key];
|
||||
//
|
||||
// if (npcData.Character != null && npcData.Character.Exists() && npcData.Health > 0)
|
||||
// {
|
||||
// npcData.Character.Kill();
|
||||
// npcData.Character.Delete();
|
||||
// }
|
||||
//
|
||||
// if (npcData.MainVehicle != null && npcData.MainVehicle.Exists() && npcData.MainVehicle.PassengerCount == 0)
|
||||
// {
|
||||
// npcData.MainVehicle.Delete();
|
||||
// }
|
||||
//
|
||||
// Main.Npcs.Remove(key);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
private void FullSyncPlayer(FullSyncPlayerPacket packet)
|
||||
@ -444,6 +411,10 @@ namespace CoopClient
|
||||
{
|
||||
arguments.Add(((FloatArgument)arg).Data);
|
||||
}
|
||||
else if (typeOf == typeof(StringArgument))
|
||||
{
|
||||
arguments.Add(((StringArgument)arg).Data);
|
||||
}
|
||||
else if (typeOf == typeof(LVector3Argument))
|
||||
{
|
||||
arguments.Add(((LVector3Argument)arg).Data.X);
|
||||
|
@ -623,7 +623,8 @@ namespace CoopClient
|
||||
[ProtoInclude(1, typeof(IntArgument))]
|
||||
[ProtoInclude(2, typeof(BoolArgument))]
|
||||
[ProtoInclude(3, typeof(FloatArgument))]
|
||||
[ProtoInclude(4, typeof(LVector3Argument))]
|
||||
[ProtoInclude(4, typeof(StringArgument))]
|
||||
[ProtoInclude(5, typeof(LVector3Argument))]
|
||||
class NativeArgument { }
|
||||
|
||||
[ProtoContract]
|
||||
@ -647,6 +648,13 @@ namespace CoopClient
|
||||
public float Data { get; set; }
|
||||
}
|
||||
|
||||
[ProtoContract]
|
||||
class StringArgument : NativeArgument
|
||||
{
|
||||
[ProtoMember(1)]
|
||||
public string Data { get; set; }
|
||||
}
|
||||
|
||||
[ProtoContract]
|
||||
class LVector3Argument : NativeArgument
|
||||
{
|
||||
|
@ -556,7 +556,8 @@ namespace CoopServer
|
||||
[ProtoInclude(1, typeof(IntArgument))]
|
||||
[ProtoInclude(2, typeof(BoolArgument))]
|
||||
[ProtoInclude(3, typeof(FloatArgument))]
|
||||
[ProtoInclude(4, typeof(LVector3Argument))]
|
||||
[ProtoInclude(4, typeof(StringArgument))]
|
||||
[ProtoInclude(5, typeof(LVector3Argument))]
|
||||
class NativeArgument { }
|
||||
|
||||
[ProtoContract]
|
||||
@ -580,6 +581,13 @@ namespace CoopServer
|
||||
public float Data { get; set; }
|
||||
}
|
||||
|
||||
[ProtoContract]
|
||||
class StringArgument : NativeArgument
|
||||
{
|
||||
[ProtoMember(1)]
|
||||
public string Data { get; set; }
|
||||
}
|
||||
|
||||
[ProtoContract]
|
||||
class LVector3Argument : NativeArgument
|
||||
{
|
||||
|
@ -425,18 +425,15 @@ namespace CoopServer
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<long, EntitiesPlayer> player in Players)
|
||||
if (Players.Any(x => x.Value.SocialClubName == packet.SocialClubName))
|
||||
{
|
||||
if (player.Value.SocialClubName == packet.SocialClubName)
|
||||
{
|
||||
local.Deny("The name of the Social Club is already taken!");
|
||||
return;
|
||||
}
|
||||
else if (player.Value.Username == packet.Username)
|
||||
{
|
||||
local.Deny("Username is already taken!");
|
||||
return;
|
||||
}
|
||||
local.Deny("The name of the Social Club is already taken!");
|
||||
return;
|
||||
}
|
||||
else if (Players.Any(x => x.Value.Username == packet.Username))
|
||||
{
|
||||
local.Deny("Username is already taken!");
|
||||
return;
|
||||
}
|
||||
|
||||
long localPlayerID = local.RemoteUniqueIdentifier;
|
||||
@ -645,7 +642,7 @@ namespace CoopServer
|
||||
else
|
||||
{
|
||||
NetConnection userConnection = Util.GetConnectionByUsername(packet.Username);
|
||||
if (userConnection == null)
|
||||
if (userConnection == default)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
@ -62,33 +62,10 @@ namespace CoopServer
|
||||
return;
|
||||
}
|
||||
|
||||
List<NativeArgument> arguments = new();
|
||||
|
||||
foreach (object arg in args)
|
||||
List<NativeArgument> arguments = Util.ParseNativeArguments(args);
|
||||
if (arguments == null)
|
||||
{
|
||||
Type typeOf = arg.GetType();
|
||||
|
||||
if (typeOf == typeof(int))
|
||||
{
|
||||
arguments.Add(new IntArgument() { Data = (int)arg });
|
||||
}
|
||||
else if (typeOf == typeof(bool))
|
||||
{
|
||||
arguments.Add(new BoolArgument() { Data = (bool)arg });
|
||||
}
|
||||
else if (typeOf == typeof(float))
|
||||
{
|
||||
arguments.Add(new FloatArgument() { Data = (float)arg });
|
||||
}
|
||||
else if (typeOf == typeof(LVector3))
|
||||
{
|
||||
arguments.Add(new LVector3Argument() { Data = (LVector3)arg });
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Error("[ServerScript->SendNativeCallToAll(" + hash + ", params object[] args)]: Type of argument not found!");
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
NativeCallPacket packet = new()
|
||||
@ -105,39 +82,16 @@ namespace CoopServer
|
||||
public static void SendNativeCallToPlayer(string username, ulong hash, params object[] args)
|
||||
{
|
||||
NetConnection userConnection = Util.GetConnectionByUsername(username);
|
||||
if (userConnection == null)
|
||||
if (userConnection == default)
|
||||
{
|
||||
Logging.Warning("[ServerScript->SendNativeCallToPlayer(\"" + username + "\", \"" + hash + "\", params object[] args)]: User not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
List<NativeArgument> arguments = new();
|
||||
|
||||
foreach (object arg in args)
|
||||
List<NativeArgument> arguments = Util.ParseNativeArguments(args);
|
||||
if (arguments == null)
|
||||
{
|
||||
Type typeOf = arg.GetType();
|
||||
|
||||
if (typeOf == typeof(int))
|
||||
{
|
||||
arguments.Add(new IntArgument() { Data = (int)arg });
|
||||
}
|
||||
else if (typeOf == typeof(bool))
|
||||
{
|
||||
arguments.Add(new BoolArgument() { Data = (bool)arg });
|
||||
}
|
||||
else if (typeOf == typeof(float))
|
||||
{
|
||||
arguments.Add(new FloatArgument() { Data = (float)arg });
|
||||
}
|
||||
else if (typeOf == typeof(LVector3))
|
||||
{
|
||||
arguments.Add(new LVector3Argument() { Data = (LVector3)arg });
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Error("[ServerScript->SendNativeCallToAll(" + hash + ", params object[] args)]: Type of argument not found!");
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
NativeCallPacket packet = new()
|
||||
@ -173,7 +127,7 @@ namespace CoopServer
|
||||
public static void KickPlayerByUsername(string username, string[] reason)
|
||||
{
|
||||
NetConnection userConnection = Util.GetConnectionByUsername(username);
|
||||
if (userConnection == null)
|
||||
if (userConnection == default)
|
||||
{
|
||||
Logging.Warning("[ServerScript->KickPlayerByUsername(\"" + username + "\", \"" + string.Join(" ", reason) + "\")]: User not found!");
|
||||
return;
|
||||
@ -204,7 +158,7 @@ namespace CoopServer
|
||||
public static void SendChatMessageToPlayer(string username, string message, string from = "Server")
|
||||
{
|
||||
NetConnection userConnection = Util.GetConnectionByUsername(username);
|
||||
if (userConnection == null)
|
||||
if (userConnection == default)
|
||||
{
|
||||
Logging.Warning("[ServerScript->SendChatMessageToPlayer(\"" + username + "\", \"" + message + "\", \"" + from + "\")]: User not found!");
|
||||
return;
|
||||
|
@ -10,20 +10,53 @@ namespace CoopServer
|
||||
{
|
||||
class Util
|
||||
{
|
||||
public static List<NativeArgument> ParseNativeArguments(params object[] args)
|
||||
{
|
||||
List<NativeArgument> result = new();
|
||||
|
||||
foreach (object arg in args)
|
||||
{
|
||||
Type typeOf = arg.GetType();
|
||||
|
||||
if (typeOf == typeof(int))
|
||||
{
|
||||
result.Add(new IntArgument() { Data = (int)arg });
|
||||
}
|
||||
else if (typeOf == typeof(bool))
|
||||
{
|
||||
result.Add(new BoolArgument() { Data = (bool)arg });
|
||||
}
|
||||
else if (typeOf == typeof(float))
|
||||
{
|
||||
result.Add(new FloatArgument() { Data = (float)arg });
|
||||
}
|
||||
else if (typeOf == typeof(string))
|
||||
{
|
||||
result.Add(new StringArgument() { Data = (string)arg });
|
||||
}
|
||||
else if (typeOf == typeof(LVector3))
|
||||
{
|
||||
result.Add(new LVector3Argument() { Data = (LVector3)arg });
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Error("[Util->ParseNativeArguments(params object[] args)]: Type of argument not found!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static NetConnection GetConnectionByUsername(string username)
|
||||
{
|
||||
long? userID = GetIdByUsername(username);
|
||||
if (userID == null || !Server.MainNetServer.Connections.Any(x => x.RemoteUniqueIdentifier == userID))
|
||||
long? userID = Server.Players.FirstOrDefault(x => x.Value.Username == username).Key;
|
||||
if (userID == default || !Server.MainNetServer.Connections.Any(x => x.RemoteUniqueIdentifier == userID))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return Server.MainNetServer.Connections.First(x => x.RemoteUniqueIdentifier == userID);
|
||||
}
|
||||
|
||||
public static long? GetIdByUsername(string username)
|
||||
{
|
||||
return Server.Players.Any(x => x.Value.Username == username) ? Server.Players.First(x => x.Value.Username == username).Key : null;
|
||||
return Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == userID);
|
||||
}
|
||||
|
||||
// Return a list of all connections but not the local connection
|
||||
|
Reference in New Issue
Block a user