Small refactoring

This commit is contained in:
momo5502 2022-06-02 20:42:58 +02:00
parent e2952e5905
commit e75e4961f6

View File

@ -37,75 +37,73 @@ namespace console
va_end(ap);
}
LRESULT con_wnd_proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
LRESULT con_wnd_proc(const HWND hwnd, const UINT msg, const WPARAM wparam, const LPARAM lparam)
{
switch (uMsg)
switch (msg)
{
case WM_CTLCOLORSTATIC:
SetBkColor((HDC)wParam, RGB(50, 50, 50));
SetTextColor((HDC)wParam, RGB(232, 230, 227));
return (INT_PTR)CreateSolidBrush(RGB(50, 50, 50));
case WM_CTLCOLOREDIT:
SetBkColor((HDC)wParam, RGB(50, 50, 50));
SetTextColor((HDC)wParam, RGB(232, 230, 227));
return (INT_PTR)CreateSolidBrush(RGB(50, 50, 50));
case WM_CTLCOLORSTATIC:
SetBkColor(reinterpret_cast<HDC>(wparam), RGB(50, 50, 50));
SetTextColor(reinterpret_cast<HDC>(wparam), RGB(232, 230, 227));
return reinterpret_cast<INT_PTR>(CreateSolidBrush(RGB(50, 50, 50)));
case WM_QUIT:
game::Cbuf_AddText(0, "quit\n");
return DefWindowProcA(hWnd, uMsg, wParam, lParam);
return DefWindowProcA(hwnd, msg, wparam, lparam);
default:
return utils::hook::invoke<LRESULT>(0x142333520_g, hWnd, uMsg, wParam, lParam);
return utils::hook::invoke<LRESULT>(0x142333520_g, hwnd, msg, wparam, lparam);
}
}
LRESULT input_line_wnd_proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
LRESULT input_line_wnd_proc(const HWND hwnd, const UINT msg, const WPARAM wparam, const LPARAM lparam)
{
return utils::hook::invoke<LRESULT>(0x142333820_g, hWnd, uMsg, wParam, lParam);
return utils::hook::invoke<LRESULT>(0x142333820_g, hwnd, msg, wparam, lparam);
}
void sys_create_console_stub(HINSTANCE hInstance)
void sys_create_console_stub(const HINSTANCE h_instance)
{
// C6262
char text[CONSOLE_BUFFER_SIZE];
char cleanConsoleBuffer[CONSOLE_BUFFER_SIZE];
char clean_console_buffer[CONSOLE_BUFFER_SIZE];
const auto* class_name = "BOIII WinConsole";
const auto* window_name = "BOIII Console";
WNDCLASSA WndClass;
WndClass.style = 0;
WndClass.lpfnWndProc = con_wnd_proc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInstance;
WndClass.hIcon = LoadIconA(hInstance, (LPCSTR)1);
WndClass.hCursor = LoadCursorA(0, (LPCSTR)0x7F00);
WndClass.hbrBackground = CreateSolidBrush(RGB(50, 50, 50));
WndClass.lpszMenuName = 0;
WndClass.lpszClassName = class_name;
WNDCLASSA wnd_class{};
wnd_class.style = 0;
wnd_class.lpfnWndProc = con_wnd_proc;
wnd_class.cbClsExtra = 0;
wnd_class.cbWndExtra = 0;
wnd_class.hInstance = h_instance;
wnd_class.hIcon = LoadIconA(h_instance, reinterpret_cast<LPCSTR>(1));
wnd_class.hCursor = LoadCursorA(nullptr, reinterpret_cast<LPCSTR>(0x7F00));
wnd_class.hbrBackground = CreateSolidBrush(RGB(50, 50, 50));
wnd_class.lpszMenuName = nullptr;
wnd_class.lpszClassName = class_name;
if (!RegisterClassA(&WndClass))
if (!RegisterClassA(&wnd_class))
{
return;
}
tagRECT Rect;
Rect.left = 0;
Rect.right = 620;
Rect.top = 0;
Rect.bottom = 450;
AdjustWindowRect(&Rect, 0x80CA0000, 0);
RECT rect;
rect.left = 0;
rect.right = 620;
rect.top = 0;
rect.bottom = 450;
AdjustWindowRect(&rect, 0x80CA0000, 0);
auto hDC = GetDC(GetDesktopWindow());
auto swidth = GetDeviceCaps(hDC, 8);
auto sheight = GetDeviceCaps(hDC, 10);
ReleaseDC(GetDesktopWindow(), hDC);
auto dc = GetDC(GetDesktopWindow());
const auto swidth = GetDeviceCaps(dc, 8);
const auto sheight = GetDeviceCaps(dc, 10);
ReleaseDC(GetDesktopWindow(), dc);
utils::hook::set<int>(game::s_wcd::windowWidth, Rect.right - Rect.left + 1);
utils::hook::set<int>(game::s_wcd::windowHeight, Rect.bottom - Rect.top + 1);
utils::hook::set<int>(game::s_wcd::windowWidth, rect.right - rect.left + 1);
utils::hook::set<int>(game::s_wcd::windowHeight, rect.bottom - rect.top + 1);
utils::hook::set<HWND>(game::s_wcd::hWnd, CreateWindowExA(
0, class_name, window_name, 0x80CA0000, (swidth - 600) / 2, (sheight - 450) / 2,
Rect.right - Rect.left + 1, Rect.bottom - Rect.top + 1, 0, 0, hInstance, 0));
0, class_name, window_name, 0x80CA0000, (swidth - 600) / 2, (sheight - 450) / 2,
rect.right - rect.left + 1, rect.bottom - rect.top + 1, nullptr, nullptr,
h_instance, nullptr));
if (!*game::s_wcd::hWnd)
{
@ -113,36 +111,41 @@ namespace console
}
// create fonts
hDC = GetDC(*game::s_wcd::hWnd);
auto nHeight = MulDiv(8, GetDeviceCaps(hDC, 90), 72);
dc = GetDC(*game::s_wcd::hWnd);
const auto n_height = MulDiv(8, GetDeviceCaps(dc, 90), 72);
utils::hook::set<HFONT>(game::s_wcd::hfBufferFont, CreateFontA(
-nHeight, 0, 0, 0, 300, 0, 0, 0, 1u, 0, 0, 0, 0x31u, "Courier New"));
-n_height, 0, 0, 0, 300, 0, 0, 0, 1u, 0, 0, 0, 0x31u, "Courier New"));
ReleaseDC(*game::s_wcd::hWnd, hDC);
ReleaseDC(*game::s_wcd::hWnd, dc);
if (logo)
{
utils::hook::set<HWND>(game::s_wcd::codLogo, CreateWindowExA(
0, "Static", 0, 0x5000000Eu, 5, 5, 0, 0, *game::s_wcd::hWnd, (HMENU)1, hInstance, 0));
SendMessageA(*game::s_wcd::codLogo, 0x172u, 0, (LPARAM)logo);
0, "Static", nullptr, 0x5000000Eu, 5, 5, 0, 0, *game::s_wcd::hWnd,
reinterpret_cast<HMENU>(1), h_instance, nullptr));
SendMessageA(*game::s_wcd::codLogo, 0x172u, 0, reinterpret_cast<LPARAM>(logo));
}
// create the input line
utils::hook::set<HWND>(game::s_wcd::hwndInputLine, CreateWindowExA(
0, "edit", 0, 0x50800080u, 6, 400, 608, 20, *game::s_wcd::hWnd, (HMENU)0x65, hInstance, 0));
0, "edit", nullptr, 0x50800080u, 6, 400, 608, 20, *game::s_wcd::hWnd,
reinterpret_cast<HMENU>(0x65), h_instance, nullptr));
utils::hook::set<HWND>(game::s_wcd::hwndBuffer, CreateWindowExA(
0, "edit", 0, 0x50A00844u, 6, 70, 606, 324, *game::s_wcd::hWnd, (HMENU)0x64, hInstance, 0));
SendMessageA(*game::s_wcd::hwndBuffer, WM_SETFONT, (WPARAM)*game::s_wcd::hfBufferFont, 0);
0, "edit", nullptr, 0x50A00844u, 6, 70, 606, 324, *game::s_wcd::hWnd,
reinterpret_cast<HMENU>(0x64), h_instance, nullptr));
SendMessageA(*game::s_wcd::hwndBuffer, WM_SETFONT, reinterpret_cast<WPARAM>(*game::s_wcd::hfBufferFont), 0);
utils::hook::set<WNDPROC>(game::s_wcd::SysInputLineWndProc, (WNDPROC)SetWindowLongPtrA(
*game::s_wcd::hwndInputLine, -4, (LONG_PTR)input_line_wnd_proc));
SendMessageA(*game::s_wcd::hwndInputLine, WM_SETFONT, (WPARAM)*game::s_wcd::hfBufferFont, 0);
utils::hook::set<WNDPROC>(game::s_wcd::SysInputLineWndProc, reinterpret_cast<WNDPROC>(SetWindowLongPtrA(
*game::s_wcd::hwndInputLine, -4,
reinterpret_cast<LONG_PTR>(input_line_wnd_proc))));
SendMessageA(*game::s_wcd::hwndInputLine, WM_SETFONT, reinterpret_cast<WPARAM>(*game::s_wcd::hfBufferFont),
0);
SetFocus(*game::s_wcd::hwndInputLine);
game::Con_GetTextCopy(text, 0x4000);
game::Conbuf_CleanText(text, cleanConsoleBuffer);
SetWindowTextA(*game::s_wcd::hwndBuffer, cleanConsoleBuffer);
game::Conbuf_CleanText(text, clean_console_buffer);
SetWindowTextA(*game::s_wcd::hwndBuffer, clean_console_buffer);
}
}
@ -151,7 +154,7 @@ namespace console
public:
void post_unpack() override
{
const utils::nt::library self;
const auto self = utils::nt::library::get_by_address(sys_create_console_stub);
logo = LoadImageA(self.get_handle(), MAKEINTRESOURCEA(IMAGE_LOGO), 0, 0, 0, LR_COPYFROMRESOURCE);
utils::hook::jump(printf, print_stub);