Facepunch / garrysmod-requests

Feature requests for Garry's Mod
85 stars 24 forks source link

Add TypeName(int ID) #1013

Open Kefta opened 7 years ago

Kefta commented 7 years ago

Gets the name of a type based on its ID (https://wiki.garrysmod.com/page/Enums/TYPE). For tables stored in the registry, this matches it's MetaName. This is different than "type" since it requires you to have an actual object of that type present to retrieve its name. It's very much like getmetatable vs FindMetaTable.

A way to register custom types from the Lua state would be nice too so hacky methods like TYPE_COLOR wouldn't have to be done, but one step at a time.

neico commented 7 years ago

When I started writing my own lua version the first thing I did was allowing for adding custom types, with 5.3.4 this was especially easy as luaL_tolstring() (alas tostring()) started checking for a metafield called __name, I only had to replace type() with a variant that is able to check for a __type metafield which get's an unique ID through an private array that get's the first values pre-filled with the default lua types. (so that custom id's are easily doable), resulting in a system very much like gmod's.

What I never liked is that in gmod's module interface you have to register a type with a name AND a id (using CreateMetaTableType( const char* strName, int iType )), resulting in a possible type id clash between modules (might be what you refer to by TYPE_COLOR).
So before this can be exposed to lua itself this should get changed to auto-generate the type id's first (alas int RegisterType( const char* sName )), as no one should have to bother with type id's that are mostly used internally anyway. (a type name lookup might be the better choice, alas int GetType( const char* sName ))

I guess part of what I just said should probably go directly as a related issue to the gmod-module-base repo, though it's more than just changing how the module interface handles it, there might be some breaking changes if not handled very carefully...

PS: the gmod module interface also has const char* GetTypeName( int iType ) and int GetType( int iStackPos ), so it's rather trivial to expose those to lua

Kefta commented 7 years ago

@neico This is actually what I mean by the problem with TYPE_COLOR: https://github.com/Facepunch/garrysmod/blob/784cd57576d85712fa13a7cea3a9523b4df966b0/garrysmod/lua/includes/extensions/net.lua#L1

The type is actually never properly "registered" (type(Color(255,255,255)) == "table"; TypeID(Color(255,255,255)) == TYPE_TABLE), and it has a value above TYPE_COUNT, meaning any addons or modules trying to implement their own type have to make sure not to use 255.