Skip to content
64 changes: 64 additions & 0 deletions garrysmod/lua/includes/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,69 @@ function Material( name, words )

end

--[[---------------------------------------------------------
TypeID
-----------------------------------------------------------]]
local getmetatable = getmetatable

-- Builtin types don't have MetaIDs in their metatables
local STORED_TYPE_IDS = {
["nil"] = TYPE_NIL,
["boolean"] = TYPE_BOOL,
["number"] = TYPE_NUMBER,
["string"] = TYPE_STRING,
["table"] = TYPE_TABLE,
["function"] = TYPE_FUNCTION,
["thread"] = TYPE_THREAD,
}

local OldTypeID = TypeID
function TypeID( v )
local vType = C_type( v )
local id = STORED_TYPE_IDS[ vType ]
if ( id ) then return id end

if ( vType == "userdata" ) then
-- Garry's Mod types have their IDs in their metatables
local vMeta = getmetatable( v )
if ( vMeta ) then
id = vMeta.MetaID
if ( id ) then return id end
end
end

return OldTypeID( v )
end

--[[---------------------------------------------------------
is* functions
-----------------------------------------------------------]]
function isstring( v ) return C_type( v ) == "string" end
function isnumber( v ) return C_type( v ) == "number" end
function istable( v ) return C_type( v ) == "table" end
function isfunction( v ) return C_type( v ) == "function" end
function isbool( v ) return v == true or v == false end
function isangle( v ) return type( v ) == "Angle" end
function isvector( v ) return type( v ) == "Vector" end
function isentity( v )
if ( C_type( v ) == "userdata" ) then
local vMeta = getmetatable( v )
if ( vMeta ) then
if ( vMeta.MetaName == "Entity" ) then
return true
end
vMeta = vMeta.MetaBaseClass
if ( vMeta ) then
return vMeta.MetaName == "Entity"
end
end
end
return false
end
IsEntity = isentity -- Backwards compatibility?
function ismatrix( v ) return type( v ) == "VMatrix" end
function ispanel( v ) return type( v ) == "Panel" end

--[[---------------------------------------------------------
IsTableOfEntitiesValid
-----------------------------------------------------------]]
Expand Down Expand Up @@ -517,3 +580,4 @@ function GetConVarString( name )
local c = GetConVar( name )
return ( c and c:GetString() ) or ""
end