From 567c2b5f4e7de6b38883910ec9372f9f1428b664 Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Sun, 2 Jan 2022 23:51:38 +0100 Subject: [PATCH] Add assetlist function --- src/client/component/command.cpp | 12 ++------- src/client/component/fastfiles.cpp | 9 +++++++ src/client/component/fastfiles.hpp | 1 + src/client/component/gui_asset_list.cpp | 12 ++------- src/client/game/scripting/lua/context.cpp | 31 +++++++++++++++++++++++ 5 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/client/component/command.cpp b/src/client/component/command.cpp index 4c16bb73..80d84330 100644 --- a/src/client/component/command.cpp +++ b/src/client/component/command.cpp @@ -7,6 +7,7 @@ #include "command.hpp" #include "game_console.hpp" #include "chat.hpp" +#include "fastfiles.hpp" #include #include @@ -31,15 +32,6 @@ namespace command } } - void enum_assets(const game::XAssetType type, const std::function& callback, const bool includeOverride) - { - game::DB_EnumXAssets_Internal(type, static_cast([](game::XAssetHeader header, void* data) - { - const auto& cb = *static_cast*>(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); diff --git a/src/client/component/fastfiles.cpp b/src/client/component/fastfiles.cpp index cded2397..3b71d881 100644 --- a/src/client/component/fastfiles.cpp +++ b/src/client/component/fastfiles.cpp @@ -27,6 +27,15 @@ namespace fastfiles } } + void enum_assets(const game::XAssetType type, const std::function& callback, const bool includeOverride) + { + game::DB_EnumXAssets_Internal(type, static_cast([](game::XAssetHeader header, void* data) + { + const auto& cb = *static_cast*>(data); + cb(header); + }), &callback, includeOverride); + } + std::string get_current_fastfile() { std::string fastfile_copy; diff --git a/src/client/component/fastfiles.hpp b/src/client/component/fastfiles.hpp index ac26d2ec..09e4767b 100644 --- a/src/client/component/fastfiles.hpp +++ b/src/client/component/fastfiles.hpp @@ -4,5 +4,6 @@ namespace fastfiles { + void enum_assets(const game::XAssetType type, const std::function& callback, const bool includeOverride); std::string get_current_fastfile(); } diff --git a/src/client/component/gui_asset_list.cpp b/src/client/component/gui_asset_list.cpp index f85333b8..122a89a3 100644 --- a/src/client/component/gui_asset_list.cpp +++ b/src/client/component/gui_asset_list.cpp @@ -7,6 +7,7 @@ #include "scheduler.hpp" #include "command.hpp" #include "gui.hpp" +#include "fastfiles.hpp" #include #include @@ -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& callback, const bool includeOverride) - { - game::DB_EnumXAssets_Internal(type, static_cast([](game::XAssetHeader header, void* data) - { - const auto& cb = *static_cast*>(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); diff --git a/src/client/game/scripting/lua/context.cpp b/src/client/game/scripting/lua/context.cpp index 3b670fd9..775b73db 100644 --- a/src/client/game/scripting/lua/context.cpp +++ b/src/client/game/scripting/lua/context.cpp @@ -10,6 +10,7 @@ #include "../../../component/scripting.hpp" #include "../../../component/command.hpp" #include "../../../component/chat.hpp" +#include "../../../component/fastfiles.hpp" #include #include @@ -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; + }; } }