Skip to content

Commit

Permalink
feat(bridge/qb): add warnings and disabling of overridden functions
Browse files Browse the repository at this point in the history
* feat(bridge/qb): add warnings and disabling of overridden functions

QBCore offered a method to modify player object functions at runtime which can lead to interesting side effects depending on implementation. This adds warnings for when this occurs and adds options to block the behavior as well as disabling the warning in either case.

* fix(bridge/qb): rename convars

* fix(bridge/qb): fix message

* refactor(bridge/qb): use getconvar
  • Loading branch information
solareon authored Sep 6, 2024
1 parent 235a995 commit 24df00e
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions bridge/qb/server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ require 'server.functions'
require 'bridge.qb.server.player'
local functions = {}

local allowMethodOverrides = GetConvar('qbx:allowmethodoverrides', 'true') == 'true'
local disableMethodOverrideWarning = GetConvar('qbx:disableoverridewarning', 'false') == 'true'

local createQbExport = require 'bridge.qb.shared.export-function'

---@deprecated use the GetEntityCoords and GetEntityHeading natives directly
Expand Down Expand Up @@ -391,6 +394,18 @@ functions.RemoveGang = function(gangName)
end
createQbExport('RemoveGang', RemoveGang)

local function checkExistingMethod(method, methodName)
local methodType = type(method)
if methodType == 'function' then
local warnMessage = allowMethodOverrides and 'A resource is overriding method %s in player class. This can cause unexpected behavior. Disable this warning by setting convar qbx:disableoverridewarning to true' or 'A resource attempted to override method %s in player object and was blocked. Disable this warning by setting convar qbx:disableoverridewarning to true'
if not disableMethodOverrideWarning then
lib.print.warn(warnMessage:format(methodName))
end
return allowMethodOverrides
end
return true
end

---Add a new function to the Functions table of the player class
---Use-case:
-- [[
Expand All @@ -409,12 +424,15 @@ function functions.AddPlayerMethod(ids, methodName, handler)
if idType == 'number' then
if ids == -1 then
for _, v in pairs(QBX.Players) do
v.Functions[methodName] = handler
if checkExistingMethod(v.Functions[methodName], methodName) then
v.Functions[methodName] = handler
end
end
else
if not QBX.Players[ids] then return end

QBX.Players[ids].Functions[methodName] = handler
if checkExistingMethod(QBX.Players[ids].Functions[methodName], methodName) then
QBX.Players[ids].Functions[methodName] = handler
end
end
elseif idType == 'table' and table.type(ids) == 'array' then
for i = 1, #ids do
Expand Down

0 comments on commit 24df00e

Please sign in to comment.