From e03d0eceac0134743213f0b2e9d5f1d1dd5fbea4 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Mon, 21 Feb 2022 17:37:27 -0500 Subject: [PATCH] Add ability to load console commands from a cfg file --- engine/core/include/console.hpp | 4 ++++ engine/core/src/console.cpp | 9 +++++++++ example/data/render_options.cfg | 1 + 3 files changed, 14 insertions(+) create mode 100644 example/data/render_options.cfg diff --git a/engine/core/include/console.hpp b/engine/core/include/console.hpp index b108b35..9f747be 100755 --- a/engine/core/include/console.hpp +++ b/engine/core/include/console.hpp @@ -6,6 +6,8 @@ #include #include +#include "path.hpp" + namespace prism::console { enum class argument_type { String, @@ -55,6 +57,8 @@ namespace prism::console { std::vector argument_types; }; + void load_cfg(const prism::path& path); + void register_command(std::string_view name, argument_format expected_format, function_ptr function); diff --git a/engine/core/src/console.cpp b/engine/core/src/console.cpp index 1403951..aaae156 100644 --- a/engine/core/src/console.cpp +++ b/engine/core/src/console.cpp @@ -6,6 +6,7 @@ #include "log.hpp" #include "string_utils.hpp" +#include "file.hpp" struct registered_command { registered_command(const prism::console::argument_format format, const prism::console::function_ptr function) : expected_format(format), function(function) {} @@ -29,6 +30,14 @@ struct RegisteredVariable { static std::unordered_map registered_variables; +void prism::console::load_cfg(const prism::path& path) { + auto file = prism::open_file(path); + auto stream = file->read_as_stream(); + for (std::string line; getline( stream, line );) { + prism::console::parse_and_invoke_command(line); + } +} + void prism::console::register_command(const std::string_view name, const prism::console::argument_format expected_format, const prism::console::function_ptr function) { registered_commands.try_emplace(name.data(), registered_command(expected_format, function)); } diff --git a/example/data/render_options.cfg b/example/data/render_options.cfg new file mode 100644 index 0000000..6c78d4d --- /dev/null +++ b/example/data/render_options.cfg @@ -0,0 +1 @@ +rs_aa false \ No newline at end of file