mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(toml): add shared TOML utilities and handle ip.toml
This commit is contained in:
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) {
|
||||
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();
|
||||
|
||||
@@ -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 <filesystem>
|
||||
#include <toml++/toml.hpp>
|
||||
|
||||
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 <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 Cubed {
|
||||
|
||||
Reference in New Issue
Block a user