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

89 lines
2.1 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>
2022-11-10 13:10:28 -05:00
#include <utils/memory.hpp>
2022-11-09 17:54:34 -05:00
#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-10 13:10:28 -05:00
if (!game::DB_XAssetExists(game::ASSET_TYPE_RAWFILE, path.data()) ||
game::DB_IsXAssetDefault(game::ASSET_TYPE_RAWFILE, path.data()))
2022-11-09 17:54:34 -05:00
{
2022-11-10 13:10:28 -05:00
return false;
2022-11-09 17:54:34 -05:00
}
2022-11-10 13:10:28 -05:00
const auto rawfile = game::DB_FindXAssetHeader(game::ASSET_TYPE_RAWFILE, path.data(), 0).rawfile;
const auto len = game::DB_GetRawFileLen(rawfile);
const auto rawfile_buffer = utils::memory::get_allocator()->allocate_array<char>(len);
const auto _1 = gsl::finally([&]
{
utils::memory::get_allocator()->free(rawfile_buffer);
});
2022-11-09 17:54:34 -05:00
2022-11-10 13:10:28 -05:00
game::DB_GetRawBuffer(rawfile, rawfile_buffer, len);
*game::ui_num_arenas += game::GameInfo_ParseArenas(rawfile_buffer, MAX_ARENAS - *game::ui_num_arenas,
&game::ui_arena_infos[*game::ui_num_arenas]);
return true;
2022-11-09 17:54:34 -05:00
}
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)