refactor: remove modules system and fix indentation

Setting a "standard" module and resource structure is too limiting, so stick to import only.
This commit is contained in:
Linden
2021-11-11 07:52:38 +11:00
parent f72665b197
commit ab4807fee0
6 changed files with 72 additions and 119 deletions

View File

@@ -1,53 +1,36 @@
if not _VERSION:find('5.4') then
error('^1Lua 5.4 must be enabled in the resource manifest!^0', 0)
error('^1Lua 5.4 must be enabled in the resource manifest!^0', 3)
end
local LIBRARY = 'library'
local LIBRARY = 'pe-lualib'
local SERVICE = IsDuplicityVersion() and 'server' or 'client'
local RESOURCE = GetCurrentResourceName()
local IMPORTS = {}
local MODULES = {}
local IMPORTS = {}
local function LoadModule(file, import)
if (import and not IMPORTS[file]) or (not import and not MODULES[file]) then
file = import and ('imports/%s'):format(file) or file
local chunk = LoadResourceFile(import or RESOURCE, ('%s/%s.lua'):format(file, SERVICE))
local shared = LoadResourceFile(import or RESOURCE, ('%s/shared.lua'):format(file))
if shared then
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
end
if chunk then
chunk, err = load(chunk, ('@@%s.lua'):format(SERVICE), 't')
if err then
error(('\n^1Error loading module (%s): %s^0'):format(file, err), 0)
else
if import then IMPORTS[file] = chunk() else MODULES[file] = chunk() end
end
else error(('\n^3Unable to load module (%s)^0'):format(file), 0) end
end
return (import and IMPORTS[file]) or MODULES[file]
local function loadFile(file)
local dir = ('imports/%s'):format(file)
local chunk = LoadResourceFile(LIBRARY, ('%s/%s.lua'):format(dir, SERVICE))
local shared = LoadResourceFile(LIBRARY, ('%s/shared.lua'):format(dir))
if shared then
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
end
if chunk then
local err
chunk, err = load(chunk, ('@@%s.lua'):format(SERVICE), 't')
if err then
error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
else
IMPORTS[file] = chunk()
end
else error(('\n^3Unable to import module (%s)^0'):format(dir), 3) end
end
---Loads a module from within the current resource.
---If the module has already been loaded then it will reference the existing chunk.
---@param file string
---@return table
function include(file) return LoadModule(file) end
---Loads a module from the library.
---If the module has already been loaded then it will reference the existing chunk.
---@param file string
---@return table
function import(file) return LoadModule(file, LIBRARY) end
do
local chunk, err = LoadResourceFile(RESOURCE, ('%s.lua'):format(SERVICE))
local shared = LoadResourceFile(RESOURCE, ('shared.lua'))
if shared then chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared end
chunk, err = load(chunk, ('@@%s/%s.lua'):format(RESOURCE, SERVICE), 't')
assert(chunk, ('^1%s^0'):format(err))
chunk()
function import(file)
if not IMPORTS[file] then loadFile(file) end
return IMPORTS[file]
end
-- Library