Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 497bee8
Author: Linden <[email protected]>
Date:   Sat Jul 29 07:52:49 2023 +1000

    chore: update readme

commit 99519d3
Author: daZepelin <[email protected]>
Date:   Thu Jul 27 12:44:08 2023 +0300

    feat(web/dialog): precision prop for number input (overextended#372)

    * feat(web/input): added precision prop for number input

    * fix(web/input): added missing disabled prop to checkbox

    * fix(web/input): correct icon for debug data

    * fix(web/input): remove step prop from typings

    * fix(web/debug): remove step prop

commit 95cdbd2
Author: Manifest Bumper <41898282+github-actions[bot]@users.noreply.github.com>
Date:   Thu Jul 27 04:29:34 2023 +0000

    chore: bump manifest version to v3.7.2

commit d2c5be8
Author: Linden <[email protected]>
Date:   Thu Jul 27 00:38:36 2023 +1000

    fix(client/zones): setDebug early return

commit 89b9016
Author: Linden <[email protected]>
Date:   Wed Jul 26 12:47:07 2023 +1000

    fix(init): enable ignore_invalid for msgpack

    Needed to prevent errors when packing glm polygons and such.

commit 8f0a8b3
Author: Manifest Bumper <41898282+github-actions[bot]@users.noreply.github.com>
Date:   Tue Jul 25 04:14:41 2023 +0000

    chore: bump manifest version to v3.7.1

commit fe0f289
Author: Linden <[email protected]>
Date:   Tue Jul 25 14:13:09 2023 +1000

    fix(client/zones): race condition when setting debug function

commit 7c0ee46
Author: Manifest Bumper <41898282+github-actions[bot]@users.noreply.github.com>
Date:   Tue Jul 25 03:14:32 2023 +0000

    chore: bump manifest version to v3.7.0

commit b06ba2d
Author: Linden <[email protected]>
Date:   Tue Jul 25 01:59:39 2023 +1000

    chore: type fixes

commit 8f5880a
Author: BerkieBb <[email protected]>
Date:   Sun Jul 23 11:37:29 2023 +0200

    feat(zones): add setDebug and debugColour (overextended#369)

commit 84b7f97
Author: Linden <[email protected]>
Date:   Sun Jul 23 13:41:20 2023 +1000

    fix(server/logger): missing )

commit f641881
Author: Lukas Brūzga <[email protected]>
Date:   Sun Jul 23 06:14:44 2023 +0300

    fix(client/setVehicleProperties): force clear custom colours (overextended#375)

commit 588d26c
Author: Jerry <[email protected]>
Date:   Sun Jul 23 11:14:03 2023 +0800

    fix(client/vehicleProperties): don't set modLivery if no livery exists (overextended#374)

commit 2fafee4
Author: Jellyton <[email protected]>
Date:   Sat Jul 22 19:59:41 2023 -0700

    refactor(server/logger): remove text formatting from hostname (overextended#361)

commit 65a7990
Author: Jag <[email protected]>
Date:   Fri Jul 21 04:54:29 2023 -0700

    chore: various fix-ups from docs clean-up (overextended#363)

    - Add in missing js functions
      - getCurrentRadialId
      - requestScaleformMove
    - Change up math.tohex to maintain lowercase `0x` prefix
    - Add missing fields
    - Removed extra whitespaces

    Co-authored-by: Luke <[email protected]>

commit 1b8e1f0
Author: Zan <[email protected]>
Date:   Fri Jul 21 13:46:40 2023 +0200

    chore(locales): update hu.json (overextended#371)

commit 5b9bb53
Author: Luke <[email protected]>
Date:   Thu Jul 20 22:39:56 2023 +0200

    fix(web/skillcheck): fail on wrong key press

commit 9c90b5b
Author: Linden <[email protected]>
Date:   Mon Jul 17 23:35:53 2023 +1000

    fix(server/cron): prevent scheduling tasks for new days

    From 8488de.

commit 1492130
Author: Linden <[email protected]>
Date:   Mon Jul 17 22:48:33 2023 +1000

    fix(server/cron): getTimeUnit corrections

    Consistency between range and lists.
    Use different time comparisons for minute units.

commit 8488dec
Author: Linden <[email protected]>
Date:   Mon Jul 17 20:07:30 2023 +1000

    refactor(server/cron): allow getNextTime minute and hour "overflow"

    Per the Lua manual,
    "if sec is -10, it means 10 seconds before the time specified by the other fields".
    i.e. { min = 61, hour = 0 } is equal to { min = 1, hour = 1 }

    Should improve scheduling and help with overextended#368.

commit 9e2076f
Author: Linden <[email protected]>
Date:   Mon Jul 17 19:56:58 2023 +1000

    refactor(server/cron): types and formatting, simplify getMaxDaysInMonth

commit cfcb9c1
Author: Manifest Bumper <41898282+github-actions[bot]@users.noreply.github.com>
Date:   Sun Jul 16 10:00:11 2023 +0000

    chore: bump manifest version to v3.6.3

commit 45f155f
Author: Luke <[email protected]>
Date:   Fri Jul 14 00:57:01 2023 +0200

    perf(package): only run points loop when there's at least one point

commit 6ab7196
Author: Linden <[email protected]>
Date:   Mon Jul 10 08:50:26 2023 +1000

    fix(server/cron): list expression should use >=

    Resolves overextended#366.

commit c6f621f
Author: Manifest Bumper <41898282+github-actions[bot]@users.noreply.github.com>
Date:   Thu Jun 29 15:10:03 2023 +0000

    chore: bump manifest version to v3.6.2

commit 78e0e52
Author: RIOT <[email protected]>
Date:   Sun Jun 25 11:41:35 2023 +0200

    fix(web/input): block form submission with unclamped numbers (overextended#360)

commit 45e9588
Author: Luke <[email protected]>
Date:   Sun Jun 25 00:15:34 2023 +0200

    fix(web/input): don't allow user to submit form with unclamped numbers

commit 8a0f5e9
Author: Linden <[email protected]>
Date:   Tue Jun 20 09:19:27 2023 +1000

    fix(client/zones): prevent malformed polyzone deadlock with debug

    Temp workaround.

commit 35448ff
Author: Manifest Bumper <41898282+github-actions[bot]@users.noreply.github.com>
Date:   Sat Jun 17 06:15:13 2023 +0000

    chore: bump manifest version to v3.6.1

commit 310b972
Author: Linden <[email protected]>
Date:   Sat Jun 17 16:11:08 2023 +1000

    fix(client/zoneCreator): require input confirmation

commit d070c63
Author: daZepelin <[email protected]>
Date:   Mon Jun 12 21:57:39 2023 +0300

    fix(cron) edge case fixes (overextended#345)

commit 0a3edcb
Author: Randolio <[email protected]>
Date:   Mon Jun 12 19:51:20 2023 +0100

    fix(imports/getClosestPed): correct variable name (overextended#348)

commit 1f76590
Author: Linden <[email protected]>
Date:   Tue Jun 13 00:36:17 2023 +1000

    fix(version): report unknown versions during checkDependency

    Rather than throwing potentially fatal and confusing errors we should
    just report the current version as 'unknown'.
  • Loading branch information
Moozdzn committed Jul 29, 2023
1 parent 3a947f2 commit 32c288e
Show file tree
Hide file tree
Showing 30 changed files with 247 additions and 143 deletions.
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
<div align='center'><h1><a href='https://overextended.github.io/docs/'>Documentation</a></h3></div>
<br>
# ox_lib

## ox_lib
A FiveM library and resource implementing reusable modules, methods, and UI elements.

Cfx provides functionality for loading external files in a resource through fxmanifest.
![](https://img.shields.io/github/downloads/overextended/ox_lib/total?logo=github)
![](https://img.shields.io/github/downloads/overextended/ox_lib/latest/total?logo=github)
![](https://img.shields.io/github/contributors/overextended/ox_lib?logo=github)
![](https://img.shields.io/github/v/release/overextended/ox_lib?logo=github)

```lua
lua54 'yes'
shared_script '@ox_lib/init.lua'
```
## 📚 Documentation

### server.cfg
https://overextended.dev/ox_lib

```
add_ace resource.ox_lib command.add_ace allow
add_ace resource.ox_lib command.remove_ace allow
add_ace resource.ox_lib command.add_principal allow
add_ace resource.ox_lib command.remove_principal allow
```
## 💾 Download

## License
https://github.com/overextended/ox_lib/releases/latest/download/ox_lib.zip

<a href='https://www.gnu.org/licenses/lgpl-3.0.en.html'>LGPL-3.0-or-later</a>
## npm Package

https://www.npmjs.com/package/@overextended/ox_lib

## Lua Language Server

- Install [Lua Language Server](https://marketplace.visualstudio.com/items?itemName=sumneko.lua) to ease development with annotations, type checking, diagnostics, and more.
- Install [cfxlua-vscode](https://marketplace.visualstudio.com/items?itemName=overextended.cfxlua-vscode) to add natives and cfxlua runtime declarations to LLS.
- You can load ox_lib into your global development environment by modifying workspace/user settings "Lua.workspace.library" with the resource path.
- e.g. "c:\\fxserver\\resources\\ox_lib"
- e.g. "c:/fxserver/resources/ox_lib"
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rdr3_warning 'I acknowledge that this is a prerelease build of RedM, and I am aw
--[[ Resource Information ]]--
name 'ox_lib'
author 'Overextended'
version '3.6.0'
version '3.7.2'
license 'LGPL-3.0-or-later'
repository 'https://github.com/overextended/ox_lib'
description 'A library of shared functions to utilise in other resources.'
Expand Down
4 changes: 2 additions & 2 deletions imports/addCommand/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

---@class OxCommandProperties
---@field help string?
---@field params OxCommandParams[]
---@field params OxCommandParams[]?
---@field restricted boolean | string | string[]?

---@type OxCommandProperties[]
Expand All @@ -25,7 +25,7 @@ end)
---@param source number
---@param args table
---@param raw string
---@param params OxCommandParams[]
---@param params OxCommandParams[]?
---@return table?
local function parseArguments(source, args, raw, params)
if not params then return args end
Expand Down
112 changes: 79 additions & 33 deletions imports/cron/server.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
lib.cron = {}
local currentDate = os.date('*t') --[[@as osdate]]

local currentDate = os.date('*t') --[[@as { year: number, month: number, day: number, hour: number, min: number, sec: number, wday: number, yday: number, isdst: boolean }]]
currentDate.sec = 0

---@class OxTaskProperties
---@field minute? number | string
---@field hour? number | string
---@field day? number | string
---@field month? number | string
---@field year? number | string
---@field weekday? number | string
---@field job fun(task: OxTask, date: osdate)
---@field isActive boolean
Expand All @@ -26,14 +28,22 @@ local maxUnits = {
month = 12,
}

--- Gets the amount of days in certain month
---@param month number
---@param year? number
---@return number
local function getMaxDaysInMonth(month, year)
return os.date('*t', os.time({ year = year or currentDate.year, month = month + 1, day = -1 })).day --[[@as number]]
end

---@param value string | number | nil
---@param unit string
---@return string | number | false | nil
local function getTimeUnit(value, unit)
local currentTime = currentDate[unit]
local unitMax = maxUnits[unit]

if type(value) == 'string' then
local unitMax = maxUnits[unit]
local stepValue = string.match(value, '*/(%d+)')

if stepValue then
Expand All @@ -55,9 +65,15 @@ local function getTimeUnit(value, unit)
local min, max = string.strsplit('-', range)
min, max = tonumber(min, 10), tonumber(max, 10)

if currentTime >= min and currentTime <= max then return currentTime end
if unit == 'min' then
if currentTime >= max then
return min + unitMax
end
elseif currentTime > max then
return min + unitMax
end

return min
return currentTime < min and min or currentTime
end

local list = string.match(value, '%d+,%d+')
Expand All @@ -66,20 +82,32 @@ local function getTimeUnit(value, unit)
for listValue in string.gmatch(value, '%d+') --[[@as number]] do
listValue = tonumber(listValue)

-- if current minute is less than in the expression 0,10,20,45 * * * *
if listValue > currentTime then
-- e.g. if current time is less than in the expression 0,10,20,45 * * * *
if unit == 'min' then
if currentTime < listValue then
return listValue
end
elseif currentTime <= listValue then
return listValue
end
end

-- if iterator failed, return the first value in the list
return tonumber(string.match(value, '%d+'))
return tonumber(string.match(value, '%d+')) + unitMax
end

return false
end

return value or currentTime
if value then
if unit == 'min' then
return value <= currentTime and value + unitMax or value
end

return value < currentTime and value + unitMax or value
end

return currentTime
end

---Get a timestamp for the next time to run the task today.
Expand All @@ -89,6 +117,12 @@ function OxTask:getNextTime()

local day = getTimeUnit(self.day, 'day')

-- If current day is the last day of the month, and the task is scheduled for the last day of the month, then the task should run.
if day == 0 then
-- Should probably be used month from getTimeUnit, but don't want to reorder this code.
day = getMaxDaysInMonth(currentDate.month)
end

if day ~= currentDate.day then return end

local month = getTimeUnit(self.month, 'month')
Expand All @@ -107,9 +141,16 @@ function OxTask:getNextTime()

if not hour then return end

if minute >= 60 then
minute = 0
hour += 1
end

if hour >= 24 and day then return end

return os.time({
min = minute < 60 and minute or 0,
hour = hour < 24 and hour or 0,
min = minute,
hour = hour,
day = day or currentDate.day,
month = month or currentDate.month,
year = currentDate.year,
Expand All @@ -120,28 +161,32 @@ end
---@return number
function OxTask:getAbsoluteNextTime()
local minute = getTimeUnit(self.minute, 'min')

local hour = getTimeUnit(self.hour, 'hour')

local day = getTimeUnit(self.day, 'day')
local month = getTimeUnit(self.month, 'month')
local year = getTimeUnit(self.year, 'year')

-- To avoid modifying getTimeUnit function, the day is adjusted here if needed.
if self.day then
if currentDate.hour < hour or (currentDate.hour == hour and currentDate.min < minute) then
day = day - 1
end
else
if day < 1 then
day = getMaxDaysInMonth(currentDate.month)
end
end

if currentDate.hour > hour or (currentDate.hour == hour and currentDate.min >= minute) then
day = day + 1
if day > getMaxDaysInMonth(currentDate.month) or day == 1 then
day = 1
month = month + 1
end
end
end

local month = getTimeUnit(self.month, 'month')

local year = getTimeUnit(self.year, 'year')

-- Check if time will be in next year.
if os.time({year=year, month=month, day=day, hour=hour, min=minute}) < os.time() then
---@diagnostic disable-next-line: assign-type-mismatch
if os.time({ year = year, month = month, day = day, hour = hour, min = minute }) < os.time() then
year = year and year + 1 or currentDate.year + 1
end

Expand Down Expand Up @@ -172,27 +217,28 @@ function OxTask:scheduleTask()
return self:stop()
end

if self.hour then
sleep += 86400
elseif self.minute then
sleep += 3600
end

if sleep < 0 then
sleep += 60
runAt += 60
end
sleep += 60
end

if self.debug then
print(('running task %s in %d seconds (%0.2f minutes or %0.2f hours)'):format(self.id, sleep, sleep / 60, sleep / 60 / 60))
print(('running task %s in %d seconds (%0.2f minutes or %0.2f hours)'):format(self.id, sleep, sleep / 60,
sleep / 60 / 60))
end

if sleep > 0 then Wait(sleep * 1000) end
if sleep > 0 then
Wait(sleep * 1000)
else -- will this even happen?
Wait(1000)
return true
end

if self.isActive then
self:job(currentDate)

if self.debug then
print(('(%s/%s/%s %s:%s) ran task %s'):format(currentDate.year, currentDate.month, currentDate.day, currentDate.hour, currentDate.min, self.id))
end

Wait(30000)

return true
Expand Down Expand Up @@ -260,8 +306,8 @@ end
---@param expression string A cron expression such as `* * * * *` representing minute, hour, day, month, and day of the week.
---@param job fun(task: OxTask, date: osdate)
---@param options? { debug?: boolean }
---Creates a new [cronjob](https://en.wikipedia.org/wiki/Cron), scheduling a task to run at fixed times or intervals.
---Supports numbers, any value `*`, lists `1,2,3`, ranges `1-3`, and steps `*/4`.
---Creates a new [cronjob](https://en.wikipedia.org/wiki/Cron), scheduling a task to run at fixed times or intervals.
---Supports numbers, any value `*`, lists `1,2,3`, ranges `1-3`, and steps `*/4`.
---Day of the week is a range of `1-7` starting from Sunday and allows short-names (i.e. sun, mon, tue).
function lib.cron.new(expression, job, options)
if not job or type(job) ~= 'function' then return end
Expand Down
2 changes: 1 addition & 1 deletion imports/getClosestPed/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function lib.getClosestPed(coords, maxDistance)

if distance < maxDistance then
maxDistance = distance
closestObject = ped
closestPed = ped
closestCoords = pedCoords
end
end
Expand Down
4 changes: 2 additions & 2 deletions imports/getNearbyObjects/client.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---@param coords vector3 The coords to check from.
---@param maxDistance number The max distance to check.
---@return table objects
---@return { object: number, coords: vector3 }[]
function lib.getNearbyObjects(coords, maxDistance)
local objects = GetGamePool('CObject')
local nearby = {}
Expand All @@ -21,7 +21,7 @@ function lib.getNearbyObjects(coords, maxDistance)
}
end
end

return nearby
end

Expand Down
2 changes: 1 addition & 1 deletion imports/getNearbyPeds/client.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---@param coords vector3 The coords to check from.
---@param maxDistance number The max distance to check.
---@return table peds
---@return { ped: number, coords: vector3 }[]
function lib.getNearbyPeds(coords, maxDistance)
local peds = GetGamePool('CPed')
local nearby = {}
Expand Down
2 changes: 1 addition & 1 deletion imports/getNearbyPlayers/client.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---@param coords vector3 The coords to check from.
---@param maxDistance number The max distance to check.
---@param includePlayer boolean? Whether or not to include the current player.
---@return table players
---@return { id: number, ped: number, coords: vector3 }[]
function lib.getNearbyPlayers(coords, maxDistance, includePlayer)
local players = GetActivePlayers()
local nearby = {}
Expand Down
4 changes: 2 additions & 2 deletions imports/getNearbyVehicles/client.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---@param coords vector3 The coords to check from.
---@param maxDistance number The max distance to check.
---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle.
---@return table vehicles
---@return { vehicle: number, coords: vector3 }[]
function lib.getNearbyVehicles(coords, maxDistance, includePlayerVehicle)
local vehicles = GetGamePool('CVehicle')
local nearby = {}
Expand All @@ -28,4 +28,4 @@ function lib.getNearbyVehicles(coords, maxDistance, includePlayerVehicle)
return nearby
end

return lib.getNearbyVehicles
return lib.getNearbyVehicles
16 changes: 15 additions & 1 deletion imports/logger/server.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
local service = GetConvar('ox:logger', 'datadog')
local hostname = GetConvar('sv_projectName', 'fxserver')
local buffer
local bufferSize = 0

local function removeColorCodes(str)
-- replace ^[0-9] with nothing
str = string.gsub(str, "%^%d", "")

-- replace ^#[0-9A-F]{3,6} with nothing
str = string.gsub(str, "%^#[%dA-Fa-f]+", "")

-- replace ~[a-z]~ with nothing
str = string.gsub(str, "~[%a]~", "")

return str
end

local hostname = removeColorCodes(GetConvar('ox:logger:hostname', GetConvar('sv_projectName', 'fxserver')))

local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

local function base64encode(data)
Expand Down
6 changes: 3 additions & 3 deletions imports/math/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ end
---@param upper? boolean
---@return string
function math.tohex(n, upper)
n = ('0x%x'):format(n)
return upper and n:upper() or n
local formatString = ('0x%s'):format(upper and '%X' or '%x')
return formatString:format(n)
end

---Converts input number into grouped digits
Expand All @@ -99,4 +99,4 @@ function math.groupdigits(number, seperator) -- credit http://richard.warburton.
return left..(num:reverse():gsub('(%d%d%d)','%1' .. (seperator or ',')):reverse())..right
end

return lib.math
return lib.math
Loading

0 comments on commit 32c288e

Please sign in to comment.