Skip to content

Commit

Permalink
Add CVar dumper button
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed May 31, 2023
1 parent 70eeb57 commit 3d0733b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/mods/vr/CVarManager.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#define NOMINMAX

#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>

#include <utility/Config.hpp>
#include <utility/String.hpp>

#include <sdk/CVar.hpp>
#include <sdk/threading/GameThreadWorker.hpp>
#include <sdk/ConsoleManager.hpp>

#include "Framework.hpp"

Expand Down Expand Up @@ -69,6 +72,14 @@ void CVarManager::on_draw_ui() {

ImGui::TextWrapped("Frozen CVars: %i", frozen_cvars);

if (ImGui::Button("Dump All CVars")) {
GameThreadWorker::get().enqueue([this]() {
dump_commands();
});
}

ImGui::SameLine();

if (ImGui::Button("Clear Frozen CVars")) {
for (auto& cvar : m_all_cvars) {
cvar->unfreeze();
Expand Down Expand Up @@ -113,6 +124,66 @@ void CVarManager::on_config_load(const utility::Config& cfg, bool set_defaults)
// TODO: Add arbitrary cvars from the other configs the user can add.
}

void CVarManager::dump_commands() {
const auto console_manager = sdk::FConsoleManager::get();

if (console_manager == nullptr) {
return;
}

nlohmann::json json;

for (auto obj : console_manager->get_console_objects()) {
if (obj.value == nullptr || obj.key == nullptr || IsBadReadPtr(obj.key, sizeof(wchar_t))) {
continue;
}

auto& entry = json[utility::narrow(obj.key)];

entry["description"] = "";
//entry["address"] = (std::stringstream{} << std::hex << (uintptr_t)obj.value).str();
//entry["vtable"] = (std::stringstream{} << std::hex << *(uintptr_t*)obj.value).str();

bool is_command = false;

try {
is_command = obj.value->AsCommand() != nullptr;
if (is_command) {
entry["command"] = true;
} else {
entry["value"] = ((sdk::IConsoleVariable*)obj.value)->GetFloat();
}
} catch(...) {
SPDLOG_WARN("Failed to check if CVar is a command: {}", utility::narrow(obj.key));
}

const auto help_string = obj.value->GetHelp();

if (help_string != nullptr && !IsBadReadPtr(help_string, sizeof(wchar_t))) {
try {
SPDLOG_INFO("Found CVar: {} {}", utility::narrow(obj.key), utility::narrow(help_string));
entry["description"] = utility::narrow(help_string);
} catch(...) {

}
}

SPDLOG_INFO("Found CVar: {}", utility::narrow(obj.key));
}

const auto persistent_dir = g_framework->get_persistent_dir();

// Dump all CVars to a JSON file.
std::ofstream file(persistent_dir / "cvardump.json");

if (file.is_open()) {
file << json.dump(4);
file.close();

SPDLOG_INFO("Dumped CVars to {}", (persistent_dir / "cvardump.json").string());
}
}

std::string CVarManager::CVar::get_key_name() {
ZoneScopedN(__FUNCTION__);

Expand Down
2 changes: 2 additions & 0 deletions src/mods/vr/CVarManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class CVarManager final : public ModComponent {
void on_draw_ui() override;
void on_config_load(const utility::Config& cfg, bool set_defaults) override;

void dump_commands();

public:
class CVar : public std::enable_shared_from_this<CVar> {
public:
Expand Down

0 comments on commit 3d0733b

Please sign in to comment.