Skip to content

Commit 78ae016

Browse files
committed
feat: Renamed and added more GUI class functionality
1 parent f9d390f commit 78ae016

File tree

3 files changed

+88
-51
lines changed

3 files changed

+88
-51
lines changed

src/modules/gui.cpp

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,91 @@
1010

1111
#include "utils.hpp"
1212

13-
HWND WindowsGUI::GetWindowHandle()
13+
bool GeneralGUI::InitializeGUISystems()
1414
{
15-
if (!WindowsGUI::hWnd)
16-
WindowsGUI::hWnd = FindWindow("Valve001", nullptr);
15+
Log(INFO, true, "Grabbing game window handle...");
16+
if (!GeneralGUI::GetWindowHandle())
17+
{
18+
//assert(0 && "Failed to get window handle!");
19+
Log(WARNING, false, "Failed to get window handle!");
20+
return false;
21+
}
22+
23+
// Log(INFO, true, "Starting ImGUI...");
24+
// if (!ImGui::Init())
25+
// {
26+
// //assert(false && "Failed to initialize ImGui!");
27+
// Log(INFO, false, "Failed to initialize ImGui!");
28+
// return false;
29+
// }
30+
31+
return true;
32+
}
33+
34+
/**
35+
* @brief Get the handle to the game Window.
36+
* @return Windows Window handle.
37+
*/
38+
HWND GeneralGUI::GetWindowHandle()
39+
{
40+
if (!GeneralGUI::hWnd)
41+
{
42+
GeneralGUI::hWnd = FindWindow("Valve001", nullptr);
43+
}
1744
// if (!WindowsGUI::hWnd)
1845
// WindowsGUI::hWnd = FindWindowA(nullptr, "Portal 2 - Direct3D 9");
1946
// if (!WindowsGUI::hWnd)
20-
// WindowsGUI::hWnd = FindWindowA(nullptr, "Portal 2 - Direct3D 9");
47+
// WindowsGUI::hWnd = FindWindowA(nullptr, "PORTAL 2 - Vulkan");
2148
// if (!WindowsGUI::hWnd)
2249
// WindowsGUI::hWnd = FindWindowA(nullptr, "PORTAL 2");
23-
return WindowsGUI::hWnd;
50+
return GeneralGUI::hWnd;
2451
}
2552

53+
/**
54+
* @brief ImGui initialization function.
55+
* @return Returns true if ImGui initialized successfully.
56+
*/
2657
bool ImGui::Init()
2758
{
28-
// Get the DirectX9 device
29-
auto scanResult = Memory::Scan(APIDX9, "A1 ? ? ? ? 6A 00", 1);
30-
if (!scanResult)
31-
scanResult = Memory::Scan(APIVULKAN, "A1 ? ? ? ? 56 84 DB", 1);
32-
ImGui::g_pDXDevice = Memory::Deref<IDirect3DDevice9*>(scanResult);
59+
// Get the DirectX9 Device.
60+
// Valve used DXVK for Vulkan, so the API it uses layers over DirectX's stuff so the device handle can be used for both cases.
61+
62+
uintptr_t deviceAddressPtr = NULL;
63+
if (Memory::GetModuleHandleByName(MODULE_SHADERAPIDX9))
64+
deviceAddressPtr = Memory::Scan(MODULE_SHADERAPIDX9, "A1 ? ? ? ? 6A 00", 1);
65+
else
66+
deviceAddressPtr = Memory::Scan(MODULE_APIVULKAN, "A1 ? ? ? ? 56 84 DB", 1);
67+
68+
if (!deviceAddressPtr)
69+
{
70+
assert(0 && "Failed to get address pointer to the DirectX9 device!");
71+
Log(WARNING, false, "Failed to get address pointer to the DirectX9 device!");
72+
return false;
73+
}
74+
75+
// Get the device itself from the pointer pointer of the device address.
76+
ImGui::g_pDXDevice = Memory::Deref<IDirect3DDevice9*>(deviceAddressPtr);
3377
if (!ImGui::g_pDXDevice)
3478
{
3579
assert(0 && "Failed to get DirectX9 device!");
80+
Log(WARNING, false, "Failed to get DirectX9 device!");
3681
return false;
3782
}
3883

39-
ImGui::present = Memory::VMT<void*>(g_pDXDevice, 17);
40-
Log(INFO, false, "Present %p", present);
84+
// Get the "Present" class function used for rendering to it can be hooked.
85+
ImGui::Present = Memory::VMT<void*>(g_pDXDevice, 17);
86+
Log(INFO, false, "Present %p", Present);
87+
88+
// TODO: Add ImGui initialization calls here.
4189

4290
return true;
4391
}
4492

