Skip to content

Commit d0a9137

Browse files
committed
Get sig/offset optimization
1 parent 1cdbe5c commit d0a9137

File tree

5 files changed

+59
-40
lines changed

5 files changed

+59
-40
lines changed

src/Client/Hook/Hooks/Game/ActorBaseTick.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ void ActorBaseTick::enableHook() {
1313
int offset = *reinterpret_cast<int *>(base + 3);
1414
auto **vft = reinterpret_cast<uintptr_t **>(base + offset + 7);
1515

16-
this->manualHook(vft[GET_OFFSET("Actor::baseTickVft")], (void *) callback, (void **) &funcOriginal);
16+
static auto vftOffset = GET_OFFSET("Actor::baseTickVft");
17+
18+
this->manualHook(vft[vftOffset], (void *) callback, (void **) &funcOriginal);
1719

1820
}
1921

src/Client/Hook/Hooks/Game/GameModeAttack.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ void GameModeAttackHook::enableHook() {
2525
int offset = *reinterpret_cast<int *>(base + 3);
2626
auto **vft = reinterpret_cast<uintptr_t **>(base + offset + 7);
2727

28-
this->manualHook(vft[GET_OFFSET("Gamemode::attackVft")], (void *) callback, (void **) &funcOriginal);
28+
static auto attackVftOffset = GET_OFFSET("Gamemode::attackVft");
29+
30+
this->manualHook(vft[attackVftOffset], (void *) callback, (void **) &funcOriginal);
2931
}
Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
11
#include "SignatureAndOffsetManager.hpp"
22

3-
#include "../../Logger/Logger.hpp"
4-
53
SignatureAndOffsetManager Mgr;
64

7-
void SignatureAndOffsetManager::addSignature(const std::string& name, const std::string& sig) {
8-
sigs.emplace(name, sig);
5+
void SignatureAndOffsetManager::addSignature(const char* name, const char* sig) {
6+
sigs[Utils::hash(name)] = sig;
97
}
108

11-
std::string SignatureAndOffsetManager::getSig(const std::string& name) const {
12-
if (!sigs.contains(name)) {
13-
#ifndef NDEBUG
14-
Logger::debug("[Signatures] Couldn't find sig: " + name);
15-
#endif
16-
return "";
17-
}
18-
return sigs.at(name);
9+
const char* SignatureAndOffsetManager::getSig(const char* name) const {
10+
auto it = sigs.find(Utils::hash(name));
11+
return it != sigs.end() ? it->second.c_str() : nullptr;
1912
}
2013

21-
void SignatureAndOffsetManager::addOffset(const std::string &name, const int& offset) {
22-
offsets.emplace(name, offset);
14+
void SignatureAndOffsetManager::addOffset(const char* name, int offset) {
15+
offsets[Utils::hash(name)] = offset;
2316
}
2417

25-
int SignatureAndOffsetManager::getOffset(const std::string& name) const {
26-
if (!offsets.contains(name)) {
27-
#ifndef NDEBUG
28-
Logger::debug("[Offsets] Couldn't find offset: " + name);
29-
#endif
30-
return 0;
31-
}
32-
return offsets.at(name);
18+
int SignatureAndOffsetManager::getOffset(const char* name) const {
19+
auto it = offsets.find(Utils::hash(name));
20+
return it != offsets.end() ? it->second : 0; // Default to 0 if not found
3321
}
3422

3523
void SignatureAndOffsetManager::clear() {
3624
sigs.clear();
37-
}
38-
25+
offsets.clear();
26+
}

src/Utils/Memory/Game/SignatureAndOffsetManager.hpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,45 @@
22

33
#include <string>
44
#include <unordered_map>
5-
6-
// TODO: Signature class
7-
8-
#define ADD_SIG(name, sig) Mgr.addSignature(name, sig)
9-
#define GET_SIG(name) Mgr.getSig(name)
10-
11-
#define ADD_OFFSET(name, offset) Mgr.addOffset(name, offset)
12-
#define GET_OFFSET(name) Mgr.getOffset(name)
5+
#include "../../../Utils/Utils.hpp"
6+
7+
#define ADD_SIG(name, sig) \
8+
[]{ \
9+
constexpr unsigned int hash_val = Utils::hash(name); \
10+
Mgr.addSignature(name, sig); \
11+
}()
12+
13+
#define GET_SIG(name) \
14+
[]{ \
15+
constexpr unsigned int hash_val = Utils::hash(name); \
16+
return Mgr.getSig(name); \
17+
}()
18+
19+
#define ADD_OFFSET(name, offset) \
20+
[]{ \
21+
constexpr unsigned int hash_val = Utils::hash(name); \
22+
Mgr.addOffset(name, offset); \
23+
}()
24+
25+
#define GET_OFFSET(name) \
26+
[]{ \
27+
constexpr unsigned int hash_val = Utils::hash(name); \
28+
return Mgr.getOffset(name); \
29+
}()
1330

1431
class SignatureAndOffsetManager {
1532
public:
16-
void addSignature(const std::string& name, const std::string& sig);
17-
[[nodiscard]] std::string getSig(const std::string& name) const;
33+
void addSignature(const char* name, const char* sig);
34+
[[nodiscard]] const char* getSig(const char* name) const;
1835

19-
void addOffset(const std::string& name, const int& offset);
20-
[[nodiscard]] int getOffset(const std::string& name) const;
36+
void addOffset(const char* name, int offset);
37+
[[nodiscard]] int getOffset(const char* name) const;
2138

2239
void clear();
2340

2441
private:
25-
std::unordered_map<std::string, std::string> sigs{};
26-
std::unordered_map<std::string, int> offsets{};
42+
std::unordered_map<unsigned int, std::string> sigs{};
43+
std::unordered_map<unsigned int, int> offsets{};
2744
};
2845

2946
extern SignatureAndOffsetManager Mgr;

src/Utils/Utils.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ class Utils {
248248
static std::string remomveNonNumeric(const std::string &input);
249249

250250
static bool CursorInEllipse(float ellipseX, float ellipseY, float radiusX, float radiusY);
251+
252+
static constexpr unsigned int hash(const char *str) {
253+
unsigned int hash = 5381;
254+
int c;
255+
256+
while ((c = *str++))
257+
hash = ((hash << 5) + hash) + c; // hash * 33 + c
258+
259+
return hash;
260+
}
251261
};
252262

253263
struct CaretMeasureData {

0 commit comments

Comments
 (0)