mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 16:06:02 +01:00
feat(init): add custom packing for metatables
Adds lib.unpack and lib.pack functions to the global lib table.
This commit is contained in:
@@ -23,7 +23,7 @@ game 'gta5'
|
|||||||
--[[ Resource Information ]]--
|
--[[ Resource Information ]]--
|
||||||
name 'pe-lualib'
|
name 'pe-lualib'
|
||||||
author 'Linden'
|
author 'Linden'
|
||||||
version '1.0.1'
|
version '1.0.2'
|
||||||
repository 'https://github.com/project-error/pe-lualib'
|
repository 'https://github.com/project-error/pe-lualib'
|
||||||
description 'A library of shared functions to utilise in other resources.'
|
description 'A library of shared functions to utilise in other resources.'
|
||||||
|
|
||||||
|
|||||||
40
init.lua
40
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)
|
error('^1Lua 5.4 must be enabled in the resource manifest!^0', 3)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
|
-- Imports
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
local LIBRARY = 'pe-lualib'
|
local LIBRARY = 'pe-lualib'
|
||||||
local SERVICE = IsDuplicityVersion() and 'server' or 'client'
|
local SERVICE = IsDuplicityVersion() and 'server' or 'client'
|
||||||
local IMPORTS = {}
|
local IMPORTS = {}
|
||||||
|
|
||||||
lib = exports[LIBRARY]
|
|
||||||
|
|
||||||
local function LoadFile(file)
|
local function LoadFile(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))
|
||||||
@@ -35,6 +38,39 @@ function import(file)
|
|||||||
return IMPORTS[file]
|
return IMPORTS[file]
|
||||||
end
|
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
|
-- Global functions
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user