4593
void ImGui::Shutdown()
4694
{
95+
// TODO: Add ImGui shutdown calls here.
4796
}
4897

49-
HWND WindowsGUI::hWnd = nullptr;
50-
IDirect3DDevice9* ImGui::g_pDXDevice = nullptr;
98+
HWND GeneralGUI::hWnd = nullptr;
99+
IDirect3DDevice9* ImGui::g_pDXDevice = nullptr;
100+
void* ImGui::Present = nullptr;

src/modules/gui.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
#endif
1818

1919
/**
20-
* @brief Windows GUI functions and variables.
20+
* @brief General GUI functions and variables.
2121
*/
22-
class WindowsGUI
22+
class GeneralGUI
2323
{
2424
public: // MARK: WindowsGUI Public Members
2525
#pragma region Public Members
26+
27+
28+
static bool InitializeGUISystems();
29+
2630
static HWND GetWindowHandle();
31+
2732
#pragma endregion
2833

2934
private: // MARK: WindowsGUI Private Members
@@ -67,8 +72,9 @@ class ImGui
6772
#else
6873
// TODO: Implement SDL and Vulkan devices
6974
#endif
70-
71-
static void* present;
75+
76+
static void* Present;
77+
7278

7379
#pragma endregion
7480
};

src/p2sm.cpp

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,24 @@ bool CP2SMPlusPlusPlugin::Load(CreateInterfaceFn interfaceFactory, const CreateI
6666
);
6767

6868
Log(INFO, false, "Loading plugin...");
69-
70-
Log(INFO, true, "Grabbing game window handle...");
71-
if (!WindowsGUI::GetWindowHandle())
72-
Log(WARNING, false, "Failed to find game window!");
73-
74-
Log(INFO, true, "Connecting tier libraries...");
69+
70+
Log(INFO, true, "Connecting tier libraries and registering plugin ConVars and ConCommands...");
71+
ConVar_Register(0);
7572
MathLib_Init(2.2f, 2.2f, 0.0f, 2.0f);
7673
ConnectTier1Libraries(&interfaceFactory, 1);
7774
ConnectTier2Libraries(&interfaceFactory, 1);
7875

