Skip to content

Commit ff93f83

Browse files
authored
Contribute to a start of entities implementation (#2)
* Refactor variable names and update comments * Update function to handle edge cases * Update compile.yml to use the main branch of common_compile.yml * Fix Entity class functions and add Entity equality operator * Fix Entity class and add testing code * Update struct LUA_Entity in class_entity.h * Add Entity class initialization * Add library_ents.h include * Fix variable name in ents_Create function * Add print statement and FindEntityByName function * Fix function reference in Global_Msg * Fix function pointer printing in Global_Msg * Update entity includes * Update entity includes * Fix formatting issue in class_entity.cpp and add missing include in library_ents.cpp * Update entity includes * Remove unnecessary includes in library_ents.cpp and add missing include in library_ents.h * Remove unnecessary include statement * Add IncludeSDKGame() function call and handle error in ents_FindEntityByName() * Add baseentity.h to manual files * Update premake5.lua to include baseentity.h in CreateProject * Update entity finding function names * Add baseentity.h include * Update premake5.lua and library_ents.cpp*** * Replace baseentity.h with player.h in library_ents.h * Update ILuaObject to IILuaObject in source files * Fix function parameter types in CLuaGameCallback and CLuaObject classes * Add missing include statement for LuaInterface * Remove unnecessary include statements * Remove unnecessary includes and namespaces * Add GarrysMod::Lua namespace to class_entity.h and library_ents.h * Remove unnecessary include of player.h * Remove unnecessary include and uncomment player include * Fix CheckString condition in ents_FindEntityByName function * Fix include statement in lua_threaded.h * Add player.h include in library_ents.cpp * Refactor IPlayerInfoManager class definition in library_engine.cpp * Refactor code in library_ents.cpp * Add file associations for C++ standard library headers and update function signatures * Fix Global_Entity function in class_entity.cpp * Fix type error in Entity_CheckType function * Fix TypeError in Entity_CheckType function * Fix function type in Global_Msg * Fix PushSpecial argument in InitEntsLib function * Update library_ents.cpp * Update library_ents.h * Commented out IPlayerInfoManager class definition * Add necessary header files for library_ents.cpp * Refactor IPlayerInfoManager class definition in library_engine.cpp * Remove unused code in Global_Msg function * Update entity search function * Fix FindEntityByName function call in library_ents.cpp * Remove unused header and update function call * Revert commented out include statement in library_ents.cpp * Update include path for itoolentity.h * Update include statements and add global entity list * Update entity list variable names * Commented out unused code in library_engine.cpp * Fix include and pointer issue in library_engine.cpp and library_ents.cpp * Update include statements and add new C++ standard library headers * Fix TODOs and update entity handling in lua_utils.cpp and lua_utils.h * Update include statements and add new C++ standard library headers * Fix pointer issue in library_ents.cpp * Add entitylist.h include statement in library_ents.cpp * Update entity list variable names and include global entity list in lua_utils.h * Update include statements and add entitylist.h in library_ents.cpp and lua_utils.h * Refactor include statements and update entity handling in library_ents.cpp * Update include statements and add entitylist.h in library_ents.cpp and lua_utils.h * Update include statements and remove unused variable in library_ents.cpp and library_ents.h * Refactor entity handling and update include statements in library_ents.cpp * Refactor entity handling and update include statements in class_entity.cpp and class_entity.h * Refactor entity handling and update include statements in class_entity.cpp and class_entity.h * Refactor entity handling and update include statements in class_entity.cpp, library_engine.cpp, library_ents.cpp, and lua_utils.h * Update include statements and remove unused variable in library_ents.cpp * Refactor entity handling and update include statements in library_ents.cpp * Update include statements in premake5.lua and lua_utils.h * Update include statements in library_ents.cpp and lua_utils.h * Refactor entity handling and update include statements in library_ents.cpp and lua_utils.h * Update include statements in lua_utils.h * Refactor entity handling and update include statements in library_ents.cpp * Refactor entity handling and update include statements in library_ents.cpp * Refactor entity handling and update include statements in library_ents.cpp * Refactor entity handling and update include statements in library_ents.cpp * Fix typo in library_ents.cpp * Refactor entity handling and update include statements in library_ents.cpp * Fix null pointer exception in ents_FindEntityByName function * Refactor LuaThreaded testing code in gmod_testing.lua and fix null pointer exception in ents_FindEntityByName function * Update Sleep function timeout in console.lua * Refactor LuaThreaded testing code in gmod_testing.lua and fix null pointer exception in ents_FindEntityByName function * Update entityinfomanager_name in library_ents.cpp * Refactor entity handling and update include statements in library_ents.cpp * Refactor entity handling and update include statements in library_ents.cpp * Update LUA->SetField argument to use std::to_string in library_ents.cpp * Refactor entity handling and update include statements in library_ents.cpp * Add ents_FindByClass function to library_ents.cpp * Refactor entity handling and update include statements in library_ents.cpp * Fix missing debug message in ents_FindByClass function * Update entityinfomanager_name in library_ents.cpp * Fix missing debug message in ents_FindByClass function * Fix missing semicolon in library_ents.cpp * Refactor debug message in ents_FindByClass function in library_ents.cpp * Update debug message in gmod_testing.lua and entityinfomanager_name in library_ents.cpp * Update entityinfomanager_name in library_ents.cpp * Update entityinfomanager_name in library_ents.cpp * Update entityinfomanager_name and IncludeSDKTier2/3 in premake5.lua * Update entityinfomanager_name and IncludeSDKTier2/3 in premake5.lua * Fix incorrect pointer access in class_entity.cpp * Fix missing debug message in ents_FindByClass function * Add debug message for Entity(39) in gmod_testing.lua * Add Entity function to library_Global.cpp * Update entityinfomanager_name in library_ents.cpp * Update IncludeSDKTier2/3 in premake5.lua and fix entity handle access in library_ents.cpp * Fix entity handle access in library_ents.cpp * Fix entity handle access in library_ents.cpp * Fix entity handle access in library_ents.cpp * Fix entity handle access in library_ents.cpp * Add debug message for entity classname in ents_FindByClass function * Add debug message for entity list index in ents_FindByClass function * Add missing semicolon in ents_FindByClass function * Refactor entity handle access in library_ents.cpp * Fix entity handle access in library_ents.cpp * Refactor entity handle access in library_ents.cpp * Add missing include for baseentity.h in library_ents.cpp * Refactor entity handle access and fix missing include in library_ents.cpp * Refactor entity handle access and fix missing include in library_ents.cpp * Refactor entity handle access and fix missing include in library_ents.cpp * Fix entity handle access and add debug message in ents_FindByClass function * Fix entity handle access and add missing semicolon in ents_FindByClass function * Refactor entity handle access and fix missing include in library_ents.cpp * Refactor entity handle access and fix missing include in library_ents.cpp * Remove debug message in ents_FindByClass function * Refactor entity handle access and fix missing include in class_entity.cpp * Refactor entity handle access and fix missing include in class_entity.cpp * chore: Add Global_Entity function to library_Global.cpp
1 parent 0bec683 commit ff93f83

15 files changed

+1048
-4
lines changed

.github/workflows/common_compile.yml

Lines changed: 577 additions & 0 deletions
Large diffs are not rendered by default.

.github/workflows/compile.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99

1010
jobs:
1111
build:
12-
uses: RaphaelIT7/gmod-common-module-base/.github/workflows/compile.yml@workflow
12+
uses: Equoo/lua_thread/.github/workflows/common_compile.yml@main
1313
with:
1414
PROJECT_NAME: "lua_threaded"
1515
BUILD_WINDOWS: "false"
@@ -19,5 +19,5 @@ jobs:
1919

2020
test:
2121
needs: build
22-
uses: RaphaelIT7/gmod-lua-threaded/.github/workflows/testing.yml@main
22+
uses: Equoo/lua_thread/.github/workflows/testing.yml@main
2323
secrets: inherit

.vscode/settings.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"files.associations": {
3+
"*.qci": "qc",
4+
"chrono": "cpp",
5+
"compare": "cpp",
6+
"format": "cpp",
7+
"memory": "cpp",
8+
"ratio": "cpp",
9+
"tuple": "cpp",
10+
"type_traits": "cpp",
11+
"utility": "cpp",
12+
"xmemory": "cpp",
13+
"xstring": "cpp",
14+
"xtr1common": "cpp",
15+
"xutility": "cpp",
16+
"algorithm": "cpp",
17+
"atomic": "cpp",
18+
"bit": "cpp",
19+
"cctype": "cpp",
20+
"charconv": "cpp",
21+
"clocale": "cpp",
22+
"cmath": "cpp",
23+
"concepts": "cpp",
24+
"cstddef": "cpp",
25+
"cstdint": "cpp",
26+
"cstdio": "cpp",
27+
"cstdlib": "cpp",
28+
"cstring": "cpp",
29+
"ctime": "cpp",
30+
"cwchar": "cpp",
31+
"exception": "cpp",
32+
"forward_list": "cpp",
33+
"initializer_list": "cpp",
34+
"iomanip": "cpp",
35+
"ios": "cpp",
36+
"iosfwd": "cpp",
37+
"istream": "cpp",
38+
"iterator": "cpp",
39+
"limits": "cpp",
40+
"list": "cpp",
41+
"locale": "cpp",
42+
"map": "cpp",
43+
"mutex": "cpp",
44+
"new": "cpp",
45+
"optional": "cpp",
46+
"ostream": "cpp",
47+
"sstream": "cpp",
48+
"stdexcept": "cpp",
49+
"stop_token": "cpp",
50+
"streambuf": "cpp",
51+
"string": "cpp",
52+
"system_error": "cpp",
53+
"thread": "cpp",
54+
"typeinfo": "cpp",
55+
"unordered_map": "cpp",
56+
"vector": "cpp",
57+
"xfacet": "cpp",
58+
"xhash": "cpp",
59+
"xiosbase": "cpp",
60+
"xlocale": "cpp",
61+
"xlocbuf": "cpp",
62+
"xlocinfo": "cpp",
63+
"xlocmes": "cpp",
64+
"xlocmon": "cpp",
65+
"xlocnum": "cpp",
66+
"xloctime": "cpp",
67+
"xstddef": "cpp",
68+
"xtree": "cpp"
69+
}
70+
}

