refactor(imports): assign modules directly to lib namespace

Mostly a change for improved intellisense with sumneko lua.
Includes some type fixes and deprecation notices.
This commit is contained in:
Linden
2022-09-14 01:20:50 +10:00
parent 107d812dc3
commit 26538b8148
19 changed files with 98 additions and 61 deletions

View File

@@ -7,11 +7,10 @@ root = true
end_of_line = lf end_of_line = lf
insert_final_newline = false insert_final_newline = false
charset = utf-8 charset = utf-8
trim_trailing_whitespace = true trim_trailing_whitespace = false
indent_size = 2 indent_size = 2
indent_style = space indent_style = space
max_line_length = 120 max_line_length = 120
[*.lua] [*.lua]
indent_size = 4 indent_size = 4
indent_style = tab

View File

@@ -37,18 +37,18 @@ end
---@param name string ---@param name string
---@param callback function ---@param callback function
---@param parameters table ---@param parameters table
local function addCommand(group, name, callback, parameters, help) function lib.addCommand(group, name, callback, parameters, help)
if not group then group = 'builtin.everyone' end if not group then group = 'builtin.everyone' end
if type(name) == 'table' then if type(name) == 'table' then
for i = 1, #name do for i = 1, #name do
addCommand(group, name[i], callback, parameters, help) lib.addCommand(group, name[i], callback, parameters, help)
end end
else else
chatSuggestion(name, parameters, help) chatSuggestion(name, parameters, help)
RegisterCommand(name, function(source, args) RegisterCommand(name, function(source, args)
source = tonumber(source) source = tonumber(source) --[[@as number]]
if parameters then if parameters then
for i = 1, #parameters do for i = 1, #parameters do
@@ -100,7 +100,7 @@ local function addCommand(group, name, callback, parameters, help)
end end
end end
return addCommand return lib.addCommand
--[[ Example --[[ Example
AddCommand('group.admin', {'additem', 'giveitem'}, function(source, args) AddCommand('group.admin', {'additem', 'giveitem'}, function(source, args)

View File

@@ -67,7 +67,7 @@ lib.callback = setmetatable({}, {
---@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.
function lib.callback.await(event, delay, ...) function lib.callback.await(event, delay, ...)
return triggerServerCallback(_, event, delay, false, ...) return triggerServerCallback(nil, event, delay, false, ...)
end end
local function callbackResponse(success, result, ...) local function callbackResponse(success, result, ...)

View File

@@ -48,7 +48,7 @@ lib.callback = setmetatable({}, {
---@param playerId number ---@param playerId number
--- Sends an event to a client and halts the current thread until a response is returned. --- Sends an event to a client and halts the current thread until a response is returned.
function lib.callback.await(event, playerId, ...) function lib.callback.await(event, playerId, ...)
return triggerClientCallback(_, event, playerId, false, ...) return triggerClientCallback(nil, event, playerId, false, ...)
end end
local function callbackResponse(success, result, ...) local function callbackResponse(success, result, ...)

View File

@@ -43,7 +43,7 @@ local keys = {}
local DisableControlAction = DisableControlAction local DisableControlAction = DisableControlAction
local pairs = pairs local pairs = pairs
return setmetatable(disableControls, { lib.disableControls = setmetatable(disableControls, {
__index = keys, __index = keys,
__newindex = keys, __newindex = keys,
__call = function() __call = function()
@@ -52,3 +52,5 @@ return setmetatable(disableControls, {
end end
end end
}) })
return lib.disableControls

View File

@@ -4,7 +4,7 @@
---@return number? playerId ---@return number? playerId
---@return number? playerPed ---@return number? playerPed
---@return vector3? playerCoords ---@return vector3? playerCoords
return function(coords, maxDistance, includePlayer) function lib.getClosestPlayer(coords, maxDistance, includePlayer)
local players = GetActivePlayers() local players = GetActivePlayers()
local closestId, closestPed, closestCoords local closestId, closestPed, closestCoords
maxDistance = maxDistance or 2.0 maxDistance = maxDistance or 2.0
@@ -28,3 +28,5 @@ return function(coords, maxDistance, includePlayer)
return closestId, closestPed, closestCoords return closestId, closestPed, closestCoords
end end
return lib.getClosestPlayer

View File

@@ -3,7 +3,7 @@
---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle. ---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle.
---@return number? vehicle ---@return number? vehicle
---@return vector3? vehicleCoords ---@return vector3? vehicleCoords
return function(coords, maxDistance, includePlayerVehicle) function lib.getClosestVehicle(coords, maxDistance, includePlayerVehicle)
local vehicles = GetGamePool('CVehicle') local vehicles = GetGamePool('CVehicle')
local closestVehicle, closestCoords local closestVehicle, closestCoords
maxDistance = maxDistance or 2.0 maxDistance = maxDistance or 2.0
@@ -25,3 +25,5 @@ return function(coords, maxDistance, includePlayerVehicle)
return closestVehicle, closestCoords return closestVehicle, closestCoords
end end
return lib.getClosestVehicle

View File

@@ -1,10 +1,16 @@
--[[
This module was experimental and won't be worked on or used further.
May be removed in the future.
]]
local Core = { local Core = {
Ox = 'ox_core', Ox = 'ox_core',
QB = 'qb-core', QB = 'qb-core',
ESX = 'es_extended', ESX = 'es_extended',
} }
return function() ---@deprecated
function lib.getCore()
local result local result
Citizen.CreateThreadNow(function() Citizen.CreateThreadNow(function()
@@ -53,6 +59,7 @@ return function()
if not success then error(result) end if not success then error(result) end
if framework == Core.Ox then if framework == Core.Ox then
---@diagnostic disable-next-line: undefined-global
result = Ox result = Ox
end end
@@ -67,3 +74,5 @@ return function()
return result return result
end end
return lib.getCore

View File

@@ -2,30 +2,32 @@
---@param maxDistance number The max distance to check. ---@param maxDistance number The max distance to check.
---@param includePlayer boolean Whether or not to include the current player. ---@param includePlayer boolean Whether or not to include the current player.
---@return number[] ---@return number[]
return function(coords, maxDistance, includePlayer) function lib.getNearbyPlayers(coords, maxDistance, includePlayer)
local players = GetActivePlayers() local players = GetActivePlayers()
local nearby = {} local nearby = {}
local count = 0 local count = 0
maxDistance = maxDistance or 2.0 maxDistance = maxDistance or 2.0
for i = 1, #players do for i = 1, #players do
local playerId = players[i] local playerId = players[i]
if playerId ~= cache.playerId or includePlayer then if playerId ~= cache.playerId or includePlayer then
local playerPed = GetPlayerPed(playerId) local playerPed = GetPlayerPed(playerId)
local playerCoords = GetEntityCoords(playerPed) local playerCoords = GetEntityCoords(playerPed)
local distance = #(coords - playerCoords) local distance = #(coords - playerCoords)
if distance < maxDistance then if distance < maxDistance then
count += 1 count += 1
nearby[count] = { nearby[count] = {
id = playerId, id = playerId,
ped = playerPed, ped = playerPed,
coords = playerCoords, coords = playerCoords,
} }
end end
end end
end end
return nearby return nearby
end end
return lib.getNearbyPlayers

View File

@@ -2,7 +2,7 @@
---@param maxDistance number The max distance to check. ---@param maxDistance number The max distance to check.
---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle. ---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle.
---@return number[] ---@return number[]
return function(coords, maxDistance, includePlayerVehicle) function lib.getNearbyVehicles(coords, maxDistance, includePlayerVehicle)
local vehicles = GetGamePool('CVehicle') local vehicles = GetGamePool('CVehicle')
local nearby = {} local nearby = {}
local count = 0 local count = 0
@@ -27,3 +27,5 @@ return function(coords, maxDistance, includePlayerVehicle)
return nearby return nearby
end end
return lib.getNearbyVehicles

View File

@@ -14,15 +14,16 @@ function locale(str, ...)
return ("Translation for '%s' does not exist"):format(str) return ("Translation for '%s' does not exist"):format(str)
end end
local function loadLocale(locale) function lib.loadLocale(locale)
if not locale then
locale = lib.service == 'server' and lib.getServerLocale() or GetExternalKvpString('ox_lib', 'locale') or 'en'
end
local resourceName = GetCurrentResourceName() local resourceName = GetCurrentResourceName()
local JSON = LoadResourceFile(resourceName, ('locales/%s.json'):format(locale)) or LoadResourceFile(resourceName, ('locales/en.json'):format(locale)) local JSON = LoadResourceFile(resourceName, ('locales/%s.json'):format(locale)) or LoadResourceFile(resourceName, ('locales/en.json'):format(locale))
dict = JSON and json.decode(JSON) or {} dict = JSON and json.decode(JSON) or {}
end end
AddEventHandler('ox_lib:setLocale', loadLocale) AddEventHandler('ox_lib:setLocale', lib.loadLocale)
return function() return lib.loadLocale
local lang = lib.service == 'server' and lib.getServerLocale() or GetExternalKvpString('ox_lib', 'locale') or 'en'
loadLocale(lang)
end

View File

@@ -5,7 +5,7 @@ if key ~= '' then
local resourceName = GetCurrentResourceName() local resourceName = GetCurrentResourceName()
key = key:gsub("[\'\"]", '') key = key:gsub("[\'\"]", '')
return function(source, event, message, ...) function lib.logger(source, event, message, ...)
local data = json.encode({ local data = json.encode({
hostname = resourceName, hostname = resourceName,
service = event, service = event,
@@ -27,4 +27,4 @@ if key ~= '' then
end end
end end
return function() end return lib.logger or function() end

View File

@@ -1,3 +1,8 @@
--[[
This module was experimental and won't be worked on or used further.
May be removed in the future.
]]
local CPlayer = {} local CPlayer = {}
function CPlayer:__index(index, ...) function CPlayer:__index(index, ...)
@@ -22,6 +27,7 @@ function CPlayer:getDistance(coords)
return #(self:getCoords() - coords) return #(self:getCoords() - coords)
end end
---@deprecated
function lib.getPlayer() function lib.getPlayer()
return CPlayer return CPlayer
end end

View File

@@ -1,3 +1,8 @@
--[[
This module was experimental and won't be worked on or used further.
May be removed in the future.
]]
local CPlayer = {} local CPlayer = {}
function CPlayer:__index(index, ...) function CPlayer:__index(index, ...)
@@ -23,13 +28,11 @@ function CPlayer:getDistance(coords)
end end
function CPlayer:getPed() function CPlayer:getPed()
if update or not self.ped then self.ped = GetPlayerPed(self.source)
self.ped = GetPlayerPed(self.source)
end
return self.ped return self.ped
end end
---@deprecated
function lib.getPlayer() function lib.getPlayer()
return CPlayer return CPlayer
end end

View File

@@ -23,6 +23,7 @@ CreateThread(function()
if distance <= point.distance then if distance <= point.distance then
point.currentDistance = distance point.currentDistance = distance
---@diagnostic disable-next-line: need-check-nil
if distance < (closest?.currentDistance or point.distance) then if distance < (closest?.currentDistance or point.distance) then
closest = point closest = point
end end
@@ -56,7 +57,7 @@ CreateThread(function()
end end
end) end)
return { lib.points = {
new = function(...) new = function(...)
local args = {...} local args = {...}
local id = #points + 1 local id = #points + 1
@@ -94,3 +95,5 @@ return {
return closest return closest
end end
} }
return lib.points

View File

@@ -1,7 +1,6 @@
local StartShapeTestLosProbe = StartShapeTestLosProbe local StartShapeTestLosProbe = StartShapeTestLosProbe
local GetShapeTestResultIncludingMaterial = GetShapeTestResultIncludingMaterial local GetShapeTestResultIncludingMaterial = GetShapeTestResultIncludingMaterial
local GetWorldCoordFromScreenCoord = GetWorldCoordFromScreenCoord local GetWorldCoordFromScreenCoord = GetWorldCoordFromScreenCoord
local raycast = {}
---@param flags number? Defaults to 1|2|8|16 (see: https://docs.fivem.net/natives/?_0x377906D8A31E5586) ---@param flags number? Defaults to 1|2|8|16 (see: https://docs.fivem.net/natives/?_0x377906D8A31E5586)
---@param p8 number? A bit mask with bits 1, 2, 4, or 7 relating to collider types. 4 and 7 are usually used. ---@param p8 number? A bit mask with bits 1, 2, 4, or 7 relating to collider types. 4 and 7 are usually used.
@@ -10,7 +9,7 @@ local raycast = {}
---@return vector3 endCoords ---@return vector3 endCoords
---@return vector3 surfaceNormal ---@return vector3 surfaceNormal
---@return number materialHash ---@return number materialHash
function raycast.cam(flags, p8) function lib.raycast.cam(flags, p8)
local coords, normal = GetWorldCoordFromScreenCoord(0.5, 0.5) local coords, normal = GetWorldCoordFromScreenCoord(0.5, 0.5)
local destination = coords + normal * 10 local destination = coords + normal * 10
local handle = StartShapeTestLosProbe(coords.x, coords.y, coords.z, destination.x, destination.y, destination.z, local handle = StartShapeTestLosProbe(coords.x, coords.y, coords.z, destination.x, destination.y, destination.z,
@@ -27,4 +26,4 @@ function raycast.cam(flags, p8)
end end
end end
return raycast return lib.raycast

View File

@@ -55,4 +55,6 @@ local function table_deepclone(tbl)
end end
table.deepclone = table_deepclone table.deepclone = table_deepclone
lib.table = table
return table return table

View File

@@ -100,6 +100,7 @@ local function getTriangles(polygon)
for i = 1, #sides do for i = 1, #sides do
local side = sides[i] local side = sides[i]
---@type number | function
local direction = side[1].y - side[2].y local direction = side[1].y - side[2].y
direction = direction > 0 and up or down direction = direction > 0 and up or down
table.sort(side, direction) table.sort(side, direction)
@@ -113,6 +114,7 @@ local function getTriangles(polygon)
local c, d local c, d
if aHorizontal[2] then if aHorizontal[2] then
---@type number | function
local direction = a.x - (a.x ~= aHorizontal[1].x and aHorizontal[1].x or aHorizontal[2].x) local direction = a.x - (a.x ~= aHorizontal[1].x and aHorizontal[1].x or aHorizontal[2].x)
direction = direction > 0 and right or left direction = direction > 0 and right or left
table.sort(aHorizontal, direction) table.sort(aHorizontal, direction)
@@ -129,6 +131,7 @@ local function getTriangles(polygon)
end end
if bHorizontal[2] then if bHorizontal[2] then
---@type number | function
local direction = b.x - (b.x ~= bHorizontal[1].x and bHorizontal[1].x or bHorizontal[2].x) local direction = b.x - (b.x ~= bHorizontal[1].x and bHorizontal[1].x or bHorizontal[2].x)
direction = direction > 0 and right or left direction = direction > 0 and right or left
table.sort(bHorizontal, direction) table.sort(bHorizontal, direction)
@@ -324,7 +327,7 @@ end
local function debugSphere(self) local function debugSphere(self)
DrawMarker(28, self.coords.x, self.coords.y, self.coords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, self.radius, self.radius, DrawMarker(28, self.coords.x, self.coords.y, self.coords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, self.radius, self.radius,
self.radius, 255, 42, 24, 100, false, false, false, true, false, false, false) self.radius, 255, 42, 24, 100, false, false, 0, true, false, false, false)
end end
local function contains(self, coords) local function contains(self, coords)
@@ -349,7 +352,7 @@ local function convertToVector(coords)
return coords return coords
end end
return { lib.zones = {
poly = function(data) poly = function(data)
data.id = #Zones + 1 data.id = #Zones + 1
data.thickness = data.thickness or 4 data.thickness = data.thickness or 4
@@ -415,3 +418,5 @@ return {
return data return data
end, end,
} }
return lib.zones

View File

@@ -149,7 +149,7 @@ function lib.cancelProgress()
if not progress then if not progress then
error('No progress bar is active') error('No progress bar is active')
elseif not progress.canCancel then elseif not progress.canCancel then
error(("Progress bar '%s' cannot be cancelled"):format(id)) error('Progress bar cannot be cancelled')
end end
progress = false progress = false