From 10ff2e78590e7c0f8ca2b99e630be4e974852c21 Mon Sep 17 00:00:00 2001 From: linuxversion <1660477+SomeCrazyGuy@users.noreply.github.com> Date: Thu, 13 Jun 2024 21:46:51 -0400 Subject: [PATCH] Add back dx12 rendere from v1.2, change hotkeys to use rawinput --- src/dx12ui.cpp | 22 +++++++++++--- src/dx12ui.h | 5 ++++ src/hotkeys.cpp | 19 +++++++----- src/hotkeys.h | 2 +- src/main.cpp | 80 +++++++++++++++++++++++++++++++++---------------- 5 files changed, 91 insertions(+), 37 deletions(-) create mode 100644 src/dx12ui.h diff --git a/src/dx12ui.cpp b/src/dx12ui.cpp index b9c6773..afe9e2b 100644 --- a/src/dx12ui.cpp +++ b/src/dx12ui.cpp @@ -1,5 +1,7 @@ #include "main.h" -#include "..\imgui\imgui_impl_dx12.h" +#include "../imgui/imgui_impl_dx12.h" +#include "../imgui/imgui_impl_win32.h" +#include "gui.h" #include #include @@ -23,9 +25,10 @@ struct RenderState { RenderState state; -static void SetupRenderState(void* dx12_swapchain, void* dx12_commandqueue) { +extern void DX12_Initialize(void* dx12_swapchain, void* dx12_commandqueue) { const auto Swapchain = (IDXGISwapChain*)dx12_swapchain; - const auto dx12queue = (ID3D12CommandQueue*)dx12_commandqueue; + + state.commandQueue = (ID3D12CommandQueue*)dx12_commandqueue; Swapchain->QueryInterface(IID_PPV_ARGS(&state.swapchain)); Swapchain->GetDevice(IID_PPV_ARGS(&state.device)); @@ -84,7 +87,14 @@ static void SetupRenderState(void* dx12_swapchain, void* dx12_commandqueue) { } -void render() { +extern void DX12_Render() { + ImGui_ImplDX12_NewFrame(); + ImGui_ImplWin32_NewFrame(); + ImGui::NewFrame(); + + draw_gui(); + + ImGui::Render(); FrameContext& currentFrameContext = state.frameContext[state.swapchain->GetCurrentBackBufferIndex()]; currentFrameContext.commandAllocator->Reset(); @@ -110,4 +120,8 @@ void render() { state.commandList->Close(); state.commandQueue->ExecuteCommandLists(1, reinterpret_cast(&state.commandList)); +} + +void DX12_Release() { + //lol } \ No newline at end of file diff --git a/src/dx12ui.h b/src/dx12ui.h new file mode 100644 index 0000000..dee75e5 --- /dev/null +++ b/src/dx12ui.h @@ -0,0 +1,5 @@ +#pragma once + +extern void DX12_Initialize(void* dx12_swapchain, void* dx12_commandqueue); +extern void DX12_Render(); +void DX12_Release(); \ No newline at end of file diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index 693365f..859262b 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -113,10 +113,12 @@ static void rebuild_hotkey_cache() { // wndproc calls this for every WM_KEYDOWN message -extern void HotkeyReceiveKeypress(unsigned vk_key) { - const bool control = GetAsyncKeyState(VK_CONTROL); - const bool alt = GetAsyncKeyState(VK_MENU); - const bool shift = GetAsyncKeyState(VK_SHIFT); +extern bool HotkeyReceiveKeypress(unsigned vk_key) { + if(vk_key == VK_SHIFT || vk_key == VK_CONTROL || vk_key == VK_MENU) return false; + + const bool control = GetAsyncKeyState(VK_CONTROL) < 0; + const bool alt = GetAsyncKeyState(VK_MENU) < 0; + const bool shift = GetAsyncKeyState(VK_SHIFT) < 0; const auto key = make_hotkey(vk_key, control, alt, shift); // first check if we are waiting for a keypress to set a hotkey @@ -125,7 +127,7 @@ extern void HotkeyReceiveKeypress(unsigned vk_key) { AllHotkeys[index_waiting_for_input].set_key = key; ActiveHotkeys[key] = index_waiting_for_input; index_waiting_for_input = UINT32_MAX; - return; + return true; } //check to see if the cache needs to be rebuilt @@ -140,7 +142,10 @@ extern void HotkeyReceiveKeypress(unsigned vk_key) { DEBUG("Activating hotkey: %s", hotkey->name); const auto callback = CallbackGetCallback(CALLBACKTYPE_HOTKEY, hotkey->owner); callback.hotkey_callback(hotkey->userdata); + return true; } + + return false; } // just for use in gui.cpp, it would be too cumbersome to try to split the hotkey code from the hotkey UI @@ -184,7 +189,7 @@ extern void draw_hotkeys_tab() { table_headers[TC_HOTKEY_NAME] = "Hotkey Name"; table_headers[TC_HOTKEY_COMBO] = "Hotkey Combo"; - static const auto table_cell_renderer = [](void* table_userdata, int cur_row, int cur_col) { + static const auto table_cell_renderer = [](uintptr_t table_userdata, int cur_row, int cur_col) { ASSERT(cur_col < TC_COUNT); const auto base_index = (unsigned)table_userdata; const auto hotkey_index = base_index + cur_row; @@ -218,7 +223,7 @@ extern void draw_hotkeys_tab() { break; } }; - SimpleDraw->Table(table_headers, TC_COUNT, (void*)infos_index, index_count, table_cell_renderer); + SimpleDraw->Table(table_headers, TC_COUNT, infos_index, index_count, table_cell_renderer); SimpleDraw->HBoxEnd(); } diff --git a/src/hotkeys.h b/src/hotkeys.h index d870fcc..a4aa5d4 100644 --- a/src/hotkeys.h +++ b/src/hotkeys.h @@ -5,6 +5,6 @@ // then update the settings and gui api to allow // creating mod menu tabs extern void draw_hotkeys_tab(); -extern void HotkeyReceiveKeypress(unsigned vk_key); +extern bool HotkeyReceiveKeypress(unsigned vk_key); // true if hotkey was activated extern void HotkeySaveSettings(); // call when normal settings are saved extern void HotkeyRequestNewHotkey(RegistrationHandle owner, const char* name, uintptr_t userdata, unsigned forced_hotkey); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 33879fd..0cc97a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,7 @@ #include "hotkeys.h" #include "d3d11on12ui.h" +#include "dx12ui.h" #define BETTERAPI_IMPLEMENTATION #include "../betterapi.h" @@ -373,21 +374,27 @@ static void Callback_Config(ConfigAction action) { auto c = GetConfigAPI(); auto s = GetSettingsMutable(); c->ConfigU32(action, "FontScaleOverride", &s->FontScaleOverride); - if (action == ConfigAction_Write) { - HotkeySaveSettings(); - } if (action != ConfigAction_Edit) { c->ConfigU32(action, "CreateSwapChainForHwndSoft", &s->CreateSwapChainForHwndSoft); c->ConfigU32(action, "SwapchainPresentHard", &s->SwapchainPresentHard); + c->ConfigU32(action, "UseOldRenderer", &s->UseOldRenderer); } else { auto UI = API.SimpleDraw; - UI->Text("Renderer quirks, you must restart game to apply"); + UI->Text("BetterConsole Renderer:"); + UI->Checkbox("Use old v1.2 renderer", (bool*)&s->UseOldRenderer); + UI->Text("Renderer quirks:"); UI->Separator(); UI->Text("CreateSwapChainForHwnd hook mode:"); UI->Checkbox("Use soft hook", (bool*)&s->CreateSwapChainForHwndSoft); UI->Text("SwapchainPresent hook mode:"); UI->Checkbox("Use hard hook", (bool*)&s->SwapchainPresentHard); + UI->Text("You must save the settings and restart the game to apply"); + } + + //do this last until i have this working with the official api + if (action == ConfigAction_Write) { + HotkeySaveSettings(); } } @@ -407,12 +414,26 @@ static void SetupModMenu() { static UINT(*OLD_GetRawInputData)(HRAWINPUT hri, UINT cmd, LPVOID data, PUINT data_size, UINT hsize) = nullptr; static decltype(OLD_GetRawInputData) FAKE_GetRawInputData = [](HRAWINPUT hri, UINT cmd, LPVOID data, PUINT data_size, UINT hsize) -> UINT { auto ret = OLD_GetRawInputData(hri, cmd, data, data_size, hsize); + if (data == NULL) return ret; + + if (cmd == RID_INPUT) { + auto input = (RAWINPUT*)data; + + if (input->header.dwType == RIM_TYPEKEYBOARD) { + auto keydata = input->data.keyboard; + if (keydata.Message == WM_KEYDOWN || keydata.Message == WM_SYSKEYDOWN) { + if (HotkeyReceiveKeypress(keydata.VKey)) { + goto HIDE_INPUT_FROM_GAME; + } + } + } - if ((should_show_ui == false) || (data == NULL)) return ret; + if (should_show_ui == false) return ret; - //hide input from the game when shouldshowui is true and data is not null - auto input = (RAWINPUT*)data; - input->header.dwType = RIM_TYPEHID; //game ignores typehid messages + HIDE_INPUT_FROM_GAME: + //hide input from the game when shouldshowui is true and data is not null + input->header.dwType = RIM_TYPEHID; //game ignores typehid messages + } return ret; }; OLD_GetRawInputData = (decltype(OLD_GetRawInputData)) API.Hook->HookFunctionIAT("user32.dll", "GetRawInputData", (FUNC_PTR)FAKE_GetRawInputData); @@ -498,7 +519,7 @@ static HRESULT FAKE_Present(IDXGISwapChain3* This, UINT SyncInterval, UINT Prese static bool hooked = false; static IDXGISwapChain3* last_swapchain = nullptr; - static ID3D12CommandQueue* comand_queue = nullptr; + static ID3D12CommandQueue* command_queue = nullptr; if (last_swapchain != This) { last_swapchain = This; @@ -506,23 +527,40 @@ static HRESULT FAKE_Present(IDXGISwapChain3* This, UINT SyncInterval, UINT Prese for (auto i : Queues) { DEBUG("Searching for CommandQueue for swapchain %p: [ chain: %p, queue: %p, age: %u ]", This, i.SwapChain, i.Queue, i.Age); if (i.SwapChain == This) { - comand_queue = i.Queue; + command_queue = i.Queue; DEBUG("Commandqueue found!"); break; } } } - if (should_show_ui) { - if (!hooked) { - UI_Initialize(This, comand_queue); - hooked = true; + static bool oldrenderer = GetSettings()->UseOldRenderer; + + if (oldrenderer) { + if (should_show_ui) { + if (!hooked) { + DX12_Initialize(This, command_queue); + hooked = true; + } + DX12_Render(); + } + else { + DX12_Release(); + hooked = false; } - UI_Render(); } else { - UI_Release(); - hooked = false; + if (should_show_ui) { + if (!hooked) { + DX11_Initialize(This, command_queue); + hooked = true; + } + DX11_Render(); + } + else { + DX11_Release(); + hooked = false; + } } // keep this detection code in place to detect breaking changes to the hook causing recursive nightmare @@ -555,14 +593,6 @@ static LRESULT FAKE_Wndproc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) ImGui_ImplWin32_Init(hWnd); } - static bool shift = false; - static bool ctrl = false; - static bool alt = false; - - if (uMsg == WM_KEYDOWN) { - HotkeyReceiveKeypress((unsigned)(wParam & 0xFF)); - } - if (should_show_ui) { ClipCursor(NULL); ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam);