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 #140

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
23 changes: 12 additions & 11 deletions src/core/commands/HotkeySystem.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#include "HotkeySystem.hpp"

#include "game/rdr/Natives.hpp" // TODO: game import in core
#include "game/backend/ScriptMgr.hpp"
#include "Commands.hpp"
#include "LoopedCommand.hpp"
#include "game/backend/ScriptMgr.hpp"
#include "game/rdr/Natives.hpp" // TODO: game import in core


// TODO: serialization isn't stable

namespace YimMenu
{
HotkeySystem::HotkeySystem() :
IStateSerializer("hotkeys")
HotkeySystem::HotkeySystem() :
IStateSerializer("hotkeys")
{
}

void HotkeySystem::RegisterCommands()
{
auto Commands = Commands::GetCommands();
auto Commands = Commands::GetCommands();

for (auto [Hash, Command] : Commands)
{
CommandLink link;
m_CommandHotkeys.insert(std::make_pair(Command->GetHash(), link));
}

LOG(INFO) << "Registered " << m_CommandHotkeys.size() << " commands";
}

Expand Down Expand Up @@ -95,17 +96,17 @@ namespace YimMenu
{
if (Link.Hotkey.empty() || Link.Listening)
continue;

bool AllKeysPressed = true;

for (auto HotkeyModifier : Link.Hotkey)
{
if (!(GetAsyncKeyState(HotkeyModifier) & 0x8000))
{
AllKeysPressed = false;
}
}

if (AllKeysPressed && GetForegroundWindow() == Pointers.Hwnd && std::chrono::system_clock::now() - m_LastHotkeyTriggerTime > 100ms)
{
auto Command = Commands::GetCommand(Hash);
Expand Down Expand Up @@ -134,7 +135,7 @@ namespace YimMenu
for (auto& [key, value] : state.items())
{
if (m_CommandHotkeys.contains(std::atoi(key.data())))
m_CommandHotkeys[std::atoi(key.data())].Hotkey = value.get<std::vector<int>>();
m_CommandHotkeys[std::atoi(key.data())].Hotkey = value.get<std::vector<int>>();
}
}
}
28 changes: 26 additions & 2 deletions src/core/memory/PatternScanner.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "PatternScanner.hpp"

#include "Module.hpp"
#include "game/pointers/Pointers.hpp"

#include <functional>
#include <future>


namespace YimMenu
{
PatternScanner::PatternScanner(const Module* module) :
Expand All @@ -17,13 +20,31 @@ namespace YimMenu
if (!m_Module || !m_Module->Valid())
return false;


Pointers.Cache.Load();
bool scanSuccess = true;
std::vector<std::future<bool>> jobs;
bool forceUpdate = false;
if (Pointers.Cache.IsCacheOutdated())
{
forceUpdate = true;
Pointers.Cache.IncrementCacheVersion();
}

for (const auto& [pattern, func] : m_Patterns)
{
jobs.emplace_back(std::async(&PatternScanner::ScanInternal, this, pattern, func));
uintptr_t cachedPointer = Pointers.Cache.GetData(pattern->Name().data());
if (cachedPointer != 0 && !forceUpdate)
{
std::invoke(func, cachedPointer);
}
else
{
jobs.emplace_back(std::async(&PatternScanner::ScanInternal, this, pattern, func));
}
}

bool scanSuccess = true;

for (auto& job : jobs)
{
job.wait();
Expand All @@ -35,6 +56,7 @@ namespace YimMenu
{
LOG(FATAL) << "Some patterns have not been found, continuing would be foolish.";
}

return scanSuccess;
}

Expand Down Expand Up @@ -63,6 +85,8 @@ namespace YimMenu

std::invoke(func, i);

Pointers.Cache.GetOrUpdate(pattern->Name().data(), i);

return true;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/memory/PatternScanner.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once
#include "Pattern.hpp"
#include "PointerCalculator.hpp"
#include "game/pointers/PointerCache.hpp"

#include <functional>
#include <vector>


namespace YimMenu
{
class Module;
Expand All @@ -16,6 +18,7 @@ namespace YimMenu
const Module* m_Module;
std::vector<std::pair<const IPattern*, PatternFunc>> m_Patterns;


public:
PatternScanner(const Module* module);

Expand Down
Loading