Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to execute console commands from file #96

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion src/mods/vr/CVarManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

constexpr std::string_view cvars_standard_txt_name = "cvars_standard.txt";
constexpr std::string_view cvars_data_txt_name = "cvars_data.txt";
constexpr std::string_view user_script_txt_name = "user_script.txt";

CVarManager::CVarManager() {
ZoneScopedN(__FUNCTION__);
Expand Down Expand Up @@ -85,6 +86,11 @@ void CVarManager::on_pre_engine_tick(sdk::UGameEngine* engine, float delta) {
cvar->update();
cvar->freeze();
}

if(m_should_execute_console_script) {
execute_console_script(engine, user_script_txt_name.data());
m_should_execute_console_script = false;
}
}

void CVarManager::on_draw_ui() {
Expand Down Expand Up @@ -168,6 +174,9 @@ void CVarManager::on_config_load(const utility::Config& cfg, bool set_defaults)
}

// TODO: Add arbitrary cvars from the other configs the user can add.

// calling UEngine::exec here causes a crash, defer to on_pre_engine_tick()
m_should_execute_console_script = true;
}

void CVarManager::dump_commands() {
Expand Down Expand Up @@ -758,4 +767,66 @@ void CVarManager::CVarData::draw_ui() try {
}
} catch (...) {
ImGui::TextWrapped("Failed to read cvar data: %s", utility::narrow(m_name).c_str());
}
}

static inline void trim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));

s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}

void CVarManager::execute_console_script(sdk::UGameEngine* engine, const std::string& filename) {
ZoneScopedN(__FUNCTION__);

if(!engine) {
spdlog::error("[execute_console_script] engine is null");
return;
}

spdlog::info("[execute_console_script] Loading {}...", filename);

const auto cscript_txt = Framework::get_persistent_dir(filename);

if (!std::filesystem::exists(cscript_txt)) {
return;
}

std::ifstream cscript_file(utility::widen(cscript_txt.string()));

if (!cscript_file) {
spdlog::error("[execute_console_script] Failed to open file {}...", filename);
return;
}

for (std::string line{}; getline(cscript_file, line); ) {
trim(line);

// handle comments
if(line.starts_with('#') || line.starts_with(';')) {
continue;
}

if(line.contains('#')) {
line = line.substr(0, line.find_first_of('#'));
trim(line);
}

if(line.contains(';')) {
line = line.substr(0, line.find_first_of(';'));
trim(line);
}

if(line.length() == 0) {
continue;
}

spdlog::debug("[execute_console_script] Attempting to execute \"{}\"", line);
engine->exec(utility::widen(line));
}

spdlog::debug("[execute_console_script] done");
}
3 changes: 3 additions & 0 deletions src/mods/vr/CVarManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class CVarManager final : public ModComponent {
void dump_commands();
void spawn_console();

void execute_console_script(sdk::UGameEngine* engine, const std::string& filename);

bool is_hzbo_frozen_and_enabled() const {
if (m_hzbo == nullptr) {
return false;
Expand Down Expand Up @@ -194,6 +196,7 @@ class CVarManager final : public ModComponent {

bool m_wants_display_console{false};
bool m_native_console_spawned{false};
bool m_should_execute_console_script{false};

static inline std::vector<std::shared_ptr<CVarStandard>> s_default_standard_cvars {
// Bools
Expand Down