From 8fc16725ff8f01754f3bb9b78ee978c2ed81c4b8 Mon Sep 17 00:00:00 2001 From: EntenKoeniq <81123713+EntenKoeniq@users.noreply.github.com> Date: Fri, 19 Nov 2021 22:08:15 +0100 Subject: [PATCH] Use TickCount64 instead of TickCount --- Client/Chat.cs | 6 +++--- Client/Entities/EntitiesPed.cs | 10 +++++----- Client/Entities/EntitiesThread.cs | 2 +- Client/Interface.cs | 5 ++--- Client/Main.cs | 18 +++++++++--------- Client/Networking.cs | 26 +++++++++++++------------- Client/PlayerList.cs | 10 +++++----- Client/Util.cs | 8 ++++++-- 8 files changed, 44 insertions(+), 41 deletions(-) diff --git a/Client/Chat.cs b/Client/Chat.cs index f20300c..30c422f 100644 --- a/Client/Chat.cs +++ b/Client/Chat.cs @@ -31,7 +31,7 @@ namespace CoopClient } } - private int LastMessageTime { get; set; } + private ulong LastMessageTime { get; set; } private bool CurrentHidden { get; set; } private bool Hidden @@ -73,7 +73,7 @@ namespace CoopClient public void Tick() { - if ((Environment.TickCount - LastMessageTime) > 15000 && !Focused && !Hidden) + if ((Util.GetTickCount64() - LastMessageTime) > 15000 && !Focused && !Hidden) { Hidden = true; } @@ -94,7 +94,7 @@ namespace CoopClient public void AddMessage(string sender, string msg) { MainScaleForm.CallFunction("ADD_MESSAGE", sender + ":", msg); - LastMessageTime = Environment.TickCount; + LastMessageTime = Util.GetTickCount64(); Hidden = false; } diff --git a/Client/Entities/EntitiesPed.cs b/Client/Entities/EntitiesPed.cs index 7eae87a..b2bc9ec 100644 --- a/Client/Entities/EntitiesPed.cs +++ b/Client/Entities/EntitiesPed.cs @@ -15,7 +15,7 @@ namespace CoopClient { private bool AllDataAvailable = false; public bool LastSyncWasFull { get; set; } = false; - public long LastUpdateReceived { get; set; } + public ulong LastUpdateReceived { get; set; } public float Latency { get; set; } public Ped Character { get; set; } @@ -44,7 +44,7 @@ namespace CoopClient public Blip PedBlip; #region -- IN VEHICLE -- - private int VehicleStopTime { get; set; } + private ulong VehicleStopTime { get; set; } public bool IsInVehicle { get; set; } public int VehicleModelHash { get; set; } @@ -450,11 +450,11 @@ namespace CoopClient MainVehicle.Velocity = VehicleVelocity + forceMultiplier * (VehiclePosition - MainVehicle.Position); MainVehicle.Quaternion = Quaternion.Slerp(MainVehicle.Quaternion, VehicleRotation, 0.5f); - VehicleStopTime = Environment.TickCount; + VehicleStopTime = Util.GetTickCount64(); } - else if ((Environment.TickCount - VehicleStopTime) <= 1000) + else if ((Util.GetTickCount64() - 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), Util.GetTickCount64() - VehicleStopTime, 1000); MainVehicle.PositionNoOffset = posTarget; MainVehicle.Quaternion = Quaternion.Slerp(MainVehicle.Quaternion, VehicleRotation, 0.5f); diff --git a/Client/Entities/EntitiesThread.cs b/Client/Entities/EntitiesThread.cs index f769fc7..c6b4f42 100644 --- a/Client/Entities/EntitiesThread.cs +++ b/Client/Entities/EntitiesThread.cs @@ -26,7 +26,7 @@ namespace CoopClient.Entities { localNpcs = new Dictionary(Main.Npcs); - int tickCount = Environment.TickCount; + ulong tickCount = Util.GetTickCount64(); foreach (KeyValuePair npc in new Dictionary(localNpcs)) { if ((tickCount - npc.Value.LastUpdateReceived) > 3000) diff --git a/Client/Interface.cs b/Client/Interface.cs index 088f487..2c46403 100644 --- a/Client/Interface.cs +++ b/Client/Interface.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -95,7 +94,7 @@ namespace CoopClient public static bool IsPlayerListVisible() { - return Environment.TickCount - PlayerList.Pressed < 5000; + return Util.GetTickCount64() - PlayerList.Pressed < 5000; } public static string GetCurrentVersion() diff --git a/Client/Main.cs b/Client/Main.cs index e13fd90..4cb419c 100644 --- a/Client/Main.cs +++ b/Client/Main.cs @@ -51,7 +51,7 @@ namespace CoopClient Util.NativeMemory(); } - private int LastDataSend; + private ulong LastDataSend; private void OnTick(object sender, EventArgs e) { if (Game.IsLoading) @@ -104,14 +104,14 @@ namespace CoopClient } #endif - if ((Environment.TickCount - LastDataSend) < (1000 / 60)) + if ((Util.GetTickCount64() - LastDataSend) < (1000 / 60)) { return; } MainNetworking.SendPlayerData(); - LastDataSend = Environment.TickCount; + LastDataSend = Util.GetTickCount64(); } #if !NON_INTERACTIVE @@ -164,7 +164,7 @@ namespace CoopClient { if (MainNetworking.IsOnServer()) { - int currentTimestamp = Environment.TickCount; + ulong currentTimestamp = Util.GetTickCount64(); PlayerList.Pressed = (currentTimestamp - PlayerList.Pressed) < 5000 ? (currentTimestamp - 6000) : currentTimestamp; } return; @@ -217,9 +217,9 @@ namespace CoopClient } #if DEBUG - private int ArtificialLagCounter; + private ulong ArtificialLagCounter; public static EntitiesPlayer DebugSyncPed; - public static int LastFullDebugSync = 0; + public static ulong LastFullDebugSync = 0; public static bool UseDebug = false; private void Debug() @@ -231,12 +231,12 @@ namespace CoopClient DebugSyncPed = Players[0]; } - if ((Environment.TickCount - ArtificialLagCounter) < 157) + if ((Util.GetTickCount64() - ArtificialLagCounter) < 157) { return; } - bool fullSync = (Environment.TickCount - LastFullDebugSync) > 1500; + bool fullSync = (Util.GetTickCount64() - LastFullDebugSync) > 1500; if (fullSync) { @@ -316,7 +316,7 @@ namespace CoopClient } } - int currentTimestamp = Environment.TickCount; + ulong currentTimestamp = Util.GetTickCount64(); DebugSyncPed.LastUpdateReceived = currentTimestamp; DebugSyncPed.Latency = currentTimestamp - ArtificialLagCounter; diff --git a/Client/Networking.cs b/Client/Networking.cs index 34ce858..b6ca7ed 100644 --- a/Client/Networking.cs +++ b/Client/Networking.cs @@ -256,7 +256,7 @@ namespace CoopClient { SocialClubName = packet.SocialClubName, Username = packet.Username, - LastUpdateReceived = Environment.TickCount + LastUpdateReceived = Util.GetTickCount64() }; Main.Players.Add(packet.ID, player); @@ -304,7 +304,7 @@ namespace CoopClient player.IsInVehicle = false; player.Latency = packet.Extra.Latency; - player.LastUpdateReceived = Environment.TickCount; + player.LastUpdateReceived = Util.GetTickCount64(); } } @@ -340,7 +340,7 @@ namespace CoopClient player.VehicleDead = (packet.Flag.Value & (byte)VehicleDataFlags.IsDead) > 0; player.Latency = packet.Extra.Latency; - player.LastUpdateReceived = Environment.TickCount; + player.LastUpdateReceived = Util.GetTickCount64(); } } @@ -367,7 +367,7 @@ namespace CoopClient player.IsInVehicle = false; player.Latency = packet.Extra.Latency; - player.LastUpdateReceived = Environment.TickCount; + player.LastUpdateReceived = Util.GetTickCount64(); } } @@ -395,7 +395,7 @@ namespace CoopClient player.VehicleDead = (packet.Flag.Value & (byte)VehicleDataFlags.IsDead) > 0; player.Latency = packet.Extra.Latency; - player.LastUpdateReceived = Environment.TickCount; + player.LastUpdateReceived = Util.GetTickCount64(); } } @@ -409,7 +409,7 @@ namespace CoopClient player.Position = packet.Extra.Position.ToVector(); player.Latency = packet.Extra.Latency; - player.LastUpdateReceived = Environment.TickCount; + player.LastUpdateReceived = Util.GetTickCount64(); } } @@ -463,7 +463,7 @@ namespace CoopClient { EntitiesNpc npc = Main.Npcs[packet.ID]; - npc.LastUpdateReceived = Environment.TickCount; + npc.LastUpdateReceived = Util.GetTickCount64(); npc.ModelHash = packet.ModelHash; npc.Props = packet.Props; @@ -487,7 +487,7 @@ namespace CoopClient { Main.Npcs.Add(packet.ID, new EntitiesNpc() { - LastUpdateReceived = Environment.TickCount, + LastUpdateReceived = Util.GetTickCount64(), ModelHash = packet.ModelHash, Props = packet.Props, @@ -519,7 +519,7 @@ namespace CoopClient { EntitiesNpc npc = Main.Npcs[packet.ID]; - npc.LastUpdateReceived = Environment.TickCount; + npc.LastUpdateReceived = Util.GetTickCount64(); npc.ModelHash = packet.ModelHash; npc.Props = packet.Props; @@ -549,7 +549,7 @@ namespace CoopClient { Main.Npcs.Add(packet.ID, new EntitiesNpc() { - LastUpdateReceived = Environment.TickCount, + LastUpdateReceived = Util.GetTickCount64(), ModelHash = packet.ModelHash, Props = packet.Props, @@ -582,7 +582,7 @@ namespace CoopClient #endregion #region -- SEND -- - private int LastPlayerFullSync = 0; + private ulong LastPlayerFullSync = 0; public void SendPlayerData() { Ped player = Game.Player.Character; @@ -590,7 +590,7 @@ namespace CoopClient NetOutgoingMessage outgoingMessage = Client.CreateMessage(); NetDeliveryMethod messageType; - if ((Environment.TickCount - LastPlayerFullSync) > 500) + if ((Util.GetTickCount64() - LastPlayerFullSync) > 500) { messageType = NetDeliveryMethod.UnreliableSequenced; @@ -681,7 +681,7 @@ namespace CoopClient }.PacketToNetOutGoingMessage(outgoingMessage); } - LastPlayerFullSync = Environment.TickCount; + LastPlayerFullSync = Util.GetTickCount64(); } else { diff --git a/Client/PlayerList.cs b/Client/PlayerList.cs index 08a23e3..eed62d5 100644 --- a/Client/PlayerList.cs +++ b/Client/PlayerList.cs @@ -11,8 +11,8 @@ namespace CoopClient public class PlayerList : Script { private readonly Scaleform MainScaleform = new Scaleform("mp_mm_card_freemode"); - private int LastUpdate = Environment.TickCount; - public static int Pressed { get; set; } + private ulong LastUpdate = Util.GetTickCount64(); + public static ulong Pressed { get; set; } public PlayerList() { @@ -28,12 +28,12 @@ namespace CoopClient return; } - if ((Environment.TickCount - LastUpdate) >= 1000) + if ((Util.GetTickCount64() - LastUpdate) >= 1000) { Update(Main.Players, Main.MainSettings.Username); } - if ((Environment.TickCount - Pressed) < 5000 && !Main.MainChat.Focused + if ((Util.GetTickCount64() - Pressed) < 5000 && !Main.MainChat.Focused #if !NON_INTERACTIVE && !Main.MainMenu.MenuPool.AreAnyVisible #endif @@ -53,7 +53,7 @@ namespace CoopClient private void Update(Dictionary players, string localUsername) { - LastUpdate = Environment.TickCount; + LastUpdate = Util.GetTickCount64(); MainScaleform.CallFunction("SET_DATA_SLOT_EMPTY", 0); MainScaleform.CallFunction("SET_DATA_SLOT", 0, $"{Main.MainNetworking.Latency * 1000:N0}ms", localUsername, 116, 0, 0, "", "", 2, "", "", ' '); diff --git a/Client/Util.cs b/Client/Util.cs index 00be56c..0718042 100644 --- a/Client/Util.cs +++ b/Client/Util.cs @@ -3,6 +3,7 @@ using System.IO; using System.Xml.Serialization; using System.Collections.Generic; using System.Diagnostics; +using System.Runtime.InteropServices; using static System.Runtime.InteropServices.Marshal; using GTA; @@ -104,7 +105,7 @@ namespace CoopClient return Comparer.Default.Compare(item, start) >= 0 && Comparer.Default.Compare(item, end) <= 0; } - public static Vector3 LinearVectorLerp(Vector3 start, Vector3 end, int currentTime, int duration) + public static Vector3 LinearVectorLerp(Vector3 start, Vector3 end, ulong currentTime, int duration) { return new Vector3() { @@ -114,7 +115,7 @@ namespace CoopClient }; } - public static float LinearFloatLerp(float start, float end, int currentTime, int duration) + public static float LinearFloatLerp(float start, float end, ulong currentTime, int duration) { return (end - start) * currentTime / duration + start; } @@ -468,5 +469,8 @@ namespace CoopClient return camPos + dir * raycastToDist; } + + [DllImport("kernel32.dll")] + public static extern ulong GetTickCount64(); } }