_testing/gmod_testing.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ local code = [[local ret, err = pcall(function()
22
print(Vector(1, 1, 1), type(Vector(1, 1, 1)), isvector(Vector(1, 1, 1)))
33
print(Angle(1, 1, 1), type(Angle(1, 1, 1)), isangle(Vector(1, 1, 1)))
44
5+
print("=== Entity ===")
6+
7+
print("Entity(39): ", Entity(39))
8+
9+
PrintTable(ents)
10+
11+
print("=== Get info_player_start ===")
12+
13+
PrintTable(ents.FindByClass("info_player_start"))
14+
15+
print("ENTITY META")
16+
17+
local meta = FindMetaTable( "Entity" )
18+
PrintTable(meta)
19+
20+
521
print("=== File Library ===")
622
723
print("file.Exists (garrysmod.ver) ", file.Exists("garrysmod.ver", "MOD"))

premake5.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CreateWorkspace({name = "lua_threaded", abi_compatible = false})
2525
IncludeHelpersExtended()
2626
IncludeLuaShared()
2727
IncludeSDKCommon()
28+
2829
IncludeSDKTier0()
2930
IncludeSDKTier1()
3031
--IncludeSDKTier2()

source/class_entity.cpp

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#include "lua_threaded.h"
2+
#include <player.h>
3+
4+
static int32_t metatype = GarrysMod::Lua::Type::Entity;
5+
static const char metaname[] = "Entity";
6+
static const char invalid_error[] = "invalid Entity";
7+
static const char table_name[] = "Entity_object";
8+
9+
void Push_Entity(GarrysMod::Lua::ILuaBase* LUA, CBaseEntity* ent)
10+
{
11+
LUA_Entity* udata = (LUA_Entity*)LUA->NewUserdata(sizeof(LUA_Entity));
12+
udata->entity = ent;
13+
14+
GarrysMod::Lua::ILuaInterface* ILUA = (GarrysMod::Lua::ILuaInterface*)LUA;
15+
ILUA->SetType(metatype);
16+
17+
LUA->CreateMetaTableType(metaname, metatype);
18+
LUA->SetMetaTable(-2);
19+
}
20+
21+
void Push_Entity(GarrysMod::Lua::ILuaBase* LUA, LUA_Entity* ent)
22+
{
23+
Push_Entity(LUA, ent->entity);
24+
}
25+
26+
void Entity_CheckType(GarrysMod::Lua::ILuaBase* LUA, int index)
27+
{
28+
if(!LUA->IsType(index, GarrysMod::Lua::Type::Entity))
29+
LUA->TypeError(index, metaname);
30+
}
31+
32+
bool IsEntity(GarrysMod::Lua::ILuaBase* LUA, int index)
33+
{
34+
if (LUA->IsType(index, GarrysMod::Lua::Type::Entity))
35+
{
36+
LUA->GetMetaTable(index);
37+
LUA->GetField(-1, "MetaName");
38+
if (LUA->IsType(-1, GarrysMod::Lua::Type::String))
39+
{
40+
if (strcmp(LUA->GetString(-1), metaname))
41+
{
42+
LUA->Pop(2);
43+
return true;
44+
} else {
45+
LUA->Pop(2);
46+
}
47+
} else {
48+
LUA->Pop(2);
49+
}
50+
}
51+
52+
return false;
53+
}
54+
55+
LUA_Entity* Entity_GetUserdata(GarrysMod::Lua::ILuaBase *LUA, int index)
56+
{
57+
return (LUA_Entity*)LUA->GetUserdata(index);
58+
}
59+
60+
LUA_Entity* Entity_Get(GarrysMod::Lua::ILuaBase* LUA, int index)
61+
{
62+
Entity_CheckType(LUA, index);
63+
64+
LUA_Entity* ent = Entity_GetUserdata(LUA, index);
65+
66+
return ent;
67+
}
68+
69+
void Entity_Destroy(GarrysMod::Lua::ILuaBase *LUA, int index)
70+
{
71+
LUA_Entity* ent = Entity_GetUserdata(LUA, index);
72+
73+
LUA->GetField(GarrysMod::Lua::INDEX_REGISTRY, table_name);
74+
LUA->PushUserdata(ent);
75+
LUA->PushNil();
76+
LUA->SetTable(-3);
77+
LUA->Pop(1);
78+
79+
LUA->SetUserType(index, nullptr);
80+
}
81+
82+
LUA_FUNCTION_STATIC(Entity__gc)
83+
{
84+
if (!LUA->IsType(1, metatype))
85+
return 0;
86+
87+
Entity_Destroy(LUA, 1);
88+
return 0;
89+
}
90+
91+
LUA_FUNCTION_STATIC(Entity__tostring)
92+
{
93+
LUA_Entity* ent = Entity_Get(LUA, 1);
94+
95+
LUA->GetMetaTable(1);
96+
LUA->GetField(-1, "MetaName");
97+
const char* name = LUA->GetString(-1);
98+
LUA->Pop(1);
99+
100+
char szBuf[64] = {};
101+
V_snprintf(szBuf, sizeof(szBuf),"%s [%i][%s]", name, engine->IndexOfEdict(ent->entity->edict()), ent->entity->edict()->GetClassName()); // EntityIndex
102+
LUA->PushString(szBuf);
103+
return 1;
104+
}
105+
106+
LUA_FUNCTION_STATIC(Entity__index)
107+
{
108+
LUA_Entity* ent = Entity_Get(LUA, 1);
109+
const char* key = LUA->GetString(2);
110+
111+
if (key != NULL)
112+
{
113+
int ref = -1;
114+
LUA->GetMetaTable(1);
115+
LUA->GetField(-1, key);
116+
ref = LUA->ReferenceCreate();
117+
118+
LUA->Pop(2);
119+
120+
LUA->ReferencePush(ref);
121+
LUA->ReferenceFree(ref);
122+
123+
return 1;
124+
}
125+
126+
LUA->GetMetaTable(1);
127+
LUA->Push(2);
128+
LUA->RawGet(-2);
129+
if(!LUA->IsType(-1, GarrysMod::Lua::Type::NIL))
130+
return 1;
131+
132+
LUA->GetFEnv(1);
133+
LUA->Push(2);
134+
LUA->RawGet(-2);
135+
return 1;
136+
}
137+
138+
LUA_FUNCTION_STATIC(Entity__newindex)
139+
{
140+
LUA->GetFEnv(1);
141+
LUA->Push(2);
142+
LUA->Push(3);
143+
LUA->RawSet(-3);
144+
const char* key = LUA->GetString(2);
145+
if (key == NULL)
146+
return 0;
147+
148+
LUA_Entity* ent = Entity_Get(LUA, 1);
149+
150+
151+
return 0;
152+
}
153+
154+
LUA_FUNCTION_STATIC(Entity__eq)
155+
{
156+
LUA->PushBool(Entity_Get(LUA, 1) == Entity_Get(LUA, 2));
157+
return 1;
158+
}
159+
160+
LUA_FUNCTION_STATIC(Entity_EntIndex)
161+
{
162+
LUA->PushBool(Entity_Get(LUA, 1) == Entity_Get(LUA, 2));
163+
return 1;
164+
}
165+
166+
167+
LUA_FUNCTION(Global_Entity)
168+
{
169+
/* double id = LUA->CheckNumber(1);
170+
171+
CBaseEntity* ent = CBaseEntity::Instance(engine->PEntityOfEntIndex((int)id));
172+
173+
if (ent == NULL)
174+
{
175+
LUA->PushNil();
176+
return 1;
177+
}
178+
179+
Push_Entity(LUA, ent);
180+
181+
return 1; */
182+
183+
double id = LUA->CheckNumber(1);
184+
185+
CHandle<CBaseEntity> hEntity = (CBaseEntity*)g_pEntityList->LookupEntityByNetworkIndex((int)id);
186+
187+
if (!hEntity.IsValid() || hEntity == nullptr)
188+
{
189+
LUA->PushNil();
190+
return 1;
191+
}
192+
193+
CBaseEntity* ent = dynamic_cast<CBaseEntity*>(hEntity.Get());
194+
195+
Push_Entity(LUA, ent);
196+
197+
return 1;
198+
}
199+
200+
void InitEntityClass(GarrysMod::Lua::ILuaInterface* LUA)
201+
{
202+
LUA->CreateTable();
203+
LUA->SetField(GarrysMod::Lua::INDEX_REGISTRY, table_name);
204+
205+
LUA->CreateMetaTableType(metaname, metatype);
206+
Add_Func(LUA, Entity__gc, "__gc"); // Gmod doesn't have __gc
207+
Add_Func(LUA, Entity__eq, "__eq");
208+
Add_Func(LUA, Entity__index, "__index");
209+
Add_Func(LUA, Entity__newindex, "__newindex");
210+
Add_Func(LUA, Entity__tostring, "__tostring");
211+
LUA->Pop(1);
212+
}

source/class_entity.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <GarrysMod/Lua/LuaInterface.h>
2+
struct LUA_Entity
3+
{
4+
CBaseEntity* entity;
5+
};
6+
7+
extern void InitEntityClass(GarrysMod::Lua::ILuaInterface*);
8+
extern int Global_Entity(lua_State*);
9+
10+
extern void Push_Entity(GarrysMod::Lua::ILuaBase* LUA, CBaseEntity* ent);
11+
extern bool IsEntity(GarrysMod::Lua::ILuaBase* LUA, int index);
12+
extern LUA_Entity* Entity_Get(GarrysMod::Lua::ILuaBase* LUA, int index);
13+
extern void Entity_CheckType(GarrysMod::Lua::ILuaBase* LUA, int index);

source/library_Global.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ void InitGlobal(GarrysMod::Lua::ILuaInterface* LUA)
394394

395395
Add_Func(LUA, Global_Angle, "Angle");
396396
Add_Func(LUA, Global_LerpAngle, "LerpAngle");
397-
Add_Func(LUA, Global_RecipientFilter, "RecipientFilter");
397+
398+
Add_Func(LUA, Global_Entity, "Entity");
398399
LUA->Pop(1);
399400
}

source/library_engine.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,35 @@
22
#include <icommandline.h>
33
#include <eiface.h>
44

5-
LUA_FUNCTION_STATIC(engine_GetAddons)
5+
/* class IPlayerInfo;
6+
class edict_t; */
7+
static const char playerinfomanager_name[] = "PlayerInfoManager002";
8+
9+
// include <player.h> defined it already
10+
/* class IPlayerInfoManager
11+
{
12+
public:
13+
virtual IPlayerInfo *GetPlayerInfo( edict_t *pEdict ) = 0;
14+
virtual CGlobalVars *GetGlobalVars( ) = 0;
15+
}; */
16+
17+
CGlobalVars* GlobalVars()
18+
{
19+
static CGlobalVars *iface_pointer = nullptr;
20+
if (iface_pointer == nullptr)
21+
{
22+
SourceSDK::FactoryLoader server_loader("server");
23+
auto player_info_manager = server_loader.GetInterface<IPlayerInfoManager>(
24+
playerinfomanager_name
25+
);
26+
if (player_info_manager != nullptr)
27+
iface_pointer = player_info_manager->GetGlobalVars();
28+
}
29+
30+
return iface_pointer;
31+
}
32+
33+
LUA_FUNCTION(engine_GetAddons)
634
{
735
PushValue(LUA, GMOD->addons);
836

0 commit comments

Comments
 (0)