h2-mod/src/client/component/mapents.cpp

97 lines
2.1 KiB
C++
Raw Normal View History

2022-06-28 06:06:04 -04:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "game/dvars.hpp"
#include "fastfiles.hpp"
#include "console.hpp"
2022-06-28 20:06:10 -04:00
#include "command.hpp"
#include "game/scripting/functions.hpp"
2022-06-28 06:06:04 -04:00
2022-09-01 13:37:27 -04:00
#include <xsk/gsc/types.hpp>
#include <xsk/resolver.hpp>
2022-06-28 06:06:04 -04:00
#include <utils/hook.hpp>
#include <utils/string.hpp>
#include <utils/io.hpp>
namespace mapents
{
namespace
{
2022-07-31 17:54:39 -04:00
std::string replace_mapents_keys(const std::string& data)
{
std::string buffer{};
const auto lines = utils::string::split(data, '\n');
for (const auto& line : lines)
{
const auto _0 = gsl::finally([&]
{
buffer.append("\n");
});
if (line.starts_with("{") || line.starts_with("}"))
{
buffer.append(line);
continue;
}
const auto first_space = line.find_first_of(' ');
if (first_space == std::string::npos)
{
continue;
}
const auto id = static_cast<unsigned int>(std::atoi(line.substr(0, first_space).data()));
2022-11-05 14:35:16 -04:00
if (id == 0) // 0 "key" "value"
{
buffer.append(line);
continue;
}
2022-09-01 13:37:27 -04:00
const auto token = xsk::gsc::h2::resolver::token_name(static_cast<std::uint16_t>(id));
const auto key = "\"" + token + "\"";
2022-07-31 17:54:39 -04:00
const auto new_line = key + line.substr(first_space);
buffer.append(new_line);
}
return buffer;
}
2022-06-28 06:06:04 -04:00
}
class component final : public component_interface
{
public:
void post_unpack() override
{
2022-06-28 20:06:10 -04:00
command::add("dumpMapEnts", []()
{
if (!game::SV_Loaded())
{
console::info("Not in game\n");
return;
}
2022-09-20 13:26:23 -04:00
fastfiles::enum_assets(game::ASSET_TYPE_MAP_ENTS, [](game::XAssetHeader header)
2022-06-28 20:06:10 -04:00
{
2022-09-20 13:26:23 -04:00
if (header.mapents == nullptr)
{
console::info("Failed to dump mapents\n");
return;
}
2022-06-28 20:06:10 -04:00
2022-09-20 13:26:23 -04:00
const auto dest = utils::string::va("dumps/%s.ents", header.mapents->name);
const auto str = std::string(header.mapents->entityString, header.mapents->numEntityChars);
const auto data = replace_mapents_keys(str);
utils::io::write_file(dest, data, false);
console::info("Mapents dumped to %s\n", dest);
}, true);
2022-06-28 20:06:10 -04:00
});
2022-06-28 06:06:04 -04:00
}
};
}
REGISTER_COMPONENT(mapents::component)