Skip to content

Commit 27773a0

Browse files
committed
refactor: replace manual dynamic allocation with smart pointers
1 parent 623b751 commit 27773a0

9 files changed

Lines changed: 58 additions & 37 deletions

File tree

src/Main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
#include <game_sa/CPlayerPed.h>
99
#include <game_sa/CTimer.h>
1010
#include "../utils/log.h"
11-
#include "events/ProcessEvent.hpp"
11+
#include "./events/ProcessEvent.hpp"
1212
#include <iostream>
1313
#include <string>
1414
#include <fstream>
15-
#include <sstream> // Para stringstream
16-
17-
// Função auxiliar para converter qualquer tipo para string
15+
#include <sstream>
16+
#include "./global/Name.hpp"
1817

1918

2019
ServerSocket* g_Server = nullptr;
21-
20+
std::string nome = "";
2221
using namespace plugin;
2322

2423
class JumpDetectorPlugin
@@ -69,7 +68,7 @@ class JumpDetectorPlugin
6968
writeLog("ERRO: playerPed é NULL!");
7069
}
7170

72-
// Ler nome
71+
7372
writeLog("Tentando ler name.txt...");
7473
std::ifstream arquivo("name.txt");
7574
std::string name;
@@ -84,6 +83,7 @@ class JumpDetectorPlugin
8483
arquivo.close();
8584
return;
8685
}
86+
nome = name;
8787
arquivo.close();
8888
writeLog("name.txt lido com sucesso: '" + name + "'");
8989

src/events/ProcessEvent.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
#include "./onPlayerJump/onPlayerJump.hpp"
33
#include "../../utils/log.h"
44
#include "./onMove/onMove.hpp"
5+
#include <memory>
56

67
#if PROCESSEVENT
78

