Skip to content

Commit

Permalink
Merge pull request #9 from overextended/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat authored Mar 2, 2022
2 parents 381935e + cdc7523 commit 2a14391
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 162 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(...)
Expand Down
4 changes: 2 additions & 2 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
File renamed without changes.
108 changes: 54 additions & 54 deletions imports/callbacks/client.lua → imports/callback/client.lua
Original file line number Diff line number Diff line change
@@ -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
})
14 changes: 14 additions & 0 deletions imports/callback/server.lua
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions imports/callbacks/server.lua

This file was deleted.

20 changes: 10 additions & 10 deletions imports/controls/client.lua → imports/disableControls/client.lua
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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]
Expand All @@ -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
Expand Down
97 changes: 80 additions & 17 deletions init.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -64,6 +80,11 @@ end

lib = setmetatable({
exports = {},
onCache = setmetatable({}, {
__call = function(self, key, cb)
self[key] = cb
end
})
}, {
__index = call,
__call = call,
Expand All @@ -76,7 +97,6 @@ lib = setmetatable({
return lualib
end,
})
import = lib

local intervals = {}
--- Dream of a world where this PR gets accepted.
Expand Down Expand Up @@ -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
Loading

0 comments on commit 2a14391

Please sign in to comment.