mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 23:46:03 +01:00
refactor(init): throw an error if ox_lib is not started
This commit is contained in:
@@ -23,7 +23,7 @@ game 'gta5'
|
|||||||
--[[ Resource Information ]]--
|
--[[ Resource Information ]]--
|
||||||
name 'ox_lib'
|
name 'ox_lib'
|
||||||
author 'Linden'
|
author 'Linden'
|
||||||
version '2.0.1'
|
version '2.0.2'
|
||||||
repository 'https://github.com/overextended/ox_lib'
|
repository 'https://github.com/overextended/ox_lib'
|
||||||
description 'A library of shared functions to utilise in other resources.'
|
description 'A library of shared functions to utilise in other resources.'
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
local callback = {}
|
local callback = {}
|
||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param callback function
|
---@param cb function
|
||||||
--- Registers an event handler and callback function to respond to client requests.
|
--- Registers an event handler and callback function to respond to client requests.
|
||||||
function callback.register(name, cb)
|
function callback.register(name, cb)
|
||||||
name = ('__cb_%s'):format(name)
|
name = ('__cb_%s'):format(name)
|
||||||
|
|||||||
396
init.lua
396
init.lua
@@ -1,197 +1,199 @@
|
|||||||
|
|
||||||
-- ox_lib
|
-- ox_lib
|
||||||
-- Copyright (C) 2021 Linden <https://github.com/thelindat>
|
-- Copyright (C) 2021 Linden <https://github.com/thelindat>
|
||||||
|
|
||||||
-- This program is free software: you can redistribute it and/or modify
|
-- This program is free software: you can redistribute it and/or modify
|
||||||
-- it under the terms of the GNU General Public License as published by
|
-- it under the terms of the GNU General Public License as published by
|
||||||
-- the Free Software Foundation, either version 3 of the License, or
|
-- the Free Software Foundation, either version 3 of the License, or
|
||||||
-- (at your option) any later version.
|
-- (at your option) any later version.
|
||||||
|
|
||||||
-- This program is distributed in the hope that it will be useful,
|
-- This program is distributed in the hope that it will be useful,
|
||||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
-- GNU General Public License for more details.
|
-- GNU General Public License for more details.
|
||||||
|
|
||||||
-- You should have received a copy of the GNU General Public License
|
-- You should have received a copy of the GNU General Public License
|
||||||
-- along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>
|
-- along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>
|
||||||
|
|
||||||
if not _VERSION:find('5.4') then
|
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', 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
local lualib = 'ox_lib'
|
||||||
-- Module
|
|
||||||
-----------------------------------------------------------------------------------------------
|
if not GetResourceState(lualib):find('start') then
|
||||||
|
error('^1ox_lib should be started before this resource^0', 2)
|
||||||
-- env
|
end
|
||||||
local lualib = 'ox_lib'
|
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
-- micro-optimise
|
-- Module
|
||||||
local LoadResourceFile = LoadResourceFile
|
-----------------------------------------------------------------------------------------------
|
||||||
local file = IsDuplicityVersion() and 'server' or 'client'
|
|
||||||
local rawset = rawset
|
local LoadResourceFile = LoadResourceFile
|
||||||
local rawget = rawget
|
local file = IsDuplicityVersion() and 'server' or 'client'
|
||||||
|
local rawset = rawset
|
||||||
local function loadModule(self, module)
|
local rawget = rawget
|
||||||
local dir = ('imports/%s'):format(module)
|
|
||||||
local chunk = LoadResourceFile(lualib, ('%s/%s.lua'):format(dir, file))
|
local function loadModule(self, module)
|
||||||
local shared = LoadResourceFile(lualib, ('%s/shared.lua'):format(dir))
|
local dir = ('imports/%s'):format(module)
|
||||||
|
local chunk = LoadResourceFile(lualib, ('%s/%s.lua'):format(dir, file))
|
||||||
if shared then
|
local shared = LoadResourceFile(lualib, ('%s/shared.lua'):format(dir))
|
||||||
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
|
|
||||||
end
|
if shared then
|
||||||
|
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
|
||||||
if chunk then
|
end
|
||||||
local err
|
|
||||||
chunk, err = load(chunk, ('@@ox_lib/%s/%s.lua'):format(module, file))
|
if chunk then
|
||||||
if err then
|
local err
|
||||||
error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
|
chunk, err = load(chunk, ('@@ox_lib/%s/%s.lua'):format(module, file))
|
||||||
else
|
if err then
|
||||||
rawset(self, module, chunk())
|
error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
|
||||||
return self[module]
|
else
|
||||||
end
|
rawset(self, module, chunk())
|
||||||
end
|
return self[module]
|
||||||
end
|
end
|
||||||
|
end
|
||||||
-----------------------------------------------------------------------------------------------
|
end
|
||||||
-- API
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
-- API
|
||||||
local function call(self, index, ...)
|
-----------------------------------------------------------------------------------------------
|
||||||
local module = rawget(self, index)
|
|
||||||
if not module then
|
local function call(self, index, ...)
|
||||||
module = loadModule(self, index)
|
local module = rawget(self, index)
|
||||||
|
if not module then
|
||||||
if not module then
|
module = loadModule(self, index)
|
||||||
local function method(...)
|
|
||||||
return exports[lualib][index](nil, ...)
|
if not module then
|
||||||
end
|
local function method(...)
|
||||||
|
return exports[lualib][index](nil, ...)
|
||||||
if not ... then
|
end
|
||||||
rawset(self, index, method)
|
|
||||||
end
|
if not ... then
|
||||||
|
rawset(self, index, method)
|
||||||
return method
|
end
|
||||||
end
|
|
||||||
end
|
return method
|
||||||
|
end
|
||||||
return module
|
end
|
||||||
end
|
|
||||||
|
return module
|
||||||
lib = setmetatable({
|
end
|
||||||
exports = {},
|
|
||||||
onCache = setmetatable({}, {
|
lib = setmetatable({
|
||||||
__call = function(self, key, cb)
|
exports = {},
|
||||||
self[key] = cb
|
onCache = setmetatable({}, {
|
||||||
end
|
__call = function(self, key, cb)
|
||||||
})
|
self[key] = cb
|
||||||
}, {
|
end
|
||||||
__index = call,
|
})
|
||||||
__call = call,
|
}, {
|
||||||
|
__index = call,
|
||||||
__newindex = function()
|
__call = call,
|
||||||
error('Cannot set index on lib')
|
|
||||||
end,
|
__newindex = function()
|
||||||
|
error('Cannot set index on lib')
|
||||||
__tostring = function()
|
end,
|
||||||
return lualib
|
|
||||||
end,
|
__tostring = function()
|
||||||
})
|
return lualib
|
||||||
|
end,
|
||||||
local intervals = {}
|
})
|
||||||
--- Dream of a world where this PR gets accepted.
|
|
||||||
---@param callback function
|
local intervals = {}
|
||||||
---@param interval? number
|
--- Dream of a world where this PR gets accepted.
|
||||||
---@param ... any
|
---@param callback function
|
||||||
function SetInterval(callback, interval, ...)
|
---@param interval? number
|
||||||
interval = interval or 0
|
---@param ... any
|
||||||
assert(type(interval) == 'number', ('Interval must be a number. Received %s'):format(json.encode(interval)))
|
function SetInterval(callback, interval, ...)
|
||||||
local cbType = type(callback)
|
interval = interval or 0
|
||||||
|
assert(type(interval) == 'number', ('Interval must be a number. Received %s'):format(json.encode(interval)))
|
||||||
if cbType == 'number' and intervals[callback] then
|
local cbType = type(callback)
|
||||||
intervals[callback] = interval or 0
|
|
||||||
return
|
if cbType == 'number' and intervals[callback] then
|
||||||
end
|
intervals[callback] = interval or 0
|
||||||
|
return
|
||||||
assert(cbType == 'function', ('Callback must be a function. Received %s'):format(tostring(cbType)))
|
end
|
||||||
local id
|
|
||||||
local args = {...}
|
assert(cbType == 'function', ('Callback must be a function. Received %s'):format(tostring(cbType)))
|
||||||
|
local id
|
||||||
Citizen.CreateThreadNow(function(ref)
|
local args = {...}
|
||||||
id = ref
|
|
||||||
intervals[id] = interval or 0
|
Citizen.CreateThreadNow(function(ref)
|
||||||
repeat
|
id = ref
|
||||||
interval = intervals[id]
|
intervals[id] = interval or 0
|
||||||
Wait(interval)
|
repeat
|
||||||
callback(table.unpack(args))
|
interval = intervals[id]
|
||||||
until interval < 0
|
Wait(interval)
|
||||||
intervals[id] = nil
|
callback(table.unpack(args))
|
||||||
end)
|
until interval < 0
|
||||||
|
intervals[id] = nil
|
||||||
return id
|
end)
|
||||||
end
|
|
||||||
|
return id
|
||||||
function ClearInterval(id)
|
end
|
||||||
assert(type(id) == 'number', ('Interval id must be a number. Received %s'):format(json.encode(id)))
|
|
||||||
assert(intervals[id], ('No interval exists with id %s'):format(id))
|
function ClearInterval(id)
|
||||||
intervals[id] = -1
|
assert(type(id) == 'number', ('Interval id must be a number. Received %s'):format(json.encode(id)))
|
||||||
end
|
assert(intervals[id], ('No interval exists with id %s'):format(id))
|
||||||
|
intervals[id] = -1
|
||||||
|
end
|
||||||
-----------------------------------------------------------------------------------------------
|
|
||||||
-- Cache
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
-- Cache
|
||||||
local ox = GetResourceState('ox_core') ~= 'missing' and setmetatable({}, {
|
-----------------------------------------------------------------------------------------------
|
||||||
__index = function()
|
|
||||||
return true
|
local ox = GetResourceState('ox_core') ~= 'missing' and setmetatable({}, {
|
||||||
end
|
__index = function()
|
||||||
}) or {
|
return true
|
||||||
groups = GetResourceState('ox_groups') ~= 'missing',
|
end
|
||||||
}
|
}) or {
|
||||||
|
groups = GetResourceState('ox_groups') ~= 'missing',
|
||||||
local cache_mt = {
|
}
|
||||||
__index = function(self, key)
|
|
||||||
return rawset(self, key, exports[lualib]['cache'](nil, key) or false)[key]
|
local cache_mt = {
|
||||||
end,
|
__index = function(self, key)
|
||||||
|
return rawset(self, key, exports[lualib]['cache'](nil, key) or false)[key]
|
||||||
__call = function(self)
|
end,
|
||||||
table.wipe(self)
|
|
||||||
|
__call = function(self)
|
||||||
if file == 'server' then
|
table.wipe(self)
|
||||||
|
|
||||||
else
|
if file == 'server' then
|
||||||
self.playerId = PlayerId()
|
|
||||||
end
|
else
|
||||||
|
self.playerId = PlayerId()
|
||||||
if ox.groups then
|
end
|
||||||
self.groups = setmetatable({}, {
|
|
||||||
__index = function(groups, index)
|
if ox.groups then
|
||||||
groups[index] = GlobalState['group:'..index]
|
self.groups = setmetatable({}, {
|
||||||
return groups[index]
|
__index = function(groups, index)
|
||||||
end
|
groups[index] = GlobalState['group:'..index]
|
||||||
})
|
return groups[index]
|
||||||
end
|
end
|
||||||
end
|
})
|
||||||
}
|
end
|
||||||
|
end
|
||||||
local cache = setmetatable({}, cache_mt)
|
}
|
||||||
|
|
||||||
Citizen.CreateThreadNow(function()
|
local cache = setmetatable({}, cache_mt)
|
||||||
while true do
|
|
||||||
cache()
|
Citizen.CreateThreadNow(function()
|
||||||
Wait(60000)
|
while true do
|
||||||
end
|
cache()
|
||||||
end)
|
Wait(60000)
|
||||||
|
end
|
||||||
AddEventHandler('lualib:updateCache', function(data)
|
end)
|
||||||
for key, value in pairs(data) do
|
|
||||||
cache[key] = value
|
AddEventHandler('lualib:updateCache', function(data)
|
||||||
|
for key, value in pairs(data) do
|
||||||
if lib.onCache[key] then
|
cache[key] = value
|
||||||
lib.onCache[key](value)
|
|
||||||
end
|
if lib.onCache[key] then
|
||||||
end
|
lib.onCache[key](value)
|
||||||
end)
|
end
|
||||||
|
end
|
||||||
_ENV.cache = cache
|
end)
|
||||||
|
|
||||||
|
_ENV.cache = cache
|
||||||
|
|||||||
Reference in New Issue
Block a user