diff --git a/fxmanifest.lua b/fxmanifest.lua index 0706ada..b53a402 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -23,7 +23,7 @@ game 'gta5' --[[ Resource Information ]]-- name 'pe-lualib' author 'Linden' -version '1.0.1' +version '1.0.2' repository 'https://github.com/project-error/pe-lualib' description 'A library of shared functions to utilise in other resources.' diff --git a/init.lua b/init.lua index d39a354..0b6e96b 100644 --- a/init.lua +++ b/init.lua @@ -2,12 +2,15 @@ if not _VERSION:find('5.4') then error('^1Lua 5.4 must be enabled in the resource manifest!^0', 3) end + +----------------------------------------------------------------------------------------------- +-- Imports +----------------------------------------------------------------------------------------------- + local LIBRARY = 'pe-lualib' local SERVICE = IsDuplicityVersion() and 'server' or 'client' local IMPORTS = {} -lib = exports[LIBRARY] - local function LoadFile(file) local dir = ('imports/%s'):format(file) local chunk = LoadResourceFile(LIBRARY, ('%s/%s.lua'):format(dir, SERVICE)) @@ -35,6 +38,39 @@ function import(file) return IMPORTS[file] end + +----------------------------------------------------------------------------------------------- +-- Namespace functions +----------------------------------------------------------------------------------------------- + +local lib = {} + +---@param tbl table +--- packs a table and metatable with msgpack +function lib.pack(tbl) + local userdata = msgpack.new() + userdata:_table(tbl) + + local mt = getmetatable(tbl) + if mt then userdata:table(mt) end + + return tostring(userdata), not mt and true +end + +---@param data string +--- unpacks a msgpack table and metatable +function lib.unpack(data) + local tbl, mt = msgpack.unpack(data) + return mt and setmetatable(tbl, mt) or tbl +end + +setmetatable(lib, { + __index = exports[LIBRARY] +}) + +_ENV.lib = lib + + ----------------------------------------------------------------------------------------------- -- Global functions -----------------------------------------------------------------------------------------------