mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(toml): add shared TOML utilities and handle ip.toml
This commit is contained in:
@@ -70,6 +70,7 @@ private:
|
|||||||
Argument m_argument;
|
Argument m_argument;
|
||||||
void init(int argc, char** argv);
|
void init(int argc, char** argv);
|
||||||
void handle_argument(int argc, char** argv);
|
void handle_argument(int argc, char** argv);
|
||||||
|
void handle_toml();
|
||||||
auto init_camera();
|
auto init_camera();
|
||||||
auto init_texture();
|
auto init_texture();
|
||||||
auto init_world();
|
auto init_world();
|
||||||
|
|||||||
@@ -1,17 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Cubed/tools/cubed_assert.hpp"
|
#include "Cubed/tools/cubed_assert.hpp"
|
||||||
|
#include "Cubed/tools/toml.utils.hpp"
|
||||||
#include <toml++/toml.hpp>
|
|
||||||
|
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
concept TomlValueType =
|
|
||||||
std::same_as<T, int> || std::same_as<T, bool> || std::same_as<T, double> ||
|
|
||||||
std::same_as<T, const char*> || std::same_as<T, toml::date> ||
|
|
||||||
std::same_as<T, toml::time> || std::same_as<T, toml::date_time> ||
|
|
||||||
std::same_as<T, std::string>;
|
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
public:
|
public:
|
||||||
Config();
|
Config();
|
||||||
@@ -24,7 +16,7 @@ public:
|
|||||||
void load_or_create_config();
|
void load_or_create_config();
|
||||||
void save_to_file();
|
void save_to_file();
|
||||||
|
|
||||||
template <TomlValueType T> T get(std::string_view key) const {
|
template <TOML::TomlValueType T> T get(std::string_view key) const {
|
||||||
size_t cur = 0;
|
size_t cur = 0;
|
||||||
auto pos = key.find('.');
|
auto pos = key.find('.');
|
||||||
const toml::table* table = &m_tbl;
|
const toml::table* table = &m_tbl;
|
||||||
@@ -61,7 +53,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
template <typename T> void set(std::string_view key, T&& val) {
|
template <typename T> void set(std::string_view key, T&& val) {
|
||||||
if constexpr (!TomlValueType<std::decay_t<T>>) {
|
if constexpr (!TOML::TomlValueType<std::decay_t<T>>) {
|
||||||
static_assert(false, "Type Not Support");
|
static_assert(false, "Type Not Support");
|
||||||
}
|
}
|
||||||
size_t cur = 0;
|
size_t cur = 0;
|
||||||
|
|||||||
41
include/Cubed/tools/toml.utils.hpp
Normal file
41
include/Cubed/tools/toml.utils.hpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Cubed/tools/log.hpp"
|
||||||
|
|
||||||
|
#include <toml++/toml.hpp>
|
||||||
|
namespace Cubed {
|
||||||
|
namespace TOML {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
concept TomlValueType =
|
||||||
|
std::same_as<std::decay_t<T>, int> || std::same_as<std::decay_t<T>, bool> ||
|
||||||
|
std::same_as<std::decay_t<T>, double> ||
|
||||||
|
std::same_as<std::decay_t<T>, char> ||
|
||||||
|
std::same_as<std::decay_t<T>, toml::date> ||
|
||||||
|
std::same_as<std::decay_t<T>, toml::time> ||
|
||||||
|
std::same_as<std::decay_t<T>, toml::date_time> ||
|
||||||
|
std::same_as<std::decay_t<T>, std::string>;
|
||||||
|
|
||||||
|
template <TomlValueType T>
|
||||||
|
std::optional<T> safe_get_value(const toml::table& table, std::string_view key,
|
||||||
|
const T& default_value) {
|
||||||
|
auto value = table[key].value<T>();
|
||||||
|
if (value == std::nullopt) {
|
||||||
|
Logger::warn("Key {} Is Not Find, Wiil Set the Default Value {}", key,
|
||||||
|
default_value);
|
||||||
|
value = default_value;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
template <typename U>
|
||||||
|
requires std::convertible_to<U, std::string>
|
||||||
|
std::optional<std::string> safe_get_value(const toml::table& table,
|
||||||
|
std::string_view key,
|
||||||
|
U&& default_value) {
|
||||||
|
return safe_get_value<std::string>(
|
||||||
|
table, key, std::string(std::forward<U>(default_value)));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace TOML
|
||||||
|
|
||||||
|
} // namespace Cubed
|
||||||
16
src/app.cpp
16
src/app.cpp
@@ -34,7 +34,9 @@ void App::cursor_position_callback(GLFWwindow* window, double xpos,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
void App::init(int argc, char** argv) {
|
void App::init(int argc, char** argv) {
|
||||||
|
handle_toml();
|
||||||
handle_argument(argc, argv);
|
handle_argument(argc, argv);
|
||||||
|
|
||||||
m_window.init();
|
m_window.init();
|
||||||
m_window.imgui_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,
|
void App::key_callback(GLFWwindow* window, int key, int scancode, int action,
|
||||||
int mods) {
|
int mods) {
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|||||||
@@ -1,31 +1,18 @@
|
|||||||
#include "Cubed/gameplay/block.hpp"
|
#include "Cubed/gameplay/block.hpp"
|
||||||
|
|
||||||
#include "Cubed/config.hpp"
|
|
||||||
#include "Cubed/tools/cubed_assert.hpp"
|
#include "Cubed/tools/cubed_assert.hpp"
|
||||||
#include "Cubed/tools/log.hpp"
|
#include "Cubed/tools/log.hpp"
|
||||||
|
#include "Cubed/tools/toml.utils.hpp"
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <toml++/toml.hpp>
|
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
using namespace std::string_literals;
|
using namespace std::string_literals;
|
||||||
|
using namespace Cubed::TOML;
|
||||||
namespace {
|
namespace {
|
||||||
std::string block_data_dir = ASSETS_PATH + "data/block"s;
|
std::string block_data_dir = ASSETS_PATH + "data/block"s;
|
||||||
|
|
||||||
template <Cubed::TomlValueType T>
|
|
||||||
std::optional<T> safe_get_value(const toml::table& table, std::string_view key,
|
|
||||||
const T& default_value) {
|
|
||||||
auto value = table[key].value<T>();
|
|
||||||
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
|
||||||
|
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
|
|||||||
Reference in New Issue
Block a user