diff --git a/lib/cache/client/cache.lua b/lib/cache/client/cache.lua index 7c6fd1d..df493c8 100644 --- a/lib/cache/client/cache.lua +++ b/lib/cache/client/cache.lua @@ -1,15 +1,31 @@ Cache = Require("lib/cache/shared/cache.lua") +local PlayerPedId = PlayerPedId +local GetVehiclePedIsIn = GetVehiclePedIsIn +local GetVehicleMaxNumberOfPassengers = GetVehicleMaxNumberOfPassengers +local GetPedInVehicleSeat = GetPedInVehicleSeat +local GetPlayerServerId = GetPlayerServerId +local GetSelectedPedWeapon = GetSelectedPedWeapon +local PlayerId = PlayerId + + -- Comparison functions for client-side state + +--- Get the player's ped ID +---@return integer local function GetPed() return PlayerPedId() end +--- Get the vehicle the player is in, if any +---@return integer | nil local function GetVehicle() local ped = Cache.Get("Ped") -- Use cached ped for efficiency - if not ped then return end -- Or handle error/default case + if not ped then return end -- Or handle error/default case return GetVehiclePedIsIn(ped, false) end +--- Get the seat of the player in the vehicle if the vehicle is occupied +---@return integer | nil local function GetVehicleSeat() local vehicle = Cache.Get("Vehicle") if not vehicle then return end @@ -22,15 +38,26 @@ local function GetVehicleSeat() end end +---Get The Server ID of the player +---@return integer | nil +local function GetServerId() + local ped = PlayerId() + if not ped then return end -- Return unarmed hash if no ped + return GetPlayerServerId(ped) +end + +--- Get the current weapon of the player +--- @return integer | nil local function GetWeapon() local ped = Cache.Get("Ped") if not ped then return end -- Return unarmed hash if no ped return GetSelectedPedWeapon(ped) end -Cache.Create("Ped", GetPed, 1000) -- Check ped every second (adjust as needed) -Cache.Create("Vehicle", GetVehicle, 500) -- Check vehicle every 500ms -Cache.Create("Seat", GetVehicleSeat, 500) -- Check if in vehicle every 500ms -Cache.Create("Weapon", GetWeapon, 250) -- Check weapon every 250ms +Cache.Create("Ped", GetPed, 1000) -- Check ped every second (adjust as needed) +Cache.Create("Vehicle", GetVehicle, 500) -- Check vehicle every 500ms +Cache.Create("Seat", GetVehicleSeat, 500) -- Check if in vehicle every 500ms +Cache.Create("Weapon", GetWeapon, 250) -- Check weapon every 250ms +Cache.Create("ServerId", GetServerId, 1000) -- Check server ID every second -return Cache \ No newline at end of file +return Cache diff --git a/lib/cache/shared/cache.lua b/lib/cache/shared/cache.lua index 30e568f..f80526a 100644 --- a/lib/cache/shared/cache.lua +++ b/lib/cache/shared/cache.lua @@ -1,8 +1,20 @@ local Cache = {} +local Config = require("lib/settings/clientConfig.lua") +local max = 5000 +local CreateThread = CreateThread +local Wait = Wait +local GetGameTimer = GetGameTimer + + Cache.Caches = Cache.Caches or {} Cache.LoopRunning = Cache.LoopRunning or false -local max = 5000 +local function print(...) + if Config.Debug then + print("^2[Cache]^0", ...) + end +end + local function StartLoop() if Cache.LoopRunning then return end Cache.LoopRunning = true @@ -44,30 +56,65 @@ local function StartLoop() end function Cache.Create(name, compare, waitTime) - Cache.Caches[name] = Cache.Caches[name] or {} - Cache.Caches[name].WaitTime = waitTime or max - Cache.Caches[name].Compare = compare - Cache.Caches[name].OnChange = Cache.Caches[name].OnChange or {} - Cache.Caches[name].Value = compare() - print("Cache created: " .. name .. " with initial value: " .. tostring(Cache.Caches[name].Value)) - for _, onChange in pairs(Cache.Caches[name].OnChange) do - onChange(Cache.Caches[name].Value, nil) + assert(name, "Cache name is required.") + assert(compare, "Comparison function is required.") + assert(type(compare) == "function", "Comparison function must be a function.") + local _name = tostring(name) -- Ensure name is a string + local cache = Cache.Caches[_name] + if cache and cache.Compare == compare then + print(name .. " already exists with the same comparison function.") + return cache end - print("Cache created: " .. name .. " with initial value: " .. tostring(Cache.Caches[name].Value)) + cache = nil -- clean variable, so we can use the same name for the new cache + local s, r = pcall(function() -- Try to create the cache and catch any errors + local _cache = { + Name = _name, + Compare = compare, + WaitTime = waitTime or max, + LastChecked = nil, + OnChange = {}, + Value = compare() + } + Cache.Caches[_name] = _cache + print(_name .. " created with initial value: " .. tostring(Cache.Caches[_name].Value)) + for _, onChange in pairs(_cache.OnChange) do + onChange(_cache.Value, nil) + end + end) + if not s then + print("Error creating cache '" .. _name .. "': " .. tostring(r)) + return nil + end + return StartLoop() end function Cache.Get(name) - local cache = Cache.Caches[name] + assert(name, "Cache name is required.") + local _name = tostring(name) + local cache = Cache.Caches[_name] return cache and cache.Value or nil end function Cache.OnChange(name, onChange) - local cache = Cache.Caches[name] - assert(cache, "Cache with name '" .. name .. "' does not exist.") - table.insert(cache.OnChange, onChange) + assert(name, "Cache name is required.") + local _name = tostring(name) -- Ensure name is a string + local cache = Cache.Caches[_name] + assert(cache, "Cache with name '" .. _name .. "' does not exist.") + cache.OnChange[#cache.OnChange + 1] = onChange +end + +function Cache.Remove(name) + assert(name, "Cache name is required.") + local _name = tostring(name) -- Ensure name is a string + local cache = Cache.Caches[_name] + if cache then + Cache.Caches[_name] = nil + print(_name .. " removed from cache.") + if next(Cache.Caches) == nil then + Cache.LoopRunning = false -- Stop the loop if no caches are left + end + end end return Cache - - diff --git a/settings/clientConfig.lua b/settings/clientConfig.lua index 9fb6add..6da8aee 100644 --- a/settings/clientConfig.lua +++ b/settings/clientConfig.lua @@ -1,9 +1,12 @@ -BridgeClientConfig = {} -BridgeClientConfig.InputSystem = "auto" -- [ auto | ox_lib | qb-input ] -BridgeClientConfig.MenuSystem = "auto" -- [ auto | ox_lib | qb-menu ] -BridgeClientConfig.ProgressBarSystem = "auto" -- [ auto | ox_lib | progressbar ] -BridgeClientConfig.VehicleKey = "auto" -- [ auto | qb-vehiclekeys | MrNewbVehicleKeys | jaksams_VehiclesKeys | wasabi_carlock | mk_vehiclekeys | qbx_vehiclekeys | qs-vehiclekeys | t1ger_keys | Renewed-Vehiclekeys | mono_carkeys| cd_garage | okokGarage | F_RealCarKeysSystem -BridgeClientConfig.Fuel = "auto" -- [ auto | LegacyFuel | ox_fuel | ps-fuel | qs-fuelstations | Renewed-Fuel | ti_fuel | lc_fuel | x-fuel | cdn-fuel | esx-sna-fuel | BigDaddy-Fuel | okokGasStation ] -BridgeClientConfig.TargetSystem = "auto" -- [ auto | ox_target | qb-target | sleepless_interact ] -- sleepless is only partially supported and would need another system in place - -return BridgeClientConfig \ No newline at end of file +BridgeClientConfig = {} +BridgeClientConfig.InputSystem = "auto" -- [ auto | ox_lib | qb-input ] +BridgeClientConfig.MenuSystem = "auto" -- [ auto | ox_lib | qb-menu ] +BridgeClientConfig.ProgressBarSystem = "auto" -- [ auto | ox_lib | progressbar ] +BridgeClientConfig.VehicleKey = +"auto" -- [ auto | qb-vehiclekeys | MrNewbVehicleKeys | jaksams_VehiclesKeys | wasabi_carlock | mk_vehiclekeys | qbx_vehiclekeys | qs-vehiclekeys | t1ger_keys | Renewed-Vehiclekeys | mono_carkeys| cd_garage | okokGarage | F_RealCarKeysSystem +BridgeClientConfig.Fuel = +"auto" -- [ auto | LegacyFuel | ox_fuel | ps-fuel | qs-fuelstations | Renewed-Fuel | ti_fuel | lc_fuel | x-fuel | cdn-fuel | esx-sna-fuel | BigDaddy-Fuel | okokGasStation ] +BridgeClientConfig.TargetSystem = +"auto" -- [ auto | ox_target | qb-target | sleepless_interact ] -- sleepless is only partially supported and would need another system in place +BridgeClientConfig.Debug = false +return BridgeClientConfig