Skip to content

Commit ceed442

Browse files
authored
Pointer Cache and Other Enhancements (#216)
* Pointer Cache * Fixes/Enhancements
1 parent f98fec8 commit ceed442

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2237
-172
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ AllowAllConstructorInitializersOnNextLine: 'false'
1010
AllowAllParametersOfDeclarationOnNextLine: 'false'
1111
AllowShortBlocksOnASingleLine: 'true'
1212
AllowShortCaseLabelsOnASingleLine: 'true'
13-
AllowShortFunctionsOnASingleLine: 'true'
13+
AllowShortFunctionsOnASingleLine: 'false'
1414
AllowShortIfStatementsOnASingleLine: Never
1515
AllowShortLambdasOnASingleLine: None
1616
AlwaysBreakAfterReturnType: None

src/core/backend/PatternCache.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "PatternCache.hpp"
2+
#include "core/filemgr/FileMgr.hpp"
3+
4+
namespace YimMenu
5+
{
6+
std::optional<int> PatternCache::GetCachedOffsetImpl(PatternHash hash)
7+
{
8+
if (auto it = m_Data.find(hash.GetHash()); it != m_Data.end())
9+
return it->second;
10+
11+
return std::nullopt;
12+
}
13+
14+
void PatternCache::UpdateCachedOffsetImpl(PatternHash hash, int offset)
15+
{
16+
m_Data[hash.GetHash()] = offset;
17+
}
18+
19+
void PatternCache::InitImpl()
20+
{
21+
auto file = FileMgr::GetProjectFile("./pattern_cache.bin");
22+
if (file.Exists())
23+
{
24+
std::ifstream stream(file.Path(), std::ios_base::binary);
25+
while (!stream.eof())
26+
{
27+
std::uint64_t hash;
28+
int offset;
29+
30+
stream.read(reinterpret_cast<char*>(&hash), sizeof(hash));
31+
stream.read(reinterpret_cast<char*>(&offset), sizeof(offset));
32+
33+
m_Data.emplace(hash, offset);
34+
}
35+
}
36+
37+
m_Initialized = true;
38+
}
39+
40+
void PatternCache::UpdateImpl()
41+
{
42+
auto file = FileMgr::GetProjectFile("./pattern_cache.bin");
43+
std::ofstream stream(file.Path(), std::ios_base::binary);
44+
45+
for (auto& [h, offset] : m_Data)
46+
{
47+
auto hash = h;
48+
stream.write(reinterpret_cast<char*>(&hash), sizeof(hash));
49+
stream.write(reinterpret_cast<char*>(&offset), sizeof(offset));
50+
}
51+
}
52+
}

src/core/backend/PatternCache.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#pragma once
2+
#include "core/memory/PatternHash.hpp"
3+
4+
namespace YimMenu
5+
{
6+
class PatternCache
7+
{
8+
bool m_Initialized;
9+
std::unordered_map<std::uint64_t, int> m_Data;
10+
public:
11+
12+
PatternCache() :
13+
m_Initialized(false)
14+
{
15+
}
16+
17+
static void Init()
18+
{
19+
GetInstance().InitImpl();
20+
}
21+
22+
static void Update()
23+
{
24+
GetInstance().UpdateImpl();
25+
}
26+
27+
static std::optional<int> GetCachedOffset(PatternHash hash)
28+
{
29+
return GetInstance().GetCachedOffsetImpl(hash);
30+
}
31+
32+
static void UpdateCachedOffset(PatternHash hash, int offset)
33+
{
34+
GetInstance().UpdateCachedOffsetImpl(hash, offset);
35+
}
36+
37+
static bool IsInitialized()
38+
{
39+
return GetInstance().m_Initialized;
40+
}
41+
42+
private:
43+
static PatternCache& GetInstance()
44+
{
45+
static PatternCache Instance;
46+
return Instance;
47+
}
48+
49+
void InitImpl();
50+
void UpdateImpl();
51+
std::optional<int> GetCachedOffsetImpl(PatternHash hash);
52+
void UpdateCachedOffsetImpl(PatternHash hash, int offset);
53+
};
54+
}

src/core/commands/ColorCommand.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,52 @@ namespace YimMenu
99

1010
void ColorCommand::SaveState(nlohmann::json& value)
1111
{
12-
value = {m_ColorState.x, m_ColorState.y, m_ColorState.z, m_ColorState.w};
12+
value = nlohmann::json::object();
13+
value["r"] = m_State.x;
14+
value["g"] = m_State.y;
15+
value["b"] = m_State.z;
16+
value["a"] = m_State.w;
1317
}
1418

1519
void ColorCommand::LoadState(nlohmann::json& value)
1620
{
17-
if (value.is_array() && value.size() == 4)
21+
if (value.is_object())
1822
{
19-
m_ColorState = ImVec4(value[0], value[1], value[2], value[3]);
23+
m_State.x = value["r"];
24+
m_State.y = value["g"];
25+
m_State.z = value["b"];
26+
m_State.w = value["a"];
2027
}
21-
else
28+
else if (value.is_array())
2229
{
23-
m_ColorState = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
30+
auto arr = value.get<std::array<float, 4>>();
31+
m_State.x = arr[0];
32+
m_State.y = arr[1];
33+
m_State.z = arr[2];
34+
m_State.w = arr[3];
2435
}
2536
}
2637

2738
ColorCommand::ColorCommand(std::string name, std::string label, std::string description, ImVec4 color) :
2839
Command(name, label, description, 0),
29-
m_ColorState(color)
40+
m_State(color)
3041
{
3142
}
3243

3344
ImVec4 ColorCommand::GetState()
3445
{
35-
return m_ColorState;
46+
return m_State;
3647
}
3748

38-
void ColorCommand::SetColorState(ImVec4 state)
49+
void ColorCommand::SetState(ImVec4 state)
3950
{
40-
FiberPool::Push([this] {
41-
OnChange();
42-
});
43-
m_ColorState = state;
44-
MarkDirty();
51+
if (m_State != state)
52+
{
53+
FiberPool::Push([this] {
54+
OnChange();
55+
});
56+
m_State = state;
57+
MarkDirty();
58+
}
4559
}
4660
}

