From bfb697d4198969e54446b3ed1c28aef4d9bb8a45 Mon Sep 17 00:00:00 2001 From: EntenKoeniq <81123713+EntenKoeniq@users.noreply.github.com> Date: Fri, 13 Aug 2021 15:20:26 +0200 Subject: [PATCH] Some changes --- Client/Entities/EntitiesNPC.cs | 2 +- Client/Entities/EntitiesPed.cs | 6 ++-- Client/Main.cs | 17 +++++------- Client/Networking.cs | 34 +++++++++++++++++++---- Client/Packets.cs | 23 ++++++++++++++- Client/PlayerList.cs | 51 ++++++++++++++++++++++++---------- Client/Util.cs | 29 +++++-------------- 7 files changed, 105 insertions(+), 57 deletions(-) diff --git a/Client/Entities/EntitiesNPC.cs b/Client/Entities/EntitiesNPC.cs index 560888c..a7a5f14 100644 --- a/Client/Entities/EntitiesNPC.cs +++ b/Client/Entities/EntitiesNPC.cs @@ -2,6 +2,6 @@ { public class EntitiesNpc : EntitiesPed { - public int LastUpdateReceived { get; set; } + //public int LastUpdateReceived { get; set; } } } diff --git a/Client/Entities/EntitiesPed.cs b/Client/Entities/EntitiesPed.cs index aa7f954..362ec92 100644 --- a/Client/Entities/EntitiesPed.cs +++ b/Client/Entities/EntitiesPed.cs @@ -15,6 +15,8 @@ namespace CoopClient { private bool AllDataAvailable = false; public bool LastSyncWasFull { get; set; } = false; + public int LastUpdateReceived { get; set; } + public int Latency { get; set; } public Ped Character { get; set; } public int Health { get; set; } @@ -351,13 +353,13 @@ namespace CoopClient { MainVehicle.Velocity = VehicleVelocity + (VehiclePosition - MainVehicle.Position); - MainVehicle.Quaternion = Quaternion.Slerp(MainVehicle.Quaternion, VehicleRotation, 0.5f); + MainVehicle.Quaternion = Quaternion.Slerp(MainVehicle.Quaternion, VehicleRotation, Math.Min(1.0f, Latency < 0.5f ? 0.5f : Latency)); VehicleStopTime = Environment.TickCount; } else if ((Environment.TickCount - VehicleStopTime) <= 1000) { - Vector3 posTarget = Util.LinearVectorLerp(MainVehicle.Position, VehiclePosition + (VehiclePosition - MainVehicle.Position), (Environment.TickCount - VehicleStopTime), 1000); + Vector3 posTarget = Util.LinearVectorLerp(MainVehicle.Position, VehiclePosition + (VehiclePosition - MainVehicle.Position), Environment.TickCount - VehicleStopTime, 1000); MainVehicle.PositionNoOffset = posTarget; MainVehicle.Quaternion = Quaternion.Slerp(MainVehicle.Quaternion, VehicleRotation, 0.5f); } diff --git a/Client/Main.cs b/Client/Main.cs index b1131cb..9ceb53c 100644 --- a/Client/Main.cs +++ b/Client/Main.cs @@ -27,7 +27,6 @@ namespace CoopClient public static MenusMain MainMenu = new MenusMain(); public static Chat MainChat = new Chat(); - public static PlayerList MainPlayerList = new PlayerList(); public static Networking MainNetworking = new Networking(); @@ -75,10 +74,6 @@ namespace CoopClient } MainChat.Tick(); - if (!MainChat.Focused && !MainMenu.MenuPool.AreAnyVisible) - { - MainPlayerList.Tick(); - } // Display all players foreach (KeyValuePair player in Players) @@ -129,9 +124,8 @@ namespace CoopClient case Keys.Y: if (MainNetworking.IsOnServer()) { - int time = Environment.TickCount; - - MainPlayerList.Pressed = (time - MainPlayerList.Pressed) < 5000 ? (time - 6000) : time; + int currentTimestamp = Environment.TickCount; + PlayerList.Pressed = (currentTimestamp - PlayerList.Pressed) < 5000 ? (currentTimestamp - 6000) : currentTimestamp; } break; case Keys.G: @@ -236,14 +230,14 @@ namespace CoopClient private void Debug() { - var player = Game.Player.Character; + Ped player = Game.Player.Character; if (!Players.ContainsKey("DebugKey")) { Players.Add("DebugKey", new EntitiesPlayer() { SocialClubName = "DEBUG", Username = "DebugPlayer" }); DebugSyncPed = Players["DebugKey"]; } - if ((Environment.TickCount - ArtificialLagCounter) < 47) + if ((Environment.TickCount - ArtificialLagCounter) < 27) { return; } @@ -311,6 +305,9 @@ namespace CoopClient } } + DebugSyncPed.LastUpdateReceived = Environment.TickCount; + DebugSyncPed.Latency = Environment.TickCount - ArtificialLagCounter; + FullDebugSync = !FullDebugSync; ArtificialLagCounter = Environment.TickCount; } diff --git a/Client/Networking.cs b/Client/Networking.cs index 5bf613b..ff71496 100644 --- a/Client/Networking.cs +++ b/Client/Networking.cs @@ -106,7 +106,6 @@ namespace CoopClient Function.Call(Hash.SET_RANDOM_TRAINS, 0); Main.MainChat.Init(); - Main.MainPlayerList.Init(Main.MainSettings.Username); // Send player connect packet NetOutgoingMessage outgoingMessage = Client.CreateMessage(); @@ -232,12 +231,11 @@ namespace CoopClient EntitiesPlayer player = new EntitiesPlayer() { SocialClubName = packet.SocialClubName, - Username = packet.Username + Username = packet.Username, + LastUpdateReceived = Environment.TickCount }; Main.Players.Add(packet.Player, player); - - Main.MainPlayerList.Update(Main.Players, Main.MainSettings.Username); } private void PlayerDisconnect(PlayerDisconnectPacket packet) @@ -255,8 +253,6 @@ namespace CoopClient Main.Players.Remove(packet.Player); } - - Main.MainPlayerList.Update(Main.Players, Main.MainSettings.Username); } private void FullSyncPlayer(FullSyncPlayerPacket packet) @@ -264,6 +260,11 @@ namespace CoopClient if (Main.Players.ContainsKey(packet.Player)) { EntitiesPlayer player = Main.Players[packet.Player]; + + int currentTimestamp = Environment.TickCount; + player.Latency = currentTimestamp - player.LastUpdateReceived; + player.LastUpdateReceived = currentTimestamp; + player.ModelHash = packet.ModelHash; player.Props = packet.Props; player.Health = packet.Health; @@ -289,6 +290,11 @@ namespace CoopClient if (Main.Players.ContainsKey(packet.Player)) { EntitiesPlayer player = Main.Players[packet.Player]; + + int currentTimestamp = Environment.TickCount; + player.Latency = currentTimestamp - player.LastUpdateReceived; + player.LastUpdateReceived = currentTimestamp; + player.ModelHash = packet.ModelHash; player.Props = packet.Props; player.Health = packet.Health; @@ -315,6 +321,11 @@ namespace CoopClient if (Main.Players.ContainsKey(packet.Player)) { EntitiesPlayer player = Main.Players[packet.Player]; + + int currentTimestamp = Environment.TickCount; + player.Latency = currentTimestamp - player.LastUpdateReceived; + player.LastUpdateReceived = currentTimestamp; + player.Health = packet.Health; player.Position = packet.Position.ToVector(); player.Rotation = packet.Rotation.ToVector(); @@ -338,6 +349,11 @@ namespace CoopClient if (Main.Players.ContainsKey(packet.Player)) { EntitiesPlayer player = Main.Players[packet.Player]; + + int currentTimestamp = Environment.TickCount; + player.Latency = currentTimestamp - player.LastUpdateReceived; + player.LastUpdateReceived = currentTimestamp; + player.Health = packet.Health; player.Position = packet.Position.ToVector(); player.VehicleModelHash = packet.VehModelHash; @@ -366,7 +382,9 @@ namespace CoopClient if (Main.Npcs.ContainsKey(packet.ID)) { EntitiesNpc npc = Main.Npcs[packet.ID]; + npc.LastUpdateReceived = Environment.TickCount; + npc.ModelHash = packet.ModelHash; npc.Props = packet.Props; npc.Health = packet.Health; @@ -390,6 +408,7 @@ namespace CoopClient Main.Npcs.Add(packet.ID, new EntitiesNpc() { LastUpdateReceived = Environment.TickCount, + ModelHash = packet.ModelHash, Props = packet.Props, Health = packet.Health, @@ -419,7 +438,9 @@ namespace CoopClient if (Main.Npcs.ContainsKey(packet.ID)) { EntitiesNpc npc = Main.Npcs[packet.ID]; + npc.LastUpdateReceived = Environment.TickCount; + npc.ModelHash = packet.ModelHash; npc.Props = packet.Props; npc.Health = packet.Health; @@ -444,6 +465,7 @@ namespace CoopClient Main.Npcs.Add(packet.ID, new EntitiesNpc() { LastUpdateReceived = Environment.TickCount, + ModelHash = packet.ModelHash, Props = packet.Props, Health = packet.Health, diff --git a/Client/Packets.cs b/Client/Packets.cs index e59f3da..7a5dd52 100644 --- a/Client/Packets.cs +++ b/Client/Packets.cs @@ -12,13 +12,34 @@ namespace CoopClient #region CLIENT-ONLY public static class VectorExtensions { + public static Vector3 ToVector(this Quaternion vec) + { + return new Vector3() + { + X = vec.X, + Y = vec.Y, + Z = vec.Z + }; + } + + public static Quaternion ToQuaternion(this Vector3 vec, float vW = 0.0f) + { + return new Quaternion() + { + X = vec.X, + Y = vec.Y, + Z = vec.Z, + W = vW + }; + } + public static LVector3 ToLVector(this Vector3 vec) { return new LVector3() { X = vec.X, Y = vec.Y, - Z = vec.Z, + Z = vec.Z }; } diff --git a/Client/PlayerList.cs b/Client/PlayerList.cs index 5412278..4a00e6f 100644 --- a/Client/PlayerList.cs +++ b/Client/PlayerList.cs @@ -8,40 +8,61 @@ using GTA.Native; namespace CoopClient { - public class PlayerList + public class PlayerList : Script { private readonly Scaleform MainScaleform = new Scaleform("mp_mm_card_freemode"); - public int Pressed { get; set; } + private int LastUpdate = Environment.TickCount; + public static int Pressed { get; set; } - public void Init(string localUsername) + public PlayerList() + { + Init(); + + Tick += OnTick; + } + + private void OnTick(object sender, EventArgs e) + { + if ((Environment.TickCount - LastUpdate) >= 1000) + { + Update(Main.Players, Main.MainSettings.Username); + } + + if (!Main.MainNetworking.IsOnServer()) + { + return; + } + + if ((Environment.TickCount - Pressed) < 5000 && !Main.MainChat.Focused && !Main.MainMenu.MenuPool.AreAnyVisible) + { + Function.Call(Hash.DRAW_SCALEFORM_MOVIE, MainScaleform.Handle, 0.122f, 0.3f, 0.28f, 0.6f, 255, 255, 255, 255, 0); + } + } + + private void Init() { MainScaleform.CallFunction("SET_DATA_SLOT_EMPTY", 0); - MainScaleform.CallFunction("SET_DATA_SLOT", 0, "", localUsername, 116, 0, 0, "", "", 2, "", "", ' '); + MainScaleform.CallFunction("SET_DATA_SLOT", 0, "", "Me", 116, 0, 0, "", "", 2, "", "", ' '); MainScaleform.CallFunction("SET_TITLE", "Player list", "1 players"); MainScaleform.CallFunction("DISPLAY_VIEW"); } - public void Update(Dictionary players, string LocalUsername) + private void Update(Dictionary players, string localUsername) { + LastUpdate = Environment.TickCount; + MainScaleform.CallFunction("SET_DATA_SLOT_EMPTY", 0); - MainScaleform.CallFunction("SET_DATA_SLOT", 0, "", LocalUsername, 116, 0, 0, "", "", 2, "", "", ' '); + MainScaleform.CallFunction("SET_DATA_SLOT", 0, "", localUsername, 116, 0, 0, "", "", 2, "", "", ' '); int i = 1; + foreach (KeyValuePair player in players) { - MainScaleform.CallFunction("SET_DATA_SLOT", i++, "", player.Value.Username, 116, 0, i - 1, "", "", 2, "", "", ' '); + MainScaleform.CallFunction("SET_DATA_SLOT", i++, player.Value.Latency + "ms", player.Value.Username, 116, 0, i - 1, "", "", 2, "", "", ' '); } MainScaleform.CallFunction("SET_TITLE", "Player list", (players.Count + 1) + " players"); MainScaleform.CallFunction("DISPLAY_VIEW"); } - - public void Tick() - { - if ((Environment.TickCount - Pressed) < 5000) - { - Function.Call(Hash.DRAW_SCALEFORM_MOVIE, MainScaleform.Handle, 0.122f, 0.3f, 0.28f, 0.6f, 255, 255, 255, 255, 0); - } - } } } diff --git a/Client/Util.cs b/Client/Util.cs index 28274f2..915d360 100644 --- a/Client/Util.cs +++ b/Client/Util.cs @@ -16,16 +16,15 @@ namespace CoopClient #region -- POINTER -- private static int SteeringAngleOffset { get; set; } + delegate ulong GetHandleAddressFuncDelegate(int handle); + static GetHandleAddressFuncDelegate GetEntityAddressFunc; + static unsafe byte* FindPattern(string pattern, string mask) { ProcessModule module = Process.GetCurrentProcess().MainModule; - return FindPattern(pattern, mask, module.BaseAddress, (ulong)module.ModuleMemorySize); - } - static unsafe byte* FindPattern(string pattern, string mask, IntPtr startAddress, ulong size) - { - ulong address = (ulong)startAddress.ToInt64(); - ulong endAddress = address + size; + ulong address = (ulong)module.BaseAddress.ToInt64(); + ulong endAddress = address + (ulong)module.ModuleMemorySize; for (; address < endAddress; address++) { @@ -52,12 +51,6 @@ namespace CoopClient address = FindPattern("\xE8\x00\x00\x00\x00\x48\x8B\xD8\x48\x85\xC0\x74\x2E\x48\x83\x3D", "x????xxxxxxxxxxx"); GetEntityAddressFunc = GetDelegateForFunctionPointer(new IntPtr(*(int*)(address + 1) + address + 5)); - // use the former pattern if the version is 1.0.1604.0 or newer - int gameVersion = (int)Game.Version; - address = gameVersion >= 46 ? - FindPattern("\xF3\x0F\x10\x9F\xD4\x08\x00\x00\x0F\x2F\xDF\x73\x0A", "xxxx????xxxxx") : - FindPattern("\xF3\x0F\x10\x8F\x68\x08\x00\x00\x88\x4D\x8C\x0F\x2F\xCF", "xxxx????xxx???"); - address = FindPattern("\x74\x0A\xF3\x0F\x11\xB3\x1C\x09\x00\x00\xEB\x25", "xxxxxx????xx"); if (address != null) { @@ -74,17 +67,9 @@ namespace CoopClient } } - delegate ulong GetHandleAddressFuncDelegate(int handle); - static GetHandleAddressFuncDelegate GetEntityAddressFunc; - - public static IntPtr GetEntityAddress(int handle) + public static unsafe void CustomSteeringAngle(int handle, float value) { - return new IntPtr((long)GetEntityAddressFunc(handle)); - } - - public static unsafe void CustomSteeringAngle(int Handle, float value) - { - IntPtr address = GetEntityAddress(Handle); + IntPtr address = new IntPtr((long)GetEntityAddressFunc(handle)); if (address == IntPtr.Zero || SteeringAngleOffset == 0) { return;