89
void ProcessEvent::create()
910
{
1011
if (m_cj)
1112
{
12-
m_jumpEvent = new CEventOnPlayerJump(m_cj);
13-
m_deadEvent = new onPlayerDead(m_cj);
14-
m_damageEvent = new onPlayerDamage(m_cj, server);
15-
onmove = new onMove(m_cj);
13+
m_jumpEvent = std::make_shared<CEventOnPlayerJump>(m_cj);
14+
m_deadEvent = std::make_unique<onPlayerDead>(m_cj);
15+
m_damageEvent = std::make_unique<onPlayerDamage>(m_cj);
16+
onmove = std::make_unique<onMove>(m_cj);
1617
}
1718
cjA = nullptr;
1819
}
@@ -32,7 +33,7 @@ void ProcessEvent::execute()
3233
CPed *playerPed = FindPlayerPed();
3334
if (playerPed && !m_jumpEvent)
3435
{
35-
m_jumpEvent = new CEventOnPlayerJump(playerPed);
36+
m_jumpEvent = std::make_shared<CEventOnPlayerJump>(playerPed);
3637
if (m_jumpEvent)
3738
{
3839

@@ -57,9 +58,6 @@ void ProcessEvent::execute()
5758

5859
ProcessEvent::~ProcessEvent()
5960
{
60-
if (m_jumpEvent)
61-
{
62-
delete m_jumpEvent;
63-
}
61+
6462
}
6563
#endif

src/events/ProcessEvent.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "./onPlayerDead/onPlayerDead.hpp"
66
#include "./onPlayerDamage/onPlayerDamage.hpp"
77
#include "./onMove/onMove.hpp"
8-
8+
#include <memory>
99

1010
#define PROCESSEVENT 1
1111

@@ -15,12 +15,15 @@ class ProcessEvent {
1515

1616
private:
1717
CPed* m_cj;
18-
ServerSocket* server;
1918
CPed* cjA;
20-
CEventOnPlayerJump* m_jumpEvent;
21-
onPlayerDead* m_deadEvent;
22-
onPlayerDamage* m_damageEvent;
23-
onMove* onmove;
19+
20+
std::unique_ptr<ServerSocket> server;
21+
22+
std::shared_ptr<CEventOnPlayerJump> m_jumpEvent;
23+
std::unique_ptr<onPlayerDead> m_deadEvent;
24+
std::unique_ptr<onPlayerDamage> m_damageEvent;
25+
std::unique_ptr<onMove> onmove;
26+
2427
unsigned int m_lastStatsTime;
2528
int m_jumpCount;
2629
bool m_initialized;

src/events/hook_base.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
#ifndef HOOK_BASE
3+
#define HOOK_BASE
4+
5+
#include <plugin.h>
6+
7+
class hook_base
8+
{
9+
private:
10+
CPed* cj;
11+
public:
12+
hook_base();
13+
~hook_base();
14+
void run();
15+
};
16+
17+
18+
19+
20+
#endif

src/events/onMove/onMove.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#include "../../../utils/log.h"
88
#include <iostream>
99
#include <string>
10-
#if onMoveDefine
10+
#include "../../global/Name.hpp"
11+
1112

1213
onMove::~onMove() {}
1314

@@ -17,11 +18,7 @@ void onMove::run(CPed *cja)
1718
if (!cja)
1819
return;
1920

20-
std::ifstream arquivo("name.txt");
21-
22-
std::string name;
23-
std::getline(arquivo, name);
24-
arquivo.close();
21+
2522
CVector currentPos = cja->GetPosition();
2623

2724
float currentX, currentY, currentZ;
@@ -52,7 +49,7 @@ void onMove::run(CPed *cja)
5249

5350
if (g_Server)
5451
{
55-
std::string msg = "onMovimentPlayer::"+ name + "::" + std::to_string(currentPos.x) + "::" + std::to_string(currentPos.y) + "::" + std::to_string(currentPos.z) + "::" + std::to_string(lastX) + "::" + std::to_string(lastY) + "::" + std::to_string(lastZ);
52+
std::string msg = "onMovimentPlayer::"+ nome + "::" + std::to_string(currentPos.x) + "::" + std::to_string(currentPos.y) + "::" + std::to_string(currentPos.z) + "::" + std::to_string(lastX) + "::" + std::to_string(lastY) + "::" + std::to_string(lastZ);
5653
g_Server->sendEventServer(msg.c_str());
5754
}
5855
}
@@ -63,4 +60,3 @@ void onMove::run(CPed *cja)
6360
lastY = currentY;
6461
lastZ = currentZ;
6562
}
66-
#endif

src/events/onMove/onMove.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
#include <CVector.h>
33
#include <plugin.h>
44
#include <game_sa/CPed.h>
5-
#define onMoveDefine 1
65

7-
#if onMoveDefine
6+
87

98
class onMove{
109

1110
private:
12-
CPed *cj;
11+
CPed* cj;
1312
CVector vector;
1413
CVector lastPosition;
1514
float lastX, lastY, lastZ;
@@ -20,8 +19,7 @@ class onMove{
2019

2120
~onMove();
2221

23-
void run(CPed *cja);
22+
void run(CPed *cja) ;
2423

2524
};
2625

27-
#endif

src/events/onPlayerDamage/onPlayerDamage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
#if ONPLAYERDAMAGE
13-
onPlayerDamage::onPlayerDamage(CPed *cj, ServerSocket* server) : cj(cj), wasVida(cj->m_fHealth),
13+
onPlayerDamage::onPlayerDamage(CPed *cj) : cj(cj), wasVida(cj->m_fHealth),
1414
server(server)
1515
{
1616
}
@@ -33,7 +33,7 @@ void onPlayerDamage::execute()
3333
float dano = wasVida - cj->m_fHealth;
3434
if (dano > 0.1f )
3535
{
36-
onPlayerDamageServer(dano, server, name);
36+
onPlayerDamageServer(dano, g_Server, name);
3737
}
3838
wasVida = cj->m_fHealth;
3939
}

src/events/onPlayerDamage/onPlayerDamage.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
#if ONPLAYERDAMAGE
99

10+
11+
1012
class onPlayerDamage {
1113
private:
1214
CPed* cj;
1315
ServerSocket* server;
1416
float wasVida;
1517
public:
16-
explicit onPlayerDamage(CPed* cj, ServerSocket* server);
18+
explicit onPlayerDamage(CPed* cj);
1719
~onPlayerDamage();
1820
void execute();
1921
};

src/global/Name.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
#include <string>
3+
4+
extern std::string nome;

0 commit comments

Comments
 (0)