From 42fe204615a70e560dc6d6586853bd30d2ad1a0e Mon Sep 17 00:00:00 2001 From: JericoFX Date: Mon, 12 May 2025 11:46:47 -0300 Subject: [PATCH] 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 + ]]