Map rotation

This commit is contained in:
Federico Cecchetto 2022-05-19 21:45:00 +02:00
parent 02fcebf24b
commit 605f04fa8d
2 changed files with 33 additions and 32 deletions

View File

@ -5,6 +5,8 @@
#include "game/game.hpp"
#include "command.hpp"
#include <utils/thread.hpp>
namespace game_console
{
void print(int type, const std::string& data);
@ -14,26 +16,8 @@ namespace console
{
namespace
{
static bool ingame = false;
DWORD WINAPI console(LPVOID)
{
ShowWindow(GetConsoleWindow(), SW_SHOW);
SetConsoleTitle("H1-Mod");
std::string cmd;
while (true)
{
std::getline(std::cin, cmd);
if (ingame)
{
game::Cbuf_AddText(0, 0, cmd.data());
}
}
return 0;
}
bool kill = false;
std::thread console_thread;
}
std::string format(va_list* ap, const char* message)
@ -43,7 +27,7 @@ namespace console
const auto count = _vsnprintf_s(buffer, sizeof(buffer), sizeof(buffer), message, *ap);
if (count < 0) return {};
return { buffer, static_cast<size_t>(count) };
return {buffer, static_cast<size_t>(count)};
}
void dispatch_message(const int type, const std::string& message)
@ -66,19 +50,36 @@ namespace console
class component final : public component_interface
{
public:
void post_start() override
component()
{
CreateThread(0, 0, console, 0, 0, 0);
ShowWindow(GetConsoleWindow(), SW_HIDE);
}
void post_unpack() override
{
ingame = true;
ShowWindow(GetConsoleWindow(), SW_SHOW);
SetConsoleTitle("H1-Mod");
console_thread = utils::thread::create_named_thread("Console", []()
{
std::string cmd;
while (!kill)
{
std::getline(std::cin, cmd);
game::Cbuf_AddText(0, 0, cmd.data());
}
});
}
void pre_destroy() override
{
ingame = false;
kill = true;
if (console_thread.joinable())
{
console_thread.join();
}
}
};
}

View File

@ -9,9 +9,10 @@
namespace map_rotation
{
DWORD previousPriority;
namespace
{
DWORD previous_priority{};
void set_dvar(const std::string& dvar, const std::string& value)
{
command::execute(utils::string::va("%s \"%s\"", dvar.data(), value.data()), true);
@ -84,10 +85,10 @@ namespace map_rotation
scheduler::on_game_initialized([]()
{
//printf("=======================setting OLD priority=======================\n");
SetPriorityClass(GetCurrentProcess(), previousPriority);
SetPriorityClass(GetCurrentProcess(), previous_priority);
}, scheduler::pipeline::main, 1s);
previousPriority = GetPriorityClass(GetCurrentProcess());
previous_priority = GetPriorityClass(GetCurrentProcess());
//printf("=======================setting NEW priority=======================\n");
SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
}
@ -147,7 +148,6 @@ namespace map_rotation
return scheduler::cond_end;
}, scheduler::pipeline::main, 1s);
}
}
class component final : public component_interface
@ -170,11 +170,11 @@ namespace map_rotation
command::add("map_rotate", &perform_map_rotation);
// Hook GScr_ExitLevel
utils::hook::jump(0x140376630, &trigger_map_rotation); // not sure if working
utils::hook::jump(0xE2670_b, &trigger_map_rotation, true); // not sure if working
previousPriority = GetPriorityClass(GetCurrentProcess());
previous_priority = GetPriorityClass(GetCurrentProcess());
}
};
}
//REGISTER_COMPONENT(map_rotation::component)
REGISTER_COMPONENT(map_rotation::component)