Add assetlist function

This commit is contained in:
Federico Cecchetto 2022-01-02 23:51:38 +01:00
parent 51d33687ba
commit 567c2b5f4e
5 changed files with 45 additions and 20 deletions

View File

@ -7,6 +7,7 @@
#include "command.hpp"
#include "game_console.hpp"
#include "chat.hpp"
#include "fastfiles.hpp"
#include <utils/hook.hpp>
#include <utils/string.hpp>
@ -31,15 +32,6 @@ namespace command
}
}
void enum_assets(const game::XAssetType type, const std::function<void(game::XAssetHeader)>& callback, const bool includeOverride)
{
game::DB_EnumXAssets_Internal(type, static_cast<void(*)(game::XAssetHeader, void*)>([](game::XAssetHeader header, void* data)
{
const auto& cb = *static_cast<const std::function<void(game::XAssetHeader)>*>(data);
cb(header);
}), &callback, includeOverride);
}
game::dvar_t* dvar_command_stub()
{
const params args;
@ -200,7 +192,7 @@ namespace command
game_console::print(game_console::con_type_info, "Listing assets in pool %s\n", game::g_assetNames[type]);
enum_assets(type, [type](const game::XAssetHeader header)
fastfiles::enum_assets(type, [type](const game::XAssetHeader header)
{
const auto asset = game::XAsset{type, header};
const auto* const asset_name = game::DB_GetXAssetName(&asset);

View File

@ -27,6 +27,15 @@ namespace fastfiles
}
}
void enum_assets(const game::XAssetType type, const std::function<void(game::XAssetHeader)>& callback, const bool includeOverride)
{
game::DB_EnumXAssets_Internal(type, static_cast<void(*)(game::XAssetHeader, void*)>([](game::XAssetHeader header, void* data)
{
const auto& cb = *static_cast<const std::function<void(game::XAssetHeader)>*>(data);
cb(header);
}), &callback, includeOverride);
}
std::string get_current_fastfile()
{
std::string fastfile_copy;

View File

@ -4,5 +4,6 @@
namespace fastfiles
{
void enum_assets(const game::XAssetType type, const std::function<void(game::XAssetHeader)>& callback, const bool includeOverride);
std::string get_current_fastfile();
}

View File

@ -7,6 +7,7 @@
#include "scheduler.hpp"
#include "command.hpp"
#include "gui.hpp"
#include "fastfiles.hpp"
#include <utils/string.hpp>
#include <utils/hook.hpp>
@ -19,15 +20,6 @@ namespace asset_list
std::string asset_type_filter;
std::string assets_name_filter;
void enum_assets(const game::XAssetType type, const std::function<void(game::XAssetHeader)>& callback, const bool includeOverride)
{
game::DB_EnumXAssets_Internal(type, static_cast<void(*)(game::XAssetHeader, void*)>([](game::XAssetHeader header, void* data)
{
const auto& cb = *static_cast<const std::function<void(game::XAssetHeader)>*>(data);
cb(header);
}), &callback, includeOverride);
}
void on_frame()
{
if (!gui::enabled_menus["asset_list"])
@ -72,7 +64,7 @@ namespace asset_list
ImGui::InputText("asset name", &assets_name_filter);
ImGui::BeginChild("assets list");
enum_assets(type, [type](const game::XAssetHeader header)
fastfiles::enum_assets(type, [type](const game::XAssetHeader header)
{
const auto asset = game::XAsset{type, header};
const auto* const asset_name = game::DB_GetXAssetName(&asset);

View File

@ -10,6 +10,7 @@
#include "../../../component/scripting.hpp"
#include "../../../component/command.hpp"
#include "../../../component/chat.hpp"
#include "../../../component/fastfiles.hpp"
#include <utils/string.hpp>
#include <utils/io.hpp>
@ -499,6 +500,36 @@ namespace scripting::lua
{
notifies::add_entity_damage_callback(callback);
};
game_type["assetlist"] = [](const game&, const sol::this_state s, const std::string& type_string)
{
auto table = sol::table::create(s.lua_state());
auto index = 1;
auto type_index = -1;
for (auto i = 0; i < ::game::XAssetType::ASSET_TYPE_COUNT; i++)
{
if (type_string == ::game::g_assetNames[i])
{
type_index = i;
}
}
if (type_index == -1)
{
throw std::runtime_error("Asset type does not exist");
}
const auto type = static_cast<::game::XAssetType>(type_index);
fastfiles::enum_assets(type, [type, &table, &index](const ::game::XAssetHeader header)
{
const auto asset = ::game::XAsset{type, header};
const std::string asset_name = ::game::DB_GetXAssetName(&asset);
table[index++] = asset_name;
}, true);
return table;
};
}
}