Updated Console

Added command line tag for external console
This commit is contained in:
bodnjenie14 2024-05-27 17:04:52 +01:00 committed by GitHub
parent d003511810
commit 324fcc2425
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,3 @@
//TODO: Fix console branding
#include <std_include.hpp> #include <std_include.hpp>
#include "loader/component_loader.hpp" #include "loader/component_loader.hpp"
#include <utilities/string.hpp> #include <utilities/string.hpp>
@ -14,9 +12,8 @@ namespace console
{ {
namespace namespace
{ {
const char* branding_str = "Project-Bo4 >"; const char* branding_str = "shield> ";
size_t branding_length = std::strlen(branding_str);
size_t branding_lenght = std::strlen(branding_str);
utilities::hook::detour printf_hook; utilities::hook::detour printf_hook;
std::recursive_mutex print_mutex; std::recursive_mutex print_mutex;
@ -64,7 +61,7 @@ namespace console
return {}; return {};
} }
return {buffer, static_cast<size_t>(count)}; return { buffer, static_cast<size_t>(count) };
} }
void update() void update()
@ -73,11 +70,8 @@ namespace console
show_cursor(false); show_cursor(false);
set_cursor_pos(0); set_cursor_pos(0);
invoke_printf("%s", con.buffer); invoke_printf("%s%s", branding_str, con.buffer);
//invoke_printf("%s %s", branding_str, con.buffer); set_cursor_pos(branding_length + con.cursor);
set_cursor_pos(con.cursor);
//set_cursor_pos(branding_lenght + con.cursor);
show_cursor(true); show_cursor(true);
} }
@ -88,18 +82,17 @@ namespace console
show_cursor(false); show_cursor(false);
set_cursor_pos(0); set_cursor_pos(0);
for (auto i = 0; i < std::strlen(con.buffer); i++) for (auto i = 0; i < branding_length + std::strlen(con.buffer); i++)
{ {
invoke_printf(" "); invoke_printf(" ");
} }
set_cursor_pos(con.cursor); set_cursor_pos(0);
show_cursor(true); show_cursor(true);
} }
int dispatch_message(const int type, const std::string& message) int dispatch_message(const int type, const std::string& message)
{ {
std::lock_guard _0(print_mutex); std::lock_guard _0(print_mutex);
clear_output(); clear_output();
@ -132,7 +125,7 @@ namespace console
CONSOLE_SCREEN_BUFFER_INFO info{}; CONSOLE_SCREEN_BUFFER_INFO info{};
GetConsoleScreenBufferInfo(OUTPUT_HANDLE, &info); GetConsoleScreenBufferInfo(OUTPUT_HANDLE, &info);
const auto columns = static_cast<size_t>(info.srWindow.Right - info.srWindow.Left - 1); const auto columns = static_cast<size_t>(info.srWindow.Right - info.srWindow.Left - 1);
return std::max(size_t(0), std::min(columns, sizeof(con.buffer))); return std::max(size_t(0), std::min(columns, sizeof(con.buffer) - branding_length));
} }
void handle_resize() void handle_resize()
@ -159,143 +152,135 @@ namespace console
const auto key = record.Event.KeyEvent.wVirtualKeyCode; const auto key = record.Event.KeyEvent.wVirtualKeyCode;
switch (key) switch (key)
{ {
case VK_UP: case VK_UP:
{
if (++con.history_index >= con.history.size())
{ {
if (++con.history_index >= con.history.size()) con.history_index = static_cast<int>(con.history.size()) - 1;
{
con.history_index = static_cast<int>(con.history.size()) - 1;
}
clear();
if (con.history_index != -1)
{
strncpy_s(con.buffer, con.history.at(con.history_index).data(), sizeof(con.buffer));
con.cursor = static_cast<int>(strlen(con.buffer));
}
update();
break;
} }
case VK_DOWN:
clear();
if (con.history_index != -1)
{ {
if (--con.history_index < -1) strncpy_s(con.buffer, con.history.at(con.history_index).data(), sizeof(con.buffer));
{ con.cursor = static_cast<int>(strlen(con.buffer));
con.history_index = -1;
}
clear();
if (con.history_index != -1)
{
strncpy_s(con.buffer, con.history.at(con.history_index).data(), sizeof(con.buffer));
con.cursor = static_cast<int>(strlen(con.buffer));
}
update();
break;
} }
case VK_LEFT:
update();
break;
}
case VK_DOWN:
{
if (--con.history_index < -1)
{ {
if (con.cursor > 0)
{
con.cursor--;
set_cursor_pos(con.cursor);
//set_cursor_pos(branding_lenght + con.cursor);
}
break;
}
case VK_RIGHT:
{
if (con.cursor < std::strlen(con.buffer))
{
con.cursor++;
set_cursor_pos(con.cursor);
//set_cursor_pos(branding_lenght + con.cursor);
}
break;
}
case VK_RETURN:
{
if (con.history_index != -1)
{
const auto itr = con.history.begin() + con.history_index;
if (*itr == con.buffer)
{
con.history.erase(con.history.begin() + con.history_index);
}
}
if (con.buffer[0])
{
con.history.push_front(con.buffer);
}
if (con.history.size() > 10)
{
con.history.erase(con.history.begin() + 10);
}
con.history_index = -1; con.history_index = -1;
game::Cbuf_AddText(0, utilities::string::va("%s \n", con.buffer));
con.cursor = 0;
clear_output();
invoke_printf("]%s\r\n", con.buffer);
strncpy_s(con.buffer, "", sizeof(con.buffer));
break;
} }
case VK_BACK:
clear();
if (con.history_index != -1)
{ {
if (con.cursor <= 0) strncpy_s(con.buffer, con.history.at(con.history_index).data(), sizeof(con.buffer));
{ con.cursor = static_cast<int>(strlen(con.buffer));
break; }
}
clear_output(); update();
break;
std::memmove(con.buffer + con.cursor - 1, con.buffer + con.cursor, strlen(con.buffer) + 1 - con.cursor); }
con.buffer - 1; case VK_LEFT:
{
if (con.cursor > 0)
{
con.cursor--; con.cursor--;
set_cursor_pos(branding_length + con.cursor);
update();
break;
} }
case VK_ESCAPE:
{
con.cursor = 0;
clear_output();
strncpy_s(con.buffer, "", sizeof(con.buffer));
break;
}
default:
{
const auto c = record.Event.KeyEvent.uChar.AsciiChar;
if (!c)
{
break;
}
if (std::strlen(con.buffer) + 1 >= get_max_input_length()) break;
{ }
break; case VK_RIGHT:
} {
if (con.cursor < std::strlen(con.buffer))
std::memmove(con.buffer + con.cursor + 1, {
con.buffer + con.cursor, std::strlen(con.buffer) + 1 - con.cursor);
con.buffer[con.cursor] = c;
con.cursor++; con.cursor++;
set_cursor_pos(branding_length + con.cursor);
}
update(); break;
}
case VK_RETURN:
{
if (con.history_index != -1)
{
const auto itr = con.history.begin() + con.history_index;
if (*itr == con.buffer)
{
con.history.erase(con.history.begin() + con.history_index);
}
}
if (con.buffer[0])
{
con.history.push_front(con.buffer);
}
if (con.history.size() > 10)
{
con.history.erase(con.history.begin() + 10);
}
con.history_index = -1;
game::Cbuf_AddText(0, utilities::string::va("%s \n", con.buffer));
con.cursor = 0;
clear_output();
invoke_printf("%s%s\r\n", branding_str, con.buffer);
strncpy_s(con.buffer, "", sizeof(con.buffer));
break;
}
case VK_BACK:
{
if (con.cursor <= 0)
{
break; break;
} }
clear_output();
std::memmove(con.buffer + con.cursor - 1, con.buffer + con.cursor, strlen(con.buffer) + 1 - con.cursor);
con.cursor--;
update();
break;
}
case VK_ESCAPE:
{
con.cursor = 0;
clear_output();
strncpy_s(con.buffer, "", sizeof(con.buffer));
break;
}
default:
{
const auto c = record.Event.KeyEvent.uChar.AsciiChar;
if (!c)
{
break;
}
if (std::strlen(con.buffer) + 1 >= get_max_input_length())
{
break;
}
std::memmove(con.buffer + con.cursor + 1, con.buffer + con.cursor, std::strlen(con.buffer) + 1 - con.cursor);
con.buffer[con.cursor] = c;
con.cursor++;
update();
break;
}
} }
} }
@ -309,7 +294,7 @@ namespace console
return dispatch_message(con_type_info, result); return dispatch_message(con_type_info, result);
} }
} }
void print(const int type, const char* fmt, ...) void print(const int type, const char* fmt, ...)
{ {
va_list ap; va_list ap;
@ -345,48 +330,47 @@ namespace console
con.kill_event = CreateEvent(NULL, TRUE, FALSE, NULL); con.kill_event = CreateEvent(NULL, TRUE, FALSE, NULL);
con.thread = utilities::thread::create_named_thread("Console", []() con.thread = utilities::thread::create_named_thread("Console", []()
{
const auto handle = GetStdHandle(STD_INPUT_HANDLE);
HANDLE handles[2] = {handle, con.kill_event};
MSG msg{};
INPUT_RECORD record{};
DWORD num_events{};
while (!con.kill)
{ {
const auto result = MsgWaitForMultipleObjects(2, handles, FALSE, INFINITE, QS_ALLINPUT); const auto handle = GetStdHandle(STD_INPUT_HANDLE);
if (con.kill) HANDLE handles[2] = { handle, con.kill_event };
{ MSG msg{};
return;
}
switch (result) INPUT_RECORD record{};
DWORD num_events{};
while (!con.kill)
{ {
case WAIT_OBJECT_0: const auto result = MsgWaitForMultipleObjects(2, handles, FALSE, INFINITE, QS_ALLINPUT);
{ if (con.kill)
if (!ReadConsoleInput(handle, &record, 1, &num_events) || num_events == 0)
{ {
break; return;
} }
handle_input(record); switch (result)
break;
}
case WAIT_OBJECT_0 + 1:
{
if (!PeekMessageA(&msg, GetConsoleWindow(), NULL, NULL, PM_REMOVE))
{ {
case WAIT_OBJECT_0:
{
if (!ReadConsoleInput(handle, &record, 1, &num_events) || num_events == 0)
{
break;
}
handle_input(record);
break; break;
} }
case WAIT_OBJECT_0 + 1:
{
if (!PeekMessageA(&msg, GetConsoleWindow(), NULL, NULL, PM_REMOVE))
{
break;
}
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
break; break;
} }
} }
} } });
});
} }
void pre_destroy() override void pre_destroy() override