79-
Log(INFO, true, "Registering plugin ConVars and ConCommands...");
80-
ConVar_Register(0);
81-
76+
Log(INFO, true, "Initializing plugin GUI systems...");
77+
if (!GeneralGUI::InitializeGUISystems())
78+
{
79+
assert(0 && "Failed to initialize plugin GUI systems!");
80+
Log(WARNING, false, "Failed to initialize plugin GUI systems!");
81+
}
82+
8283
// Make sure that all the interfaces needed are loaded and usable.
8384
Log(INFO, true, "Loading interfaces...");
8485
Log(INFO, true, "Loading engineServer...");
85-
engineServer = static_cast<IVEngineServer*>(interfaceFactory(INTERFACEVERSION_VENGINESERVER, 0));
86+
engineServer = static_cast<IVEngineServer*>(interfaceFactory(INTERFACEVERSION_VENGINESERVER, nullptr));
8687
if (!engineServer)
8788
{
8889
assert(false && "Unable to load engineServer!");
@@ -92,7 +93,7 @@ bool CP2SMPlusPlusPlugin::Load(CreateInterfaceFn interfaceFactory, const CreateI
9293
}
9394

9495
Log(INFO, true, "Loading engineClient...");
95-
engineClient = static_cast<IVEngineClient*>(interfaceFactory(VENGINE_CLIENT_INTERFACE_VERSION, 0));
96+
engineClient = static_cast<IVEngineClient*>(interfaceFactory(VENGINE_CLIENT_INTERFACE_VERSION, nullptr));
9697
if (!engineClient)
9798
{
9899
assert(false && "Unable to load engineClient!");
@@ -102,7 +103,7 @@ bool CP2SMPlusPlusPlugin::Load(CreateInterfaceFn interfaceFactory, const CreateI
102103
}
103104

104105
Log(INFO, true, "Loading g_pPlayerInfoManager...");
105-
g_pPlayerInfoManager = static_cast<IPlayerInfoManager*>(gameServerFactory(INTERFACEVERSION_PLAYERINFOMANAGER, 0));
106+
g_pPlayerInfoManager = static_cast<IPlayerInfoManager*>(gameServerFactory(INTERFACEVERSION_PLAYERINFOMANAGER, nullptr));
106107
if (!g_pPlayerInfoManager)
107108
{
108109
assert(false && "Unable to load g_pPlayerInfoManager!");
@@ -188,19 +189,6 @@ bool CP2SMPlusPlusPlugin::Load(CreateInterfaceFn interfaceFactory, const CreateI
188189
&CUGCFileRequestManager__Update_hook, reinterpret_cast<void**>(&CUGCFileRequestManager__Update_orig)
189190
);
190191

191-
// MH_CreateHook(
192-
// Memory::Scanner::Scan(CLIENT, "55 8B EC 51 56 6A 20 8B F1"),
193-
// &CUGCFileRequestManager__Update_hook, reinterpret_cast<void**>(&CUGCFileRequestManager__Update_orig)
194-
// );
195-
196-
//
197-
// Stop workshop map downloads by not returning false on the download request.
198-
// Log(INFO, true, "Hooking CWorkshopManager::CreateFileDownloadRequest...");
199-
// MH_CreateHook( //55 8B EC 8B 45 ?? 83 EC 14 53 57
200-
// Memory::Scanner::Scan(CLIENT, "55 8B EC 8B 45 ?? 83 EC 14 53 57"),
201-
// &CUGCFileRequestManager__CreateFileDownloadRequest_hook, reinterpret_cast<void**>(&CUGCFileRequestManager__CreateFileDownloadRequest_orig)
202-
// );
203-
204192
// Log(INFO, true, "Hooking CEnvProjectedTexture::EnforceSingleProjectionRules...");
205193
// MH_CreateHook(
206194
// Memory::Scan<void*>(CLIENT, "55 8B EC 8B 45 ? 8B 55 ? 50 8B 45 ? 52 8B 55 ? 50 8B 45 ? 52 8B 55 ? 50 8B 45"),
@@ -241,13 +229,6 @@ bool CP2SMPlusPlusPlugin::Load(CreateInterfaceFn interfaceFactory, const CreateI
241229
if (ConVar* ifuCVar = g_pCVar->FindVar("in_forceuser"))
242230
ifuCVar->RemoveFlags(FCVAR_CHEAT);
243231

244-
Log(INFO, true, "Starting ImGUI...");
245-
if (!ImGui::Init())
246-
{
247-
assert(false && "Failed to initialize ImGui!");
248-
Log(INFO, false, "Failed to initialize ImGui!");
249-
}
250-
251232
Log(INFO, false, "Loaded plugin! Yay! :D");
252233
m_bPluginLoaded = true;
253234

@@ -264,7 +245,7 @@ void CP2SMPlusPlusPlugin::Unload(void)
264245
{
265246
m_bNoUnload = false;
266247
Log(WARNING, true, "Failed to load plugin!");
267-
MessageBox(WindowsGUI::GetWindowHandle(), "P2SM++ ran into a error when starting!\nPlease check the console for more info!", "P2SM++ Startup Error", MB_OK | MB_ICONERROR);
248+
MessageBox(GeneralGUI::GetWindowHandle(), "P2SM++ ran into a error when starting!\nPlease check the console for more info!", "P2SM++ Startup Error", MB_OK | MB_ICONERROR);
268249
return;
269250
}
270251

0 commit comments

Comments
 (0)