Skip to content

Commit adc1894

Browse files
committed
feat(scripting/entities): Entity Custom Classname
1 parent 05ef041 commit adc1894

File tree

12 files changed

+56
-26
lines changed

12 files changed

+56
-26
lines changed

docgen/data/data.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,13 @@
367367
"cpp": "entities->Create"
368368
},
369369
"return": "Entity*",
370-
"params": {},
371-
"additional": {}
370+
"params": {
371+
"classname": "const char*"
372+
},
373+
"additional": {
374+
"lua": ":::note\nA list of the entity class names can be found here: [https://cs2.poggu.me/dumped-data/entity-list](https://cs2.poggu.me/dumped-data/entity-list).\nAs a class name it can also be used the following class name format to select a player: \"player:PLAYERID\", where PLAYERID is the player id.\n:::",
375+
"cpp": ":::note\nA list of the entity class names can be found here: [https://cs2.poggu.me/dumped-data/entity-list](https://cs2.poggu.me/dumped-data/entity-list).\nAs a class name it can also be used the following class name format to select a player: \"player:PLAYERID\", where PLAYERID is the player id.\n:::"
376+
}
372377
},
373378
"entity": {
374379
"title": "Entity",

plugin_files/scripting/includes/swiftly/entities.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "entities.h"
22

3-
Entity *Entities::Create()
3+
Entity *Entities::Create(const char *clsname)
44
{
5-
Entity *entity = new Entity();
5+
Entity *entity = new Entity(clsname);
66
this->entities.insert(std::make_pair(entity->GetEntityID(), entity));
77
return entity;
88
}

plugin_files/scripting/includes/swiftly/entities.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Entities
1414
public:
1515
Entities(){};
1616

17-
Entity *Create();
17+
Entity *Create(const char *clsname = nullptr);
1818
Entity *Fetch(uint32_t id);
1919
void Destroy(uint32_t id);
2020

plugin_files/scripting/includes/swiftly/entities/entity.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#include "entity.h"
22
#include "../swiftly.h"
33

4-
typedef uint32_t (*CreateEntity)();
4+
typedef uint32_t (*CreateEntity)(const char *);
55
typedef void (*Entity_Spawn)(uint32_t);
66
typedef void (*Entity_Destroy)(uint32_t);
77
typedef void (*Entity_SetModel)(uint32_t, const char *);
88
typedef void (*Entity_AcceptInput)(uint32_t, const char *, const char *, const char *, double *);
99

10-
Entity::Entity()
10+
Entity::Entity(const char *clsname)
1111
{
1212
void *createEntity = FetchFunctionPtr(nullptr, "scripting_Entity_Create");
1313
if (createEntity)
14-
this->entityID = reinterpret_cast<CreateEntity>(createEntity)();
14+
this->entityID = reinterpret_cast<CreateEntity>(createEntity)(clsname);
1515
else
1616
NOT_SUPPORTED("scripting_Entity_Create");
1717

plugin_files/scripting/includes/swiftly/entities/entity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Entity
1919
Angles *angles;
2020
Colors *color;
2121

22-
Entity();
22+
Entity(const char *clsname = nullptr);
2323
~Entity();
2424

2525
void Spawn();

src/components/Plugins/inc/Scripting.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ SMM_API const char *scripting_Database_EscapeString(const char *connectionName,
4444
std::vector<std::map<const char *, std::any>> scripting_Database_QueryRaw(const char *connectionName, const char *query);
4545
SMM_API const char *scripting_Database_Query(const char *connectionName, const char *query);
4646

47-
SMM_API uint32_t scripting_Entity_Create();
47+
SMM_API uint32_t scripting_Entity_Create(const char *classname);
4848
SMM_API void scripting_Entity_Spawn(uint32_t entityID);
4949
SMM_API void scripting_Entity_Destroy(uint32_t entityID);
5050
SMM_API void scripting_Entity_SetModel(uint32_t entityID, const char *model);

src/components/Plugins/src/language/lua/entities.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class LuaEntityClass
1818
uint32_t entityID = 0;
1919

2020
public:
21-
LuaEntityClass()
21+
LuaEntityClass(const char *clsname)
2222
{
23-
this->entityID = scripting_Entity_Create();
23+
this->entityID = scripting_Entity_Create(clsname);
2424
}
2525

2626
uint32_t GetEntityID()
@@ -37,9 +37,9 @@ class LuaEntitiesClass
3737
public:
3838
LuaEntitiesClass() {}
3939

40-
luacpp::LuaObject CreateEntity(luacpp::LuaClass<LuaEntityClass> entityClass)
40+
luacpp::LuaObject CreateEntity(luacpp::LuaClass<LuaEntityClass> entityClass, const char *clsname)
4141
{
42-
luacpp::LuaObject entityObject = entityClass.CreateInstance();
42+
luacpp::LuaObject entityObject = entityClass.CreateInstance(clsname);
4343
LuaEntityClass *entity = static_cast<LuaEntityClass *>(entityObject.ToPointer());
4444

4545
this->entities.insert(std::make_pair(entity->GetEntityID(), entityObject));
@@ -86,14 +86,14 @@ class LuaEntityArgsClass
8686
void SetupLuaEntities(luacpp::LuaState *state, Plugin *plugin)
8787
{
8888
auto entitiesClass = state->CreateClass<LuaEntitiesClass>("Entities").DefConstructor();
89-
auto entityClass = state->CreateClass<LuaEntityClass>().DefConstructor();
89+
auto entityClass = state->CreateClass<LuaEntityClass>().DefConstructor<const char *>();
9090

9191
auto coordsClass = state->CreateClass<LuaEntityArgsClass>().DefConstructor<uint32_t>();
9292
auto anglesClass = state->CreateClass<LuaEntityArgsClass>().DefConstructor<uint32_t>();
9393
auto colorsClass = state->CreateClass<LuaEntityArgsClass>().DefConstructor<uint32_t>();
9494

95-
entitiesClass.DefMember("Create", [entityClass](LuaEntitiesClass *base) -> luacpp::LuaObject
96-
{ return base->CreateEntity(entityClass); })
95+
entitiesClass.DefMember("Create", [entityClass](LuaEntitiesClass *base, luacpp::LuaObject clsObj) -> luacpp::LuaObject
96+
{ return base->CreateEntity(entityClass, clsObj.GetType() != LUA_TSTRING ? nullptr : clsObj.ToString()); })
9797
.DefMember("Destroy", [](LuaEntitiesClass *base, int entityid) -> void
9898
{ base->DestroyEntity(entityid); })
9999
.DefMember("Fetch", [state](LuaEntitiesClass *base, int entityID) -> luacpp::LuaObject

src/components/Plugins/src/scripting/Entity.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ CEntityInstance *FetchInstanceByInput(std::string input)
4848
if (player == nullptr)
4949
return nullptr;
5050

51-
return player->GetPlayerController();
51+
return player->GetPlayerPawn();
5252
}
5353
else
5454
return nullptr;
5555
}
5656

57-
SMM_API uint32_t scripting_Entity_Create()
57+
SMM_API uint32_t scripting_Entity_Create(const char *classname)
5858
{
59-
uint32_t entityID = g_entityManager->CreateEntity();
59+
std::string clsname = classname;
60+
if (classname == nullptr)
61+
clsname = "prop_dynamic_override";
62+
63+
uint32_t entityID = g_entityManager->CreateEntity(classname);
6064
return entityID;
6165
}
6266

src/entities/Entity.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,27 @@
33
#include <thread>
44

55
typedef void *(*UTIL_CreateEntityByName)(const char *, int);
6+
CEntityInstance *FetchInstanceByInput(std::string input);
67

7-
Entity::Entity()
8+
Entity::Entity(std::string classname)
89
{
9-
this->worldEntity = reinterpret_cast<CBaseModelEntity *>(g_Signatures->FetchSignature<UTIL_CreateEntityByName>("UTIL_CreateEntityByName")("prop_dynamic_override", -1));
10+
CEntityInstance *instance = FetchInstanceByInput(classname);
11+
if (starts_with(classname, "player:"))
12+
{
13+
this->worldEntity = reinterpret_cast<CBaseModelEntity *>(instance);
14+
this->player = true;
15+
this->spawned = true;
16+
}
17+
else if (starts_with(classname, "entity:"))
18+
{
19+
this->worldEntity = nullptr;
20+
PRINTF("Entity::Entity", "You can't create an entity from another entity.\n");
21+
}
22+
else
23+
{
24+
this->worldEntity = reinterpret_cast<CBaseModelEntity *>(g_Signatures->FetchSignature<UTIL_CreateEntityByName>("UTIL_CreateEntityByName")(classname.c_str(), -1));
25+
}
26+
1027
if (this->worldEntity == nullptr)
1128
PRINTF("Entity::Entity", "Failed to create entity.\n");
1229
}
@@ -41,6 +58,9 @@ void Entity::SetAngle(float x, float y, float z)
4158

4259
void Entity::SetSolidType(SolidType_t solid_type)
4360
{
61+
if (this->player)
62+
return;
63+
4464
this->worldEntity->m_Collision.Get().m_nSolidType = solid_type;
4565
}
4666

src/entities/Entity.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ class Entity
1313
Z_CBaseEntity *entity = nullptr;
1414
std::string model;
1515
bool spawned = false;
16+
bool player = false;
1617

1718
public:
18-
Entity();
19+
Entity(std::string classname);
1920
Entity(CEntityInstance *entityInstance);
2021

2122
Vector GetCoords();

0 commit comments

Comments
 (0)