mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
refactor: rename imports
BREAKING! Since the folder name determines the import name (i.e. lib.callbacks), use better names and lowerCamelCase.
This commit is contained in:
@@ -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 startCallback(resource, event, ...)
|
||||||
local id = math.random(0, 100000)
|
local id = math.random(0, 100000)
|
||||||
event = ('__cb_%s:%s'):format(resource, event)
|
event = ('__cb_%s:%s'):format(resource, event)
|
||||||
TriggerServerEvent(event, id, ...)
|
TriggerServerEvent(event, id, ...)
|
||||||
return event..id
|
return event..id
|
||||||
end
|
end
|
||||||
|
|
||||||
local ServerCallback = table.create(0, 2)
|
local ServerCallback = table.create(0, 2)
|
||||||
|
|
||||||
---@param resource string
|
---@param resource string
|
||||||
---@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
|
||||||
--- Sends an event to the server and halts the current thread until a response is returned.
|
--- Sends an event to the server and halts the current thread until a response is returned.
|
||||||
ServerCallback.Await = function(resource, event, delay, ...)
|
ServerCallback.Await = function(resource, event, delay, ...)
|
||||||
if callbackTimer(event, delay) then
|
if callbackTimer(event, delay) then
|
||||||
local promise = promise.new()
|
local promise = promise.new()
|
||||||
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
||||||
promise:resolve(response)
|
promise:resolve(response)
|
||||||
RemoveEventHandler(event)
|
RemoveEventHandler(event)
|
||||||
end)
|
end)
|
||||||
return table.unpack(Citizen.Await(promise))
|
return table.unpack(Citizen.Await(promise))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param resource string
|
---@param resource string
|
||||||
---@param event string
|
---@param event string
|
||||||
---@param delay number
|
---@param delay number
|
||||||
---@param cb function
|
---@param cb function
|
||||||
--- Sends an event to the server and triggers a callback function once the response is returned.
|
--- Sends an event to the server and triggers a callback function once the response is returned.
|
||||||
ServerCallback.Async = function(resource, event, delay, cb, ...)
|
ServerCallback.Async = function(resource, event, delay, cb, ...)
|
||||||
if callbackTimer(event, delay) then
|
if callbackTimer(event, delay) then
|
||||||
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
event = RegisterNetEvent(startCallback(resource, event, ...), function(response)
|
||||||
cb(table.unpack(response))
|
cb(table.unpack(response))
|
||||||
RemoveEventHandler(event)
|
RemoveEventHandler(event)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return ServerCallback
|
return ServerCallback
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
local ServerCallback = {}
|
local ServerCallback = {}
|
||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param callback function
|
---@param callback 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.
|
||||||
ServerCallback.Register = function(name, callback)
|
ServerCallback.Register = function(name, callback)
|
||||||
name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name)
|
name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name)
|
||||||
|
|
||||||
RegisterServerEvent(name, function(id, ...)
|
RegisterServerEvent(name, function(id, ...)
|
||||||
TriggerClientEvent(name..id, source, {callback(source, ...)})
|
TriggerClientEvent(name..id, source, {callback(source, ...)})
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
return ServerCallback
|
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
|
||||||
7
init.lua
7
init.lua
@@ -2,19 +2,18 @@ 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user