Skip to content

Commit

Permalink
Add back dx12 rendere from v1.2, change hotkeys to use rawinput
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeCrazyGuy committed Jun 14, 2024
1 parent fc97d17 commit 10ff2e7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 37 deletions.
22 changes: 18 additions & 4 deletions src/dx12ui.cpp
Original file line number Diff line number Diff line change
@@ -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 <dxgi1_6.h>
#include <d3d12.h>

Expand All @@ -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));
Expand Down Expand Up @@ -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();

Expand All @@ -110,4 +120,8 @@ void render() {
state.commandList->Close();

state.commandQueue->ExecuteCommandLists(1, reinterpret_cast<ID3D12CommandList* const*>(&state.commandList));
}

void DX12_Release() {
//lol
}
5 changes: 5 additions & 0 deletions src/dx12ui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

extern void DX12_Initialize(void* dx12_swapchain, void* dx12_commandqueue);
extern void DX12_Render();
void DX12_Release();
19 changes: 12 additions & 7 deletions src/hotkeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/hotkeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
80 changes: 55 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "hotkeys.h"

#include "d3d11on12ui.h"
#include "dx12ui.h"

#define BETTERAPI_IMPLEMENTATION
#include "../betterapi.h"
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -498,31 +519,48 @@ 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;

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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 10ff2e7

Please sign in to comment.