From a3f38723e3c7cafe6559edb9ef579677566c65f8 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sun, 1 May 2022 22:51:59 +1000 Subject: [PATCH] refactor(getCore): rename loadFramework, tweaks ESX imports are limited to an event and getSharedObject export. In the future I want to introduce some generic functions to simplify cross-framework compatibility when working with player, accounts, inventories, and groups (jobs/gangs). --- imports/getCore/shared.lua | 65 +++++++++++++++++++++++++++++++ imports/loadFramework/shared.lua | 66 -------------------------------- 2 files changed, 65 insertions(+), 66 deletions(-) create mode 100644 imports/getCore/shared.lua delete mode 100644 imports/loadFramework/shared.lua diff --git a/imports/getCore/shared.lua b/imports/getCore/shared.lua new file mode 100644 index 0000000..c99a0b4 --- /dev/null +++ b/imports/getCore/shared.lua @@ -0,0 +1,65 @@ +return function() + local result + + Citizen.CreateThreadNow(function() + local framework = GetConvar('framework', '') + + if framework == '' then + framework = GetResourceState('ox_core'):find('start') and 'ox_core' + or GetResourceState('qb-core'):find('start') and 'qb-core' + or GetResourceState('es_extended'):find('start') and 'es_extended' + + if not framework then + error('Unable to determine framework (convar is not set, or resource was renamed)') + end + end + + local success, import + + if framework == 'qb-core' then + import = exports[framework]:GetCoreObject() + elseif framework == 'es_extended' then + import = exports[framework]:getSharedObject() + end + + if import then + import.resource = framework + else + if framework == 'ox_core' then + import = ('%s/import.lua'):format(lib.service) + else + error('no loader exists for %s'):format(framework) + end + + success, result = pcall(LoadResourceFile, framework, import) + + if not result then + error(("Unable to load '@%s/%s'"):format(framework, import)) + end + + if not success then + error(result and result or ("Unable to load '@%s/%s'"):format(framework, import), 0) + end + + success, result = load(result, ('@@%s/%s'):format(framework, import)) + + if not success then + error(result, 0) + end + + success, result = pcall(success) + + if not success then error(result) end + + if framework == 'ox_core' then + import = Ox + end + + import.resource = framework + end + + result = import + end) + + return result +end diff --git a/imports/loadFramework/shared.lua b/imports/loadFramework/shared.lua deleted file mode 100644 index 5973787..0000000 --- a/imports/loadFramework/shared.lua +++ /dev/null @@ -1,66 +0,0 @@ -local function ref(framework) - if framework == 'ox_core' or framework == 'es_extended'then - return 'imports.lua' - elseif framework == 'qb-core' then - return exports['qb-core'].GetCoreObject - end -end - -local function err(e, n) - error(('^1%s^0'):format(e), n) -end - -return function() - local framework = GetResourceState('ox_core'):find('start') and 'ox_core' - - if not framework then - framework = GetResourceState('es_extended'):find('start') and 'es_extended' - end - - if not framework then - framework = GetResourceState('qb-core'):find('start') and 'qb-core' - end - - local result - local success, import = pcall(ref, framework) - - if not success or not import then - err(import and import or ('no loader exists for %s'):format(framework), 1) - end - - -- Return early since framework does not use an imports file - if type(import) == 'table' then - import.resource = framework - return import - end - - success, result = pcall(LoadResourceFile, framework, import) - - if not result then - err(("unable to load '@%s/%s'"):format(framework, import)) - end - - if not success then - err(result and result or ("unable to load '@%s/%s'"):format(framework, import), 0) - end - - success, result = load(result, ('@@%s/%s'):format(framework, import)) - - if not success then - err(result, 0) - end - - success, result = pcall(success) - - if not success then err(result) end - - if framework == 'ox_core' then - import = Ox - elseif framework == 'es_extended' then - import = ESX - end - - import.resource = framework - - return import -end