Merge pull request #9 from overextended/dev

This commit is contained in:
Linden
2022-03-02 17:50:37 +11:00
committed by GitHub
11 changed files with 232 additions and 162 deletions

View File

@@ -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.
```lua
lib.callbacks.Register(...)

View File

@@ -23,11 +23,11 @@ game 'gta5'
--[[ Resource Information ]]--
name 'ox_lib'
author 'Linden'
version '1.5.0'
version '2.0.0'
repository 'https://github.com/overextended/ox_lib'
description 'A library of shared functions to utilise in other resources.'
[[ Manifest ]]--
--[[ Manifest ]]--
dependencies {
'/server:5104',
'/onesync',

View File

@@ -1,54 +1,54 @@
local ServerCallbacks = {}
---@param event string
---@param delay number prevent the event from being called for the given time
local function callbackTimer(event, delay)
if type(delay) == 'number' then
local time = GetGameTimer()
if (ServerCallbacks[event] or 0) > time then
return false
end
ServerCallbacks[event] = time + delay
end
return true
end
local function startCallback(resource, event, ...)
local id = math.random(0, 100000)
event = ('__cb_%s:%s'):format(resource, event)
TriggerServerEvent(event, id, ...)
return event..id
end
local ServerCallback = table.create(0, 2)
---@param resource string
---@param event string
---@param delay number prevent the event from being called for the given time
--- Sends an event to the server and halts the current thread until a response is returned.
ServerCallback.Await = function(resource, event, delay, ...)
if callbackTimer(event, delay) then
local promise = promise.new()
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
promise:resolve(response)
RemoveEventHandler(event)
end)
return table.unpack(Citizen.Await(promise))
end
end
---@param resource string
---@param event string
---@param delay number
---@param cb function
--- Sends an event to the server and triggers a callback function once the response is returned.
ServerCallback.Async = function(resource, event, delay, cb, ...)
if callbackTimer(event, delay) then
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
cb(table.unpack(response))
RemoveEventHandler(event)
end)
end
end
return ServerCallback
local ServerCallbacks = {}
---@param event string
---@param delay number prevent the event from being called for the given time
local function callbackTimer(event, delay)
if type(delay) == 'number' then
local time = GetGameTimer()
if (ServerCallbacks[event] or 0) > time then
return false
end
ServerCallbacks[event] = time + delay
end
return true
end
local function triggerCallback(event, ...)
local id = math.random(0, 100000)
event = ('__cb_%s'):format(event)
TriggerServerEvent(event, id, ...)
return event..id
end
---Sends an event to the server and triggers a callback function once the response is returned.
---```
---callback(event: string, delay: number, function(...)
--- print(...)
---end)
---```
local callback = {}
---@param event string
---@param delay number prevent the event from being called for the given time
--- Sends an event to the server and halts the current thread until a response is returned.
function callback.await(event, delay, ...)
if callbackTimer(event, delay) then
local promise = promise.new()
event = RegisterNetEvent(triggerCallback(event, ...), function(response)
promise:resolve(response)
RemoveEventHandler(event)
end)
return table.unpack(Citizen.Await(promise))
end
end
return setmetatable(callback, {
__call = function(event, delay, cb, ...)
if callbackTimer(event, delay) then
event = RegisterNetEvent(triggerCallback(event, ...), function(response)
cb(table.unpack(response))
RemoveEventHandler(event)
end)
end
end
})

View 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

View File

@@ -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

View File

@@ -1,11 +1,11 @@
--- Call on frame to disable all stored keys.
--- ```
--- DisableControlActions()
--- disableControls()
--- ```
local DisableControlActions = {}
local disableControls = {}
---@param ... number
function DisableControlActions:Add(...)
function disableControls:Add(...)
local keys = type(...) == 'table' and ... or {...}
for i=1, #keys do
local key = keys[i]
@@ -18,7 +18,7 @@ function DisableControlActions:Add(...)
end
---@param ... number
function DisableControlActions:Remove(...)
function disableControls:Remove(...)
local keys = type(...) == 'table' and ... or {...}
for i=1, #keys do
local key = keys[i]
@@ -32,22 +32,22 @@ function DisableControlActions:Remove(...)
end
---@param ... number
function DisableControlActions:Clear(...)
function disableControls:Clear(...)
local keys = type(...) == 'table' and ... or {...}
for i=1, #keys do
self[keys[i]] = nil
end
end
local Keys = {}
local keys = {}
local DisableControlAction = DisableControlAction
local pairs = pairs
return setmetatable(DisableControlActions, {
__index = Keys,
__newindex = Keys,
return setmetatable(disableControls, {
__index = keys,
__newindex = keys,
__call = function()
for k in pairs(Keys) do
for k in pairs(keys) do
DisableControlAction(0, k, true)
end
end

View File

@@ -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
error('^1Lua 5.4 must be enabled in the resource manifest!^0', 3)
end
-----------------------------------------------------------------------------------------------
-- Module
-----------------------------------------------------------------------------------------------
-- env
local lualib = 'ox_lib'
local file = IsDuplicityVersion() and 'server' or 'client'
-- micro-optimise
local rawget = rawget
local rawset = rawset
local LoadResourceFile = LoadResourceFile
local file = IsDuplicityVersion() and 'server' or 'client'
local rawset = rawset
local rawget = rawget
local function loadModule(self, module)
local dir = ('imports/%s'):format(module)
@@ -64,6 +80,11 @@ end
lib = setmetatable({
exports = {},
onCache = setmetatable({}, {
__call = function(self, key, cb)
self[key] = cb
end
})
}, {
__index = call,
__call = call,
@@ -76,7 +97,6 @@ lib = setmetatable({
return lualib
end,
})
import = lib
local intervals = {}
--- Dream of a world where this PR gets accepted.
@@ -117,18 +137,61 @@ function ClearInterval(id)
intervals[id] = -1
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
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-----------------------------------------------------------------------------------------------
-- Cache
-----------------------------------------------------------------------------------------------
-- 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.
local ox = GetResourceState('ox_core') ~= 'missing' and setmetatable({}, {
__index = function()
return true
end
}) or {
groups = GetResourceState('ox_groups') ~= 'missing',
}
-- 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>
local cache_mt = {
__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
View 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

View File

@@ -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)

View File

@@ -55,8 +55,8 @@ function lib.getVehicleProperties(vehicle)
end
for i = 0, 5 do
if IsVehicleTyreBurst(vehicle, i, true) then
damage.tyres[i] = IsVehicleTyreBurst(vehicle, i, false) and 2 or 1
if IsVehicleTyreBurst(vehicle, i, false) then
damage.tyres[i] = IsVehicleTyreBurst(vehicle, i, true) and 2 or 1
end
end