Skip to content

Commit 8fe1a3d

Browse files
committed
fix(scripting): Pointers -> Objects
1 parent bbbec4c commit 8fe1a3d

File tree

9 files changed

+92
-153
lines changed

9 files changed

+92
-153
lines changed

src/plugins/core/scripting.h

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,11 @@
2020

2121
#include <any>
2222

23+
#include "scripting/generated/classes.h"
24+
2325
#define INVALID_MESSAGE_ID -1
2426

25-
class GCBaseEntity;
26-
class GCBasePlayerController;
27-
class GCBasePlayerPawn;
28-
class GCCSPlayerController;
29-
class GCCSPlayerPawn;
30-
class GCCSPlayerPawnBase;
31-
class GCEntityInstance;
3227
class PluginMemory;
33-
class GCBasePlayerWeapon;
34-
class GCCSWeaponBase;
35-
class GCBasePlayerWeaponVData;
36-
class GCCSWeaponBaseVData;
37-
class GCCSGameRules;
3828

3929
//////////////////////////////////////////////////////////////
4030
///////////////// Entity IO //////////////
@@ -539,18 +529,16 @@ class PluginPlayer
539529
std::string plugin_name;
540530
int playerId;
541531

542-
std::map<std::string, void*> ptrs;
543-
544532
public:
545533
PluginPlayer(std::string m_plugin_name, int m_playerId);
546534
~PluginPlayer();
547535

548-
GCBaseEntity* GetCBaseEntity();
549-
GCBasePlayerController* GetCBasePlayerController();
550-
GCBasePlayerPawn* GetCBasePlayerPawn();
551-
GCCSPlayerController* GetCCSPlayerController();
552-
GCCSPlayerPawn* GetCCSPlayerPawn();
553-
GCCSPlayerPawnBase* GetCCSPlayerPawnBase();
536+
GCBaseEntity GetCBaseEntity();
537+
GCBasePlayerController GetCBasePlayerController();
538+
GCBasePlayerPawn GetCBasePlayerPawn();
539+
GCCSPlayerController GetCCSPlayerController();
540+
GCCSPlayerPawn GetCCSPlayerPawn();
541+
GCCSPlayerPawnBase GetCCSPlayerPawnBase();
554542
void Drop(int reason);
555543
uint32_t GetConnectedTime();
556544
PluginWeaponManager GetWeaponManager();
@@ -597,6 +585,8 @@ class PluginPlayer
597585

598586
void SetBunnyhop(bool state);
599587
bool GetBunnyhop();
588+
589+
bool IsValid();
600590
};
601591

602592
//////////////////////////////////////////////////////////////
@@ -718,8 +708,8 @@ class PluginHooks
718708
///////////////// Entities //////////////
719709
////////////////////////////////////////////////////////////
720710

721-
std::vector<GCEntityInstance*> UTIL_FindEntitiesByClassname(const char* name);
722-
GCEntityInstance* CreateEntityByName(const char* name);
711+
std::vector<GCEntityInstance> UTIL_FindEntitiesByClassname(const char* name);
712+
GCEntityInstance CreateEntityByName(const char* name);
723713

724714
//////////////////////////////////////////////////////////////
725715
///////////////// Utils //////////////
@@ -728,7 +718,7 @@ GCEntityInstance* CreateEntityByName(const char* name);
728718
bool scripting_IsWindows();
729719
bool scripting_IsLinux();
730720
std::string scripting_GetOS();
731-
GCCSGameRules* scripting_GetCCSGameRules();
721+
GCCSGameRules scripting_GetCCSGameRules();
732722
std::string scripting_GetPluginPath(std::string plugin_name);
733723
void scripting_StateUpdate(std::string ptr, std::string classname, std::string field, bool isStruct);
734724

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

3-
#include "generated/classes.h"
4-
5-
std::map<CEntityIdentity *, GCEntityInstance *> entityInstanceMapping;
6-
7-
std::vector<GCEntityInstance *> UTIL_FindEntitiesByClassname(const char *name)
3+
std::vector<GCEntityInstance> UTIL_FindEntitiesByClassname(const char* name)
84
{
9-
extern CEntitySystem *g_pEntitySystem;
10-
CEntityIdentity *pEntity = g_pEntitySystem->m_EntityList.m_pFirstActiveEntity;
11-
std::vector<GCEntityInstance *> entities;
5+
extern CEntitySystem* g_pEntitySystem;
6+
CEntityIdentity* pEntity = g_pEntitySystem->m_EntityList.m_pFirstActiveEntity;
7+
std::vector<GCEntityInstance> entities;
128

139
for (; pEntity; pEntity = pEntity->m_pNext)
14-
{
1510
if (!strcmp(pEntity->m_designerName.String(), name))
16-
{
17-
if (entityInstanceMapping.find(pEntity) == entityInstanceMapping.end())
18-
entityInstanceMapping.insert({pEntity, new GCEntityInstance((void *)(pEntity->m_pInstance))});
19-
20-
entities.push_back(entityInstanceMapping.at(pEntity));
21-
}
22-
};
11+
entities.push_back(GCEntityInstance((void*)(pEntity->m_pInstance)));
2312

2413
return entities;
2514
}
2615

