Skip to content

Commit a61a7d1

Browse files
X-rays5Copilot
andauthored
Make the injector use less cpu (#78)
* Make it use a bit less cpu * Fix game running state inverted Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix game running check thread not being joined * Remove useless logic * Catch filesystem error in logger by const reference Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 43a3f7c commit a61a7d1

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

common/src/logging/logger.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,16 @@ namespace base::common::logging {
6868

6969
void MovePossibleCrashLog() {
7070
const auto log_file_path = GetLogFile();
71-
if (std::filesystem::exists(log_file_path)) {
72-
const auto log_file_path_tmp = log_file_path.parent_path() / fmt::format("{}_{}_hard_crash{}", util::time::GetTimeStamp(), log_file_path.stem().string(), log_file_path.extension().string());
73-
std::filesystem::rename(log_file_path, log_file_path_tmp);
74-
SaveLogFile(log_file_path_tmp);
71+
try {
72+
if (std::filesystem::exists(log_file_path)) {
73+
const auto log_file_path_tmp = log_file_path.parent_path() / fmt::format("{}_{}_hard_crash{}", util::time::GetTimeStamp(), log_file_path.stem().string(), log_file_path.extension().string());
74+
std::filesystem::rename(log_file_path, log_file_path_tmp);
75+
SaveLogFile(log_file_path_tmp);
76+
}
77+
} catch (const std::filesystem::filesystem_error& e) {
78+
MessageBoxA(nullptr, fmt::format("{}\n{}\n{}", e.what(), e.path1().string(), e.path2().string()).c_str(), "exception while moving log file", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
79+
} catch (...) {
80+
MessageBoxA(nullptr, xorstr_("An unknown error occurred while moving the log file."), xorstr_("Critical logging error"), MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
7581
}
7682
}
7783

injector/src/main.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#include "window.hpp"
1313
#include <SDL3/SDL_timer.h>
1414

15-
std::atomic<bool> kRUNNING = true;
15+
std::atomic_bool kRUNNING = true;
16+
std::atomic_bool kGAME_RUNNING = false;
1617

1718
namespace {
1819
auto dll_path = std::make_unique<char[]>(MAX_PATH);
@@ -36,19 +37,27 @@ void AtExit() {
3637
base::common::logging::Manager::Shutdown();
3738
}
3839

39-
void DoFrame(const base::injector::Window& window) {
40-
const HWND game_wnd = base::win32::GetHwnd(kSETTINGS.target_window_class, kSETTINGS.target_window_name).value_or(nullptr);
40+
void GameRunningChecker() {
41+
while (kRUNNING) {
42+
const HWND game_wnd = base::win32::GetHwnd(kSETTINGS.target_window_class, kSETTINGS.target_window_name).value_or(nullptr);
43+
kGAME_RUNNING = !base::win32::GetPIDFromHWND(game_wnd).has_error();
44+
45+
std::this_thread::sleep_for(std::chrono::seconds(1));
46+
}
47+
}
4148

49+
void DoFrame(const base::injector::Window& window) {
4250
window.PreFrame();
4351

4452
{
4553
if (ImGui::Begin("Injector window", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize)) {
4654
if (ImGui::BeginTabBar("tabs")) {
4755
if (ImGui::BeginTabItem("Injector")) {
48-
ImGui::Text(!game_wnd ? "Game not running" : fmt::format("Game running: {}", base::win32::GetPIDFromHWND(game_wnd).value_or(0)).c_str());
49-
if (game_wnd) {
56+
ImGui::Text(!kGAME_RUNNING ? "Game not running" : "Game running");
57+
if (kGAME_RUNNING) {
5058
if (ImGui::Button("Inject")) {
51-
auto _ = std::async(std::launch::async, [game_wnd]() {
59+
auto _ = std::async(std::launch::async, []() {
60+
const HWND game_wnd = base::win32::GetHwnd(kSETTINGS.target_window_class, kSETTINGS.target_window_name).value_or(nullptr);
5261
if (const auto res = base::injector::Inject(base::win32::GetPIDFromHWND(game_wnd).value_or(0), kSETTINGS.dll_path); res.error()) {
5362
LOG_ERROR("Failed to inject: {}", res.error());
5463
MessageBoxA(nullptr, res.error().GetResultMessage().c_str(), "Error", MB_OK | MB_ICONERROR);
@@ -120,6 +129,8 @@ std::int32_t APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
120129

121130
const Window window(kWINDOW_WIDTH, kWINDOW_HEIGHT);
122131

132+
std::thread game_running_check_thread(GameRunningChecker);
133+
123134
// init to initial values
124135
strncpy_s(dll_path.get(), MAX_PATH, kSETTINGS.dll_path.c_str(), _TRUNCATE);
125136
strncpy_s(target_window_name.get(), 255, kSETTINGS.target_window_name.c_str(), _TRUNCATE);
@@ -143,6 +154,12 @@ std::int32_t APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
143154
prev_tick = cur_tick;
144155
DoFrame(window);
145156
}
157+
158+
std::this_thread::yield();
159+
}
160+
161+
if (game_running_check_thread.joinable()) {
162+
game_running_check_thread.join();
146163
}
147164

148165
return 0;

0 commit comments

Comments
 (0)