From c4a840f2c127d4b9f5848fe0b2ddcba14900982f Mon Sep 17 00:00:00 2001 From: Manason Date: Tue, 17 Dec 2024 08:48:30 -0800 Subject: [PATCH 1/3] feat: semaphore syncing --- client.lua | 40 ++++++++++++++++++++++++++++++++++++++++ config/client.lua | 4 ++++ fxmanifest.lua | 18 ++++++++++++++++++ types.lua | 5 +++++ 4 files changed, 67 insertions(+) create mode 100644 client.lua create mode 100644 config/client.lua create mode 100644 fxmanifest.lua create mode 100644 types.lua diff --git a/client.lua b/client.lua new file mode 100644 index 0000000..f7e5c61 --- /dev/null +++ b/client.lua @@ -0,0 +1,40 @@ +local config = require 'config.client' + +---@type table +local states = {} + +---Sets a boolean state. Runs its onSet function if the state is not previously set and the onSet function exists in config +---@param name string +---@return boolean firstSetter true if the state had not previously been set +local function setState(name) + if not states[name] then + if config.states[name]?.onSet then + config.states[name].onSet() + end + states[name] = 1 + return true + else + states[name] += 1 + return false + end +end + +---Releases a boolean state. Runs its onRelease function if this is the last call to release the state and the release function exists in config. +---@param name string +---@return boolean lastReleaser true if the state is fully released after this function +local function releaseState(name) + if not states[name] then states[name] = 0 end + if states[name] <= 1 then + states[name] = nil + if config.states[name]?.onRelease() then + config.states[name].onRelease() + end + return true + else + states[name] -= 1 + return false + end +end + +exports('SetState', setState) +exports('ReleaseState', releaseState) \ No newline at end of file diff --git a/config/client.lua b/config/client.lua new file mode 100644 index 0000000..4988e81 --- /dev/null +++ b/config/client.lua @@ -0,0 +1,4 @@ +return { + ---@type table + states = {} +} \ No newline at end of file diff --git a/fxmanifest.lua b/fxmanifest.lua new file mode 100644 index 0000000..06b21d4 --- /dev/null +++ b/fxmanifest.lua @@ -0,0 +1,18 @@ +fx_version 'cerulean' +game 'gta5' + +name 'qbx_playerstates' +description 'Manage the state of the player' +repository 'https://github.com/Qbox-project/qbx_playerstates' +version '0.0.0' + +client_scripts { + 'client.lua', +} + +files { + 'config/client.lua', +} + +lua54 'yes' +use_experimental_fxv2_oal 'yes' \ No newline at end of file diff --git a/types.lua b/types.lua new file mode 100644 index 0000000..1e8c00e --- /dev/null +++ b/types.lua @@ -0,0 +1,5 @@ +---@meta + +---@class State +---@field onSet? function +---@field onRelease? function \ No newline at end of file From 93006655b50213fbe3bc5323523df9d2de08d399 Mon Sep 17 00:00:00 2001 From: Manason Date: Thu, 19 Dec 2024 09:19:18 -0800 Subject: [PATCH 2/3] feat: optional force param --- client.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/client.lua b/client.lua index f7e5c61..21890c3 100644 --- a/client.lua +++ b/client.lua @@ -21,10 +21,11 @@ end ---Releases a boolean state. Runs its onRelease function if this is the last call to release the state and the release function exists in config. ---@param name string +---@param force? boolean if true, releases the state regardless of the number of locks on it ---@return boolean lastReleaser true if the state is fully released after this function -local function releaseState(name) - if not states[name] then states[name] = 0 end - if states[name] <= 1 then +local function releaseState(name, force) + if not states[name] then return true end + if states[name] <= 1 or force then states[name] = nil if config.states[name]?.onRelease() then config.states[name].onRelease() From b59d74359dec4188e453860ede76e3ad3d83bcc1 Mon Sep 17 00:00:00 2001 From: Manason Date: Thu, 19 Dec 2024 09:22:55 -0800 Subject: [PATCH 3/3] already released returns false --- client.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.lua b/client.lua index 21890c3..be53809 100644 --- a/client.lua +++ b/client.lua @@ -24,7 +24,7 @@ end ---@param force? boolean if true, releases the state regardless of the number of locks on it ---@return boolean lastReleaser true if the state is fully released after this function local function releaseState(name, force) - if not states[name] then return true end + if not states[name] then return false end if states[name] <= 1 or force then states[name] = nil if config.states[name]?.onRelease() then