h1-mod/src/client/component/arena.cpp

79 lines
1.7 KiB
C++
Raw Normal View History

2022-11-09 17:54:34 -05:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "game/game.hpp"
#include "filesystem.hpp"
#include "console.hpp"
#include <utils/hook.hpp>
#include <utils/io.hpp>
#define MAX_ARENAS 64
namespace
{
2022-11-09 19:16:50 -05:00
std::recursive_mutex arena_mutex;
2022-11-09 17:54:34 -05:00
bool parse_arena(const std::string& path)
{
2022-11-09 19:16:50 -05:00
std::lock_guard<std::recursive_mutex> _0(arena_mutex);
2022-11-09 17:54:34 -05:00
2022-11-09 19:16:50 -05:00
std::string buffer{};
if (filesystem::read_file(path, &buffer) && !buffer.empty())
2022-11-09 17:54:34 -05:00
{
2022-11-09 19:16:50 -05:00
*game::ui_num_arenas += game::GameInfo_ParseArenas(buffer.data(), MAX_ARENAS - *game::ui_num_arenas,
&game::ui_arena_infos[*game::ui_num_arenas]);
2022-11-09 17:54:34 -05:00
return true;
}
2022-11-09 19:16:50 -05:00
char rawfile_buffer[0x18000] = {0};
const auto buf = game::DB_ReadRawFile(path.data(), rawfile_buffer, sizeof(rawfile_buffer));
2022-11-09 17:54:34 -05:00
if (buf)
{
2022-11-09 19:16:50 -05:00
*game::ui_num_arenas += game::GameInfo_ParseArenas(buf, MAX_ARENAS - *game::ui_num_arenas,
&game::ui_arena_infos[*game::ui_num_arenas]);
2022-11-09 17:54:34 -05:00
return true;
}
return false;
}
void load_arenas_stub()
{
2022-11-09 19:16:50 -05:00
*game::ui_num_arenas = 0;
*game::ui_arena_buf_pos = 0;
2022-11-09 17:54:34 -05:00
parse_arena("mp/basemaps.arena");
// read usermap arena from disk
2022-11-09 19:16:50 -05:00
const auto mapname = game::Dvar_FindVar("ui_mapname");
2022-11-09 17:54:34 -05:00
if (mapname && mapname->current.string)
{
2022-11-09 19:16:50 -05:00
const auto usermap_path = "usermaps/"s + mapname->current.string;
const auto arena_path = usermap_path + "/" + mapname->current.string + ".arena";
2022-11-09 17:54:34 -05:00
parse_arena(arena_path);
}
}
}
namespace arena
{
class component final : public component_interface
{
public:
void post_unpack() override
{
if (!game::environment::is_mp())
{
return;
}
// load custom arenas
utils::hook::jump(0x4DE030_b, load_arenas_stub);
}
};
}
2022-11-09 19:16:50 -05:00
REGISTER_COMPONENT(arena::component)