2022-01-27 18:51:44 -05:00
|
|
|
#include <std_include.hpp>
|
|
|
|
#include "loader/component_loader.hpp"
|
|
|
|
|
|
|
|
#include "game/game.hpp"
|
|
|
|
|
|
|
|
#include "command.hpp"
|
|
|
|
#include "game_console.hpp"
|
|
|
|
#include "scheduler.hpp"
|
2022-03-19 18:06:00 -04:00
|
|
|
#include "filesystem.hpp"
|
|
|
|
#include "materials.hpp"
|
|
|
|
#include "fonts.hpp"
|
2022-01-27 18:51:44 -05:00
|
|
|
|
|
|
|
#include <utils/hook.hpp>
|
|
|
|
#include <utils/io.hpp>
|
|
|
|
|
|
|
|
namespace mods
|
|
|
|
{
|
2022-03-19 18:06:00 -04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
utils::hook::detour db_release_xassets_hook;
|
|
|
|
|
2022-03-20 14:40:15 -04:00
|
|
|
bool release_assets = false;
|
|
|
|
|
2022-03-19 18:06:00 -04:00
|
|
|
void db_release_xassets_stub()
|
|
|
|
{
|
2022-03-20 14:40:15 -04:00
|
|
|
if (release_assets)
|
|
|
|
{
|
|
|
|
materials::clear();
|
|
|
|
fonts::clear();
|
|
|
|
}
|
|
|
|
|
2022-03-19 18:06:00 -04:00
|
|
|
db_release_xassets_hook.invoke<void>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void restart()
|
|
|
|
{
|
|
|
|
scheduler::once([]()
|
|
|
|
{
|
2022-03-20 14:40:15 -04:00
|
|
|
release_assets = true;
|
2022-03-19 18:06:00 -04:00
|
|
|
game::Com_Shutdown("");
|
2022-03-20 14:40:15 -04:00
|
|
|
release_assets = false;
|
2022-03-19 18:06:00 -04:00
|
|
|
}, scheduler::pipeline::main);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:51:44 -05:00
|
|
|
class component final : public component_interface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void post_unpack() override
|
|
|
|
{
|
|
|
|
if (!utils::io::directory_exists("mods"))
|
|
|
|
{
|
|
|
|
utils::io::create_directory("mods");
|
|
|
|
}
|
|
|
|
|
2022-03-19 18:06:00 -04:00
|
|
|
db_release_xassets_hook.create(0x140416A80, db_release_xassets_stub);
|
|
|
|
|
2022-01-27 18:51:44 -05:00
|
|
|
command::add("loadmod", [](const command::params& params)
|
|
|
|
{
|
|
|
|
if (params.size() < 2)
|
|
|
|
{
|
|
|
|
game_console::print(game_console::con_type_info, "Usage: loadmod mods/<modname>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-19 18:06:00 -04:00
|
|
|
if (!game::Com_InFrontend())
|
2022-01-27 18:51:44 -05:00
|
|
|
{
|
|
|
|
game_console::print(game_console::con_type_error, "Cannot load mod while in-game!\n");
|
|
|
|
game::CG_GameMessage(0, "^1Cannot unload mod while in-game!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto path = params.get(1);
|
|
|
|
game_console::print(game_console::con_type_info, "Loading mod %s\n", path);
|
|
|
|
|
|
|
|
if (!utils::io::directory_exists(path))
|
|
|
|
{
|
|
|
|
game_console::print(game_console::con_type_error, "Mod %s not found!\n", path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-19 18:06:00 -04:00
|
|
|
filesystem::get_search_paths().insert(path);
|
2022-01-27 18:51:44 -05:00
|
|
|
game::mod_folder = path;
|
2022-03-19 18:06:00 -04:00
|
|
|
restart();
|
2022-01-27 18:51:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
command::add("unloadmod", [](const command::params& params)
|
|
|
|
{
|
|
|
|
if (game::mod_folder.empty())
|
|
|
|
{
|
|
|
|
game_console::print(game_console::con_type_info, "No mod loaded\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-19 18:06:00 -04:00
|
|
|
if (!game::Com_InFrontend())
|
2022-01-27 18:51:44 -05:00
|
|
|
{
|
|
|
|
game_console::print(game_console::con_type_error, "Cannot unload mod while in-game!\n");
|
|
|
|
game::CG_GameMessage(0, "^1Cannot unload mod while in-game!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
game_console::print(game_console::con_type_info, "Unloading mod %s\n", game::mod_folder.data());
|
2022-03-19 18:06:00 -04:00
|
|
|
filesystem::get_search_paths().erase(game::mod_folder);
|
2022-01-27 18:51:44 -05:00
|
|
|
game::mod_folder.clear();
|
2022-03-19 18:06:00 -04:00
|
|
|
restart();
|
2022-01-27 18:51:44 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_COMPONENT(mods::component)
|