mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
Merge pull request #9 from overextended/dev
This commit is contained in:
@@ -5,6 +5,9 @@ shared_script '@ox_lib/init.lua'
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
<div align='center'><h3><a href='https://overextended.github.io/docs/'>Documentation</a></h3></div>
|
||||||
|
|
||||||
|
|
||||||
Once loaded, any resource can call exports or load modules with the `lib` keyword, i.e.
|
Once loaded, any resource can call exports or load modules with the `lib` keyword, i.e.
|
||||||
```lua
|
```lua
|
||||||
lib.callbacks.Register(...)
|
lib.callbacks.Register(...)
|
||||||
|
|||||||
@@ -23,11 +23,11 @@ game 'gta5'
|
|||||||
--[[ Resource Information ]]--
|
--[[ Resource Information ]]--
|
||||||
name 'ox_lib'
|
name 'ox_lib'
|
||||||
author 'Linden'
|
author 'Linden'
|
||||||
version '1.5.0'
|
version '2.0.0'
|
||||||
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.'
|
||||||
|
|
||||||
[[ Manifest ]]--
|
--[[ Manifest ]]--
|
||||||
dependencies {
|
dependencies {
|
||||||
'/server:5104',
|
'/server:5104',
|
||||||
'/onesync',
|
'/onesync',
|
||||||
|
|||||||
@@ -1,54 +1,54 @@
|
|||||||
local ServerCallbacks = {}
|
local ServerCallbacks = {}
|
||||||
|
|
||||||
---@param event string
|
---@param event string
|
||||||
---@param delay number prevent the event from being called for the given time
|
---@param delay number prevent the event from being called for the given time
|
||||||
local function callbackTimer(event, delay)
|
local function callbackTimer(event, delay)
|
||||||
if type(delay) == 'number' then
|
if type(delay) == 'number' then
|
||||||
local time = GetGameTimer()
|
local time = GetGameTimer()
|
||||||
if (ServerCallbacks[event] or 0) > time then
|
if (ServerCallbacks[event] or 0) > time then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
ServerCallbacks[event] = time + delay
|
ServerCallbacks[event] = time + delay
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function startCallback(resource, event, ...)
|
local function triggerCallback(event, ...)
|
||||||
local id = math.random(0, 100000)
|
local id = math.random(0, 100000)
|
||||||
event = ('__cb_%s:%s'):format(resource, event)
|
event = ('__cb_%s'):format(event)
|
||||||
TriggerServerEvent(event, id, ...)
|
TriggerServerEvent(event, id, ...)
|
||||||
return event..id
|
return event..id
|
||||||
end
|
end
|
||||||
|
|
||||||
local ServerCallback = table.create(0, 2)
|
---Sends an event to the server and triggers a callback function once the response is returned.
|
||||||
|
---```
|
||||||
---@param resource string
|
---callback(event: string, delay: number, function(...)
|
||||||
---@param event string
|
--- print(...)
|
||||||
---@param delay number prevent the event from being called for the given time
|
---end)
|
||||||
--- Sends an event to the server and halts the current thread until a response is returned.
|
---```
|
||||||
ServerCallback.Await = function(resource, event, delay, ...)
|
local callback = {}
|
||||||
if callbackTimer(event, delay) then
|
|
||||||
local promise = promise.new()
|
---@param event string
|
||||||
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
---@param delay number prevent the event from being called for the given time
|
||||||
promise:resolve(response)
|
--- Sends an event to the server and halts the current thread until a response is returned.
|
||||||
RemoveEventHandler(event)
|
function callback.await(event, delay, ...)
|
||||||
end)
|
if callbackTimer(event, delay) then
|
||||||
return table.unpack(Citizen.Await(promise))
|
local promise = promise.new()
|
||||||
end
|
event = RegisterNetEvent(triggerCallback(event, ...), function(response)
|
||||||
end
|
promise:resolve(response)
|
||||||
|
RemoveEventHandler(event)
|
||||||
---@param resource string
|
end)
|
||||||
---@param event string
|
return table.unpack(Citizen.Await(promise))
|
||||||
---@param delay number
|
end
|
||||||
---@param cb function
|
end
|
||||||
--- Sends an event to the server and triggers a callback function once the response is returned.
|
|
||||||
ServerCallback.Async = function(resource, event, delay, cb, ...)
|
return setmetatable(callback, {
|
||||||
if callbackTimer(event, delay) then
|
__call = function(event, delay, cb, ...)
|
||||||
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
if callbackTimer(event, delay) then
|
||||||
cb(table.unpack(response))
|
event = RegisterNetEvent(triggerCallback(event, ...), function(response)
|
||||||
RemoveEventHandler(event)
|
cb(table.unpack(response))
|
||||||
end)
|
RemoveEventHandler(event)
|
||||||
end
|
end)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
return ServerCallback
|
})
|
||||||
14
imports/callback/server.lua
Normal file
14
imports/callback/server.lua
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
local callback = {}
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@param callback function
|
||||||
|
--- Registers an event handler and callback function to respond to client requests.
|
||||||
|
function callback.register(name, cb)
|
||||||
|
name = ('__cb_%s'):format(name)
|
||||||
|
|
||||||
|
RegisterServerEvent(name, function(id, ...)
|
||||||
|
TriggerClientEvent(name..id, source, {cb(source, ...)})
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
return callback
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
local ServerCallback = {}
|
|
||||||
|
|
||||||
---@param name string
|
|
||||||
---@param callback function
|
|
||||||
--- Registers an event handler and callback function to respond to client requests.
|
|
||||||
ServerCallback.Register = function(name, callback)
|
|
||||||
name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name)
|
|
||||||
|
|
||||||
RegisterServerEvent(name, function(id, ...)
|
|
||||||
TriggerClientEvent(name..id, source, {callback(source, ...)})
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
return ServerCallback
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
--- Call on frame to disable all stored keys.
|
--- Call on frame to disable all stored keys.
|
||||||
--- ```
|
--- ```
|
||||||
--- DisableControlActions()
|
--- disableControls()
|
||||||
--- ```
|
--- ```
|
||||||
local DisableControlActions = {}
|
local disableControls = {}
|
||||||
|
|
||||||
---@param ... number
|
---@param ... number
|
||||||
function DisableControlActions:Add(...)
|
function disableControls:Add(...)
|
||||||
local keys = type(...) == 'table' and ... or {...}
|
local keys = type(...) == 'table' and ... or {...}
|
||||||
for i=1, #keys do
|
for i=1, #keys do
|
||||||
local key = keys[i]
|
local key = keys[i]
|
||||||
@@ -18,7 +18,7 @@ function DisableControlActions:Add(...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@param ... number
|
---@param ... number
|
||||||
function DisableControlActions:Remove(...)
|
function disableControls:Remove(...)
|
||||||
local keys = type(...) == 'table' and ... or {...}
|
local keys = type(...) == 'table' and ... or {...}
|
||||||
for i=1, #keys do
|
for i=1, #keys do
|
||||||
local key = keys[i]
|
local key = keys[i]
|
||||||
@@ -32,22 +32,22 @@ function DisableControlActions:Remove(...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@param ... number
|
---@param ... number
|
||||||
function DisableControlActions:Clear(...)
|
function disableControls:Clear(...)
|
||||||
local keys = type(...) == 'table' and ... or {...}
|
local keys = type(...) == 'table' and ... or {...}
|
||||||
for i=1, #keys do
|
for i=1, #keys do
|
||||||
self[keys[i]] = nil
|
self[keys[i]] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local Keys = {}
|
local keys = {}
|
||||||
local DisableControlAction = DisableControlAction
|
local DisableControlAction = DisableControlAction
|
||||||
local pairs = pairs
|
local pairs = pairs
|
||||||
|
|
||||||
return setmetatable(DisableControlActions, {
|
return setmetatable(disableControls, {
|
||||||
__index = Keys,
|
__index = keys,
|
||||||
__newindex = Keys,
|
__newindex = keys,
|
||||||
__call = function()
|
__call = function()
|
||||||
for k in pairs(Keys) do
|
for k in pairs(keys) do
|
||||||
DisableControlAction(0, k, true)
|
DisableControlAction(0, k, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
97
init.lua
97
init.lua
@@ -1,20 +1,36 @@
|
|||||||
|
|
||||||
|
-- ox_lib
|
||||||
|
-- Copyright (C) 2021 Linden <https://github.com/thelindat>
|
||||||
|
|
||||||
|
-- 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
|
||||||
|
-- the Free Software Foundation, either version 3 of the License, or
|
||||||
|
-- (at your option) any later version.
|
||||||
|
|
||||||
|
-- This program is distributed in the hope that it will be useful,
|
||||||
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
-- GNU General Public License for more details.
|
||||||
|
|
||||||
|
-- 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>
|
||||||
|
|
||||||
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', 3)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Module
|
-- Module
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- env
|
-- env
|
||||||
local lualib = 'ox_lib'
|
local lualib = 'ox_lib'
|
||||||
local file = IsDuplicityVersion() and 'server' or 'client'
|
|
||||||
|
|
||||||
-- micro-optimise
|
-- micro-optimise
|
||||||
local rawget = rawget
|
|
||||||
local rawset = rawset
|
|
||||||
local LoadResourceFile = LoadResourceFile
|
local LoadResourceFile = LoadResourceFile
|
||||||
|
local file = IsDuplicityVersion() and 'server' or 'client'
|
||||||
|
local rawset = rawset
|
||||||
|
local rawget = rawget
|
||||||
|
|
||||||
local function loadModule(self, module)
|
local function loadModule(self, module)
|
||||||
local dir = ('imports/%s'):format(module)
|
local dir = ('imports/%s'):format(module)
|
||||||
@@ -64,6 +80,11 @@ end
|
|||||||
|
|
||||||
lib = setmetatable({
|
lib = setmetatable({
|
||||||
exports = {},
|
exports = {},
|
||||||
|
onCache = setmetatable({}, {
|
||||||
|
__call = function(self, key, cb)
|
||||||
|
self[key] = cb
|
||||||
|
end
|
||||||
|
})
|
||||||
}, {
|
}, {
|
||||||
__index = call,
|
__index = call,
|
||||||
__call = call,
|
__call = call,
|
||||||
@@ -76,7 +97,6 @@ lib = setmetatable({
|
|||||||
return lualib
|
return lualib
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
import = lib
|
|
||||||
|
|
||||||
local intervals = {}
|
local intervals = {}
|
||||||
--- Dream of a world where this PR gets accepted.
|
--- Dream of a world where this PR gets accepted.
|
||||||
@@ -117,18 +137,61 @@ function ClearInterval(id)
|
|||||||
intervals[id] = -1
|
intervals[id] = -1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ox_lib
|
|
||||||
-- Copyright (C) 2021 Linden <https://github.com/thelindat>
|
|
||||||
|
|
||||||
-- 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
|
-- Cache
|
||||||
-- the Free Software Foundation, either version 3 of the License, or
|
-----------------------------------------------------------------------------------------------
|
||||||
-- (at your option) any later version.
|
|
||||||
|
|
||||||
-- This program is distributed in the hope that it will be useful,
|
local ox = GetResourceState('ox_core') ~= 'missing' and setmetatable({}, {
|
||||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
__index = function()
|
||||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
return true
|
||||||
-- GNU General Public License for more details.
|
end
|
||||||
|
}) or {
|
||||||
|
groups = GetResourceState('ox_groups') ~= 'missing',
|
||||||
|
}
|
||||||
|
|
||||||
-- You should have received a copy of the GNU General Public License
|
local cache_mt = {
|
||||||
-- along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>
|
__index = function(self, key)
|
||||||
|
return rawset(self, key, exports[lualib]['cache'](nil, key) or false)[key]
|
||||||
|
end,
|
||||||
|
|
||||||
|
__call = function(self)
|
||||||
|
table.wipe(self)
|
||||||
|
|
||||||
|
if file == 'server' then
|
||||||
|
|
||||||
|
else
|
||||||
|
self.playerId = PlayerId()
|
||||||
|
end
|
||||||
|
|
||||||
|
if ox.groups then
|
||||||
|
self.groups = setmetatable({}, {
|
||||||
|
__index = function(groups, index)
|
||||||
|
groups[index] = GlobalState['group:'..index]
|
||||||
|
return groups[index]
|
||||||
|
end
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
local cache = setmetatable({}, cache_mt)
|
||||||
|
|
||||||
|
Citizen.CreateThreadNow(function()
|
||||||
|
while true do
|
||||||
|
cache()
|
||||||
|
Wait(60000)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
AddEventHandler('lualib:updateCache', function(data)
|
||||||
|
for key, value in pairs(data) do
|
||||||
|
cache[key] = value
|
||||||
|
|
||||||
|
if lib.onCache[key] then
|
||||||
|
lib.onCache[key](value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
_ENV.cache = cache
|
||||||
|
|||||||
67
resource/cache/client.lua
vendored
Normal file
67
resource/cache/client.lua
vendored
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
local cache = {}
|
||||||
|
|
||||||
|
function cache:getVehicle()
|
||||||
|
local vehicle = GetVehiclePedIsIn(self.ped, false)
|
||||||
|
if vehicle > 0 then
|
||||||
|
if self:set('vehicle', vehicle) then
|
||||||
|
local seats = GetVehicleMaxNumberOfPassengers(vehicle) - 1
|
||||||
|
local GetPedInVehicleSeat = GetPedInVehicleSeat
|
||||||
|
for i = -1, seats do
|
||||||
|
if GetPedInVehicleSeat(vehicle, i) == self.ped then
|
||||||
|
return self:set('seat', i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
self:set('vehicle', false)
|
||||||
|
self:set('seat', false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cache:onFoot()
|
||||||
|
-- todo
|
||||||
|
-- print('on foot')
|
||||||
|
end
|
||||||
|
|
||||||
|
function cache:inVehicle()
|
||||||
|
-- todo
|
||||||
|
if self.driver then
|
||||||
|
-- print('driving vehicle')
|
||||||
|
else
|
||||||
|
-- print('in vehicle')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local update = {}
|
||||||
|
|
||||||
|
function cache:set(key, value)
|
||||||
|
if value ~= self[key] then
|
||||||
|
self[key] = value
|
||||||
|
update[key] = value
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
CreateThread(function()
|
||||||
|
while true do
|
||||||
|
cache:set('ped', PlayerPedId())
|
||||||
|
cache:getVehicle()
|
||||||
|
|
||||||
|
-- if not cache.vehicle then
|
||||||
|
-- cache:onFoot()
|
||||||
|
-- else
|
||||||
|
-- cache:inVehicle()
|
||||||
|
-- end
|
||||||
|
|
||||||
|
if next(update) then
|
||||||
|
TriggerEvent('lualib:updateCache', update)
|
||||||
|
table.wipe(update)
|
||||||
|
end
|
||||||
|
|
||||||
|
Wait(100)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
function lib.cache(key)
|
||||||
|
return cache[key]
|
||||||
|
end
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
local cache = {}
|
|
||||||
local player = LocalPlayer.state
|
|
||||||
local states = {
|
|
||||||
['vehicle'] = true,
|
|
||||||
['driver'] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
function cache:set(key, value)
|
|
||||||
if value ~= self[key] then
|
|
||||||
self[key] = value
|
|
||||||
|
|
||||||
if states[key] then player:set(key, value, false) end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function cache:getPed()
|
|
||||||
local ped = PlayerPedId()
|
|
||||||
if ped ~= self.ped then
|
|
||||||
self.ped = ped
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function cache:getVehicle()
|
|
||||||
local vehicle = GetVehiclePedIsIn(self.ped, false)
|
|
||||||
if vehicle > 0 then
|
|
||||||
if vehicle ~= self.vehicle then
|
|
||||||
self:set('vehicle', vehicle)
|
|
||||||
|
|
||||||
if GetPedInVehicleSeat(vehicle, -1) == self.ped then
|
|
||||||
self:set('driver', true)
|
|
||||||
else
|
|
||||||
self:set('driver', false)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else self:set('vehicle', nil) end
|
|
||||||
end
|
|
||||||
|
|
||||||
function cache:onFoot()
|
|
||||||
-- print('on foot')
|
|
||||||
end
|
|
||||||
|
|
||||||
function cache:inVehicle()
|
|
||||||
if self.driver then
|
|
||||||
-- print('driving vehicle')
|
|
||||||
else
|
|
||||||
-- print('in vehicle')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
CreateThread(function()
|
|
||||||
while true do
|
|
||||||
cache:getPed()
|
|
||||||
cache:getVehicle()
|
|
||||||
|
|
||||||
if not cache.vehicle then
|
|
||||||
cache:onFoot()
|
|
||||||
else
|
|
||||||
cache:inVehicle()
|
|
||||||
end
|
|
||||||
|
|
||||||
Wait(100)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
@@ -55,8 +55,8 @@ function lib.getVehicleProperties(vehicle)
|
|||||||
end
|
end
|
||||||
|
|
||||||
for i = 0, 5 do
|
for i = 0, 5 do
|
||||||
if IsVehicleTyreBurst(vehicle, i, true) then
|
if IsVehicleTyreBurst(vehicle, i, false) then
|
||||||
damage.tyres[i] = IsVehicleTyreBurst(vehicle, i, false) and 2 or 1
|
damage.tyres[i] = IsVehicleTyreBurst(vehicle, i, true) and 2 or 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Reference in New Issue
Block a user