From 8c3a73c5d2e32e3140d6541599ea03816763a3b9 Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Wed, 22 Dec 2021 22:25:41 +0100 Subject: [PATCH] Cleanup + fixes --- src/client/component/gui.cpp | 28 ++++---- src/client/component/gui_asset_list.cpp | 22 +++--- src/client/component/gui_console.cpp | 17 +++-- src/client/component/gui_entity_list.cpp | 85 +++++++++++++----------- src/common/utils/string.cpp | 7 ++ src/common/utils/string.hpp | 2 + 6 files changed, 89 insertions(+), 72 deletions(-) diff --git a/src/client/component/gui.cpp b/src/client/component/gui.cpp index 48ba7b97..e45cd3c1 100644 --- a/src/client/component/gui.cpp +++ b/src/client/component/gui.cpp @@ -61,9 +61,9 @@ namespace gui void show_notifications() { - static auto window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoSavedSettings | - ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav | - ImGuiWindowFlags_NoMove; + static const auto window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoSavedSettings | + ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav | + ImGuiWindowFlags_NoMove; notifications.access([](std::vector& notifications_) { @@ -77,13 +77,8 @@ namespace gui continue; } - const auto title = i->title.size() <= 34 - ? i->title - : i->title.substr(0, 31) + "..."; - - const auto text = i->text.size() <= 34 - ? i->text - : i->text.substr(0, 31) + "..."; + const auto title = utils::string::truncate(i->title, 34, "..."); + const auto text = utils::string::truncate(i->text, 34, "..."); ImGui::SetNextWindowSizeConstraints(ImVec2(250, 50), ImVec2(250, 50)); ImGui::SetNextWindowBgAlpha(0.6f); @@ -101,6 +96,11 @@ namespace gui }); } + void menu_checkbox(const std::string& name, const std::string& menu) + { + ImGui::Checkbox(name.data(), &enabled_menus[menu]); + } + void gui_draw() { show_notifications(); @@ -117,9 +117,9 @@ namespace gui { if (ImGui::BeginMenu("Windows")) { - ImGui::Checkbox("Asset list", &enabled_menus["asset_list"]); - ImGui::Checkbox("Entity list", &enabled_menus["entity_list"]); - ImGui::Checkbox("Console", &enabled_menus["console"]); + menu_checkbox("Asset list", "asset_list"); + menu_checkbox("Entity list", "entity_list"); + menu_checkbox("Console", "console"); ImGui::EndMenu(); } @@ -177,7 +177,7 @@ namespace gui utils::hook::detour wnd_proc_hook; LRESULT wnd_proc_stub(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { - if (toggled && ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) + if (wParam != VK_ESCAPE && toggled && ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) { return TRUE; } diff --git a/src/client/component/gui_asset_list.cpp b/src/client/component/gui_asset_list.cpp index 7f77abe3..76cf0f72 100644 --- a/src/client/component/gui_asset_list.cpp +++ b/src/client/component/gui_asset_list.cpp @@ -16,7 +16,8 @@ namespace asset_list namespace { bool shown_assets[game::XAssetType::ASSET_TYPE_COUNT]; - std::string filter_buffer{}; + std::string asset_type_filter{}; + std::string assets_name_filter{}; void enum_assets(const game::XAssetType type, const std::function& callback, const bool includeOverride) { @@ -37,18 +38,21 @@ namespace asset_list { ImGui::Begin("Asset list", &gui::enabled_menus["asset_list"]); - ImGui::InputText("asset type", &filter_buffer); + ImGui::InputText("asset type", &asset_type_filter); + ImGui::BeginChild("asset type list"); + for (auto i = 0; i < game::XAssetType::ASSET_TYPE_COUNT; i++) { const auto name = game::g_assetNames[i]; const auto type = static_cast(i); - if (utils::string::find_lower(name, filter_buffer)) + if (utils::string::find_lower(name, asset_type_filter)) { ImGui::Checkbox(name, &shown_assets[type]); } } + ImGui::EndChild(); ImGui::End(); } @@ -65,25 +69,21 @@ namespace asset_list ImGui::SetNextWindowSizeConstraints(ImVec2(500, 500), ImVec2(1000, 1000)); ImGui::Begin(name, &shown_assets[type]); - static char filter[0x200]{}; - ImGui::InputText("asset name", filter, IM_ARRAYSIZE(filter)); + ImGui::InputText("asset name", &assets_name_filter); + ImGui::BeginChild("assets list"); enum_assets(type, [type](const game::XAssetHeader header) { const auto asset = game::XAsset{type, header}; const auto* const asset_name = game::DB_GetXAssetName(&asset); - if (!utils::string::find_lower(asset_name, filter)) - { - return; - } - - if (ImGui::Button(asset_name)) + if (utils::string::find_lower(asset_name, assets_name_filter) && ImGui::Button(asset_name)) { gui::copy_to_clipboard(asset_name); } }, true); + ImGui::EndChild(); ImGui::End(); } } diff --git a/src/client/component/gui_console.cpp b/src/client/component/gui_console.cpp index 5bf2bf7b..22163bd5 100644 --- a/src/client/component/gui_console.cpp +++ b/src/client/component/gui_console.cpp @@ -96,8 +96,8 @@ namespace gui_console { std::string text{}; - const auto history = game_console::get_output(); - for (const auto& line : history) + const auto output = game_console::get_output(); + for (const auto& line : output) { if (utils::string::find_lower(line, filter)) { @@ -116,17 +116,17 @@ namespace gui_console void on_frame() { - auto* open = &gui::enabled_menus["console"]; - if (!*open) + if (!gui::enabled_menus["console"]) { return; } - auto filtered_text = get_filtered_text(); - static auto input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | - ImGuiInputTextFlags_CallbackHistory; + const auto filtered_text = get_filtered_text(); + const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); + static const auto input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | + ImGuiInputTextFlags_CallbackHistory; - ImGui::Begin("Console", open); + ImGui::Begin("Console", &gui::enabled_menus["console"]); if (ImGui::BeginPopup("Options")) { @@ -157,7 +157,6 @@ namespace gui_console ImGui::SameLine(); ImGui::InputText("Filter", &filter); - const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); ImGui::BeginChild("console_scroll", ImVec2(0, -footer_height_to_reserve), false); ImGui::Text(filtered_text.data(), ImVec2(-1, -1)); diff --git a/src/client/component/gui_entity_list.cpp b/src/client/component/gui_entity_list.cpp index f1cae700..dcd5501b 100644 --- a/src/client/component/gui_entity_list.cpp +++ b/src/client/component/gui_entity_list.cpp @@ -19,8 +19,6 @@ namespace entity_list { namespace { - game::dvar_t* sv_running = nullptr; - enum entity_type { type_any, @@ -282,17 +280,13 @@ namespace entity_list utils::concurrency::container data_; unsigned int selected_entity{}; bool set_field_window{}; + bool selected_fields_window{}; int selected_type = game::SCRIPT_INTEGER; - bool is_sv_running() - { - if (!sv_running) - { - return false; - } - - return sv_running->current.enabled; - } + std::string field_filter{}; + std::string field_name_buffer{}; + std::string field_value_buffer{}; + std::string vector_input[3]{}; bool verify_entity(unsigned int id) { @@ -597,13 +591,6 @@ namespace entity_list void show_set_field_window(data_t& data) { - static char name[0x100]{}; - static char value[0x100]{}; - - static char x[0x100]{}; - static char y[0x100]{}; - static char z[0x100]{}; - ImGui::SetNextWindowSizeConstraints(ImVec2(300, 300), ImVec2(300, 300)); ImGui::Begin("Set entity field", &set_field_window); ImGui::SetWindowSize(ImVec2(300, 300)); @@ -619,16 +606,16 @@ namespace entity_list ImGui::TreePop(); } - ImGui::InputText("name", name, IM_ARRAYSIZE(name)); + ImGui::InputText("name", &field_name_buffer); if (selected_type == game::SCRIPT_VECTOR) { - ImGui::InputText("x", x, IM_ARRAYSIZE(x)); - ImGui::InputText("y", y, IM_ARRAYSIZE(y)); - ImGui::InputText("z", z, IM_ARRAYSIZE(z)); + ImGui::InputText("x", &vector_input[0]); + ImGui::InputText("y", &vector_input[1]); + ImGui::InputText("z", &vector_input[2]); } else { - ImGui::InputText("value", value, IM_ARRAYSIZE(value)); + ImGui::InputText("value", &field_value_buffer); } if (ImGui::Button("set", ImVec2(300, 0))) @@ -637,16 +624,18 @@ namespace entity_list { const scripting::vector vector { - static_cast(atof(x)), - static_cast(atof(y)), - static_cast(atof(z)) + static_cast(atof(vector_input[0].data())), + static_cast(atof(vector_input[1].data())), + static_cast(atof(vector_input[2].data())) }; - set_entity_field_vector(data, selected_entity, name, vector); + set_entity_field_vector(data, selected_entity, + field_name_buffer, vector); } else { - set_entity_field(data, selected_entity, name, value, selected_type); + set_entity_field(data, selected_entity, field_name_buffer, + field_value_buffer, selected_type); } } @@ -664,6 +653,7 @@ namespace entity_list } ImGui::Checkbox("Auto update", &data.auto_update); + ImGui::Checkbox("Field list", &selected_fields_window); auto interval = static_cast(data.interval.count()); if (data.auto_update && ImGui::SliderInt("Interval", &interval, 0, 1000 * 30)) @@ -748,11 +738,23 @@ namespace entity_list } ImGui::Separator(); + ImGui::BeginChild("entity list"); for (const auto& info : data.entity_info) { if (ImGui::TreeNode(utils::string::va("Entity num %i id %i", info.num, info.id))) { + ImGui::Text("Info"); + + const auto num_str = utils::string::va("%i", info.num); + + ImGui::Text("Entity num"); + ImGui::SameLine(); + if (ImGui::Button(num_str)) + { + gui::copy_to_clipboard(num_str); + } + ImGui::Text("Commands"); if (ImGui::Button("Set field")) @@ -805,22 +807,28 @@ namespace entity_list } } + ImGui::EndChild(); ImGui::End(); + } + void show_selected_fields_window(data_t& data) + { ImGui::SetNextWindowSizeConstraints(ImVec2(500, 500), ImVec2(1000, 1000)); - ImGui::Begin("Selected fields"); + ImGui::Begin("Selected fields", &selected_fields_window); + + ImGui::InputText("field name", &field_filter); + ImGui::BeginChild("field list"); - static char field_filter[0x100]{}; - ImGui::InputText("field name", field_filter, IM_ARRAYSIZE(field_filter)); for (auto& field : data.selected_fields) { - if (utils::string::find_lower(field.first, field_filter) && + if (utils::string::find_lower(field.first, field_filter) && ImGui::Checkbox(field.first.data(), &field.second)) { data.force_update = true; } } + ImGui::EndChild(); ImGui::End(); } @@ -833,7 +841,7 @@ namespace entity_list data_.access([](data_t& data) { - if (!is_sv_running()) + if (!game::CL_IsCgameInitialized()) { selected_entity = 0; data.entity_info = {}; @@ -841,6 +849,12 @@ namespace entity_list } show_entity_list_window(data); + + if (selected_fields_window) + { + show_selected_fields_window(data); + } + if (selected_entity && set_field_window) { show_set_field_window(data); @@ -854,11 +868,6 @@ namespace entity_list public: void post_unpack() override { - scheduler::on_game_initialized([]() - { - sv_running = game::Dvar_FindVar("sv_running"); - }); - gui::on_frame(on_frame); scheduler::loop(update_entity_list, scheduler::pipeline::server, 0ms); } diff --git a/src/common/utils/string.cpp b/src/common/utils/string.cpp index 546433db..f47703ce 100644 --- a/src/common/utils/string.cpp +++ b/src/common/utils/string.cpp @@ -197,4 +197,11 @@ namespace utils::string { return to_lower(a).find(to_lower(b)) != std::string::npos; } + + std::string truncate(const std::string& text, const size_t length, const std::string& end) + { + return text.size() <= length + ? text + : text.substr(0, length - end.size()) + end; + } } diff --git a/src/common/utils/string.hpp b/src/common/utils/string.hpp index 9d6afb16..89f0da65 100644 --- a/src/common/utils/string.hpp +++ b/src/common/utils/string.hpp @@ -100,4 +100,6 @@ namespace utils::string std::string replace(std::string str, const std::string& from, const std::string& to); bool find_lower(const std::string& a, const std::string& b); + + std::string truncate(const std::string& text, int length, const std::string& end); }