From 5f84803ac6ff7ba13d4d662c8c627b9f83287c8c Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Thu, 20 Oct 2022 12:29:21 +0200 Subject: [PATCH] ImGui and DirectX code improvements * Fix history duplication by removing trailing white space characters from submitted commands in console. * Fix out of range exception caused by caching svConVar.size() in CConsole::BuildSummary while we are modifying it. * Fixed memory leak caused by extraneous Strdup calls in CConsole. * Renamed variables and structure members, static vars in id3dx.cpp are not prefixed with s_, IBrowser_Config is now m_BrowserConfig. * Performed code cleanup in id3dx.cpp. --- r5dev/gameui/IBrowser.cpp | 11 +- r5dev/gameui/IConsole.cpp | 59 +++--- r5dev/gameui/IConsole.h | 4 +- r5dev/public/utility/utility.cpp | 23 +++ r5dev/public/utility/utility.h | 4 + .../thirdparty/imgui/include/imgui_utility.h | 4 +- r5dev/thirdparty/imgui/src/imgui_utility.cpp | 66 +++--- r5dev/vgui/vgui_debugpanel.cpp | 2 +- r5dev/windows/id3dx.cpp | 188 +++++++++--------- r5dev/windows/id3dx.h | 4 +- 10 files changed, 197 insertions(+), 168 deletions(-) diff --git a/r5dev/gameui/IBrowser.cpp b/r5dev/gameui/IBrowser.cpp index 5b581e11..7b8cfb43 100644 --- a/r5dev/gameui/IBrowser.cpp +++ b/r5dev/gameui/IBrowser.cpp @@ -71,17 +71,18 @@ bool CBrowser::Init(void) //----------------------------------------------------------------------------- void CBrowser::RunFrame(void) { + // Uncomment these when adjusting the theme or layout. + { + //ImGui::ShowStyleEditor(); + //ImGui::ShowDemoWindow(); + } + if (!m_bInitialized) { Init(); m_bInitialized = true; } - { - //ImGui::ShowStyleEditor(); - //ImGui::ShowDemoWindow(); - } - int nVars = 0; if (m_Style == ImGuiStyle_t::MODERN) { diff --git a/r5dev/gameui/IConsole.cpp b/r5dev/gameui/IConsole.cpp index c8a44a51..4d4926ec 100644 --- a/r5dev/gameui/IConsole.cpp +++ b/r5dev/gameui/IConsole.cpp @@ -96,12 +96,7 @@ bool CConsole::Init(void) //----------------------------------------------------------------------------- void CConsole::RunFrame(void) { - if (!m_bInitialized) - { - Init(); - m_bInitialized = true; - } - + // Uncomment these when adjusting the theme or layout. { //ImGui::ShowStyleEditor(); //ImGui::ShowDemoWindow(); @@ -111,11 +106,17 @@ void CConsole::RunFrame(void) * BASE PANEL SETUP * **************************/ { - int nVars = 0; if (!m_bActivate) { return; } + if (!m_bInitialized) + { + Init(); + m_bInitialized = true; + } + + int nVars = 0; if (m_Style == ImGuiStyle_t::MODERN) { ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 8.f, 10.f }); nVars++; @@ -338,7 +339,7 @@ void CConsole::OptionsPanel(void) ImGui::Text("Console Hotkey:"); ImGui::SameLine(); - if (ImGui::Hotkey("##ToggleConsole", &g_pImGuiConfig->IConsole_Config.m_nBind0, ImVec2(80, 80))) + if (ImGui::Hotkey("##ToggleConsole", &g_pImGuiConfig->m_ConsoleConfig.m_nBind0, ImVec2(80, 80))) { g_pImGuiConfig->Save(); } @@ -346,7 +347,7 @@ void CConsole::OptionsPanel(void) ImGui::Text("Browser Hotkey:"); ImGui::SameLine(); - if (ImGui::Hotkey("##ToggleBrowser", &g_pImGuiConfig->IBrowser_Config.m_nBind0, ImVec2(80, 80))) + if (ImGui::Hotkey("##ToggleBrowser", &g_pImGuiConfig->m_BrowserConfig.m_nBind0, ImVec2(80, 80))) { g_pImGuiConfig->Save(); } @@ -369,7 +370,7 @@ void CConsole::SuggestPanel(void) if (con_suggestion_showflags->GetBool()) { - const int k = ColorCodeFlags(m_vSuggest[i].m_nFlags); + const int k = GetFlagColorIndex(m_vSuggest[i].m_nFlags); ImGui::Image(m_vFlagIcons[k].m_idIcon, ImVec2(m_vFlagIcons[k].m_nWidth, m_vFlagIcons[k].m_nHeight)); ImGui::SameLine(); } @@ -447,7 +448,7 @@ bool CConsole::AutoComplete(void) // Don't suggest if user tries to assign value to ConVar or execute ConCommand. if (strstr(m_szInputBuf, " ") || strstr(m_szInputBuf, ";")) { - // !TODO: Add completion logic here. + // !TODO: Add IConVar completion logic here. m_bCanAutoComplete = false; m_bSuggestActive = false; m_nSuggestPos = -1; @@ -482,6 +483,7 @@ void CConsole::ClearAutoComplete(void) //----------------------------------------------------------------------------- // Purpose: find ConVars/ConCommands from user input and add to vector +// - Ignores ConVars marked FCVAR_HIDDEN //----------------------------------------------------------------------------- void CConsole::FindFromPartial(void) { @@ -557,24 +559,26 @@ void CConsole::FindFromPartial(void) //----------------------------------------------------------------------------- // Purpose: processes submitted commands for the main thread -// Input : pszCommand - +// Input : svCommand - //----------------------------------------------------------------------------- -void CConsole::ProcessCommand(const char* pszCommand) +void CConsole::ProcessCommand(string svCommand) { - AddLog(ImVec4(1.00f, 0.80f, 0.60f, 1.00f), "] %s\n", pszCommand); - Cbuf_AddText(Cbuf_GetCurrentPlayer(), pszCommand, cmd_source_t::kCommandSrcCode); + StringRTrim(svCommand, " "); // Remove trailing white space characters to prevent history duplication. + AddLog(ImVec4(1.00f, 0.80f, 0.60f, 1.00f), "%s] %s\n", Plat_GetProcessUpTime(), svCommand.c_str()); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), svCommand.c_str(), cmd_source_t::kCommandSrcCode); m_nHistoryPos = -1; + for (size_t i = m_vHistory.size(); i-- > 0;) { - if (m_vHistory[i].compare(pszCommand) == 0) + if (m_vHistory[i].compare(svCommand) == 0) { m_vHistory.erase(m_vHistory.begin() + i); break; } } - m_vHistory.push_back(Strdup(pszCommand)); + m_vHistory.push_back(string(svCommand)); m_Logger.m_bScrollToBottom = true; } @@ -586,7 +590,7 @@ void CConsole::BuildSummary(string svConVar) { if (!svConVar.empty()) { - for (size_t i = 0, s = svConVar.size(); i < s; i++) + for (size_t i = 0; i < svConVar.size(); i++) { const char c = svConVar[i]; if (c == ' ' || c == ';') @@ -599,19 +603,12 @@ void CConsole::BuildSummary(string svConVar) { // Display the current and default value of ConVar if found. snprintf(m_szSummary, sizeof(m_szSummary), "(\"%s\", default \"%s\")", pConVar->GetString(), pConVar->GetDefault()); - } - else - { - // Display amount of history items if ConVar cannot be found. - ClampHistorySize(); - snprintf(m_szSummary, sizeof(m_szSummary), "%zu history items", m_vHistory.size()); + return; } } - else // Default or empty param. - { - ClampHistorySize(); - snprintf(m_szSummary, sizeof(m_szSummary), "%zu history items", m_vHistory.size()); - } + // Display amount of history items if ConVar cannot be found or input is empty. + ClampHistorySize(); + snprintf(m_szSummary, sizeof(m_szSummary), "%zu history items", m_vHistory.size()); } //----------------------------------------------------------------------------- @@ -695,7 +692,7 @@ bool CConsole::LoadFlagIcons(void) // Purpose: returns flag image index for CommandBase (must be aligned with resource.h!) // Input : nFlags - //----------------------------------------------------------------------------- -int CConsole::ColorCodeFlags(int nFlags) const +int CConsole::GetFlagColorIndex(int nFlags) const { switch (nFlags) { @@ -918,7 +915,7 @@ void CConsole::AddLog(const ImVec4& color, const char* fmt, ...) IM_FMTARGS(2) buf[IM_ARRAYSIZE(buf) - 1] = 0; va_end(args); - m_Logger.InsertText(ConLog_t(Strdup(buf), color)); + m_Logger.InsertText(ConLog_t(buf, color)); } //----------------------------------------------------------------------------- diff --git a/r5dev/gameui/IConsole.h b/r5dev/gameui/IConsole.h index db1931dd..43dbf9bd 100644 --- a/r5dev/gameui/IConsole.h +++ b/r5dev/gameui/IConsole.h @@ -30,7 +30,7 @@ private: void ClearAutoComplete(void); void FindFromPartial(void); - void ProcessCommand(const char* pszCommand); + void ProcessCommand(string svCommand); void BuildSummary(string svConVar = ""); void BuildSuggestPanelRect(void); @@ -39,7 +39,7 @@ private: void ClampHistorySize(void); bool LoadFlagIcons(void); - int ColorCodeFlags(int nFlags) const; + int GetFlagColorIndex(int nFlags) const; int TextEditCallback(ImGuiInputTextCallbackData* pData); static int TextEditCallbackStub(ImGuiInputTextCallbackData* pData); diff --git a/r5dev/public/utility/utility.cpp b/r5dev/public/utility/utility.cpp index 6bfdde44..aa264200 100644 --- a/r5dev/public/utility/utility.cpp +++ b/r5dev/public/utility/utility.cpp @@ -683,6 +683,29 @@ vector StringSplit(string svInput, char cDelim, size_t nMax) return vSubStrings; } +/////////////////////////////////////////////////////////////////////////////// +// For trimming leading characters from input. +string& StringLTrim(string& svInput, const char* pszToTrim) +{ + svInput.erase(0, svInput.find_first_not_of(pszToTrim)); + return svInput; +} + +/////////////////////////////////////////////////////////////////////////////// +// For trimming trailing characters from input. +string& StringRTrim(string& svInput, const char* pszToTrim) +{ + svInput.erase(svInput.find_last_not_of(pszToTrim) + 1); + return svInput; +} + +/////////////////////////////////////////////////////////////////////////////// +// For trimming leading and trailing characters from input. +string& StringTrim(string& svInput, const char* pszToTrim) +{ + return StringLTrim(StringRTrim(svInput, pszToTrim), pszToTrim); +} + /////////////////////////////////////////////////////////////////////////////// // For converting a string to an array of bytes. vector StringToBytes(const string& svInput, bool bNullTerminator) diff --git a/r5dev/public/utility/utility.h b/r5dev/public/utility/utility.h index 24edebcd..3ad1644b 100644 --- a/r5dev/public/utility/utility.h +++ b/r5dev/public/utility/utility.h @@ -51,6 +51,10 @@ string StringUnescape(const string& svInput); size_t StringCount(const string& svInput, char cDelim); vector StringSplit(string svInput, char cDelim, size_t nMax = SIZE_MAX); +string& StringLTrim(string& svInput, const char* pszToTrim); +string& StringRTrim(string& svInput, const char* pszToTrim); +string& StringTrim(string& svInput, const char* pszToTrim); + ///////////////////////////////////////////////////////////////////////////// // Bytes vector StringToBytes(const string& svInput, bool bNullTerminator); diff --git a/r5dev/thirdparty/imgui/include/imgui_utility.h b/r5dev/thirdparty/imgui/include/imgui_utility.h index 11305036..5f6f9aa7 100644 --- a/r5dev/thirdparty/imgui/include/imgui_utility.h +++ b/r5dev/thirdparty/imgui/include/imgui_utility.h @@ -22,13 +22,13 @@ public: { int m_nBind0 = VK_OEM_3; int m_nBind1 = VK_INSERT; - } IConsole_Config; + } m_ConsoleConfig; struct { int m_nBind0 = VK_HOME; int m_nBind1 = VK_F10; - } IBrowser_Config; + } m_BrowserConfig; void Load(); void Save(); diff --git a/r5dev/thirdparty/imgui/src/imgui_utility.cpp b/r5dev/thirdparty/imgui/src/imgui_utility.cpp index a474a865..8a909677 100644 --- a/r5dev/thirdparty/imgui/src/imgui_utility.cpp +++ b/r5dev/thirdparty/imgui/src/imgui_utility.cpp @@ -37,45 +37,47 @@ char* Strdup(const char* s) void Strtrim(char* s) { char* str_end = s + strlen(s); - while (str_end > s && str_end[-1] == ' ') + { str_end--; *str_end = 0; + } } void ImGuiConfig::Load() { - fs::path fsPath = "platform\\imgui.json"; + static const fs::path fsPath = "platform\\imgui.json"; DevMsg(eDLL_T::MS, "Loading ImGui config file '%s'\n", fsPath.relative_path().u8string().c_str()); - if (fs::exists(fsPath)) + if (!fs::exists(fsPath)) { - try + return; + } + + try + { + nlohmann::json jsIn; + std::ifstream configFile(fsPath, std::ios::binary); // Parse config file. + + configFile >> jsIn; + configFile.close(); + + if (jsIn.is_null() || jsIn["config"].is_null()) { - nlohmann::json jsIn; - std::ifstream configFile(fsPath, std::ios::binary); // Parse config file. - - configFile >> jsIn; - configFile.close(); - - if (!jsIn.is_null()) - { - if (!jsIn["config"].is_null()) - { - // IConsole - IConsole_Config.m_nBind0 = jsIn["config"]["GameConsole"]["bind0"].get(); - IConsole_Config.m_nBind1 = jsIn["config"]["GameConsole"]["bind1"].get(); - - // IBrowser - IBrowser_Config.m_nBind0 = jsIn["config"]["GameBrowser"]["bind0"].get(); - IBrowser_Config.m_nBind1 = jsIn["config"]["GameBrowser"]["bind1"].get(); - } - } - } - catch (const std::exception& ex) - { - Warning(eDLL_T::MS, "Exception while parsing ImGui config file:\n%s\n", ex.what()); - return; + return; // Invalid or no config. } + + // IConsole + m_ConsoleConfig.m_nBind0 = jsIn["config"]["GameConsole"]["bind0"].get(); + m_ConsoleConfig.m_nBind1 = jsIn["config"]["GameConsole"]["bind1"].get(); + + // IBrowser + m_BrowserConfig.m_nBind0 = jsIn["config"]["GameBrowser"]["bind0"].get(); + m_BrowserConfig.m_nBind1 = jsIn["config"]["GameBrowser"]["bind1"].get(); + } + catch (const std::exception& ex) + { + Warning(eDLL_T::MS, "Exception while parsing ImGui config file:\n%s\n", ex.what()); + return; } } @@ -84,12 +86,12 @@ void ImGuiConfig::Save() nlohmann::json jsOut; // IConsole - jsOut["config"]["GameConsole"]["bind0"] = IConsole_Config.m_nBind0; - jsOut["config"]["GameConsole"]["bind1"] = IConsole_Config.m_nBind1; + jsOut["config"]["GameConsole"]["bind0"] = m_ConsoleConfig.m_nBind0; + jsOut["config"]["GameConsole"]["bind1"] = m_ConsoleConfig.m_nBind1; // IBrowser - jsOut["config"]["GameBrowser"]["bind0"] = IBrowser_Config.m_nBind0; - jsOut["config"]["GameBrowser"]["bind1"] = IBrowser_Config.m_nBind1; + jsOut["config"]["GameBrowser"]["bind0"] = m_BrowserConfig.m_nBind0; + jsOut["config"]["GameBrowser"]["bind1"] = m_BrowserConfig.m_nBind1; fs::path fsPath = "platform\\imgui.json"; diff --git a/r5dev/vgui/vgui_debugpanel.cpp b/r5dev/vgui/vgui_debugpanel.cpp index f111b8c7..fddb1291 100644 --- a/r5dev/vgui/vgui_debugpanel.cpp +++ b/r5dev/vgui/vgui_debugpanel.cpp @@ -181,7 +181,7 @@ void CLogSystem::DrawHostStats(void) const if (cl_hoststats_invert_x->GetBool()) { - nWidth = g_nWindowWidth - nWidth; + nWidth = g_nWindowWidth - nWidth; } if (cl_hoststats_invert_y->GetBool()) { diff --git a/r5dev/windows/id3dx.cpp b/r5dev/windows/id3dx.cpp index e5f36395..4c48af10 100644 --- a/r5dev/windows/id3dx.cpp +++ b/r5dev/windows/id3dx.cpp @@ -29,24 +29,24 @@ typedef BOOL(WINAPI* IPostMessageA)(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM l typedef BOOL(WINAPI* IPostMessageW)(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); /////////////////////////////////////////////////////////////////////////////////// -static BOOL g_bInitialized = false; -static BOOL g_bImGuiInitialized = false; +static BOOL s_bInitialized = false; +static BOOL s_bImGuiInitialized = false; /////////////////////////////////////////////////////////////////////////////////// -static WNDPROC g_oWndProc = NULL; -static HWND g_hGameWindow = NULL; +static WNDPROC s_oWndProc = NULL; +static HWND s_hGameWindow = NULL; /////////////////////////////////////////////////////////////////////////////////// -static IPostMessageA g_oPostMessageA = NULL; -static IPostMessageW g_oPostMessageW = NULL; +static IPostMessageA s_oPostMessageA = NULL; +static IPostMessageW s_oPostMessageW = NULL; /////////////////////////////////////////////////////////////////////////////////// -static IDXGIResizeBuffers g_oResizeBuffers = NULL; -static IDXGISwapChainPresent g_fnIDXGISwapChainPresent = NULL; -static IDXGISwapChain* g_pSwapChain = nullptr; -static ID3D11DeviceContext* g_pDeviceContext = nullptr; -static ID3D11Device* g_pDevice = nullptr; -static ID3D11RenderTargetView* g_pRenderTargetView = nullptr; -static ID3D11DepthStencilView* g_pDepthStencilView = nullptr; +static IDXGIResizeBuffers s_oResizeBuffers = NULL; +static IDXGISwapChainPresent s_fnSwapChainPresent = NULL; +static IDXGISwapChain* s_pSwapChain = nullptr; +static ID3D11DeviceContext* s_pDeviceContext = nullptr; +static ID3D11Device* s_pDevice = nullptr; +static ID3D11RenderTargetView* s_pRenderTargetView = nullptr; +static ID3D11DepthStencilView* s_pDepthStencilView = nullptr; //################################################################################# // WINDOW PROCEDURE @@ -63,13 +63,13 @@ LRESULT CALLBACK HwndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) if (uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYDOWN) { - if (wParam == g_pImGuiConfig->IConsole_Config.m_nBind0 || wParam == g_pImGuiConfig->IConsole_Config.m_nBind1) + if (wParam == g_pImGuiConfig->m_ConsoleConfig.m_nBind0 || wParam == g_pImGuiConfig->m_ConsoleConfig.m_nBind1) { g_pConsole->m_bActivate ^= true; ResetInput(); // Disable input to game when console is drawn. } - if (wParam == g_pImGuiConfig->IBrowser_Config.m_nBind0 || wParam == g_pImGuiConfig->IBrowser_Config.m_nBind1) + if (wParam == g_pImGuiConfig->m_BrowserConfig.m_nBind0 || wParam == g_pImGuiConfig->m_BrowserConfig.m_nBind1) { g_pBrowser->m_bActivate ^= true; ResetInput(); // Disable input to game when browser is drawn. @@ -111,7 +111,7 @@ LRESULT CALLBACK HwndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) } /////////////////////////////////////////////////////////////////////////////// - return CallWindowProc(g_oWndProc, hWnd, uMsg, wParam, lParam); + return CallWindowProc(s_oWndProc, hWnd, uMsg, wParam, lParam); } //################################################################################# @@ -125,7 +125,7 @@ BOOL WINAPI HPostMessageA(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) return TRUE; } - return g_oPostMessageA(hWnd, Msg, wParam, lParam); + return s_oPostMessageA(hWnd, Msg, wParam, lParam); } BOOL WINAPI HPostMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) @@ -135,7 +135,7 @@ BOOL WINAPI HPostMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) return TRUE; } - return g_oPostMessageW(hWnd, Msg, wParam, lParam); + return s_oPostMessageW(hWnd, Msg, wParam, lParam); } //################################################################################# @@ -144,10 +144,10 @@ BOOL WINAPI HPostMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) void GetPresent() { - WNDCLASSEXA wc = { sizeof(WNDCLASSEX), CS_CLASSDC, DXGIMsgProc, 0L, 0L, GetModuleHandleA(NULL), NULL, NULL, NULL, NULL, "DX", NULL }; + WNDCLASSEXA wc = { sizeof(WNDCLASSEX), CS_CLASSDC, DXGIMsgProc, 0L, 0L, GetModuleHandleA(NULL), NULL, NULL, NULL, NULL, "GameSDK001", NULL }; RegisterClassExA(&wc); - HWND hWnd = CreateWindowA("DX", NULL, WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL); + HWND hWnd = CreateWindowA("GameSDK001", NULL, WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL); DXGI_SWAP_CHAIN_DESC sd = { 0 }; D3D_FEATURE_LEVEL nFeatureLevelsSet = D3D_FEATURE_LEVEL_11_0; D3D_FEATURE_LEVEL nFeatureLevelsSupported; @@ -171,9 +171,9 @@ void GetPresent() sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; /////////////////////////////////////////////////////////////////////////////// - g_hGameWindow = sd.OutputWindow; + s_hGameWindow = sd.OutputWindow; UINT nFeatureLevelsRequested = 1; - HRESULT hr = 0; + HRESULT hr = NULL; IDXGISwapChain* pSwapChain = nullptr; ID3D11Device* pDevice = nullptr; ID3D11DeviceContext* pContext = nullptr; @@ -201,18 +201,18 @@ void GetPresent() DWORD_PTR* pContextVTable = nullptr; DWORD_PTR* pDeviceVTable = nullptr; - pSwapChainVtable = (DWORD_PTR*)pSwapChain; - pSwapChainVtable = (DWORD_PTR*)pSwapChainVtable[0]; - pContextVTable = (DWORD_PTR*)pContext; - pContextVTable = (DWORD_PTR*)pContextVTable[0]; - pDeviceVTable = (DWORD_PTR*)pDevice; - pDeviceVTable = (DWORD_PTR*)pDeviceVTable[0]; + pSwapChainVtable = reinterpret_cast(pSwapChain); + pSwapChainVtable = reinterpret_cast(pSwapChainVtable[0]); + pContextVTable = reinterpret_cast(pContext); + pContextVTable = reinterpret_cast(pContextVTable[0]); + pDeviceVTable = reinterpret_cast(pDevice); + pDeviceVTable = reinterpret_cast(pDeviceVTable[0]); - int pIDX = (int)DXGISwapChainVTbl::Present; - int rIDX = (int)DXGISwapChainVTbl::ResizeBuffers; + int pIDX = static_cast(DXGISwapChainVTbl::Present); + int rIDX = static_cast(DXGISwapChainVTbl::ResizeBuffers); - g_fnIDXGISwapChainPresent = (IDXGISwapChainPresent)(DWORD_PTR)pSwapChainVtable[pIDX]; - g_oResizeBuffers = (IDXGIResizeBuffers)(DWORD_PTR)pSwapChainVtable[rIDX]; + s_fnSwapChainPresent = reinterpret_cast(pSwapChainVtable[pIDX]); + s_oResizeBuffers = reinterpret_cast(pSwapChainVtable[rIDX]); pSwapChain->Release(); pContext->Release(); @@ -230,15 +230,15 @@ void SetupImGui() /////////////////////////////////////////////////////////////////////////////// IMGUI_CHECKVERSION(); ImGui::CreateContext(); - ImGui_ImplWin32_Init(g_hGameWindow); - ImGui_ImplDX11_Init(g_pDevice, g_pDeviceContext); - ImGui::GetIO().ImeWindowHandle = g_hGameWindow; + ImGui_ImplWin32_Init(s_hGameWindow); + ImGui_ImplDX11_Init(s_pDevice, s_pDeviceContext); + ImGui::GetIO().ImeWindowHandle = s_hGameWindow; /////////////////////////////////////////////////////////////////////////////// - ImGuiIO& io = ImGui::GetIO(); (void)io; + ImGuiIO& io = ImGui::GetIO(); io.ConfigFlags |= ImGuiConfigFlags_IsSRGB; - g_bImGuiInitialized = true; + s_bImGuiInitialized = true; } void DrawImGui() @@ -257,7 +257,7 @@ void DrawImGui() ImGui::EndFrame(); ImGui::Render(); - g_pDeviceContext->OMSetRenderTargets(1, &g_pRenderTargetView, NULL); + s_pDeviceContext->OMSetRenderTargets(1, &s_pRenderTargetView, NULL); ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); } @@ -272,23 +272,26 @@ void CreateRenderTarget(IDXGISwapChain* pSwapChain) pSwapChain->GetDesc(&sd); ZeroMemory(&rd, sizeof(rd)); - g_hGameWindow = sd.OutputWindow; + s_hGameWindow = sd.OutputWindow; rd.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; rd.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; /////////////////////////////////////////////////////////////////////////////// - pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer); - if (pBackBuffer != NULL) { g_pDevice->CreateRenderTargetView(pBackBuffer, &rd, &g_pRenderTargetView); } - g_pDeviceContext->OMSetRenderTargets(1, &g_pRenderTargetView, NULL); + pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast(&pBackBuffer)); + if (pBackBuffer) + { + s_pDevice->CreateRenderTargetView(pBackBuffer, &rd, &s_pRenderTargetView); + } + + s_pDeviceContext->OMSetRenderTargets(1, &s_pRenderTargetView, NULL); pBackBuffer->Release(); } -void CreateViewPort( UINT nWidth, UINT nHeight) +void CreateViewPort(UINT nWidth, UINT nHeight) { - float width = *(float*)(&nWidth); - float height = *(float*)(&nHeight); - - D3D11_VIEWPORT vp{}; + FLOAT width = *reinterpret_cast(&nWidth); + FLOAT height = *reinterpret_cast(&nHeight); + D3D11_VIEWPORT vp; /////////////////////////////////////////////////////////////////////////////// vp.Width = width; @@ -299,16 +302,16 @@ void CreateViewPort( UINT nWidth, UINT nHeight) vp.TopLeftY = 0; /////////////////////////////////////////////////////////////////////////////// - g_pDeviceContext->RSSetViewports(1, &vp); + s_pDeviceContext->RSSetViewports(1, &vp); } void DestroyRenderTarget() { - if (g_pRenderTargetView != nullptr) + if (s_pRenderTargetView) { - g_pRenderTargetView->Release(); - g_pRenderTargetView = nullptr; - g_pDeviceContext->OMSetRenderTargets(0, 0, 0); + s_pRenderTargetView->Release(); + s_pRenderTargetView = nullptr; + s_pDeviceContext->OMSetRenderTargets(0, 0, 0); if (mat_showdxoutput->GetBool()) { @@ -325,7 +328,7 @@ void DestroyRenderTarget() HRESULT GetDeviceAndCtxFromSwapchain(IDXGISwapChain* pSwapChain, ID3D11Device** ppDevice, ID3D11DeviceContext** ppContext) { - HRESULT ret = pSwapChain->GetDevice(__uuidof(ID3D11Device), (PVOID*)ppDevice); + HRESULT ret = pSwapChain->GetDevice(__uuidof(ID3D11Device), reinterpret_cast(ppDevice)); if (SUCCEEDED(ret)) { (*ppDevice)->GetImmediateContext(ppContext); @@ -337,7 +340,7 @@ HRESULT __stdcall GetResizeBuffers(IDXGISwapChain* pSwapChain, UINT nBufferCount { g_pConsole->m_bActivate = false; g_pBrowser->m_bActivate = false; - g_bInitialized = false; + s_bInitialized = false; g_nWindowWidth = nWidth; g_nWindowHeight = nHeight; @@ -346,36 +349,36 @@ HRESULT __stdcall GetResizeBuffers(IDXGISwapChain* pSwapChain, UINT nBufferCount DestroyRenderTarget(); /////////////////////////////////////////////////////////////////////////////// - return g_oResizeBuffers(pSwapChain, nBufferCount, nWidth, nHeight, dxFormat, nSwapChainFlags); + return s_oResizeBuffers(pSwapChain, nBufferCount, nWidth, nHeight, dxFormat, nSwapChainFlags); } HRESULT __stdcall Present(IDXGISwapChain* pSwapChain, UINT nSyncInterval, UINT nFlags) { - if (!g_bInitialized) + if (!s_bInitialized) { - HRESULT hr = 0; - if (FAILED(hr = GetDeviceAndCtxFromSwapchain(pSwapChain, &g_pDevice, &g_pDeviceContext))) + HRESULT hr; + if (FAILED(hr = GetDeviceAndCtxFromSwapchain(pSwapChain, &s_pDevice, &s_pDeviceContext))) { Error(eDLL_T::MS, EXIT_FAILURE, "Failed to get device and context from swap chain: error code = %08x\n", hr); - return g_fnIDXGISwapChainPresent(pSwapChain, nSyncInterval, nFlags); + return s_fnSwapChainPresent(pSwapChain, nSyncInterval, nFlags); } CreateRenderTarget(pSwapChain); - if (!g_oWndProc) - { // Only initialize HwndProc pointer once to avoid stack overflow during ResizeBuffers(..) - SetupImGui(); // Don't re-init imgui every time. - g_oWndProc = (WNDPROC)SetWindowLongPtr(g_hGameWindow, GWLP_WNDPROC, (LONG_PTR)HwndProc); + if (!s_oWndProc) // Only initialize HwndProc pointer once to avoid stack overflow during ResizeBuffers(..) + { + SetupImGui(); + s_oWndProc = reinterpret_cast(SetWindowLongPtr(s_hGameWindow, GWLP_WNDPROC, reinterpret_cast(HwndProc))); } - g_pSwapChain = pSwapChain; + s_pSwapChain = pSwapChain; g_ThreadRenderThreadID = GetCurrentThreadId(); - g_bInitialized = true; + s_bInitialized = true; } DrawImGui(); /////////////////////////////////////////////////////////////////////////////// - return g_fnIDXGISwapChainPresent(pSwapChain, nSyncInterval, nFlags); + return s_fnSwapChainPresent(pSwapChain, nSyncInterval, nFlags); } bool LoadTextureBuffer(unsigned char* buffer, int len, ID3D11ShaderResourceView** out_srv, int* out_width, int* out_height) @@ -385,16 +388,16 @@ bool LoadTextureBuffer(unsigned char* buffer, int len, ID3D11ShaderResourceView* int image_height = 0; unsigned char* image_data = stbi_load_from_memory(buffer, len, &image_width, &image_height, NULL, 4); - if (image_data == NULL) + if (!image_data) { - assert(image_data == NULL); + assert(image_data); return false; } /////////////////////////////////////////////////////////////////////////////// - ID3D11Texture2D* pTexture = NULL; + ID3D11Texture2D* pTexture = nullptr; D3D11_TEXTURE2D_DESC desc; - D3D11_SUBRESOURCE_DATA subResource{}; + D3D11_SUBRESOURCE_DATA subResource; D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; /////////////////////////////////////////////////////////////////////////////// @@ -413,7 +416,7 @@ bool LoadTextureBuffer(unsigned char* buffer, int len, ID3D11ShaderResourceView* subResource.pSysMem = image_data; subResource.SysMemPitch = desc.Width * 4; subResource.SysMemSlicePitch = 0; - g_pDevice->CreateTexture2D(&desc, &subResource, &pTexture); + s_pDevice->CreateTexture2D(&desc, &subResource, &pTexture); // Create texture view ZeroMemory(&srvDesc, sizeof(srvDesc)); @@ -424,7 +427,7 @@ bool LoadTextureBuffer(unsigned char* buffer, int len, ID3D11ShaderResourceView* if (pTexture) { - g_pDevice->CreateShaderResourceView(pTexture, &srvDesc, out_srv); + s_pDevice->CreateShaderResourceView(pTexture, &srvDesc, out_srv); pTexture->Release(); } @@ -448,20 +451,20 @@ void ResetInput() void InstallDXHooks() { /////////////////////////////////////////////////////////////////////////////// - g_oPostMessageA = (IPostMessageA)DetourFindFunction("user32.dll", "PostMessageA"); - g_oPostMessageW = (IPostMessageW)DetourFindFunction("user32.dll", "PostMessageW"); + s_oPostMessageA = (IPostMessageA)DetourFindFunction("user32.dll", "PostMessageA"); + s_oPostMessageW = (IPostMessageW)DetourFindFunction("user32.dll", "PostMessageW"); // Begin the detour transaction DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); // Hook PostMessage - DetourAttach(&(LPVOID&)g_oPostMessageA, (PBYTE)HPostMessageA); - DetourAttach(&(LPVOID&)g_oPostMessageW, (PBYTE)HPostMessageW); + DetourAttach(&(LPVOID&)s_oPostMessageA, (PBYTE)HPostMessageA); + DetourAttach(&(LPVOID&)s_oPostMessageW, (PBYTE)HPostMessageW); // Hook SwapChain - DetourAttach(&(LPVOID&)g_fnIDXGISwapChainPresent, (PBYTE)Present); - DetourAttach(&(LPVOID&)g_oResizeBuffers, (PBYTE)GetResizeBuffers); + DetourAttach(&(LPVOID&)s_fnSwapChainPresent, (PBYTE)Present); + DetourAttach(&(LPVOID&)s_oResizeBuffers, (PBYTE)GetResizeBuffers); // Commit the transaction HRESULT hr = DetourTransactionCommit(); @@ -479,35 +482,35 @@ void DirectX_Shutdown() DetourUpdateThread(GetCurrentThread()); // Unhook PostMessage - DetourDetach(&(LPVOID&)g_oPostMessageA, (PBYTE)HPostMessageA); - DetourDetach(&(LPVOID&)g_oPostMessageW, (PBYTE)HPostMessageW); + DetourDetach(&(LPVOID&)s_oPostMessageA, (PBYTE)HPostMessageA); + DetourDetach(&(LPVOID&)s_oPostMessageW, (PBYTE)HPostMessageW); // Unhook SwapChain - DetourDetach(&(LPVOID&)g_fnIDXGISwapChainPresent, (PBYTE)Present); - DetourDetach(&(LPVOID&)g_oResizeBuffers, (PBYTE)GetResizeBuffers); + DetourDetach(&(LPVOID&)s_fnSwapChainPresent, (PBYTE)Present); + DetourDetach(&(LPVOID&)s_oResizeBuffers, (PBYTE)GetResizeBuffers); // Commit the transaction DetourTransactionCommit(); /////////////////////////////////////////////////////////////////////////////// // Shutdown ImGui - if (g_bImGuiInitialized) + if (s_bImGuiInitialized) { ImGui_ImplWin32_Shutdown(); ImGui_ImplDX11_Shutdown(); - g_bImGuiInitialized = false; + s_bImGuiInitialized = false; } - g_bInitialized = false; + s_bInitialized = false; } void VDXGI::GetAdr(void) const { /////////////////////////////////////////////////////////////////////////////// - spdlog::debug("| FUN: IDXGISwapChain::Present : {:#18x} |\n", reinterpret_cast(g_fnIDXGISwapChainPresent)); - spdlog::debug("| VAR: g_pSwapChain : {:#18x} |\n", reinterpret_cast(g_pSwapChain) ); - spdlog::debug("| VAR: g_pRenderTargetView : {:#18x} |\n", reinterpret_cast(g_pRenderTargetView) ); - spdlog::debug("| VAR: g_pDeviceContext : {:#18x} |\n", reinterpret_cast(g_pDeviceContext) ); - spdlog::debug("| VAR: g_pDevice : {:#18x} |\n", reinterpret_cast(g_pDevice) ); + spdlog::debug("| FUN: IDXGISwapChain::Present : {:#18x} |\n", reinterpret_cast(s_fnSwapChainPresent)); + spdlog::debug("| VAR: s_pSwapChain : {:#18x} |\n", reinterpret_cast(s_pSwapChain) ); + spdlog::debug("| VAR: s_pRenderTargetView : {:#18x} |\n", reinterpret_cast(s_pRenderTargetView) ); + spdlog::debug("| VAR: s_pDeviceContext : {:#18x} |\n", reinterpret_cast(s_pDeviceContext) ); + spdlog::debug("| VAR: s_pDevice : {:#18x} |\n", reinterpret_cast(s_pDevice) ); spdlog::debug("| VAR: g_ppGameDevice : {:#18x} |\n", reinterpret_cast(g_ppGameDevice) ); spdlog::debug("+----------------------------------------------------------------+\n"); } @@ -521,15 +524,14 @@ DWORD __stdcall DXSwapChainWorker(LPVOID) g_pImGuiConfig->Load(); // Load ImGui configs. GetPresent(); InstallDXHooks(); - return true; + + return NULL; } void DirectX_Init() { // Create a worker thread for the in-game console frontend - DWORD __stdcall DXSwapChainWorker(LPVOID); HANDLE hThread = CreateThread(NULL, 0, DXSwapChainWorker, NULL, 0, NULL); - if (hThread) { CloseHandle(hThread); diff --git a/r5dev/windows/id3dx.h b/r5dev/windows/id3dx.h index 6bf12f6a..e0a1d12f 100644 --- a/r5dev/windows/id3dx.h +++ b/r5dev/windows/id3dx.h @@ -28,8 +28,8 @@ typedef HRESULT(__stdcall* IDXGIResizeBuffers) (IDXGISwapChain* pSwapChain, UI ///////////////////////////////////////////////////////////////////////////// // Globals -inline INT g_nWindowWidth; -inline INT g_nWindowHeight; +inline UINT g_nWindowWidth; +inline UINT g_nWindowHeight; ///////////////////////////////////////////////////////////////////////////// // Enums