refactor(resource/init): dynamic module loading

This commit is contained in:
Linden
2023-07-31 15:14:10 +10:00
parent 2161694f84
commit 4babb3fd46
2 changed files with 27 additions and 8 deletions

View File

@@ -33,16 +33,11 @@ files {
shared_script 'resource/init.lua' shared_script 'resource/init.lua'
shared_scripts { shared_scripts {
'imports/locale/shared.lua',
'resource/**/shared.lua', 'resource/**/shared.lua',
-- 'resource/**/shared/*.lua' -- 'resource/**/shared/*.lua'
} }
client_scripts { client_scripts {
'imports/callback/client.lua',
'imports/requestModel/client.lua',
'imports/requestAnimDict/client.lua',
'imports/addKeybind/client.lua',
'resource/**/client.lua', 'resource/**/client.lua',
'resource/**/client/*.lua' 'resource/**/client/*.lua'
} }

View File

@@ -1,14 +1,38 @@
local debug_getinfo = debug.getinfo local debug_getinfo = debug.getinfo
function noop() end
lib = setmetatable({ lib = setmetatable({
name = 'ox_lib', name = 'ox_lib',
context = IsDuplicityVersion() and 'server' or 'client', context = IsDuplicityVersion() and 'server' or 'client',
}, { }, {
__newindex = function(self, name, fn) __newindex = function(self, key, fn)
rawset(self, name, fn) rawset(self, key, fn)
if debug_getinfo(2, 'S').short_src:find('@ox_lib/resource') then if debug_getinfo(2, 'S').short_src:find('@ox_lib/resource') then
exports(name, fn) exports(key, fn)
end
end,
__index = function(self, key)
local dir = ('imports/%s'):format(key)
local chunk = LoadResourceFile(self.name, ('%s/%s.lua'):format(dir, self.context))
local shared = LoadResourceFile(self.name, ('%s/shared.lua'):format(dir))
if shared then
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
end
if chunk then
local fn, err = load(chunk, ('@@ox_lib/%s/%s.lua'):format(key, self.context))
if not fn or err then
return error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
end
rawset(self, key, fn() or noop)
return self[key]
end end
end end
}) })