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
insert_final_newline = false
charset = utf-8
trim_trailing_whitespace = true
trim_trailing_whitespace = false
indent_size = 2
indent_style = space
max_line_length = 120
[*.lua]
indent_size = 4
indent_style = tab

View File

@@ -37,18 +37,18 @@ end
---@param name string
---@param callback function
---@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 type(name) == 'table' then
for i = 1, #name do
addCommand(group, name[i], callback, parameters, help)
lib.addCommand(group, name[i], callback, parameters, help)
end
else
chatSuggestion(name, parameters, help)
RegisterCommand(name, function(source, args)
source = tonumber(source)
source = tonumber(source) --[[@as number]]
if parameters then
for i = 1, #parameters do
@@ -100,7 +100,7 @@ local function addCommand(group, name, callback, parameters, help)
end
end
return addCommand
return lib.addCommand
--[[ Example
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
--- Sends an event to the server and halts the current thread until a response is returned.
function lib.callback.await(event, delay, ...)
return triggerServerCallback(_, event, delay, false, ...)
return triggerServerCallback(nil, event, delay, false, ...)
end
local function callbackResponse(success, result, ...)

View File

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

View File

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

View File

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

View File

@@ -3,7 +3,7 @@
---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle.
---@return number? vehicle
---@return vector3? vehicleCoords
return function(coords, maxDistance, includePlayerVehicle)
function lib.getClosestVehicle(coords, maxDistance, includePlayerVehicle)
local vehicles = GetGamePool('CVehicle')
local closestVehicle, closestCoords
maxDistance = maxDistance or 2.0
@@ -25,3 +25,5 @@ return function(coords, maxDistance, includePlayerVehicle)
return closestVehicle, closestCoords
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 = {
Ox = 'ox_core',
QB = 'qb-core',
ESX = 'es_extended',
}
return function()
---@deprecated
function lib.getCore()
local result
Citizen.CreateThreadNow(function()
@@ -53,6 +59,7 @@ return function()
if not success then error(result) end
if framework == Core.Ox then
---@diagnostic disable-next-line: undefined-global
result = Ox
end
@@ -67,3 +74,5 @@ return function()
return result
end
return lib.getCore

View File

@@ -2,7 +2,7 @@
---@param maxDistance number The max distance to check.
---@param includePlayer boolean Whether or not to include the current player.
---@return number[]
return function(coords, maxDistance, includePlayer)
function lib.getNearbyPlayers(coords, maxDistance, includePlayer)
local players = GetActivePlayers()
local nearby = {}
local count = 0
@@ -29,3 +29,5 @@ return function(coords, maxDistance, includePlayer)
return nearby
end
return lib.getNearbyPlayers

View File

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

View File

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

View File

@@ -5,7 +5,7 @@ if key ~= '' then
local resourceName = GetCurrentResourceName()
key = key:gsub("[\'\"]", '')
return function(source, event, message, ...)
function lib.logger(source, event, message, ...)
local data = json.encode({
hostname = resourceName,
service = event,
@@ -27,4 +27,4 @@ if key ~= '' then
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 = {}
function CPlayer:__index(index, ...)
@@ -22,6 +27,7 @@ function CPlayer:getDistance(coords)
return #(self:getCoords() - coords)
end
---@deprecated
function lib.getPlayer()
return CPlayer
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 = {}
function CPlayer:__index(index, ...)
@@ -23,13 +28,11 @@ function CPlayer:getDistance(coords)
end
function CPlayer:getPed()
if update or not self.ped then
self.ped = GetPlayerPed(self.source)
end
return self.ped
end
---@deprecated
function lib.getPlayer()
return CPlayer
end

View File

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

View File

@@ -1,7 +1,6 @@
local StartShapeTestLosProbe = StartShapeTestLosProbe
local GetShapeTestResultIncludingMaterial = GetShapeTestResultIncludingMaterial
local GetWorldCoordFromScreenCoord = GetWorldCoordFromScreenCoord
local raycast = {}
---@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.
@@ -10,7 +9,7 @@ local raycast = {}
---@return vector3 endCoords
---@return vector3 surfaceNormal
---@return number materialHash
function raycast.cam(flags, p8)
function lib.raycast.cam(flags, p8)
local coords, normal = GetWorldCoordFromScreenCoord(0.5, 0.5)
local destination = coords + normal * 10
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
return raycast
return lib.raycast

View File

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

View File

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

View File

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