Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer Cache and Other Improvements #215

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AllowAllConstructorInitializersOnNextLine: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowShortBlocksOnASingleLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: 'false'
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AlwaysBreakAfterReturnType: None
Expand Down
52 changes: 52 additions & 0 deletions src/core/backend/PatternCache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "PatternCache.hpp"
#include "core/filemgr/FileMgr.hpp"

namespace YimMenu
{
std::optional<int> PatternCache::GetCachedOffsetImpl(PatternHash hash)
{
if (auto it = m_Data.find(hash.GetHash()); it != m_Data.end())
return it->second;

return std::nullopt;
}

void PatternCache::UpdateCachedOffsetImpl(PatternHash hash, int offset)
{
m_Data[hash.GetHash()] = offset;
}

void PatternCache::InitImpl()
{
auto file = FileMgr::GetProjectFile("./pattern_cache.bin");
if (file.Exists())
{
std::ifstream stream(file.Path(), std::ios_base::binary);
while (!stream.eof())
{
std::uint64_t hash;
int offset;

stream.read(reinterpret_cast<char*>(&hash), sizeof(hash));
stream.read(reinterpret_cast<char*>(&offset), sizeof(offset));

m_Data.emplace(hash, offset);
}
}

m_Initialized = true;
}

void PatternCache::UpdateImpl()
{
auto file = FileMgr::GetProjectFile("./pattern_cache.bin");
std::ofstream stream(file.Path(), std::ios_base::binary);

for (auto& [h, offset] : m_Data)
{
auto hash = h;
stream.write(reinterpret_cast<char*>(&hash), sizeof(hash));
stream.write(reinterpret_cast<char*>(&offset), sizeof(offset));
}
}
}
54 changes: 54 additions & 0 deletions src/core/backend/PatternCache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once
#include "core/memory/PatternHash.hpp"

namespace YimMenu
{
class PatternCache
{
bool m_Initialized;
std::unordered_map<std::uint64_t, int> m_Data;
public:

PatternCache() :
m_Initialized(false)
{
}

static void Init()
{
GetInstance().InitImpl();
}

static void Update()
{
GetInstance().UpdateImpl();
}

static std::optional<int> GetCachedOffset(PatternHash hash)
{
return GetInstance().GetCachedOffsetImpl(hash);
}

static void UpdateCachedOffset(PatternHash hash, int offset)
{
GetInstance().UpdateCachedOffsetImpl(hash, offset);
}

static bool IsInitialized()
{
return GetInstance().m_Initialized;
}

private:
static PatternCache& GetInstance()
{
static PatternCache Instance;
return Instance;
}

void InitImpl();
void UpdateImpl();
std::optional<int> GetCachedOffsetImpl(PatternHash hash);
void UpdateCachedOffsetImpl(PatternHash hash, int offset);
};
}
40 changes: 27 additions & 13 deletions src/core/commands/ColorCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,52 @@ namespace YimMenu

void ColorCommand::SaveState(nlohmann::json& value)
{
value = {m_ColorState.x, m_ColorState.y, m_ColorState.z, m_ColorState.w};
value = nlohmann::json::object();
value["r"] = m_State.x;
value["g"] = m_State.y;
value["b"] = m_State.z;
value["a"] = m_State.w;
}

void ColorCommand::LoadState(nlohmann::json& value)
{
if (value.is_array() && value.size() == 4)
if (value.is_object())
{
m_ColorState = ImVec4(value[0], value[1], value[2], value[3]);
m_State.x = value["r"];
m_State.y = value["g"];
m_State.z = value["b"];
m_State.w = value["a"];
}
else
else if (value.is_array())
{
m_ColorState = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
auto arr = value.get<std::array<float, 4>>();
m_State.x = arr[0];
m_State.y = arr[1];
m_State.z = arr[2];
m_State.w = arr[3];
}
}

ColorCommand::ColorCommand(std::string name, std::string label, std::string description, ImVec4 color) :
Command(name, label, description, 0),
m_ColorState(color)
m_State(color)
{
}

ImVec4 ColorCommand::GetState()
{
return m_ColorState;
return m_State;
}

void ColorCommand::SetColorState(ImVec4 state)
void ColorCommand::SetState(ImVec4 state)
{
FiberPool::Push([this] {
OnChange();
});
m_ColorState = state;
MarkDirty();
if (m_State != state)
{
FiberPool::Push([this] {
OnChange();
});
m_State = state;
MarkDirty();
}
}
}
4 changes: 2 additions & 2 deletions src/core/commands/ColorCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace YimMenu
virtual void SaveState(nlohmann::json& value) override;
virtual void LoadState(nlohmann::json& value) override;

ImVec4 m_ColorState = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
ImVec4 m_State = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);

public:
ColorCommand(std::string name, std::string label, std::string description, ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
ImVec4 GetState();
void SetColorState(ImVec4 state);
void SetState(ImVec4 state);
};
}
12 changes: 8 additions & 4 deletions src/core/hooking/BaseHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ namespace YimMenu
return m_Hooks;
}

void BaseHook::EnableAll()
bool BaseHook::EnableAll()
{
bool status = true;
for (auto hook : m_Hooks)
{
hook->Enable();
status = hook->Enable() && status;
}
return status;
}

void BaseHook::DisableAll()
bool BaseHook::DisableAll()
{
bool status = true;
for (auto hook : m_Hooks)
{
hook->Disable();
status = hook->Disable() && status;
}
return status;
}
}
4 changes: 2 additions & 2 deletions src/core/hooking/BaseHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ namespace YimMenu

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

static void EnableAll();
static void DisableAll();
static bool EnableAll();
static bool DisableAll();

private:
inline static std::vector<BaseHook*> m_Hooks;
Expand Down
4 changes: 2 additions & 2 deletions src/core/hooking/CallHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace YimMenu
inline bool CallHook<T>::Enable()
{
if (m_Enabled)
return false;
return true;

memcpy(m_TargetFunc, m_PatchedBytes, 5);
m_Enabled = true;
Expand All @@ -94,7 +94,7 @@ namespace YimMenu
inline bool CallHook<T>::Disable()
{
if (!m_Enabled)
return false;
return true;

memcpy(m_TargetFunc, m_OriginalBytes, 5);
m_Enabled = false;
Expand Down
8 changes: 4 additions & 4 deletions src/core/hooking/DetourHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace YimMenu
inline bool DetourHook<T>::Enable()
{
if (m_Enabled)
return false;
return true;

if (const auto result = MH_QueueEnableHook(m_TargetFunc); result != MH_OK)
{
Expand All @@ -71,7 +71,7 @@ namespace YimMenu
inline bool DetourHook<T>::Disable()
{
if (!m_Enabled)
return false;
return true;

if (const auto result = MH_QueueDisableHook(m_TargetFunc); result != MH_OK)
{
Expand All @@ -86,7 +86,7 @@ namespace YimMenu
inline bool DetourHook<T>::EnableNow()
{
if (m_Enabled)
return false;
return true;

if (const auto result = MH_EnableHook(m_TargetFunc); result != MH_OK)
{
Expand All @@ -101,7 +101,7 @@ namespace YimMenu
inline bool DetourHook<T>::DisableNow()
{
if (!m_Enabled)
return false;
return true;

if (const auto result = MH_DisableHook(m_TargetFunc); result != MH_OK)
{
Expand Down
Loading
Loading