refactor(config): separate game and server configuration with explicit paths

This commit is contained in:
2026-07-10 20:47:25 +08:00
parent 176df4ee04
commit 9a510d2319
9 changed files with 35 additions and 19 deletions

1
.gitignore vendored
View File

@@ -41,6 +41,7 @@ CMakeError.log
*~ *~
.DS_Store .DS_Store
assets/config.toml assets/config.toml
assets/server-config.toml
.venv/ .venv/
pyout/ pyout/
vcpkg_installed/ vcpkg_installed/

View File

@@ -55,7 +55,7 @@ public:
AudioEngine& audio(); AudioEngine& audio();
private: private:
Config m_config; Config m_game_config;
Camera m_camera; Camera m_camera;
TextureManager m_texture_manager; TextureManager m_texture_manager;
NetworkServer m_server; NetworkServer m_server;

View File

@@ -5,7 +5,7 @@ namespace Cubed {
class Config { class Config {
public: public:
Config(); explicit Config(std::string_view path);
Config(const Config&) = delete; Config(const Config&) = delete;
Config(Config&&) = delete; Config(Config&&) = delete;
Config& operator=(const Config&) = delete; Config& operator=(const Config&) = delete;
@@ -54,8 +54,7 @@ public:
private: private:
toml::table m_tbl; toml::table m_tbl;
constexpr static inline std::string_view CONGIF_PATH = const std::string CONGIF_PATH;
ASSETS_PATH "config.toml";
const toml::node* find_node(const toml::table& root, const toml::node* find_node(const toml::table& root,
std::string_view path) const; std::string_view path) const;
// Follow the path to find the last-level toml::table, creating it if it // Follow the path to find the last-level toml::table, creating it if it

View File

@@ -1,4 +1,5 @@
#pragma once #pragma once
#include "Cubed/config.hpp"
#include "Cubed/gameplay/server_world.hpp" #include "Cubed/gameplay/server_world.hpp"
#include "Cubed/gameplay/session.hpp" #include "Cubed/gameplay/session.hpp"
@@ -8,17 +9,19 @@ namespace Cubed {
class NetworkServer { class NetworkServer {
public: public:
NetworkServer(int port = 25530); explicit NetworkServer();
~NetworkServer(); ~NetworkServer();
void stop(); void stop();
// Run in another thread after initialization is complete // Run in another thread after initialization is complete
void start_server(int port = 25530); void start_server(int port);
void start_server();
int port() const; int port() const;
ServerWorld& server_world(); ServerWorld& server_world();
private: private:
Config m_config;
asio::io_context m_io; asio::io_context m_io;
std::thread m_net_thread; std::thread m_net_thread;
int m_port = 25530; int m_port = 25530;

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "Cubed/config.hpp"
#include "Cubed/gameplay/cave_carver.hpp" #include "Cubed/gameplay/cave_carver.hpp"
#include "Cubed/gameplay/chunk_pos.hpp" #include "Cubed/gameplay/chunk_pos.hpp"
#include "Cubed/gameplay/game_time.hpp" #include "Cubed/gameplay/game_time.hpp"
@@ -25,7 +26,7 @@ class Session;
class ServerWorld { class ServerWorld {
public: public:
enum class ThreadPoolKind { NET, GEN }; enum class ThreadPoolKind { NET, GEN };
ServerWorld(); ServerWorld(Config& config);
~ServerWorld(); ~ServerWorld();
void stop(); void stop();
void handle_player_exit(const std::string& uuid); void handle_player_exit(const std::string& uuid);
@@ -118,6 +119,9 @@ private:
using uuid_acc = PlayerUUIDMap::accessor; using uuid_acc = PlayerUUIDMap::accessor;
using uuid_cacc = PlayerUUIDMap::const_accessor; using uuid_cacc = PlayerUUIDMap::const_accessor;
Config& m_config;
// key = uuid // key = uuid
PlayerHashMap m_players; PlayerHashMap m_players;
ChunkHashMap m_chunks; ChunkHashMap m_chunks;

View File

@@ -13,11 +13,13 @@
namespace Cubed { namespace Cubed {
App::App() App::App()
: m_texture_manager(m_config), m_audio(m_config),
m_client_world(m_audio, m_config), m_dev_panel(*this), : m_game_config(ASSETS_PATH "config.toml"),
m_texture_manager(m_game_config), m_audio(m_game_config),
m_client_world(m_audio, m_game_config), m_dev_panel(*this),
m_renderer(m_camera, m_client_world, m_texture_manager, m_dev_panel, m_renderer(m_camera, m_client_world, m_texture_manager, m_dev_panel,
m_config), m_game_config),
m_window(m_renderer, m_config) {} m_window(m_renderer, m_game_config) {}
App::~App() { App::~App() {
if (m_client) { if (m_client) {
@@ -362,7 +364,7 @@ void App::update() {
const auto& player = m_client_world.get_player(); const auto& player = m_client_world.get_player();
if (player_gait != player.get_gait()) { if (player_gait != player.get_gait()) {
player_gait = player.get_gait(); player_gait = player.get_gait();
float fov = m_config.get("player.fov", 70.0f); float fov = m_game_config.get("player.fov", 70.0f);
if (player_gait == Gait::WALK) { if (player_gait == Gait::WALK) {
m_renderer.update_fov(fov); m_renderer.update_fov(fov);
} }
@@ -406,7 +408,7 @@ TextureManager& App::texture_manager() { return m_texture_manager; }
Window& App::window() { return m_window; } Window& App::window() { return m_window; }
ClientWorld& App::client_world() { return m_client_world; } ClientWorld& App::client_world() { return m_client_world; }
ServerWorld& App::server_world() { return m_server.server_world(); } ServerWorld& App::server_world() { return m_server.server_world(); }
Config& App::config() { return m_config; } Config& App::config() { return m_game_config; }
const App::Argument& App::argument() const { return m_argument; } const App::Argument& App::argument() const { return m_argument; }
AudioEngine& App::audio() { return m_audio; } AudioEngine& App::audio() { return m_audio; }
} // namespace Cubed } // namespace Cubed

View File

@@ -10,7 +10,7 @@ using namespace std::string_view_literals;
namespace Cubed { namespace Cubed {
Config::Config() { load_config(); } Config::Config(std::string_view path) : CONGIF_PATH(path) { load_config(); }
Config::~Config() { save_to_file(); } Config::~Config() { save_to_file(); }

View File

@@ -4,7 +4,10 @@
using asio::ip::tcp; using asio::ip::tcp;
namespace Cubed { namespace Cubed {
NetworkServer::NetworkServer(int port) : m_port(port) {} NetworkServer::NetworkServer()
: m_config(ASSETS_PATH "server-config.toml"), m_world(m_config) {
m_port = m_config.get("port", 25530);
}
NetworkServer::~NetworkServer() { stop(); } NetworkServer::~NetworkServer() { stop(); }
@@ -84,6 +87,11 @@ void NetworkServer::net_run() {
void NetworkServer::start_server(int port) { void NetworkServer::start_server(int port) {
m_port = port; m_port = port;
m_config.set("port", m_port);
start_server();
}
void NetworkServer::start_server() {
m_world.init_world(); m_world.init_world();
net_run(); net_run();
m_started = true; m_started = true;

View File

@@ -13,7 +13,7 @@ using namespace std::chrono_literals;
using namespace google::protobuf; using namespace google::protobuf;
namespace Cubed { namespace Cubed {
ServerWorld::ServerWorld() {} ServerWorld::ServerWorld(Config& config) : m_config(config) {}
ServerWorld::~ServerWorld() { stop(); } ServerWorld::~ServerWorld() { stop(); }
@@ -500,8 +500,7 @@ bool ServerWorld::set_block(const glm::ivec3& block_pos, unsigned id) {
} }
void ServerWorld::hot_reload() { void ServerWorld::hot_reload() {
int dist = m_config.get("server_distance", 24);
int dist = 24;
m_rendering_distance = dist <= MAX_DISTANCE ? dist : MAX_DISTANCE; m_rendering_distance = dist <= MAX_DISTANCE ? dist : MAX_DISTANCE;
} }