mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
feat: read network config from JSON
This commit is contained in:
@@ -80,7 +80,14 @@ struct Viewport {
|
||||
SDL_FRect dst{};
|
||||
};
|
||||
|
||||
struct NetworkConfig {
|
||||
int port = 12345;
|
||||
int maxPlayers = 2;
|
||||
std::string serverIP = "127.0.0.1";
|
||||
};
|
||||
|
||||
struct Config {
|
||||
WindowConfig window;
|
||||
RenderConfig render;
|
||||
NetworkConfig network;
|
||||
};
|
||||
@@ -29,6 +29,20 @@ static void loadRender(const json& j, RenderConfig& render) {
|
||||
get("logical_height", render.logicalHeight);
|
||||
}
|
||||
|
||||
static void loadNetwork(const json& j, NetworkConfig& network) {
|
||||
auto get = [&](const char* key, auto& out) {
|
||||
if (j.contains(key))
|
||||
j.at(key).get_to(out);
|
||||
else {
|
||||
std::cout << "Unkonw key " << key << "\n";
|
||||
}
|
||||
};
|
||||
get("port", network.port);
|
||||
get("max_players", network.maxPlayers);
|
||||
get("server_ip", network.serverIP);
|
||||
}
|
||||
|
||||
|
||||
bool ConfigLoader::load(const std::string& path, Config& config) {
|
||||
std::ifstream file(path);
|
||||
|
||||
@@ -53,7 +67,18 @@ bool ConfigLoader::load(const std::string& path, Config& config) {
|
||||
if (j.contains("render")) {
|
||||
loadRender(j["render"], config.render);
|
||||
}
|
||||
if (j.contains("network")) {
|
||||
loadNetwork(j["network"], config.network);
|
||||
}
|
||||
std::cout << "load json success!\n";
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
Config ConfigLoader::load(const std::string& path) {
|
||||
Config config;
|
||||
if (!load(path, config)) {
|
||||
std::cerr << "[ConfigLoader] Failed to load config from " << path << "\n";
|
||||
}
|
||||
return config;
|
||||
}
|
||||
@@ -6,6 +6,6 @@
|
||||
class ConfigLoader {
|
||||
public:
|
||||
static bool load(const std::string& path, Config& config);
|
||||
|
||||
static Config load(const std::string&path);
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user