This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoreLogic.cpp
223 lines (160 loc) · 5.25 KB
/
CoreLogic.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "CoreLogic.h"
#include <string>
static WNDPROC LastWndProc{ nullptr };
// General functions.
bool CCoreGeneral::IsMinecraftProcess() {
auto procCmdLine = std::string(GetCommandLineA());
return (
procCmdLine.find("mojang") != std::string::npos
&& procCmdLine.find(".minecraft") != std::string::npos
);
};
void CCoreGeneral::GetWindowByPID(DWORD dwPID, std::vector<HWND>& vecWindows) {
struct WindowsOfProcess
{
std::vector<HWND>* pvecWindows;
DWORD dwProcId;
static BOOL CALLBACK EnumProc(HWND hWnd, LPARAM lParam)
{
DWORD dwProcId;
GetWindowThreadProcessId(hWnd, &dwProcId);
if (dwProcId == reinterpret_cast<WindowsOfProcess*>(lParam)->dwProcId)
reinterpret_cast<WindowsOfProcess*>(lParam)->pvecWindows->push_back(hWnd);
return TRUE;
}
WindowsOfProcess(DWORD dwId, std::vector<HWND>* pvec) : dwProcId(dwId), pvecWindows(pvec) {
EnumWindows(EnumProc, reinterpret_cast<LPARAM>(this));
}
};
WindowsOfProcess wop(dwPID, &vecWindows);
}
HWND CCoreGeneral::GetMinecraftHWND() {
if (!IsMinecraftProcess()) {
g_logger << logger_level::LL_ERROR
<< "It is not a real Minecraft game process. "
<< "[CCoreGeneral::GetMinecraftHWND()]" << std::endl;
return NULL;
};
HWND curHWND = NULL;
auto currentPID = GetCurrentProcessId();
std::vector<HWND> vecWnd(0);
GetWindowByPID(currentPID, vecWnd);
for (auto item : vecWnd) {
char* chClassName = new char[255];
GetClassNameA(item, chClassName, 255);
if (
strcmp(chClassName, "LWJGL") == NULL
|| strcmp(chClassName, "GLFW30") == NULL
) {
curHWND = item;
break;
}
delete[] chClassName;
}
return curHWND;
}
void CCoreLogic::PrivateInit() {
pidMinecraft = GetCurrentProcessId();
g_logger << logger_level::LL_INFO << "Have a good day! :D " << std::endl;
hMinecraft = CCoreGeneral::GetMinecraftHWND();
if (!hMinecraft) {
return void(g_logger << logger_level::LL_ERROR << "Failed to get game window handle. " << std::endl);
};
LastWndProc = (WNDPROC)SetWindowLongPtrW(hMinecraft, GWLP_WNDPROC, (LONG_PTR)LogicWndProc);
MH_Initialize();
ptrSwapBuffers = (void*)GetProcAddress((HMODULE)GetModuleHandleW(L"opengl32.dll"), "wglSwapBuffers");
if (!ptrSwapBuffers) {
return void(
g_logger << logger_level::LL_ERROR
<< "[x] Failed to get wglSwapBuffers(opengl32.dll). "
<< std::endl
);
}
MH_CreateHook(ptrSwapBuffers, &wglSwapBuffersHook, (LPVOID*)&lastWglSwapBuffers);
MH_EnableHook(MH_ALL_HOOKS);
};
void CCoreLogic::PrivateDestroy() {
SetWindowLongPtrW(hMinecraft, GWLP_WNDPROC, (LONG_PTR)LastWndProc);
MH_DisableHook(MH_ALL_HOOKS);
CRender::Get()->Destroy();
MH_RemoveHook(MH_ALL_HOOKS);
}
// Logic Functions
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall CCoreLogic::LogicWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
if (CRender::Get()) {
if (uMsg == WM_KEYDOWN && wParam == VK_INSERT) {
CRender::Get()->Visible(!CRender::Get()->IsVisible());
};
if (CRender::Get()->IsVisible()) {
if (ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam)) {
if ((uMsg == WM_KEYUP || uMsg == WM_SYSKEYUP) && (wParam < 256)) {
return CallWindowProcA(LastWndProc, hWnd, uMsg, wParam, lParam);
}
return true;
}
};
};
return CallWindowProcA(LastWndProc, hWnd, uMsg, wParam, lParam);
}
bool __stdcall CCoreLogic::wglSwapBuffersHook(HDC hDC) {
static HGLRC LastContext{ wglGetCurrentContext() };
static HGLRC NewContext{};
static GLint LastViewport[4];
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
static bool init = false;
if (!init || viewport[2] != LastViewport[2] || viewport[3] != LastViewport[3]) {
if (NewContext) {
wglMakeCurrent(hDC, LastContext);
CRender::Get()->Shutdown();
wglDeleteContext(NewContext);
};
NewContext = wglCreateContext(hDC);
wglMakeCurrent(hDC, NewContext);
glViewport(0, 0, viewport[2], viewport[3]);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// glDisable(GL_DEPTH_TEST);
if (!CRender::IsInit())
CRender::Init(CCoreLogic::Get()->hMinecraft);
else
CRender::Get()->Update(CCoreLogic::Get()->hMinecraft);
memcpy(LastViewport, viewport, sizeof(GLint) * 4);
init = true;
}
else {
wglMakeCurrent(hDC, NewContext);
CRender::Get()->Draw();
}
wglMakeCurrent(hDC, LastContext);
return CCoreLogic::Get()->lastWglSwapBuffers(hDC);
}
// Singleton stuff
CCoreLogic* CCoreLogic::p_instance;
std::mutex CCoreLogic::mutex;
void CCoreLogic::Init() {
if (p_instance != nullptr)
return;
p_instance = new CCoreLogic;
}
void CCoreLogic::Destroy() {
if (p_instance == nullptr)
return;
p_instance->PrivateDestroy();
delete p_instance;
p_instance = nullptr;
}
CCoreLogic* CCoreLogic::Get() {
std::lock_guard<std::mutex> lock(mutex);
if (p_instance == nullptr) {
return nullptr;
}
return p_instance;
};
bool CCoreLogic::IsInit() {
return p_instance != nullptr;
};