Skip to content

Commit

Permalink
Contribute to a start of entities implementation (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Equoo committed Jun 9, 2024
1 parent 0bec683 commit ff93f83
Show file tree
Hide file tree
Showing 15 changed files with 1,048 additions and 4 deletions.
577 changes: 577 additions & 0 deletions .github/workflows/common_compile.yml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
build:
uses: RaphaelIT7/gmod-common-module-base/.github/workflows/compile.yml@workflow
uses: Equoo/lua_thread/.github/workflows/common_compile.yml@main
with:
PROJECT_NAME: "lua_threaded"
BUILD_WINDOWS: "false"
Expand All @@ -19,5 +19,5 @@ jobs:

test:
needs: build
uses: RaphaelIT7/gmod-lua-threaded/.github/workflows/testing.yml@main
uses: Equoo/lua_thread/.github/workflows/testing.yml@main
secrets: inherit
70 changes: 70 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"files.associations": {
"*.qci": "qc",
"chrono": "cpp",
"compare": "cpp",
"format": "cpp",
"memory": "cpp",
"ratio": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"xmemory": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp",
"algorithm": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"forward_list": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"thread": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"xfacet": "cpp",
"xhash": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocbuf": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xstddef": "cpp",
"xtree": "cpp"
}
}
16 changes: 16 additions & 0 deletions _testing/gmod_testing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ local code = [[local ret, err = pcall(function()
print(Vector(1, 1, 1), type(Vector(1, 1, 1)), isvector(Vector(1, 1, 1)))
print(Angle(1, 1, 1), type(Angle(1, 1, 1)), isangle(Vector(1, 1, 1)))
print("=== Entity ===")
print("Entity(39): ", Entity(39))
PrintTable(ents)
print("=== Get info_player_start ===")
PrintTable(ents.FindByClass("info_player_start"))
print("ENTITY META")
local meta = FindMetaTable( "Entity" )
PrintTable(meta)
print("=== File Library ===")
print("file.Exists (garrysmod.ver) ", file.Exists("garrysmod.ver", "MOD"))
Expand Down
1 change: 1 addition & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CreateWorkspace({name = "lua_threaded", abi_compatible = false})
IncludeHelpersExtended()
IncludeLuaShared()
IncludeSDKCommon()

IncludeSDKTier0()
IncludeSDKTier1()
--IncludeSDKTier2()
Expand Down
212 changes: 212 additions & 0 deletions source/class_entity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
#include "lua_threaded.h"
#include <player.h>

static int32_t metatype = GarrysMod::Lua::Type::Entity;
static const char metaname[] = "Entity";
static const char invalid_error[] = "invalid Entity";
static const char table_name[] = "Entity_object";

void Push_Entity(GarrysMod::Lua::ILuaBase* LUA, CBaseEntity* ent)
{
LUA_Entity* udata = (LUA_Entity*)LUA->NewUserdata(sizeof(LUA_Entity));
udata->entity = ent;

GarrysMod::Lua::ILuaInterface* ILUA = (GarrysMod::Lua::ILuaInterface*)LUA;
ILUA->SetType(metatype);

LUA->CreateMetaTableType(metaname, metatype);
LUA->SetMetaTable(-2);
}

void Push_Entity(GarrysMod::Lua::ILuaBase* LUA, LUA_Entity* ent)
{
Push_Entity(LUA, ent->entity);
}

void Entity_CheckType(GarrysMod::Lua::ILuaBase* LUA, int index)
{
if(!LUA->IsType(index, GarrysMod::Lua::Type::Entity))
LUA->TypeError(index, metaname);
}

bool IsEntity(GarrysMod::Lua::ILuaBase* LUA, int index)
{
if (LUA->IsType(index, GarrysMod::Lua::Type::Entity))
{
LUA->GetMetaTable(index);
LUA->GetField(-1, "MetaName");
if (LUA->IsType(-1, GarrysMod::Lua::Type::String))
{
if (strcmp(LUA->GetString(-1), metaname))
{
LUA->Pop(2);
return true;
} else {
LUA->Pop(2);
}
} else {
LUA->Pop(2);
}
}

return false;
}

LUA_Entity* Entity_GetUserdata(GarrysMod::Lua::ILuaBase *LUA, int index)
{
return (LUA_Entity*)LUA->GetUserdata(index);
}

LUA_Entity* Entity_Get(GarrysMod::Lua::ILuaBase* LUA, int index)
{
Entity_CheckType(LUA, index);

LUA_Entity* ent = Entity_GetUserdata(LUA, index);

return ent;
}

void Entity_Destroy(GarrysMod::Lua::ILuaBase *LUA, int index)
{
LUA_Entity* ent = Entity_GetUserdata(LUA, index);

LUA->GetField(GarrysMod::Lua::INDEX_REGISTRY, table_name);
LUA->PushUserdata(ent);
LUA->PushNil();
LUA->SetTable(-3);
LUA->Pop(1);

LUA->SetUserType(index, nullptr);
}

LUA_FUNCTION_STATIC(Entity__gc)
{
if (!LUA->IsType(1, metatype))
return 0;

Entity_Destroy(LUA, 1);
return 0;
}

LUA_FUNCTION_STATIC(Entity__tostring)
{
LUA_Entity* ent = Entity_Get(LUA, 1);

LUA->GetMetaTable(1);
LUA->GetField(-1, "MetaName");
const char* name = LUA->GetString(-1);
LUA->Pop(1);

char szBuf[64] = {};
V_snprintf(szBuf, sizeof(szBuf),"%s [%i][%s]", name, engine->IndexOfEdict(ent->entity->edict()), ent->entity->edict()->GetClassName()); // EntityIndex
LUA->PushString(szBuf);
return 1;
}

LUA_FUNCTION_STATIC(Entity__index)
{
LUA_Entity* ent = Entity_Get(LUA, 1);
const char* key = LUA->GetString(2);

if (key != NULL)
{
int ref = -1;
LUA->GetMetaTable(1);
LUA->GetField(-1, key);
ref = LUA->ReferenceCreate();

LUA->Pop(2);

LUA->ReferencePush(ref);
LUA->ReferenceFree(ref);

return 1;
}

LUA->GetMetaTable(1);
LUA->Push(2);
LUA->RawGet(-2);
if(!LUA->IsType(-1, GarrysMod::Lua::Type::NIL))
return 1;

LUA->GetFEnv(1);
LUA->Push(2);
LUA->RawGet(-2);
return 1;
}

LUA_FUNCTION_STATIC(Entity__newindex)
{
LUA->GetFEnv(1);
LUA->Push(2);
LUA->Push(3);
LUA->RawSet(-3);
const char* key = LUA->GetString(2);
if (key == NULL)
return 0;

LUA_Entity* ent = Entity_Get(LUA, 1);


return 0;
}

LUA_FUNCTION_STATIC(Entity__eq)
{
LUA->PushBool(Entity_Get(LUA, 1) == Entity_Get(LUA, 2));
return 1;
}

LUA_FUNCTION_STATIC(Entity_EntIndex)
{
LUA->PushBool(Entity_Get(LUA, 1) == Entity_Get(LUA, 2));
return 1;
}


LUA_FUNCTION(Global_Entity)
{
/* double id = LUA->CheckNumber(1);
CBaseEntity* ent = CBaseEntity::Instance(engine->PEntityOfEntIndex((int)id));
if (ent == NULL)
{
LUA->PushNil();
return 1;
}
Push_Entity(LUA, ent);
return 1; */

double id = LUA->CheckNumber(1);

CHandle<CBaseEntity> hEntity = (CBaseEntity*)g_pEntityList->LookupEntityByNetworkIndex((int)id);

if (!hEntity.IsValid() || hEntity == nullptr)
{
LUA->PushNil();
return 1;
}

CBaseEntity* ent = dynamic_cast<CBaseEntity*>(hEntity.Get());

Push_Entity(LUA, ent);

return 1;
}

void InitEntityClass(GarrysMod::Lua::ILuaInterface* LUA)
{
LUA->CreateTable();
LUA->SetField(GarrysMod::Lua::INDEX_REGISTRY, table_name);

LUA->CreateMetaTableType(metaname, metatype);
Add_Func(LUA, Entity__gc, "__gc"); // Gmod doesn't have __gc
Add_Func(LUA, Entity__eq, "__eq");
Add_Func(LUA, Entity__index, "__index");
Add_Func(LUA, Entity__newindex, "__newindex");
Add_Func(LUA, Entity__tostring, "__tostring");
LUA->Pop(1);
}
13 changes: 13 additions & 0 deletions source/class_entity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <GarrysMod/Lua/LuaInterface.h>
struct LUA_Entity
{
CBaseEntity* entity;
};

extern void InitEntityClass(GarrysMod::Lua::ILuaInterface*);
extern int Global_Entity(lua_State*);

extern void Push_Entity(GarrysMod::Lua::ILuaBase* LUA, CBaseEntity* ent);
extern bool IsEntity(GarrysMod::Lua::ILuaBase* LUA, int index);
extern LUA_Entity* Entity_Get(GarrysMod::Lua::ILuaBase* LUA, int index);
extern void Entity_CheckType(GarrysMod::Lua::ILuaBase* LUA, int index);
3 changes: 2 additions & 1 deletion source/library_Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ void InitGlobal(GarrysMod::Lua::ILuaInterface* LUA)

Add_Func(LUA, Global_Angle, "Angle");
Add_Func(LUA, Global_LerpAngle, "LerpAngle");
Add_Func(LUA, Global_RecipientFilter, "RecipientFilter");

Add_Func(LUA, Global_Entity, "Entity");
LUA->Pop(1);
}
30 changes: 29 additions & 1 deletion source/library_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,35 @@
#include <icommandline.h>
#include <eiface.h>

LUA_FUNCTION_STATIC(engine_GetAddons)
/* class IPlayerInfo;
class edict_t; */
static const char playerinfomanager_name[] = "PlayerInfoManager002";

// include <player.h> defined it already
/* class IPlayerInfoManager
{
public:
virtual IPlayerInfo *GetPlayerInfo( edict_t *pEdict ) = 0;
virtual CGlobalVars *GetGlobalVars( ) = 0;
}; */

CGlobalVars* GlobalVars()
{
static CGlobalVars *iface_pointer = nullptr;
if (iface_pointer == nullptr)
{
SourceSDK::FactoryLoader server_loader("server");
auto player_info_manager = server_loader.GetInterface<IPlayerInfoManager>(
playerinfomanager_name
);
if (player_info_manager != nullptr)
iface_pointer = player_info_manager->GetGlobalVars();
}

return iface_pointer;
}

LUA_FUNCTION(engine_GetAddons)
{
PushValue(LUA, GMOD->addons);

Expand Down
Loading

0 comments on commit ff93f83

Please sign in to comment.