Skip to content

Commit

Permalink
Merge branch 'overextended:master' into Redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
mur4i authored Aug 10, 2024
2 parents 78e8ce3 + e10e8af commit 9a1330d
Show file tree
Hide file tree
Showing 20 changed files with 184 additions and 126 deletions.
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ rdr3_warning 'I acknowledge that this is a prerelease build of RedM, and I am aw

name 'ox_lib'
author 'Overextended'
version '3.22.1'
version '3.24.0'
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
2 changes: 1 addition & 1 deletion imports/__addCommand/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SetTimeout(1000, function()
TriggerClientEvent('chat:addSuggestions', -1, commands)
end)

AddEventHandler('playerJoining', function(source)
AddEventHandler('playerJoining', function()
TriggerClientEvent('chat:addSuggestions', source, commands)
end)

Expand Down
15 changes: 12 additions & 3 deletions imports/addCommand/server.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---@class OxCommandParams
---@field name string
---@field help? string
---@field type? 'number' | 'playerId' | 'string'
---@field type? 'number' | 'playerId' | 'string' | 'longString'
---@field optional? boolean

---@class OxCommandProperties
Expand All @@ -18,7 +18,7 @@ SetTimeout(1000, function()
TriggerClientEvent('chat:addSuggestions', -1, registeredCommands)
end)

AddEventHandler('playerJoining', function(source)
AddEventHandler('playerJoining', function()
TriggerClientEvent('chat:addSuggestions', source, registeredCommands)
end)

Expand All @@ -30,7 +30,8 @@ end)
local function parseArguments(source, args, raw, params)
if not params then return args end

for i = 1, #params do
local paramsNum = #params
for i = 1, paramsNum do
local arg, param = args[i], params[i]
local value

Expand All @@ -44,6 +45,13 @@ local function parseArguments(source, args, raw, params)
if not value or not DoesPlayerExist(value--[[@as string]]) then
value = false
end
elseif param.type == 'longString' and i == paramsNum then
if arg then
local start = raw:find(arg, 1, true)
value = start and raw:sub(start)
else
value = nil
end
else
value = arg
end
Expand Down Expand Up @@ -131,6 +139,7 @@ function lib.addCommand(commandName, properties, cb, ...)
end

if properties then
---@diagnostic disable-next-line: inject-field
properties.name = ('/%s'):format(commandName)
properties.restricted = nil
registeredCommands[totalCommands] = properties
Expand Down
40 changes: 19 additions & 21 deletions imports/array/shared.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---@class Array : OxClass
local Array = lib.class('Array')
lib.array = lib.class('Array')

---@alias ArrayLike<T> Array | { [number]: T }

---@private
function Array:constructor(...)
function lib.array:constructor(...)
local arr = { ... }

for i = 1, #arr do
Expand All @@ -13,15 +13,15 @@ function Array:constructor(...)
end

---@private
function Array:__newindex(index, value)
function lib.array:__newindex(index, value)
if type(index) ~= 'number' then error(("Cannot insert non-number index '%s' into an array."):format(index)) end

rawset(self, index, value)
end

---Create a new array containing the elements from two arrays.
---@param arr ArrayLike
function Array:merge(arr)
function lib.array:merge(arr)
local newArr = table.clone(self)
local length = #self

Expand All @@ -30,12 +30,12 @@ function Array:merge(arr)
newArr[length] = arr[i]
end

return Array:new(table.unpack(newArr))
return lib.array:new(table.unpack(newArr))
end

---Tests if all elements in an array succeed in passing the provided test function.
---@param testFn fun(element: unknown): boolean
function Array:every(testFn)
function lib.array:every(testFn)
for i = 1, #self do
if not testFn(self[i]) then
return false
Expand All @@ -47,7 +47,7 @@ end

---Creates a new array containing the elements from an array thtat pass the test of the provided function.
---@param testFn fun(element: unknown): boolean
function Array:filter(testFn)
function lib.array:filter(testFn)
local newArr = {}
local length = 0

Expand All @@ -60,13 +60,13 @@ function Array:filter(testFn)
end
end

return Array:new(table.unpack(newArr))
return lib.array:new(table.unpack(newArr))
end

---Returns the first or last element of an array that passes the provided test function.
---@param testFn fun(element: unknown): boolean
---@param last? boolean
function Array:find(testFn, last)
function lib.array:find(testFn, last)
local a = last and #self or 1
local b = last and 1 or #self
local c = last and -1 or 1
Expand All @@ -83,7 +83,7 @@ end
---Returns the first or last index of the first element of an array that passes the provided test function.
---@param testFn fun(element: unknown): boolean
---@param last? boolean
function Array:findIndex(testFn, last)
function lib.array:findIndex(testFn, last)
local a = last and #self or 1
local b = last and 1 or #self
local c = last and -1 or 1
Expand All @@ -100,7 +100,7 @@ end
---Returns the first or last index of the first element of an array that matches the provided value.
---@param value unknown
---@param last? boolean
function Array:indexOf(value, last)
function lib.array:indexOf(value, last)
local a = last and #self or 1
local b = last and 1 or #self
local c = last and -1 or 1
Expand All @@ -116,26 +116,26 @@ end

---Executes the provided function for each element in an array.
---@param cb fun(element: unknown)
function Array:forEach(cb)
function lib.array:forEach(cb)
for i = 1, #self do
cb(self[i])
end
end

---Concatenates all array elements into a string, seperated by commas or the specified seperator.
---@param seperator? string
function Array:join(seperator)
function lib.array:join(seperator)
return table.concat(self, seperator or ',')
end

---Removes the last element from an array and returns the removed element.
function Array:pop()
function lib.array:pop()
return table.remove(self)
end

---Adds the given elements to the end of an array and returns the new array length.
---@param ... any
function Array:push(...)
function lib.array:push(...)
local elements = { ... }
local length = #self

Expand All @@ -148,7 +148,7 @@ function Array:push(...)
end

---Removes the first element from an array and returns the removed element.
function Array:shift()
function lib.array:shift()
return table.remove(self, 1)
end

Expand All @@ -158,7 +158,7 @@ end
---@param reducer fun(accumulator: T, currentValue: T, index?: number): T
---@param initialValue? T
---@return T
function Array:reduce(reducer, initialValue)
function lib.array:reduce(reducer, initialValue)
local initialIndex = initialValue and 1 or 2
local accumulator = initialValue or self[1]

Expand All @@ -172,18 +172,16 @@ end
---Returns true if the given table is an instance of array or an array-like table.
---@param tbl ArrayLike
---@return boolean
function Array.isArray(tbl)
function lib.array.isArray(tbl)
if not type(tbl) == 'table' then return false end

local tableType = table.type(tbl)

if tableType == 'array' or tableType == 'empty' or Array.instanceOf(tbl, Array) then
if tableType == 'array' or tableType == 'empty' or lib.array.instanceOf(tbl, lib.array) then
return true
end

return false
end

lib.array = Array

return lib.array
7 changes: 4 additions & 3 deletions imports/class/shared.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---@diagnostic disable: invisible
lib.print.warn('ox_lib\'s class module is experimental and may break without warning.')
local getinfo = debug.getinfo

---Ensure the given argument or property has a valid type, otherwise throwing an error.
---@param id number | string
Expand Down Expand Up @@ -31,7 +31,6 @@ end
---@field protected constructor? OxClassConstructor
local mixins = {}
local constructors = {}
local getinfo = debug.getinfo

---Somewhat hacky way to remove the constructor from the class.__index.
---Maybe add static fields in the future?
Expand Down Expand Up @@ -129,8 +128,10 @@ end

---Creates a new class.
---@generic S : OxClass
---@param name string
---@generic T : string
---@param name `T`
---@param super? S
---@return `T`
function lib.class(name, super)
assertType(1, name, 'string')

Expand Down
5 changes: 2 additions & 3 deletions imports/logger/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ if service == 'loki' then
local timestamp = ('%s000000000'):format(os.time(os.date('*t')))

-- Initializes values table with the message
local values = {}
local values = {message = message}

-- Format the args into strings
local tags = formatTags(source, ... and string.strjoin(',', string.tostringall(...)) or nil)
Expand All @@ -264,8 +264,7 @@ if service == 'loki' then
values = {
{
timestamp,
message,
values
json.encode(values)
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions imports/math/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,25 @@ function math.lerp(start, finish, duration)
end
end

---Rounds a number to a whole number or to the specified number of decimal places.
---@param value number | string
---@param places? number | string
---@return number
function math.round(value, places)
if type(value) == 'string' then value = tonumber(value) end
if type(value) ~= 'number' then error('Value must be a number') end

if places then
if type(places) == 'string' then places = tonumber(places) end
if type(places) ~= 'number' then error('Places must be a number') end

if places > 0 then
local mult = 10 ^ (places or 0)
return math.floor(value * mult + 0.5) / mult
end
end

return math.floor(value + 0.5)
end

return lib.math
6 changes: 5 additions & 1 deletion imports/print/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ local levelPrefixes = {

local resourcePrintLevel = printLevel[GetConvar('ox:printlevel:' .. cache.resource, GetConvar('ox:printlevel', 'info'))]
local template = ('^5[%s] %%s %%s^7'):format(cache.resource)
local jsonOptions = { sort_keys = true, indent = true }
local function handleException(reason, value)
if type(value) == 'function' then return tostring(value) end
return reason
end
local jsonOptions = { sort_keys = true, indent = true, exception = handleException }

---Prints to console conditionally based on what ox:printlevel is.
---Any print with a level more severe will also print. If ox:printlevel is info, then warn and error prints will appear as well, but debug prints will not.
Expand Down
2 changes: 1 addition & 1 deletion imports/requestModel/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function lib.requestModel(model, timeout)
if type(model) ~= 'number' then model = joaat(model) end
if HasModelLoaded(model) then return model end

if not IsModelValid(model) then
if not IsModelValid(model) and not IsModelInCdimage(model) then
error(("attempted to load invalid model '%s'"):format(model))
end

Expand Down
18 changes: 6 additions & 12 deletions imports/timer/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,22 @@ function timer:start(async)

self.private.startTime = GetGameTimer()

local function tick(instance)
while true do
while instance.private.paused do
local function tick()
while self:getTimeLeft('ms') > 0 do
while self:isPaused() do
Wait(0)
end

if instance:getTimeLeft('ms') <= 0 then
break
end

Wait(0)
end
self:onEnd()
end

if async then
Citizen.CreateThreadNow(function()
tick(self)
self:onEnd()
tick()
end)
else
tick(self)
self:onEnd()
tick()
end
end

Expand Down
Loading

0 comments on commit 9a1330d

Please sign in to comment.