Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getUserInfo function at convar (fixes #1996) #1999

Merged
merged 11 commits into from
Jan 30, 2025
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

local checkluatype = SF.CheckLuaType
local PLY_META = FindMetaTable("Player")

SF.Permissions.registerPrivilege("convar", "Read ConVars", "Allows Starfall to read your game settings", { client = {} })
if CLIENT then
SF.Permissions.registerPrivilege("convar", "Read ConVars", "Allows Starfall to read your game settings", { client = {} })
end


--- ConVar library https://wiki.facepunch.com/gmod/ConVar
Expand All @@ -12,9 +15,11 @@ SF.RegisterLibrary("convar")

return function(instance)
local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end
local Ply_GetInfo = PLY_META.GetInfo

local convar_library = instance.Libraries.convar

if CLIENT then

local function getValidConVar(name)
checkpermission(instance, nil, "convar")
Expand All @@ -27,6 +32,7 @@ end


--- Check if the given ConVar exists
-- @client
-- @param string name Name of the ConVar
-- @return boolean True if exists
function convar_library.exists(name)
Expand All @@ -36,20 +42,23 @@ function convar_library.exists(name)
end

--- Returns default value of the ConVar
-- @client
-- @param string name Name of the ConVar
-- @return string Default value as a string
function convar_library.getDefault(name)
return getValidConVar(name):GetDefault()
end

--- Returns the minimum value of the convar
-- @client
-- @param string name Name of the ConVar
-- @return number The minimum value or nil if not specified
function convar_library.getMin(name)
return getValidConVar(name):GetMin()
end

--- Returns the maximum value of the convar
-- @client
-- @param string name Name of the ConVar
-- @return number? The maximum value or nil if not specified
function convar_library.getMax(name)
Expand All @@ -58,6 +67,7 @@ end

--- Returns value of the ConVar as a boolean.
-- True for numeric ConVars with the value of 1, false otherwise.
-- @client
-- @param string name Name of the ConVar
-- @return boolean The boolean value
function convar_library.getBool(name)
Expand All @@ -66,20 +76,23 @@ end

--- Returns value of the ConVar as a whole number.
-- Floats values will be floored.
-- @client
-- @param string name Name of the ConVar
-- @return number The integer value or 0 if converting fails
function convar_library.getInt(name)
return getValidConVar(name):GetInt()
end

--- Returns value of the ConVar as a floating-point number.
-- @client
-- @param string name Name of the ConVar
-- @return number The float value or 0 if converting fails
function convar_library.getFloat(name)
return getValidConVar(name):GetFloat()
end

--- Returns value of the ConVar as a string.
-- @client
-- @param string name Name of the ConVar
-- @return string Value as a string
function convar_library.getString(name)
Expand All @@ -88,13 +101,15 @@ end

--- Returns FCVAR flags of the given ConVar.
-- https://wiki.facepunch.com/gmod/Enums/FCVAR
-- @client
-- @param string name Name of the ConVar
-- @return number Number consisting of flag values
function convar_library.getFlags(name)
return getValidConVar(name):GetFlags()
end

--- Returns true if a given FCVAR flag is set for this ConVar.
-- @client
-- @param string name Name of the ConVar
-- @param number flag Convar Flag, see https://wiki.facepunch.com/gmod/Enums/FCVAR
-- @return boolean Whether the flag is set
Expand All @@ -103,5 +118,18 @@ function convar_library.hasFlag(name, flag)
return getValidConVar(name):IsFlagSet(flag)
end

end

--- Retrieves the value of a client-side userinfo ConVar.
-- @param string name The name of userinfo variable.
-- @return string Returns the value of the given client-side userinfo ConVar (truncated to 31 bytes).
function convar_library.getUserInfo(name)
checkluatype(name, TYPE_STRING)
if CLIENT then
checkpermission(instance, name, "convar")
end
local ply = SERVER and instance.player or LocalPlayer()
return IsValid(ply) and Ply_GetInfo(ply, name) or ""
end

end