refactor(init): metatables and micro-optimise

import now stores all imported modules and uses call and index methods to load modules.

Defining an import as before (local ServerCallbacks = import 'callbacks') works as before, or you can use ServerCallbacks = import.callbacks

You could also use a lazy loading method by never defining ServerCallbacks, i.e. import.callbacks.Register(...) will work without explicitly importing the module previously.
This commit is contained in:
Linden
2021-11-28 18:04:09 +11:00
parent 0ad0201ab2
commit 4d51879afd

View File

@@ -4,27 +4,44 @@ end
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
-- Imports -- Module
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
local LIBRARY = 'pe-lualib' -- env
local SERVICE = IsDuplicityVersion() and 'server' or 'client' local LIBRARY = 'pe-lualib'
local IMPORTS = {} local SERVICE = IsDuplicityVersion() and 'server' or 'client'
local function LoadFile(file) -- micro-optimise
local rawget = rawget
local rawset = rawset
local LoadResourceFile = LoadResourceFile
local function loadFile(self, file)
local dir = ('imports/%s'):format(file) local dir = ('imports/%s'):format(file)
local chunk = LoadResourceFile(LIBRARY, ('%s/%s.lua'):format(dir, SERVICE)) local chunk = LoadResourceFile(LIBRARY, ('%s/%s.lua'):format(dir, SERVICE))
local shared = LoadResourceFile(LIBRARY, ('%s/shared.lua'):format(dir)) local shared = LoadResourceFile(LIBRARY, ('%s/shared.lua'):format(dir))
if shared then if shared then
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
end end
if chunk then if chunk then
local err local err
chunk, err = load(chunk, ('@@%s.lua'):format(SERVICE), 't') chunk, err = load(chunk, ('@@%s.lua'):format(SERVICE), 't')
if err then if err then
error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3) error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
else else
IMPORTS[file] = chunk() rawset(self, file, chunk())
return setmetatable(self[file], {
__index = {
name = file,
resource = LIBRARY
},
__newindex = function()
error('Cannot add indexes to imports')
end
})
end end
else error(('\n^3Unable to import module (%s)^0'):format(dir), 3) end else error(('\n^3Unable to import module (%s)^0'):format(dir), 3) end
end end
@@ -33,22 +50,36 @@ end
---If the module has already been loaded then it will reference the existing chunk. ---If the module has already been loaded then it will reference the existing chunk.
---@param file string ---@param file string
---@return table ---@return table
function import(file) local function getImport(self, file)
if not IMPORTS[file] then LoadFile(file) end local import = rawget(self, file)
return IMPORTS[file] return import and import or loadFile(self, file)
end end
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
-- Namespace functions -- Interface
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
import = setmetatable({}, {
__index = getImport,
__call = getImport,
__newindex = function()
error('Cannot add indexes to imports')
end
})
local lib = {} local lib = {}
local msgpack_new = msgpack.new
local msgpack_unpack = msgpack.unpack
local getmetatable = getmetatable
local setmetatable = setmetatable
---@param tbl table ---@param tbl table
--- packs a table and metatable with msgpack --- packs a table and metatable with msgpack
function lib.pack(tbl) function lib.pack(tbl)
local userdata = msgpack.new() local userdata = msgpack_new()
userdata:_table(tbl) userdata:_table(tbl)
local mt = getmetatable(tbl) local mt = getmetatable(tbl)
@@ -60,21 +91,20 @@ end
---@param data string ---@param data string
--- unpacks a msgpack table and metatable --- unpacks a msgpack table and metatable
function lib.unpack(data) function lib.unpack(data)
local tbl, mt = msgpack.unpack(data) local tbl, mt = msgpack_unpack(data)
return mt and setmetatable(tbl, mt) or tbl return mt and setmetatable(tbl, mt) or tbl
end end
setmetatable(lib, { setmetatable(lib, {
__index = exports[LIBRARY] __index = exports[LIBRARY],
__tostring = function()
return LIBRARY
end,
}) })
_ENV.lib = lib _ENV.lib = lib
-----------------------------------------------------------------------------------------------
-- Global functions
-----------------------------------------------------------------------------------------------
--- Dream of a world where this PR gets accepted. --- Dream of a world where this PR gets accepted.
--- ```lua --- ```lua
--- SetInterval(callback: function, timer: number) --- SetInterval(callback: function, timer: number)