forked from maximegmd/CyberEngineTweaks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
95 lines (77 loc) · 2.52 KB
/
window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdafx.h>
#include "window.h"
#include <CET.h>
using namespace std::chrono_literals;
static Window* s_pWindow = nullptr;
static BOOL CALLBACK EnumWindowsProcCP77(HWND ahWnd, LPARAM alParam)
{
DWORD lpdwProcessId;
GetWindowThreadProcessId(ahWnd, &lpdwProcessId);
if (lpdwProcessId == GetCurrentProcessId())
{
wchar_t name[512] = {0};
RealGetWindowClassW(ahWnd,name,511);
if (wcscmp (L"W2ViewportClass", name) == 0)
{
*reinterpret_cast<HWND*>(alParam) = ahWnd;
return FALSE;
}
}
return TRUE;
}
LRESULT APIENTRY Window::WndProc(HWND ahWnd, UINT auMsg, WPARAM awParam, LPARAM alParam)
{
if (s_pWindow)
{
if (auMsg == WM_WINDOWPOSCHANGED)
{
const auto* wp = reinterpret_cast<WINDOWPOS*>(alParam);
if ((wp->flags & SWP_NOMOVE) == 0)
s_pWindow->m_wndPos = {wp->x, wp->y};
if ((wp->flags & SWP_NOSIZE) == 0)
s_pWindow->m_wndSize = {wp->cx, wp->cy};
RECT cr;
GetClientRect(ahWnd, &cr);
s_pWindow->m_clientPos = {cr.left, cr.top};
s_pWindow->m_clientSize = {cr.right - cr.left, cr.bottom - cr.top};
}
{
if (s_pWindow->m_pBindings->OnWndProc(ahWnd, auMsg, awParam, alParam))
return 0; // VKBindings wants this input ignored!
}
{
if (s_pWindow->m_pD3D12->OnWndProc(ahWnd, auMsg, awParam, alParam))
return 0; // D3D12 wants this input ignored!
}
return CallWindowProc(s_pWindow->m_wndProc, ahWnd, auMsg, awParam, alParam);
}
return 0;
}
Window::Window(VKBindings* apBindings, D3D12* apD3D12)
: m_pBindings(apBindings)
, m_pD3D12(apD3D12)
{
s_pWindow = this;
std::thread t(
[this]
{
while (m_hWnd == nullptr)
{
if (EnumWindows(EnumWindowsProcCP77, reinterpret_cast<LPARAM>(&m_hWnd)))
std::this_thread::sleep_for(50ms);
else
{
m_wndProc = reinterpret_cast<WNDPROC>(SetWindowLongPtr(m_hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(WndProc)));
Log::Info("Window::Initialize() - window hook complete.");
}
}
m_initialized = true;
});
t.detach();
}
Window::~Window()
{
s_pWindow = nullptr;
if (m_hWnd != nullptr)
SetWindowLongPtr(m_hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(m_wndProc));
}