27-
GCEntityInstance *CreateEntityByName(const char *name)
16+
GCEntityInstance CreateEntityByName(const char* name)
2817
{
2918
std::string className = (!name ? "prop_dynamic_override" : name);
3019

31-
CEntityIdentity *entityPtr = reinterpret_cast<CEntityIdentity *>(g_Signatures->FetchSignature<UTIL_CreateEntityByName>("UTIL_CreateEntityByName")(className.c_str(), -1));
32-
33-
GCEntityInstance *instance = new GCEntityInstance(entityPtr);
34-
if (entityInstanceMapping.find(entityPtr) == entityInstanceMapping.end())
35-
entityInstanceMapping.insert({entityPtr, instance});
20+
CEntityIdentity* entityPtr = reinterpret_cast<CEntityIdentity*>(g_Signatures->FetchSignature<UTIL_CreateEntityByName>("UTIL_CreateEntityByName")(className.c_str(), -1));
3621

22+
GCEntityInstance instance(entityPtr);
3723
return instance;
3824
}

src/plugins/core/scripting/generated/classes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "../../scripting_includes.h"
24
#include "enums.h"
35

src/plugins/core/scripting/player.cpp

Lines changed: 55 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include "../../../commands/CommandsManager.h"
55
#include "../../../precacher/precacher.h"
66

7-
#include "generated/classes.h"
8-
97
PluginPlayer::PluginPlayer(std::string m_plugin_name, int m_playerId)
108
{
119
REGISTER_CALLSTACK(this->plugin_name, string_format("PluginPlayer::PluginPlayer(m_plugin_name=\"%s\", m_playerId=%d)", m_plugin_name.c_str(), m_playerId));
@@ -14,123 +12,78 @@ PluginPlayer::PluginPlayer(std::string m_plugin_name, int m_playerId)
1412
this->playerId = m_playerId;
1513
}
1614

17-
PluginPlayer::~PluginPlayer()
18-
{
19-
20-
for (auto it = this->ptrs.begin(); it != this->ptrs.end(); ++it)
21-
{
22-
delete it->second;
23-
}
24-
this->ptrs.clear();
25-
}
15+
PluginPlayer::~PluginPlayer() {}
2616

