From 42fe204615a70e560dc6d6586853bd30d2ad1a0e Mon Sep 17 00:00:00 2001 From: JericoFX Date: Mon, 12 May 2025 11:46:47 -0300 Subject: [PATCH 1/4] Refactor cLib initialization and modularize server/client libraries --- init.lua | 33 +++++++++++------ lib/init.lua | 100 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 109 insertions(+), 24 deletions(-) diff --git a/init.lua b/init.lua index 8db2673..d9818c8 100644 --- a/init.lua +++ b/init.lua @@ -22,7 +22,7 @@ function Bridge.RegisterModule(moduleName, moduleTable) end Bridge[moduleName] = wrappedModule _ENV[moduleName] = wrappedModule -- Add to the ENV table the modules so is more easy and safe to call from inside - _ENV.Bridge = Bridge -- Add the bridge too to the _ENV + _ENV.Bridge = Bridge -- Add the bridge too to the _ENV exports(moduleName, function() return wrappedModule @@ -31,19 +31,26 @@ function Bridge.RegisterModule(moduleName, moduleTable) --trigger update object event TriggerEvent("Bridge:Refresh", moduleName, wrappedModule) end +CreateThread(function() -- lets try to load the lib first to use it on the rest of the modules + for k, v in pairs(cLib) do + if v then + Bridge.RegisterModule(k, v) + end + end +end) exports("RegisterModule", Bridge.RegisterModule) --TODO: Create a way to overide functions or create a new functions for module function Bridge.RegisterModuleFunction(moduleName, functionName, func) - assert(moduleName and functionName and func, string.format("Bridge.RegisterModuleFunction(%s, %s, %s) - Invalid arguments", moduleName, functionName, func)) + assert(moduleName and functionName and func, + string.format("Bridge.RegisterModuleFunction(%s, %s, %s) - Invalid arguments", moduleName, functionName, func)) Bridge[moduleName] = Bridge[moduleName] or {} Bridge[moduleName][functionName] = func --trigger update object event TriggerEvent("Bridge:Refresh", moduleName, Bridge[moduleName]) end - --Bridge Bridge.RegisterModule("Framework", Framework) Bridge.RegisterModule("Inventory", Inventory) @@ -75,23 +82,27 @@ Bridge.RegisterModule("Require", Require) -- Bridge.RegisterModule("Skills", Skills) -for k, v in pairs(cLib) do - if v then - Bridge.RegisterModule(k, v) + + +--[[ CreateThread(function() -- using metatables + for k, v in pairs(cLib.loaded) do + if v then + Bridge.RegisterModule(k, v) + end end -end +end) ]] exports('Bridge', function() return Bridge end) --- ▄▀▀ ██▀ █▀▄ █ █ ██▀ █▀▄ --- ▄█▀ █▄▄ █▀▄ ▀▄▀ █▄▄ █▀▄ +-- ▄▀▀ ██▀ █▀▄ █ █ ██▀ █▀▄ +-- ▄█▀ █▄▄ █▀▄ ▀▄▀ █▄▄ █▀▄ if not IsDuplicityVersion() then goto client end Bridge.RegisterModule('Version', Version) --- ▄▀▀ █ █ ██▀ █▄ █ ▀█▀ --- ▀▄▄ █▄▄ █ █▄▄ █ ▀█ █ +-- ▄▀▀ █ █ ██▀ █▄ █ ▀█▀ +-- ▀▄▄ █▄▄ █ █▄▄ █ ▀█ █ if IsDuplicityVersion() then return end ::client:: diff --git a/lib/init.lua b/lib/init.lua index 7aa6a34..9db3466 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -1,4 +1,5 @@ loadedModules = {} +local cLib = {} function Require(modulePath, resourceName) if resourceName and type(resourceName) ~= "string" then @@ -35,23 +36,95 @@ function Require(modulePath, resourceName) return result end +local function getServerLib() + return { + SQL = SQL or Require("lib/sql/server/sqlHandler.lua"), + Logs = Logs or Require("lib/logs/server/logs.lua"), + ItemsBuilder = ItemsBuilder or Require("lib/generators/server/ItemsBuilder.lua"), + LootTables = LootTables or Require("lib/generators/server/lootTables.lua"), + Cache = Cache or Require("lib/cache/shared/cache.lua"), + ServerEntity = ServerEntity or Require("lib/entities/server/server_entity.lua"), + Marker = Marker or Require("lib/markers/server/server.lua"), + Particle = Particle or Require("lib/particles/server/particles.lua"), + } +end +local function getClientLib() + return { + Require = Require, + Gizmo = Gizmo or Require("lib/placers/client/gizmo.lua"), + Scaleform = Scaleform or Require("lib/scaleform/client/scaleform.lua"), + Placeable = Placeable or Require("lib/placers/client/object_placer.lua"), + Utility = Utility or Require("lib/utility/client/utility.lua"), + PlaceableObject = PlaceableObject or Require("lib/placers/client/placeable_object.lua"), + Raycast = Raycast or Require("lib/raycast/client/raycast.lua"), + Point = Point or Require("lib/points/client/points.lua"), + Particle = Particle or Require("lib/particles/client/particles.lua"), + Cache = Cache or Require("lib/cache/client/cache.lua"), + ClientEntity = ClientEntity or Require("lib/entities/client/client_entity.lua"), + ClientEntityActions = ClientEntityActions or Require("lib/entities/client/client_entity_actions.lua"), + ClientStateBag = ClientStateBag or Require("lib/statebags/client/client.lua"), + Marker = Marker or Require("lib/markers/client/markers.lua"), + Anim = Anim or Require("lib/anim/client/client.lua"), + Cutscene = Cutscene or Require("lib/cutscenes/client/cutscene.lua"), + DUI = DUI or Require("lib/dui/client/dui.lua"), + } +end -cLib = { - Require = Require, - Callback = Callback or Require("lib/utility/shared/callbacks.lua"), - Ids = Ids or Require("lib/utility/shared/ids.lua"), - ReboundEntities = ReboundEntities or Require("lib/utility/shared/rebound_entities.lua"), - Tables = Tables or Require("lib/utility/shared/tables.lua"), - Prints = Prints or Require("lib/utility/shared/prints.lua"), - Math = Math or Require("lib/utility/shared/math.lua"), - LA = LA or Require("lib/utility/shared/la.lua"), - Perlin = Perlin or Require("lib/utility/shared/perlin.lua"), - -- Action = Action or Require("lib/entities/shared/actions.lua"), -} +local function getSharedLib() + return { + Require = Require, + Callback = Callback or Require("lib/utility/shared/callbacks.lua"), + Ids = Ids or Require("lib/utility/shared/ids.lua"), + ReboundEntities = ReboundEntities or Require("lib/utility/shared/rebound_entities.lua"), + Tables = Tables or Require("lib/utility/shared/tables.lua"), + Prints = Prints or Require("lib/utility/shared/prints.lua"), + Math = Math or Require("lib/utility/shared/math.lua"), + LA = LA or Require("lib/utility/shared/la.lua"), + Perlin = Perlin or Require("lib/utility/shared/perlin.lua"), + -- Action = Action or Require("lib/entities/shared/actions.lua"), + } +end -exports('cLib', cLib) +cLib = setmetatable(getSharedLib(), { __index = IsDuplicityVersion() and getServerLib() or getClientLib() }) +_ENV.cLib = cLib +_ENV.Require = Require +exports('cLib', function() + return cLib +end) +--[[ _ENV.cLib = setmetatable({ + context = not IsDuplicityVersion() and "client" or "server", + resource = GetCurrentResourceName(), + loaded = {} +}, { + __newindex = function(self, key, value) + return error("Attempt to modify read-only table: " .. key) + end, + __index = function(self, key) -- modify the index function to load the library modules + if self.context == "server" then + return rawget(self.loaded, key) + else + return rawget(self.loaded, key) + end + end, + __call = function(self) -- modify the call function to load the library modules + if self.context == "server" then + if not next(self.loaded) then + self.loaded = getServerLib() + return true + end + else + if not next(self.loaded) then + self.loaded = getClientLib() + return true + end + end + end, +}) +cLib() -- load the library modules ]] + +--[[ if not IsDuplicityVersion() then goto client end cLib.SQL = SQL or Require("lib/sql/server/sqlHandler.lua") @@ -84,3 +157,4 @@ cLib.DUI = DUI or Require("lib/dui/client/dui.lua") cLib.Particle = Particle or Require("lib/particles/client/particles.lua") return cLib + ]] From 68aa645494d27f9dde8522a58b01afea687d9d26 Mon Sep 17 00:00:00 2001 From: JericoFX Date: Mon, 12 May 2025 12:13:16 -0300 Subject: [PATCH 2/4] Refactor global environment setup by removing redundant assignments to _ENV --- init.lua | 14 +++++++++++++- lib/init.lua | 2 -- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/init.lua b/init.lua index d9818c8..6a1c9cc 100644 --- a/init.lua +++ b/init.lua @@ -22,7 +22,7 @@ function Bridge.RegisterModule(moduleName, moduleTable) end Bridge[moduleName] = wrappedModule _ENV[moduleName] = wrappedModule -- Add to the ENV table the modules so is more easy and safe to call from inside - _ENV.Bridge = Bridge -- Add the bridge too to the _ENV + exports(moduleName, function() return wrappedModule @@ -31,6 +31,7 @@ function Bridge.RegisterModule(moduleName, moduleTable) --trigger update object event TriggerEvent("Bridge:Refresh", moduleName, wrappedModule) end + CreateThread(function() -- lets try to load the lib first to use it on the rest of the modules for k, v in pairs(cLib) do if v then @@ -38,6 +39,17 @@ CreateThread(function() -- lets try to load the lib first to use it on the rest end end end) + +--- @module GlobalEnvironment +--- @field cLib table # Core library utilities, available globally after initialization. +--- @field Require fun(module: string): any # Function to require/load modules by name. + +--- Global environment setup. +-- The following assignments expose core utilities globally, +-- making them accessible in all subsequently loaded modules. + +_ENV.cLib = cLib ~= nil and cLib or export.community_lib.cLib() ---@type table +_ENV.Require = Require ---@type fun(module: string): any exports("RegisterModule", Bridge.RegisterModule) --TODO: Create a way to overide functions or create a new functions for module diff --git a/lib/init.lua b/lib/init.lua index 9db3466..4003b66 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -87,8 +87,6 @@ local function getSharedLib() end cLib = setmetatable(getSharedLib(), { __index = IsDuplicityVersion() and getServerLib() or getClientLib() }) -_ENV.cLib = cLib -_ENV.Require = Require exports('cLib', function() return cLib end) From 3ea66043ea71b862d6c17fc2da8936bce8f398ff Mon Sep 17 00:00:00 2001 From: JericoFX Date: Mon, 12 May 2025 14:24:32 -0300 Subject: [PATCH 3/4] Enhance cache module with improved type annotations and error handling; allow nil wait times for dynamic caching --- lib/cache/client/cache.lua | 10 +-- lib/cache/shared/cache.lua | 121 ++++++++++++++++++++++++++----------- 2 files changed, 92 insertions(+), 39 deletions(-) diff --git a/lib/cache/client/cache.lua b/lib/cache/client/cache.lua index df493c8..b9aefdc 100644 --- a/lib/cache/client/cache.lua +++ b/lib/cache/client/cache.lua @@ -54,10 +54,10 @@ local function GetWeapon() 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("ServerId", GetServerId, 1000) -- Check server ID every second +Cache.Create("Ped", GetPed, nil) -- 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, nil) -- Check server ID every second return Cache diff --git a/lib/cache/shared/cache.lua b/lib/cache/shared/cache.lua index ed42a50..c6fe0b1 100644 --- a/lib/cache/shared/cache.lua +++ b/lib/cache/shared/cache.lua @@ -1,29 +1,54 @@ -local Cache = {} +---@class CacheEntry +---@field Name string +---@field Compare fun(): any +---@field WaitTime integer +---@field LastChecked integer|nil +---@field OnChange fun(new:any, old:any)[] +---@field Value any + +---@class CacheModule +---@field Caches table +---@field LoopRunning boolean +---@field Create fun(name:string, compare:fun():any, waitTime?:integer): CacheEntry +---@field Get fun(name:string): any +---@field OnChange fun(name:string, onChange:fun(new:any, old:any)) +---@field Remove fun(name:string) +local Cache = {} ---@type CacheModule + local Config = Require("settings/sharedConfig.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 function print(...) +---@param ... any +local function debugPrint(...) if Config.DebugLevel == 0 then return end print("^2[Cache]^0", ...) end +local function HasActiveCaches() + for _, cache in pairs(Cache.Caches) do + if cache.WaitTime ~= nil then + return true + end + end + return false +end + local function StartLoop() if Cache.LoopRunning then return end + if not HasActiveCaches() then return end Cache.LoopRunning = true CreateThread(function() while Cache.LoopRunning do local now = GetGameTimer() local minWait = nil - local caches = Cache.Caches - for name, cache in pairs(caches) do - if cache.Compare then + for name, cache in pairs(Cache.Caches) do + if cache.Compare and cache.WaitTime ~= nil then cache.LastChecked = cache.LastChecked or now cache.WaitTime = cache.WaitTime or max local elapsed = now - cache.LastChecked @@ -32,8 +57,8 @@ local function StartLoop() local oldValue = cache.Value cache.Value = cache.Compare() if cache.Value ~= oldValue and cache.OnChange then - for _, onChange in pairs(cache.OnChange) do - onChange(cache.Value, oldValue) + for i = 1, #cache.OnChange do + cache.OnChange[i](cache.Value, oldValue) end end cache.LastChecked = now @@ -50,44 +75,53 @@ local function StartLoop() else Wait(max) end + -- if there is no cache valid, stop the loop + if not HasActiveCaches() then + Cache.LoopRunning = false + end end end) end +---@param name string +---@param compare fun():any +---@param waitTime? integer +---@return CacheEntry function Cache.Create(name, compare, waitTime) 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 _name = tostring(name) local cache = Cache.Caches[_name] if cache and cache.Compare == compare then - print(name .. " already exists with the same comparison function.") + debugPrint(_name .. " already exists with the same comparison function.") return cache end - 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)) + local ok, result = pcall(compare) + if not ok then + debugPrint("Error creating cache '" .. _name .. "': " .. tostring(result)) return nil end - - return StartLoop() + ---@type CacheEntry + local newCache = { + Name = _name, + Compare = compare, + WaitTime = waitTime, -- can be nil + LastChecked = nil, + OnChange = {}, + Value = result + } + Cache.Caches[_name] = newCache + debugPrint(_name .. " created with initial value: " .. tostring(result)) + for i = 1, #newCache.OnChange do + newCache.OnChange[i](newCache.Value, nil) + end + StartLoop() + return newCache end +---@param name string +---@return any function Cache.Get(name) assert(name, "Cache name is required.") local _name = tostring(name) @@ -95,25 +129,44 @@ function Cache.Get(name) return cache and cache.Value or nil end +---@param name string +---@param onChange fun(new:any, old:any) function Cache.OnChange(name, onChange) assert(name, "Cache name is required.") - local _name = tostring(name) -- Ensure name is a string + local _name = tostring(name) local cache = Cache.Caches[_name] assert(cache, "Cache with name '" .. _name .. "' does not exist.") cache.OnChange[#cache.OnChange + 1] = onChange end +---@param name string function Cache.Remove(name) assert(name, "Cache name is required.") - local _name = tostring(name) -- Ensure name is a string + local _name = tostring(name) local cache = Cache.Caches[_name] if cache then Cache.Caches[_name] = nil - print(_name .. " removed from cache.") + debugPrint(_name .. " removed from cache.") if next(Cache.Caches) == nil then - Cache.LoopRunning = false -- Stop the loop if no caches are left + Cache.LoopRunning = false end end end +setmetatable(Cache, { + __index = function(self, key) + local cache = self.Caches[key] + if cache then + return cache.Value + else + return rawget(self, key) + end + end, + __call = function(self, key) + return self.Get(self, key) + end, +}) + +-- with this change now we can use Cache("name") to get the value or simple Cache.name (Cache.Get("name")) + return Cache From c1c5626df365d99d278b82d2ef0523622f90e6af Mon Sep 17 00:00:00 2001 From: JericoFX Date: Mon, 12 May 2025 14:31:33 -0300 Subject: [PATCH 4/4] add update function to cache --- lib/cache/shared/cache.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/cache/shared/cache.lua b/lib/cache/shared/cache.lua index c6fe0b1..05b5359 100644 --- a/lib/cache/shared/cache.lua +++ b/lib/cache/shared/cache.lua @@ -139,6 +139,22 @@ function Cache.OnChange(name, onChange) cache.OnChange[#cache.OnChange + 1] = onChange end +---@param name string +---@param newValue any +function Cache.Update(name, newValue) + assert(name, "Cache name is required.") + local _name = tostring(name) + local cache = Cache.Caches[_name] + assert(cache, "Cache with name '" .. _name .. "' does not exist.") + local oldValue = cache.Value + if oldValue ~= newValue then + cache.Value = newValue + for i = 1, #cache.OnChange do + cache.OnChange[i](newValue, oldValue) + end + end +end + ---@param name string function Cache.Remove(name) assert(name, "Cache name is required.")