From 71be624697dcce392991ac57e5e9c71221e049ba Mon Sep 17 00:00:00 2001 From: Kenshin13 <63159154+Kenshiin13@users.noreply.github.com> Date: Wed, 1 Jan 2025 19:35:15 +0100 Subject: [PATCH 1/4] feat(es_extended/imports): add require --- [core]/es_extended/imports.lua | 64 ++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/[core]/es_extended/imports.lua b/[core]/es_extended/imports.lua index 2727995d..80f1438a 100644 --- a/[core]/es_extended/imports.lua +++ b/[core]/es_extended/imports.lua @@ -1,4 +1,5 @@ ESX = exports["es_extended"]:getSharedObject() +_resourceName = GetCurrentResourceName() OnPlayerData = function (key, val, last) end @@ -42,3 +43,66 @@ if not IsDuplicityVersion() then -- Only register this event for the client end end end + +local cachedModules = {} ---@type table +local loadingModules = {} ---@type table + +---@param modulePath string +---@return string +local function getResourceNameFromModulePath(modulePath) + local externalResourceName = modulePath:match("^@(.-)%.") + if externalResourceName then + return externalResourceName + end + + return _resourceName +end + +---@param modulePath string +---@return string, number +local function getModuleFilePath(modulePath) + if modulePath:sub(1, 1) == "@" then + modulePath = modulePath:sub(modulePath:find("%.") + 1) + end + + return modulePath:gsub("%.", "/") +end + +---@param modulePath string +---@return any +function require(modulePath) + assert(type(modulePath) == "string", "Module name must be a string") + + if loadingModules[modulePath] then + error(("Circular dependency detected for module '%s'."):format(modulePath)) + end + + if cachedModules[modulePath] then + return cachedModules[modulePath] + end + + loadingModules[modulePath] = true + + local resourceName = getResourceNameFromModulePath(modulePath) + local moduleFilePath = getModuleFilePath(modulePath) + local moduleFileContent = LoadResourceFile(resourceName, moduleFilePath .. ".lua") + + if not moduleFileContent then + loadingModules[modulePath] = nil + error(("Module '%s' not found in resource '%s'."):format(moduleFilePath, resourceName)) + end + + local chunk, err = load(moduleFileContent, ("@%s/%s"):format(resourceName, moduleFilePath), "t") + + if not chunk then + loadingModules[modulePath] = nil + error(("Failed to load module '%s': %s"):format(moduleFilePath, err)) + end + + local result = chunk() + + cachedModules[modulePath] = result ~= nil and result or true + loadingModules[modulePath] = nil + + return result +end From 7b01753274fbfb2837230eb6ba359596576dba26 Mon Sep 17 00:00:00 2001 From: Kenshin13 <63159154+Kenshiin13@users.noreply.github.com> Date: Wed, 1 Jan 2025 19:54:07 +0100 Subject: [PATCH 2/4] fix(es_extended/imports): fix typo --- [core]/es_extended/imports.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/[core]/es_extended/imports.lua b/[core]/es_extended/imports.lua index 80f1438a..ea7bf8f3 100644 --- a/[core]/es_extended/imports.lua +++ b/[core]/es_extended/imports.lua @@ -71,7 +71,7 @@ end ---@param modulePath string ---@return any function require(modulePath) - assert(type(modulePath) == "string", "Module name must be a string") + assert(type(modulePath) == "string", "Module path must be a string") if loadingModules[modulePath] then error(("Circular dependency detected for module '%s'."):format(modulePath)) From 354b46a005131f901d41f61d46f4971c7620240c Mon Sep 17 00:00:00 2001 From: Kenshin13 <63159154+Kenshiin13@users.noreply.github.com> Date: Wed, 1 Jan 2025 20:03:07 +0100 Subject: [PATCH 3/4] feat(es_extended/imports): avoid overwriting ox_lib require --- [core]/es_extended/imports.lua | 98 ++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/[core]/es_extended/imports.lua b/[core]/es_extended/imports.lua index ea7bf8f3..d28c05a4 100644 --- a/[core]/es_extended/imports.lua +++ b/[core]/es_extended/imports.lua @@ -44,65 +44,69 @@ if not IsDuplicityVersion() then -- Only register this event for the client end end -local cachedModules = {} ---@type table -local loadingModules = {} ---@type table +if not lib?.require then + local cachedModules = {} ---@type table + local loadingModules = {} ---@type table ----@param modulePath string ----@return string -local function getResourceNameFromModulePath(modulePath) - local externalResourceName = modulePath:match("^@(.-)%.") - if externalResourceName then - return externalResourceName + ---@param modulePath string + ---@return string + local function getResourceNameFromModulePath(modulePath) + local externalResourceName = modulePath:match("^@(.-)%.") + if externalResourceName then + return externalResourceName + end + + return _resourceName end - return _resourceName -end + ---@param modulePath string + ---@return string, number + local function getModuleFilePath(modulePath) + if modulePath:sub(1, 1) == "@" then + modulePath = modulePath:sub(modulePath:find("%.") + 1) + end ----@param modulePath string ----@return string, number -local function getModuleFilePath(modulePath) - if modulePath:sub(1, 1) == "@" then - modulePath = modulePath:sub(modulePath:find("%.") + 1) + return modulePath:gsub("%.", "/") end - return modulePath:gsub("%.", "/") -end + ---@param modulePath string + ---@return any + function require(modulePath) + assert(type(modulePath) == "string", "Module path must be a string") ----@param modulePath string ----@return any -function require(modulePath) - assert(type(modulePath) == "string", "Module path must be a string") + if loadingModules[modulePath] then + error(("Circular dependency detected for module '%s'."):format(modulePath)) + end - if loadingModules[modulePath] then - error(("Circular dependency detected for module '%s'."):format(modulePath)) - end + if cachedModules[modulePath] then + return cachedModules[modulePath] + end - if cachedModules[modulePath] then - return cachedModules[modulePath] - end + loadingModules[modulePath] = true - loadingModules[modulePath] = true + local resourceName = getResourceNameFromModulePath(modulePath) + local moduleFilePath = getModuleFilePath(modulePath) + local moduleFileContent = LoadResourceFile(resourceName, moduleFilePath .. ".lua") - local resourceName = getResourceNameFromModulePath(modulePath) - local moduleFilePath = getModuleFilePath(modulePath) - local moduleFileContent = LoadResourceFile(resourceName, moduleFilePath .. ".lua") + if not moduleFileContent then + loadingModules[modulePath] = nil + error(("Module '%s' not found in resource '%s'."):format(moduleFilePath, resourceName)) + end - if not moduleFileContent then + local chunk, err = load(moduleFileContent, ("@%s/%s"):format(resourceName, moduleFilePath), "t") + + if not chunk then + loadingModules[modulePath] = nil + error(("Failed to load module '%s': %s"):format(moduleFilePath, err)) + end + + local result = chunk() + + cachedModules[modulePath] = result ~= nil and result or true loadingModules[modulePath] = nil - error(("Module '%s' not found in resource '%s'."):format(moduleFilePath, resourceName)) + + return result end - - local chunk, err = load(moduleFileContent, ("@%s/%s"):format(resourceName, moduleFilePath), "t") - - if not chunk then - loadingModules[modulePath] = nil - error(("Failed to load module '%s': %s"):format(moduleFilePath, err)) - end - - local result = chunk() - - cachedModules[modulePath] = result ~= nil and result or true - loadingModules[modulePath] = nil - - return result +else + print("LIB LOADED") end From 1ec3f8a02ecfba086120feff97df2aec501a8f39 Mon Sep 17 00:00:00 2001 From: Kenshin13 <63159154+Kenshiin13@users.noreply.github.com> Date: Wed, 1 Jan 2025 20:04:02 +0100 Subject: [PATCH 4/4] refactor(es_extended/imports): remove debug print --- [core]/es_extended/imports.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/[core]/es_extended/imports.lua b/[core]/es_extended/imports.lua index d28c05a4..6d6a862e 100644 --- a/[core]/es_extended/imports.lua +++ b/[core]/es_extended/imports.lua @@ -107,6 +107,4 @@ if not lib?.require then return result end -else - print("LIB LOADED") end