-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChat.cpp
More file actions
440 lines (340 loc) · 11.8 KB
/
Copy pathChat.cpp
File metadata and controls
440 lines (340 loc) · 11.8 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include "Chat.h"
#include <iostream>
#include "ChatEntryManager.h"
#include "Menu.h"
struct LineVertex
{
float x, y, z, rhw;
D3DCOLOR color;
};
#define LINE_FVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
struct Rect
{
DWORD top, bottom, left, right;
};
uintptr_t SAMPGetAddress(SAMPAddressesType type) {
static const auto versionIndex = Chat::getSampVersion() - 2;
static const auto base = Chat::getSampBaseAddress();
return (base + SAMPAddresses[versionIndex][type]);
}
uintptr_t SAMPGetOffset(SAMPAddressesType type)
{
static const auto versionIndex = Chat::getSampVersion() - 2;
return SAMPAddresses[versionIndex][type];
}
void Chat::MainLoop(const decltype(mainLoopHook)& hook)
{
static bool init = false;
if (!init && isSampAvailable())
{
// Setup hooks
auto& instance = getInstance();
SetHook(instance.mChatRenderEntryHook, SAMPGetAddress(SAMP_ADDRESS_CHAT_RENDER_ENTRY), &CChat__RenderEntry);
// Setup patches
SetPatch(SAMPGetAddress(SAMP_ADDRESS_COMMAND_SET_PAGESIZE) + 0x2F, { 0x83, 0xFE, 0x40 }); // cmp esi, 0x40
SetPatch(SAMPGetAddress(SAMP_ADDRESS_COMMAND_SET_PAGESIZE_HINT) + 0xD, { '6', '4' }); // "20" -> "64"
init = true;
}
else if (init)
{
// While true
}
return hook.get_trampoline()();
}
uintptr_t Chat::getSampBaseAddress()
{
return reinterpret_cast<uintptr_t>(GetModuleHandleA("samp.dll"));
}
bool Chat::isSampAvailable()
{
return *reinterpret_cast<uintptr_t*>(0x00C8D4C0) == 9 && getSampBaseAddress() > 0;
}
bool Chat::isGTAMenuActive()
{
return *reinterpret_cast<uint32_t*>(0xBA67A4) != 0;
}
SampVersion Chat::getSampVersion()
{
static SampVersion version = SAMP_NOT_LOADED;
if (version <= SAMP_UNKNOWN)
{
const auto base = getSampBaseAddress();
if (base == 0) return SAMP_NOT_LOADED;
const auto* ntHeader = reinterpret_cast<IMAGE_NT_HEADERS*>(base + reinterpret_cast<IMAGE_DOS_HEADER*>(base)->e_lfanew);
static const auto entryPoint = ntHeader->OptionalHeader.AddressOfEntryPoint;
for (size_t i = 0; i < SAMPEntryPoints.size(); i++)
{
const auto& curEntryPoint = SAMPEntryPoints[i];
if (curEntryPoint == entryPoint)
{
return static_cast<SampVersion>(i + 2);
}
}
}
return SAMP_UNKNOWN;
}
HWND Chat::getGameHWND()
{
static const HWND gameHWND = **reinterpret_cast<HWND**>(0xC17054);
return gameHWND;
}
ChatEntryManager& Chat::getChatEntryManager()
{
return getInstance().mChatEntryManager;
}
int Chat::getSampCursorMode()
{
const auto CGame = *reinterpret_cast<uintptr_t*>(SAMPGetAddress(SAMP_ADDRESS_GLOBAL_GAME_PTR));
if (!CGame) return 0;
return *reinterpret_cast<int*>(CGame + SAMPGetOffset(SAMP_OFFSET_GLOBAL_GAME_MOUSEMODE));
}
void Chat::setSampCursorMode(const int nMode)
{
const auto CGame = *reinterpret_cast<uintptr_t*>(SAMPGetAddress(SAMP_ADDRESS_GLOBAL_GAME_PTR));
if (!CGame || getSampCursorMode() == nMode) return;
const auto setCursorModeFunc = reinterpret_cast<dGameSetCursorMode>(SAMPGetAddress(SAMP_ADDRESS_GAME_SET_CURSOR_MODE));
setCursorModeFunc(reinterpret_cast<void*>(CGame), nMode, 0);
}
float Chat::getSampFontSize()
{
return static_cast<float>(reinterpret_cast<int(*)()>(SAMPGetAddress(SAMP_ADDRESS_FONTSIZE_VALUE))());
}
float Chat::getSampFontSizeParam()
{
return (getSampFontSize() - 20) / 2;
}
char* Chat::getSampFontName()
{
return reinterpret_cast<char* (*)()>(SAMPGetAddress(SAMP_ADDRESS_CHAT_GET_FONTFACE))();
}
std::string Chat::getFontRelativePathByName(const std::string& fontName)
{
HKEY hKey;
const char* subKey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";
LONG result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hKey);
if (result != ERROR_SUCCESS) return std::string{};
char value[512];
DWORD valueLength = sizeof(value);
const std::string fontRegistryName = fontName + " (TrueType)";
result = RegQueryValueExA(hKey, fontRegistryName.c_str(), nullptr, nullptr, (LPBYTE)value, &valueLength);
if (result != ERROR_SUCCESS) {
RegCloseKey(hKey);
return std::string{};
}
RegCloseKey(hKey);
std::string fontPath = value;
return fontPath;
}
void Chat::chatUpdate()
{
const auto& pChat = getInstance().pChat;
if (pChat == nullptr) return;
pChat->m_bRedraw = 1;
reinterpret_cast<int(__thiscall*)(void*)>(SAMPGetAddress(SAMP_ADDRESS_CHAT_RENDER))(pChat);
}
void Chat::sampDeleteChatLine(const int& id)
{
const auto& pChat = getInstance().pChat;
for (int i = id; i > 0; --i)
pChat->m_entry[i] = pChat->m_entry[i - 1];
pChat->m_entry[0] = CChatEntry();
getInstance().chatUpdate();
}
void Chat::sampDeleteChatLineAll()
{
const auto& pChat = getInstance().pChat;
for (auto& entry : pChat->m_entry)
entry = CChatEntry();
getInstance().chatUpdate();
}
std::string Chat::convertToUTF8(const std::string& os_str) {
const UINT os_codepage = GetACP();
const int wchars_num = MultiByteToWideChar(os_codepage, 0, os_str.c_str(), -1, nullptr, 0);
if (wchars_num == 0) {
return std::string{};
}
std::wstring wstr(wchars_num, 0);
MultiByteToWideChar(os_codepage, 0, os_str.c_str(), -1, wstr.data(), wchars_num);
const int utf8_chars_num = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (utf8_chars_num == 0) {
return std::string{};
}
std::string utf8_str(utf8_chars_num, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, utf8_str.data(), utf8_chars_num, nullptr, nullptr);
return utf8_str;
}
std::string Chat::convertFromUTF8(const std::string& utf8_str) {
const UINT os_codepage = GetACP();
const int len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, nullptr, 0);
auto* utf16_buffer = new wchar_t[len];
MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, utf16_buffer, len);
const int os_chars_num = WideCharToMultiByte(os_codepage, 0, utf16_buffer, -1, nullptr, 0, nullptr, nullptr);
if (os_chars_num == 0) {
delete[] utf16_buffer;
return std::string{};
}
auto* os_buffer = new char[os_chars_num];
WideCharToMultiByte(os_codepage, 0, utf16_buffer, -1, os_buffer, os_chars_num, nullptr, nullptr);
delete[] utf16_buffer;
std::string result(os_buffer);
delete[] os_buffer;
return result;
}
HRESULT __stdcall Chat::OnWndProc(const decltype(mWndProcHook)& hook, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
auto& menu = Menu::getInstance();
wchar_t wch;
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, reinterpret_cast<char*>(&wParam), 1, &wch, 1);
// ImGui key handle
ImGui_ImplWin32_WndProcHandler(hwnd, msg, wParam, lParam);
// Check for mouse move
if (msg == WM_MOUSEMOVE || msg == WM_RBUTTONDOWN) {
const auto cursorMode = getInstance().getSampCursorMode();
auto& mSelectedEntry = getInstance().mSelectedEntry;
const bool isEdit = menu.IsPopupActive() || menu.IsEditLineActive();
if (cursorMode >= 2 && cursorMode <= 3) // SAMP Cursor is enabled
{
if (!isEdit)
{
auto xPos = GET_X_LPARAM(lParam);
auto yPos = GET_Y_LPARAM(lParam);
const auto entryId = getChatEntryManager().getEntryIdByScreenCoords(xPos, yPos);
auto& cchat = Chat::getInstance().pChat;
if (
entryId > -1 &&
cchat->m_entry[entryId].m_textColor != 0
) {
mSelectedEntry = entryId;
if (msg == WM_RBUTTONDOWN) menu.ShowPopup();
}
else mSelectedEntry = -1;
}
}
else if (!isEdit) mSelectedEntry = -1;
}
// Close editline window by esc/enter + block keys for game
if (menu.IsEditLineActive())
{
if ((msg == WM_CHAR || msg == WM_KEYUP || msg == WM_KEYDOWN) && (wParam == VK_RETURN || wParam == VK_ESCAPE))
{
if (msg != WM_KEYUP) return TRUE;
if (!menu.IsColorPopupActive()) menu.CloseEditLine();
}
}
return hook.call_trampoline(hwnd, msg, wParam, lParam);
}
std::optional<HRESULT> Chat::OnPresent(const decltype(mOnPresentHook)& hook, IDirect3DDevice9* pDevice, const RECT*, const RECT*, HWND, const RGNDATA*) {
auto& menu = Menu::getInstance();
if (!menu.imguiInited) {
ImGui::CreateContext();
ImGui_ImplWin32_Init(getGameHWND());
ImGui_ImplDX9_Init(pDevice);
ImGui_ImplDX9_InvalidateDeviceObjects();
ImGui_ImplDX9_CreateDeviceObjects();
Menu::getInstance().RebuildFonts();
menu.imguiInited = true;
}
ImGui_ImplDX9_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
menu.Render();
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
return std::nullopt;
}
std::optional<HRESULT> Chat::OnLost(const decltype(mOnResetHook)& hook, IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* parameters) {
ImGui_ImplDX9_InvalidateDeviceObjects();
return std::nullopt;
}
void Chat::OnReset(const decltype(mOnResetHook)& hook, HRESULT& return_value, IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* parameters) {
ImGui_ImplDX9_InvalidateDeviceObjects();
}
void DrawRect(
IDirect3DDevice9* device,
float x1, float y1,
float x2, float y2,
D3DCOLOR color = D3DCOLOR_ARGB(255, 255, 0, 0))
{
LineVertex v[8] =
{
{ x1, y1, 0.f, 1.f, color }, { x2, y1, 0.f, 1.f, color },
{ x2, y1, 0.f, 1.f, color }, { x2, y2, 0.f, 1.f, color },
{ x2, y2, 0.f, 1.f, color }, { x1, y2, 0.f, 1.f, color },
{ x1, y2, 0.f, 1.f, color }, { x1, y1, 0.f, 1.f, color }
};
device->SetFVF(LINE_FVF);
device->DrawPrimitiveUP(
D3DPT_LINELIST,
4,
v,
sizeof(LineVertex)
);
}
void* __fastcall Chat::CChat__Render(const decltype(mChatRenderHook)& hook, void* ptr, void*)
{
auto chat = reinterpret_cast<CChat*>(ptr);
int firstLine = max(1, chat->m_pScrollbar->m_nPos);
int charH = chat->m_nCharHeight;
auto& manager = Chat::getInstance().getChatEntryManager();
Chat::getInstance().pChat = chat;
manager.setChatPointer(chat);
int maxTextWidth = 445;
for (int screenIndex = 0; screenIndex < chat->m_nPageSize; ++screenIndex)
{
int entryId = firstLine + screenIndex;
auto& entry = chat->m_entry[entryId];
auto cfont = chat->m_pFontRenderer->m_pFont;
using DrawTextFn = int(__stdcall*)(
void*, void*, const char*, int, RECT*, unsigned int, D3DCOLOR
);
auto drawText = reinterpret_cast<DrawTextFn>(cfont->m_lpVtbl[14]);
RECT rc{};
drawText(cfont, nullptr, entry.m_szText, -1, &rc, DT_CALCRECT, 0);
int width = rc.right - rc.left;
if (chat->m_bTimestamp)
width += chat->m_nTimestampWidth;
maxTextWidth = max(maxTextWidth, width);
}
int y = 10;
for (int screenIndex = 0; screenIndex < chat->m_nPageSize; ++screenIndex)
{
int entryId = firstLine + screenIndex;
CRect rect{};
rect.x1 = 45;
rect.y1 = y;
rect.x2 = rect.x1 + maxTextWidth + 5;
rect.y2 = y + charH + 1;
manager.push(entryId, screenIndex, rect);
y += charH + 1;
}
return hook.call_trampoline(ptr, nullptr);
}
int __fastcall Chat::CChat__RenderEntry(const decltype(mChatRenderEntryHook)& hook, void* ptr, void*, const char* src, CRect rect, uint32_t color)
{
static bool init = false;
if (!init)
{
auto& instance = getInstance();
SetHook(instance.mChatRenderHook, SAMPGetAddress(SAMP_ADDRESS_CHAT_RENDER), &CChat__Render);
SetHook(instance.mWndProcHook, SAMPGetAddress(SAMP_ADDRESS_CHATINPUT_WNDPROC), &OnWndProc);
SetHook(instance.mChatAddEntryHook, SAMPGetAddress(SAMP_ADDRESS_CHAT_ADD_ENTRY), &CChat__AddEntry);
SetHook(instance.mChatRecalcFontSizeHook, SAMPGetAddress(SAMP_ADDRESS_RECALC_FONTSIZE), &CChat__RecalcFontSize);
instance.mOnPresentHook.before += instance.OnPresent;
instance.mOnResetHook.before += instance.OnLost;
instance.mOnResetHook.after += instance.OnReset;
init = true;
}
return hook.call_trampoline(ptr, nullptr, src, rect, color);
}
void __fastcall Chat::CChat__AddEntry(const decltype(mChatAddEntryHook)& hook, void* ptr, void*, int nType, const char* szText, const char* szPrefix, D3DCOLOR textColor, D3DCOLOR prefixColor)
{
auto& mSelected = getInstance().mSelectedEntry;
if (mSelected > -1) mSelected--;
return hook.call_trampoline(ptr, nullptr, nType, szText, szPrefix, textColor, prefixColor);
}
int __fastcall Chat::CChat__RecalcFontSize(const decltype(mChatRecalcFontSizeHook)& hook, void* ptr, void*)
{
Menu::getInstance().RebuildFonts();
return hook.call_trampoline(ptr, nullptr);
}