27-
GCBaseEntity* PluginPlayer::GetCBaseEntity()
17+
GCBaseEntity PluginPlayer::GetCBaseEntity()
2818
{
2919
Player* player = g_playerManager->GetPlayer(this->playerId);
30-
31-
if (this->ptrs.find("CBaseEntity") == this->ptrs.end())
32-
{
33-
void* ptr = (void*)(new GCBaseEntity(player->GetPlayerPawn()));
34-
this->ptrs.insert({ "CBaseEntity", ptr });
35-
}
36-
else {
37-
if (((GCBaseEntity*)this->ptrs.at("CBaseEntity"))->GetPtr() != (void*)player->GetPlayerPawn()) {
38-
delete this->ptrs["CBaseEntity"];
39-
this->ptrs["CBaseEntity"] = (void*)(new GCBaseEntity(player->GetPlayerPawn()));
40-
}
20+
if (!player) {
21+
GCBaseEntity val(nullptr);
22+
return val;
4123
}
4224

43-
return (GCBaseEntity*)this->ptrs.at("CBaseEntity");
25+
GCBaseEntity val((void*)player->GetPlayerPawn());
26+
return val;
4427
}
4528

46-
GCBasePlayerController* PluginPlayer::GetCBasePlayerController()
29+
GCBasePlayerController PluginPlayer::GetCBasePlayerController()
4730
{
4831
Player* player = g_playerManager->GetPlayer(this->playerId);
49-
if (this->ptrs.find("CBasePlayerController") == this->ptrs.end())
50-
{
51-
void* ptr = (void*)(new GCBasePlayerController(player->GetController()));
52-
this->ptrs.insert({ "CBasePlayerController", ptr });
53-
}
54-
else {
55-
if (((GCBasePlayerController*)this->ptrs.at("CBasePlayerController"))->GetPtr() != (void*)player->GetController()) {
56-
delete this->ptrs["CBasePlayerController"];
57-
this->ptrs["CBasePlayerController"] = (void*)(new GCBasePlayerController(player->GetController()));
58-
}
32+
if (!player) {
33+
GCBasePlayerController val(nullptr);
34+
return val;
5935
}
6036

61-
return (GCBasePlayerController*)this->ptrs.at("CBasePlayerController");
37+
GCBasePlayerController val((void*)player->GetController());
38+
return val;
6239
}
6340

64-
GCBasePlayerPawn* PluginPlayer::GetCBasePlayerPawn()
41+
GCBasePlayerPawn PluginPlayer::GetCBasePlayerPawn()
6542
{
6643
Player* player = g_playerManager->GetPlayer(this->playerId);
67-
if (this->ptrs.find("CBasePlayerPawn") == this->ptrs.end())
68-
{
69-
void* ptr = (void*)(new GCBasePlayerPawn(player->GetPawn()));
70-
this->ptrs.insert({ "CBasePlayerPawn", ptr });
71-
}
72-
else {
73-
if (((GCBasePlayerPawn*)this->ptrs.at("CBasePlayerPawn"))->GetPtr() != (void*)player->GetPawn()) {
74-
delete this->ptrs["CBasePlayerPawn"];
75-
this->ptrs["CBasePlayerPawn"] = (void*)(new GCBasePlayerPawn(player->GetPawn()));
76-
}
44+
if (!player) {
45+
GCBasePlayerPawn val(nullptr);
46+
return val;
7747
}
7848

79-
return (GCBasePlayerPawn*)this->ptrs.at("CBasePlayerPawn");
49+
GCBasePlayerPawn val((void*)player->GetPawn());
50+
return val;
8051
}
8152

82-
GCCSPlayerController* PluginPlayer::GetCCSPlayerController()
53+
GCCSPlayerController PluginPlayer::GetCCSPlayerController()
8354
{
8455
Player* player = g_playerManager->GetPlayer(this->playerId);
85-
if (this->ptrs.find("CCSPlayerController") == this->ptrs.end())
86-
{
87-
void* ptr = (void*)(new GCCSPlayerController(player->GetPlayerController()));
88-
this->ptrs.insert({ "CCSPlayerController", ptr });
89-
}
90-
else {
91-
if (((GCCSPlayerController*)this->ptrs.at("CCSPlayerController"))->GetPtr() != (void*)player->GetPlayerController()) {
92-
delete this->ptrs["CCSPlayerController"];
93-
this->ptrs["CCSPlayerController"] = (void*)(new GCCSPlayerController(player->GetPlayerController()));
94-
}
56+
if (!player) {
57+
GCCSPlayerController val(nullptr);
58+
return val;
9559
}
9660

97-
return (GCCSPlayerController*)this->ptrs.at("CCSPlayerController");
61+
GCCSPlayerController val((void*)player->GetPlayerController());
62+
return val;
9863
}
9964

100-
GCCSPlayerPawn* PluginPlayer::GetCCSPlayerPawn()
65+
GCCSPlayerPawn PluginPlayer::GetCCSPlayerPawn()
10166
{
10267
Player* player = g_playerManager->GetPlayer(this->playerId);
103-
if (this->ptrs.find("CCSPlayerPawn") == this->ptrs.end())
104-
{
105-
void* ptr = (void*)(new GCCSPlayerPawn(player->GetPlayerPawn()));
106-
this->ptrs.insert({ "CCSPlayerPawn", ptr });
107-
}
108-
else {
109-
if (((GCCSPlayerPawn*)this->ptrs.at("CCSPlayerPawn"))->GetPtr() != (void*)player->GetPlayerPawn()) {
110-
delete this->ptrs["CCSPlayerPawn"];
111-
this->ptrs["CCSPlayerPawn"] = (void*)(new GCCSPlayerPawn(player->GetPlayerPawn()));
112-
}
68+
if (!player) {
69+
GCCSPlayerPawn val(nullptr);
70+
return val;
11371
}
11472

115-
return (GCCSPlayerPawn*)this->ptrs.at("CCSPlayerPawn");
73+
GCCSPlayerPawn val((void*)player->GetPlayerPawn());
74+
return val;
11675
}
11776

118-
GCCSPlayerPawnBase* PluginPlayer::GetCCSPlayerPawnBase()
77+
GCCSPlayerPawnBase PluginPlayer::GetCCSPlayerPawnBase()
11978
{
12079
Player* player = g_playerManager->GetPlayer(this->playerId);
121-
if (this->ptrs.find("CCSPlayerPawnBase") == this->ptrs.end())
122-
{
123-
void* ptr = (void*)(new GCCSPlayerPawnBase(player->GetPlayerBasePawn()));
124-
this->ptrs.insert({ "CCSPlayerPawnBase", ptr });
125-
}
126-
else {
127-
if (((GCCSPlayerPawnBase*)this->ptrs.at("CCSPlayerPawnBase"))->GetPtr() != (void*)player->GetPlayerBasePawn()) {
128-
delete this->ptrs["CCSPlayerPawnBase"];
129-
this->ptrs["CCSPlayerPawnBase"] = (void*)(new GCCSPlayerPawnBase(player->GetPlayerBasePawn()));
130-
}
80+
if (!player) {
81+
GCCSPlayerPawnBase val(nullptr);
82+
return val;
13183
}
13284

133-
return (GCCSPlayerPawnBase*)this->ptrs.at("CCSPlayerPawnBase");
85+
GCCSPlayerPawnBase val((void*)player->GetPlayerBasePawn());
86+
return val;
13487
}
13588

13689
void PluginPlayer::Drop(int reason)
@@ -563,3 +516,21 @@ bool PluginPlayer::GetBunnyhop()
563516

564517
return self->bunnyhopState;
565518
}
519+
520+
bool PluginPlayer::IsValid()
521+
{
522+
Player* self = g_playerManager->GetPlayer(this->playerId);
523+
if (!self) return false;
524+
if (self->IsFirstSpawn()) return false;
525+
if (self->IsFakeClient()) return false;
526+
527+
CBasePlayerController* controller = self->GetController();
528+
if (!controller) return false;
529+
if (controller->m_bIsHLTV()) return false;
530+
if (controller->m_iConnected() != CPlayerConnectedState::PlayerConnected) return false;
531+
532+
CBasePlayerPawn* pawn = self->GetPawn();
533+
if (!pawn) return false;
534+
535+
return true;
536+
}
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
#include "../scripting.h"
22
#include "../../../sdk/entity/CGameRules.h"
33
#include "../../../hooks/FuncHook.h"
4-
#include "generated/classes.h"
54
#include "../../PluginManager.h"
65

7-
void Hook_CGameRules_Constructor(CGameRules *pThis);
8-
CCSGameRules *gameRules = nullptr;
9-
GCCSGameRules *gameRulesPtr = nullptr;
6+
void Hook_CGameRules_Constructor(CGameRules* pThis);
7+
CCSGameRules* gameRules = nullptr;
108

119
FuncHook<decltype(Hook_CGameRules_Constructor)> CGameRules_ConstructorT(Hook_CGameRules_Constructor, "CGameRules_Constructor");
1210

13-
void Hook_CGameRules_Constructor(CGameRules *pThis)
11+
void Hook_CGameRules_Constructor(CGameRules* pThis)
1412
{
15-
gameRules = (CCSGameRules *)pThis;
13+
gameRules = (CCSGameRules*)pThis;
1614
CGameRules_ConstructorT(pThis);
1715
}
1816

@@ -31,23 +29,16 @@ std::string scripting_GetOS()
3129
return WIN_LINUX("Windows", "Linux");
3230
}
3331

34-
GCCSGameRules *scripting_GetCCSGameRules()
32+
GCCSGameRules scripting_GetCCSGameRules()
3533
{
36-
if (!gameRulesPtr)
37-
gameRulesPtr = new GCCSGameRules((void *)gameRules);
38-
else if (gameRulesPtr->GetPtr() != (void *)gameRules)
39-
{
40-
delete gameRulesPtr;
41-
gameRulesPtr = new GCCSGameRules((void *)gameRules);
42-
}
43-
44-
return gameRulesPtr;
34+
GCCSGameRules val(gameRules);
35+
return val;
4536
}
4637

4738
std::string scripting_GetPluginPath(std::string plugin_name)
4839
{
4940
Plugin* plugin = g_pluginManager->FetchPlugin(plugin_name);
50-
if(!plugin) return "";
41+
if (!plugin) return "";
5142

5243
return string_format("addons/swiftly/plugins/%s", plugin->GetName().c_str());
5344
}

src/plugins/core/scripting/weapons.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "../scripting.h"
22
#include "../../../player/PlayerManager.h"
3-
#include "generated/classes.h"
43

54
PluginWeaponManager::PluginWeaponManager(int playerId)
65
{

src/plugins/lua/scripting/weapons.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "core.h"
2-
#include "../../core/scripting/generated/classes.h"
32

4-
void SetupLuaWeapons(LuaPlugin *plugin, lua_State *state)
3+
void SetupLuaWeapons(LuaPlugin* plugin, lua_State* state)
54
{
65
luabridge::getGlobalNamespace(state)
76
.beginClass<PluginWeaponManager>("WeaponManager")

0 commit comments

Comments
 (0)