Properly shutdown game from ext console

This commit is contained in:
fed 2023-07-15 23:26:50 +02:00
parent ca4cdc31ff
commit a5d3b55681
4 changed files with 40 additions and 13 deletions

View File

@ -24,6 +24,8 @@ namespace command
std::unordered_map<std::string, std::function<void(params&)>> handlers;
bool game_initialized{};
void main_handler()
{
params params = {};
@ -147,6 +149,11 @@ namespace command
void execute(std::string command, const bool sync)
{
if (!game_initialized)
{
return;
}
command += "\n";
if (sync)
@ -159,6 +166,11 @@ namespace command
}
}
bool is_game_initialized()
{
return game_initialized;
}
class component final : public component_interface
{
public:
@ -166,6 +178,11 @@ namespace command
{
utils::hook::jump(0x1405A74F0, dvar_command_stub, true);
scheduler::once([]
{
game_initialized = true;
}, scheduler::main);
add("quit", game::Quit);
add("startmap", [](const params& params)

View File

@ -25,4 +25,6 @@ namespace command
void add(const char* name, const std::function<void()>& callback);
void execute(std::string command, bool sync = false);
bool is_game_initialized();
}

View File

@ -26,7 +26,7 @@ namespace console
struct
{
bool kill;
std::atomic_bool kill;
std::thread thread;
HANDLE kill_event;
char buffer[512]{};
@ -328,6 +328,25 @@ namespace console
return dispatch_message(con_type_info, result);
}
BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
{
if (ctrl_type == CTRL_CLOSE_EVENT)
{
if (command::is_game_initialized())
{
command::execute("quit");
while (!con.kill)
{
std::this_thread::sleep_for(10ms);
}
return TRUE;
}
}
return FALSE;
}
}
void print(const int type, const char* fmt, ...)
@ -357,6 +376,7 @@ namespace console
{
ShowWindow(GetConsoleWindow(), SW_SHOW);
SetConsoleTitle("H2-Mod");
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
con.kill_event = CreateEvent(NULL, TRUE, FALSE, NULL);

View File

@ -63,8 +63,6 @@ namespace game_console
float color_white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float color_h2[4] = {0.9f, 0.9f, 0.5f, 1.0f};
bool is_initialized{};
void clear()
{
strncpy_s(con.buffer, "", 256);
@ -550,11 +548,6 @@ namespace game_console
void add(const std::string& cmd)
{
if (!is_initialized)
{
return;
}
execute(cmd.data());
history.push_front(cmd);
@ -764,11 +757,6 @@ namespace game_console
{
scheduler::loop(draw_console, scheduler::pipeline::renderer);
scheduler::once([]
{
is_initialized = true;
}, scheduler::main);
con.cursor = 0;
con.visible_line_count = 0;
con.output_visible = false;