-
Notifications
You must be signed in to change notification settings - Fork 5
/
LogWindow.cpp
278 lines (216 loc) · 6.86 KB
/
LogWindow.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#pragma once
#pragma comment(lib, "comctl32.lib")
#include "GameAPI.h"
#include "GameForms.h"
#include "LogWindow.h"
#include <Richedit.h>
#include <regex>
#include "Settings.h"
#include "GECKUtility.h"
HWND g_ConsoleHwnd;
extern HWND g_MainHwnd;
extern bool g_bPauseLogging = false;
void Console_PrintVa(const char* Format, va_list Va)
{
char buffer[2048];
int len = stbsp_vsnprintf(buffer, sizeof(buffer), Format, Va);
if (len <= 0)
return;
if (len >= 2 && buffer[len - 1] != '\n')
strcat_s(buffer, "\n");
if (g_ConsoleHwnd)
PostMessageA(g_ConsoleHwnd, UI_CMD_ADDLOGTEXT, 0, (LPARAM)_strdup(buffer));
_MESSAGE("%s", buffer);
}
void Console_Print(const char* Format, ...)
{
if (g_bPauseLogging)
return;
va_list va;
va_start(va, Format);
Console_PrintVa(Format, va);
va_end(va);
}
int __cdecl Console_Print2(const char* Format, ...)
{
if (g_bPauseLogging)
return 0;
va_list va;
va_start(va, Format);
Console_PrintVa(Format, va);
va_end(va);
return 0;
}
void AddLogText(LPARAM lParam, HWND richEditHwnd, bool doFree)
{
void* textData = (void*)lParam;
// Save old position if not scrolling
POINT scrollRange;
if (!config.bAutoScroll)
{
SendMessageA(richEditHwnd, WM_SETREDRAW, FALSE, 0);
SendMessageA(richEditHwnd, EM_GETSCROLLPOS, 0, (WPARAM)&scrollRange);
}
// Move caret to the end, then write
CHARRANGE range;
range.cpMin = LONG_MAX;
range.cpMax = LONG_MAX;
SendMessageA(richEditHwnd, EM_EXSETSEL, 0, (LPARAM)&range);
SendMessageA(richEditHwnd, EM_REPLACESEL, FALSE, (LPARAM)textData);
if (!config.bAutoScroll)
{
SendMessageA(richEditHwnd, EM_SETSCROLLPOS, 0, (WPARAM)&scrollRange);
SendMessageA(richEditHwnd, WM_SETREDRAW, TRUE, 0);
RedrawWindow(richEditHwnd, nullptr, nullptr, RDW_ERASE | RDW_INVALIDATE | RDW_NOCHILDREN);
}
if (doFree)
free(textData);
}
LRESULT CALLBACK EditorUI_LogWndProc(HWND Hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
static HWND richEditHwnd;
switch (Message)
{
case WM_CREATE:
{
const CREATESTRUCT* info = (CREATESTRUCT*)lParam;
// Create the rich edit control (https://docs.microsoft.com/en-us/windows/desktop/Controls/rich-edit-controls)
uint32_t style = WS_VISIBLE | WS_CHILD | WS_VSCROLL | ES_MULTILINE | ES_LEFT | ES_NOHIDESEL | ES_AUTOVSCROLL | ES_READONLY;
richEditHwnd = CreateWindowW(MSFTEDIT_CLASS, L"", style, 0, 0, info->cx, info->cy, Hwnd, nullptr, info->hInstance, nullptr);
if (!richEditHwnd)
return -1;
// Set a better font
CHARFORMAT2W format;
memset(&format, 0, sizeof(format));
format.cbSize = sizeof(format);
format.dwMask = CFM_FACE | CFM_SIZE | CFM_WEIGHT;
// Convert Twips to points (1 point = 20 Twips)
int pointSize = GetOrCreateINIValue("Log", "FontSize", 10, IniPath) * 20;
format.yHeight = pointSize;
char fontNameBuf[32];
GetPrivateProfileStringA("Log", "Font", "Consolas", fontNameBuf, 31, IniPath);
mbstowcs(format.szFaceName, fontNameBuf, 31);
format.wWeight = (WORD)GetOrCreateINIValue("Log", "FontWeight", FW_MEDIUM, IniPath);
SendMessageA(richEditHwnd, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&format);
// Subscribe to EN_MSGFILTER and EN_SELCHANGE
SendMessageW(richEditHwnd, EM_SETEVENTMASK, 0, ENM_MOUSEEVENTS | ENM_SELCHANGE);
}
return 0;
case WM_DESTROY:
DestroyWindow(richEditHwnd);
return 0;
case WM_SIZE:
{
int w = LOWORD(lParam);
int h = HIWORD(lParam);
MoveWindow(richEditHwnd, 0, 0, w, h, TRUE);
}
break;
case WM_ACTIVATE:
{
if (wParam != WA_INACTIVE)
SetFocus(richEditHwnd);
}
return 0;
case WM_CLOSE:
ShowWindow(Hwnd, SW_HIDE);
return 0;
case WM_NOTIFY:
{
static uint64_t lastDoubleClickTime;
LPNMHDR notification = (LPNMHDR)lParam;
if (notification->code == EN_MSGFILTER) {
auto msgFilter = (MSGFILTER*)notification;
if (msgFilter->msg == WM_LBUTTONDBLCLK)
lastDoubleClickTime = GetTickCount();
}
else if (notification->code == EN_SELCHANGE) {
auto selChange = (SELCHANGE*)lParam;
auto selLen = abs(selChange->chrg.cpMax - selChange->chrg.cpMin);
if ((GetTickCount() - lastDoubleClickTime > 1000) || selLen == 0)
break;
if (selChange->chrg.cpMin == 0 && selChange->chrg.cpMax == -1)
break;
// expand the selection to include any brackets if double clicking on a number
selChange->chrg.cpMin--;
selChange->chrg.cpMax++;
selLen += 2;
// Get the selected text from the rich edit control
char buf[0x400]; *buf = 0;
if (2 * (selLen + 1) >= sizeof(buf))
break;
TEXTRANGEA textRange;
textRange.chrg = selChange->chrg;
textRange.lpstrText = buf;
SendMessageA(richEditHwnd, EM_GETTEXTRANGE, 0, (LPARAM)&textRange);
char selectedText[0x400];
WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)buf, -1, selectedText, sizeof(selectedText), nullptr, nullptr);
// Ensure null termination
selectedText[selLen] = '\0';
// Capture the first form id with regex "\(([0-9a-fA-F]*)\)"
static const std::regex formIdRegex("\\(([0-9a-fA-F]{1,8})\\)");
std::smatch sm;
std::string selectedStr = selectedText;
if (std::regex_search(selectedStr, sm, formIdRegex)) {
uint32_t id = strtoul(sm[1].str().c_str(), nullptr, 16);
if (auto form = LookupFormByID(id)) {
OpenForm(form, g_MainHwnd);
}
}
}
}
break;
case UI_CMD_ADDLOGTEXT:
{
AddLogText(lParam, richEditHwnd, true);
}
return 0;
case UI_CMD_ADDLOGTEXT_NOFREE:
{
AddLogText(lParam, richEditHwnd, false);
}
return 0;
case UI_CMD_CLEARLOGTEXT:
{
char emptyString[1] = { '\0' };
SendMessageA(richEditHwnd, WM_SETTEXT, 0, (LPARAM)&emptyString);
}
return 0;
case UI_CMD_AUTOSCROLL:
{
config.bAutoScroll = wParam;
char buffer[8];
WritePrivateProfileString("Log", "bAutoScroll", _itoa(config.bAutoScroll, buffer, 2), IniPath);
}
return 0;
}
return DefWindowProc(Hwnd, Message, wParam, lParam);
}
bool EditorUI_CreateLogWindow()
{
HINSTANCE instance = (HINSTANCE)GetModuleHandle(nullptr);
WNDCLASSEX wc;
memset(&wc, 0, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hInstance = instance;
wc.hIcon = LoadIconA(instance, MAKEINTRESOURCE(0x13E));
wc.hIconSm = LoadIconA(instance, MAKEINTRESOURCE(0x13E));
wc.lpfnWndProc = EditorUI_LogWndProc;
wc.lpszClassName = TEXT("RTEDITLOG");
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
if (!RegisterClassEx(&wc))
return false;
// get previous stored position
int posX = GetPrivateProfileIntA("Log", "iWindowPosX", 64, IniPath);
int posY = GetPrivateProfileIntA("Log", "iWindowPosY", 64, IniPath);
int width = GetPrivateProfileIntA("Log", "iWindowPosDX", 1024, IniPath);
int height = GetPrivateProfileIntA("Log", "iWindowPosDY", 768, IniPath);
g_ConsoleHwnd = CreateWindowExA(0, "RTEDITLOG", "Message Log", WS_OVERLAPPEDWINDOW, posX, posY, width, height, nullptr, nullptr, instance, nullptr);
if (!g_ConsoleHwnd)
return false;
ShowWindow(g_ConsoleHwnd, SW_SHOW);
UpdateWindow(g_ConsoleHwnd);
return true;
}