diff --git a/changelog.md b/changelog.md index ed5e30a..6dccf27 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +# v1.1.22 + +* Potentially improve performance + # v1.1.21 * Fix an exploit that could allow you to double jump diff --git a/src/includes.hpp b/src/includes.hpp index 0e7c7ad..11147a1 100644 --- a/src/includes.hpp +++ b/src/includes.hpp @@ -1,6 +1,9 @@ #pragma once +#include #include +#include + #include using namespace geode::prelude; @@ -39,13 +42,13 @@ struct Step { extern std::queue inputQueue; -extern std::unordered_set inputBinds[6]; +extern std::array, 6> inputBinds; extern std::unordered_set heldInputs; -extern CRITICAL_SECTION inputQueueLock; -extern CRITICAL_SECTION keybindsLock; +extern std::mutex inputQueueLock; +extern std::mutex keybindsLock; -extern bool enableRightClick; +extern std::atomic enableRightClick; extern bool threadPriority; void inputThread(); \ No newline at end of file diff --git a/src/input.cpp b/src/input.cpp index 3517c8c..1369a42 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -2,13 +2,13 @@ std::queue inputQueue; -std::unordered_set inputBinds[6]; +std::array, 6> inputBinds; std::unordered_set heldInputs; -CRITICAL_SECTION inputQueueLock; -CRITICAL_SECTION keybindsLock; +std::mutex inputQueueLock; +std::mutex keybindsLock; -bool enableRightClick; +std::atomic enableRightClick; bool threadPriority; LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { @@ -50,40 +50,40 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) bool shouldEmplace = true; player = Player1; - EnterCriticalSection(&keybindsLock); + std::array, 6> binds; + { + std::lock_guard lock(keybindsLock); + binds = inputBinds; + } - if (inputBinds[p1Jump].contains(vkey)) inputType = PlayerButton::Jump; - else if (inputBinds[p1Left].contains(vkey)) inputType = PlayerButton::Left; - else if (inputBinds[p1Right].contains(vkey)) inputType = PlayerButton::Right; + if (binds[p1Jump].contains(vkey)) inputType = PlayerButton::Jump; + else if (binds[p1Left].contains(vkey)) inputType = PlayerButton::Left; + else if (binds[p1Right].contains(vkey)) inputType = PlayerButton::Right; else { player = Player2; - if (inputBinds[p2Jump].contains(vkey)) inputType = PlayerButton::Jump; - else if (inputBinds[p2Left].contains(vkey)) inputType = PlayerButton::Left; - else if (inputBinds[p2Right].contains(vkey)) inputType = PlayerButton::Right; + if (binds[p2Jump].contains(vkey)) inputType = PlayerButton::Jump; + else if (binds[p2Left].contains(vkey)) inputType = PlayerButton::Left; + else if (binds[p2Right].contains(vkey)) inputType = PlayerButton::Right; else shouldEmplace = false; } - if (!inputState) heldInputs.emplace(vkey); - LeaveCriticalSection(&keybindsLock); + if (!inputState) heldInputs.emplace(vkey); + if (!shouldEmplace) return 0; - if (!shouldEmplace) return 0; // has to be done outside of the critical section break; } case RIM_TYPEMOUSE: { USHORT flags = raw->data.mouse.usButtonFlags; bool shouldEmplace = true; + player = Player1; inputType = PlayerButton::Jump; - EnterCriticalSection(&keybindsLock); - bool rc = enableRightClick; - LeaveCriticalSection(&keybindsLock); - if (flags & RI_MOUSE_BUTTON_1_DOWN) inputState = Press; else if (flags & RI_MOUSE_BUTTON_1_UP) inputState = Release; else { player = Player2; - if (!rc) return 0; + if (!enableRightClick.load()) return 0; if (flags & RI_MOUSE_BUTTON_2_DOWN) inputState = Press; else if (flags & RI_MOUSE_BUTTON_2_UP) inputState = Release; else return 0; @@ -99,9 +99,10 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) return DefWindowProcA(hwnd, uMsg, wParam, lParam); } - EnterCriticalSection(&inputQueueLock); - inputQueue.emplace(InputEvent{ time, inputType, inputState, player }); - LeaveCriticalSection(&inputQueueLock); + { + std::lock_guard lock(inputQueueLock); + inputQueue.emplace(InputEvent{ time, inputType, inputState, player }); + } return 0; } diff --git a/src/main.cpp b/src/main.cpp index aa727f5..c86e43a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,5 @@ #include "includes.hpp" -#include -#include #include #include @@ -48,31 +46,31 @@ void updateInputQueueAndTime(int stepCount) { else { nextInput = emptyInput; lastFrameTime = lastPhysicsFrameTime; - std::queue().swap(stepQueue); // just in case + stepQueue = {}; // just in case - EnterCriticalSection(&inputQueueLock); + { + std::lock_guard lock(inputQueueLock); - if (lateCutoff) { - QueryPerformanceCounter(¤tFrameTime); // done within the critical section to prevent a race condition which could cause dropped inputs - inputQueueCopy = inputQueue; - std::queue().swap(inputQueue); - } - else { - while (!inputQueue.empty() && inputQueue.front().time.QuadPart <= currentFrameTime.QuadPart) { - inputQueueCopy.push(inputQueue.front()); - inputQueue.pop(); + if (lateCutoff) { + QueryPerformanceCounter(¤tFrameTime); + inputQueueCopy = inputQueue; + inputQueue = {}; + } + else { + while (!inputQueue.empty() && inputQueue.front().time.QuadPart <= currentFrameTime.QuadPart) { + inputQueueCopy.push(inputQueue.front()); + inputQueue.pop(); + } } } - LeaveCriticalSection(&inputQueueLock); - lastPhysicsFrameTime = currentFrameTime; if (!firstFrame) skipUpdate = false; else { skipUpdate = true; firstFrame = false; - if (!lateCutoff) std::queue().swap(inputQueueCopy); + if (!lateCutoff) inputQueueCopy = {}; return; } @@ -126,32 +124,33 @@ Step updateDeltaFactorAndInput() { } void updateKeybinds() { + std::array, 6> binds; std::vector> v; - EnterCriticalSection(&keybindsLock); - - enableRightClick = Mod::get()->getSettingValue("right-click"); - inputBinds->clear(); + enableRightClick.store(Mod::get()->getSettingValue("right-click")); v = keybinds::BindManager::get()->getBindsFor("robtop.geometry-dash/jump-p1"); - for (int i = 0; i < v.size(); i++) inputBinds[p1Jump].emplace(v[i]->getHash()); + for (int i = 0; i < v.size(); i++) binds[p1Jump].emplace(v[i]->getHash()); v = keybinds::BindManager::get()->getBindsFor("robtop.geometry-dash/move-left-p1"); - for (int i = 0; i < v.size(); i++) inputBinds[p1Left].emplace(v[i]->getHash()); + for (int i = 0; i < v.size(); i++) binds[p1Left].emplace(v[i]->getHash()); v = keybinds::BindManager::get()->getBindsFor("robtop.geometry-dash/move-right-p1"); - for (int i = 0; i < v.size(); i++) inputBinds[p1Right].emplace(v[i]->getHash()); + for (int i = 0; i < v.size(); i++) binds[p1Right].emplace(v[i]->getHash()); v = keybinds::BindManager::get()->getBindsFor("robtop.geometry-dash/jump-p2"); - for (int i = 0; i < v.size(); i++) inputBinds[p2Jump].emplace(v[i]->getHash()); + for (int i = 0; i < v.size(); i++) binds[p2Jump].emplace(v[i]->getHash()); v = keybinds::BindManager::get()->getBindsFor("robtop.geometry-dash/move-left-p2"); - for (int i = 0; i < v.size(); i++) inputBinds[p2Left].emplace(v[i]->getHash()); + for (int i = 0; i < v.size(); i++) binds[p2Left].emplace(v[i]->getHash()); v = keybinds::BindManager::get()->getBindsFor("robtop.geometry-dash/move-right-p2"); - for (int i = 0; i < v.size(); i++) inputBinds[p2Right].emplace(v[i]->getHash()); + for (int i = 0; i < v.size(); i++) binds[p2Right].emplace(v[i]->getHash()); - LeaveCriticalSection(&keybindsLock); + { + std::lock_guard lock(keybindsLock); + inputBinds = binds; + } } void newResetCollisionLog(PlayerObject* p) { // inlined in 2.206... @@ -188,11 +187,12 @@ class $modify(CCDirector) { skipUpdate = true; enableInput = true; - std::queue().swap(inputQueueCopy); + inputQueueCopy = {}; - EnterCriticalSection(&inputQueueLock); - std::queue().swap(inputQueue); - LeaveCriticalSection(&inputQueueLock); + { + std::lock_guard lock(inputQueueLock); + inputQueue = {}; + } } CCDirector::setDeltaTime(dTime); @@ -377,16 +377,6 @@ void toggleMod(bool disable) { } $on_mod(Loaded) { - if (!InitializeCriticalSectionAndSpinCount(&inputQueueLock, 0x00040000)) { - log::error("Failed to initialize input queue lock"); - return; - } - - if (!InitializeCriticalSectionAndSpinCount(&keybindsLock, 0x00040000)) { - log::error("Failed to initialize keybind lock"); - return; - } - toggleMod(Mod::get()->getSettingValue("soft-toggle")); listenForSettingChanges("soft-toggle", toggleMod);