diff --git a/include/Cubed/app.hpp b/include/Cubed/app.hpp index 74b5d67..7ffb49e 100644 --- a/include/Cubed/app.hpp +++ b/include/Cubed/app.hpp @@ -70,6 +70,7 @@ private: Argument m_argument; void init(int argc, char** argv); void handle_argument(int argc, char** argv); + void handle_toml(); auto init_camera(); auto init_texture(); auto init_world(); diff --git a/include/Cubed/config.hpp b/include/Cubed/config.hpp index 48f92e3..113dc2b 100644 --- a/include/Cubed/config.hpp +++ b/include/Cubed/config.hpp @@ -1,17 +1,9 @@ #pragma once #include "Cubed/tools/cubed_assert.hpp" - -#include +#include "Cubed/tools/toml.utils.hpp" namespace Cubed { -template -concept TomlValueType = - std::same_as || std::same_as || std::same_as || - std::same_as || std::same_as || - std::same_as || std::same_as || - std::same_as; - class Config { public: Config(); @@ -24,7 +16,7 @@ public: void load_or_create_config(); void save_to_file(); - template T get(std::string_view key) const { + template T get(std::string_view key) const { size_t cur = 0; auto pos = key.find('.'); const toml::table* table = &m_tbl; @@ -61,7 +53,7 @@ public: } } template void set(std::string_view key, T&& val) { - if constexpr (!TomlValueType>) { + if constexpr (!TOML::TomlValueType>) { static_assert(false, "Type Not Support"); } size_t cur = 0; diff --git a/include/Cubed/tools/toml.utils.hpp b/include/Cubed/tools/toml.utils.hpp new file mode 100644 index 0000000..cfec703 --- /dev/null +++ b/include/Cubed/tools/toml.utils.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "Cubed/tools/log.hpp" + +#include +namespace Cubed { +namespace TOML { + +template +concept TomlValueType = + std::same_as, int> || std::same_as, bool> || + std::same_as, double> || + std::same_as, char> || + std::same_as, toml::date> || + std::same_as, toml::time> || + std::same_as, toml::date_time> || + std::same_as, std::string>; + +template +std::optional safe_get_value(const toml::table& table, std::string_view key, + const T& default_value) { + auto value = table[key].value(); + if (value == std::nullopt) { + Logger::warn("Key {} Is Not Find, Wiil Set the Default Value {}", key, + default_value); + value = default_value; + } + return value; +} +template + requires std::convertible_to +std::optional safe_get_value(const toml::table& table, + std::string_view key, + U&& default_value) { + return safe_get_value( + table, key, std::string(std::forward(default_value))); +} + +} // namespace TOML + +} // namespace Cubed diff --git a/src/app.cpp b/src/app.cpp index a706452..a825173 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -34,7 +34,9 @@ void App::cursor_position_callback(GLFWwindow* window, double xpos, } } void App::init(int argc, char** argv) { + handle_toml(); handle_argument(argc, argv); + m_window.init(); m_window.imgui_init(); @@ -127,6 +129,20 @@ void App::handle_argument(int argc, char** argv) { } } +void App::handle_toml() { + toml::table ip; + try { + ip = toml::parse_file("ip.toml"); + } catch (const toml::parse_error& e) { + Logger::warn("Ip toml parse error {}", e.what()); + return; + } + + m_argument.ip = *TOML::safe_get_value(ip, "ip", "127.0.01"); + m_argument.port = *TOML::safe_get_value(ip, "port", 25530); + m_argument.is_client = *TOML::safe_get_value(ip, "client", false); +} + void App::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { ImGuiIO& io = ImGui::GetIO(); diff --git a/src/gameplay/block.cpp b/src/gameplay/block.cpp index 9be8c94..4f4d80b 100644 --- a/src/gameplay/block.cpp +++ b/src/gameplay/block.cpp @@ -1,31 +1,18 @@ #include "Cubed/gameplay/block.hpp" -#include "Cubed/config.hpp" #include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/log.hpp" +#include "Cubed/tools/toml.utils.hpp" #include -#include namespace fs = std::filesystem; using namespace std::string_literals; - +using namespace Cubed::TOML; namespace { std::string block_data_dir = ASSETS_PATH + "data/block"s; -template -std::optional safe_get_value(const toml::table& table, std::string_view key, - const T& default_value) { - auto value = table[key].value(); - if (value == std::nullopt) { - Cubed::Logger::warn("Key {} Is Not Find, Wiil Set the Default Value {}", - key, default_value); - value = default_value; - } - return value; -} - } // namespace namespace Cubed {