src/core/commands/ColorCommand.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ namespace YimMenu
1212
virtual void SaveState(nlohmann::json& value) override;
1313
virtual void LoadState(nlohmann::json& value) override;
1414

15-
ImVec4 m_ColorState = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
15+
ImVec4 m_State = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
1616

1717
public:
1818
ColorCommand(std::string name, std::string label, std::string description, ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
1919
ImVec4 GetState();
20-
void SetColorState(ImVec4 state);
20+
void SetState(ImVec4 state);
2121
};
2222
}

src/core/hooking/BaseHook.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@ namespace YimMenu
1616
return m_Hooks;
1717
}
1818

19-
void BaseHook::EnableAll()
19+
bool BaseHook::EnableAll()
2020
{
21+
bool status = true;
2122
for (auto hook : m_Hooks)
2223
{
23-
hook->Enable();
24+
status = hook->Enable() && status;
2425
}
26+
return status;
2527
}
2628

27-
void BaseHook::DisableAll()
29+
bool BaseHook::DisableAll()
2830
{
31+
bool status = true;
2932
for (auto hook : m_Hooks)
3033
{
31-
hook->Disable();
34+
status = hook->Disable() && status;
3235
}
36+
return status;
3337
}
3438
}

src/core/hooking/BaseHook.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ namespace YimMenu
4141

4242
static std::vector<BaseHook*>& Hooks();
4343

44-
static void EnableAll();
45-
static void DisableAll();
44+
static bool EnableAll();
45+
static bool DisableAll();
4646

4747
private:
4848
inline static std::vector<BaseHook*> m_Hooks;

src/core/hooking/CallHook.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ namespace YimMenu
8383
inline bool CallHook<T>::Enable()
8484
{
8585
if (m_Enabled)
86-
return false;
86+
return true;
8787

8888
memcpy(m_TargetFunc, m_PatchedBytes, 5);
8989
m_Enabled = true;
@@ -94,7 +94,7 @@ namespace YimMenu
9494
inline bool CallHook<T>::Disable()
9595
{
9696
if (!m_Enabled)
97-
return false;
97+
return true;
9898

9999
memcpy(m_TargetFunc, m_OriginalBytes, 5);
100100
m_Enabled = false;

src/core/hooking/DetourHook.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace YimMenu
5656
inline bool DetourHook<T>::Enable()
5757
{
5858
if (m_Enabled)
59-
return false;
59+
return true;
6060

6161
if (const auto result = MH_QueueEnableHook(m_TargetFunc); result != MH_OK)
6262
{
@@ -71,7 +71,7 @@ namespace YimMenu
7171
inline bool DetourHook<T>::Disable()
7272
{
7373
if (!m_Enabled)
74-
return false;
74+
return true;
7575

7676
if (const auto result = MH_QueueDisableHook(m_TargetFunc); result != MH_OK)
7777
{
@@ -86,7 +86,7 @@ namespace YimMenu
8686
inline bool DetourHook<T>::EnableNow()
8787
{
8888
if (m_Enabled)
89-
return false;
89+
return true;
9090

9191
if (const auto result = MH_EnableHook(m_TargetFunc); result != MH_OK)
9292
{
@@ -101,7 +101,7 @@ namespace YimMenu
101101
inline bool DetourHook<T>::DisableNow()
102102
{
103103
if (!m_Enabled)
104-
return false;
104+
return true;
105105

106106
if (const auto result = MH_DisableHook(m_TargetFunc); result != MH_OK)
107107
{

0 commit comments

Comments
 (0)