]> git.localhorst.tv Git - blank.git/blobdiff - src/app/runtime.cpp
add TCP based CLI
[blank.git] / src / app / runtime.cpp
index a7beeb0f64c0e7e732809e69ccafaa2e56fc5868..75eeec55dd2c3e9c790ad2dfcd510a1c08396ff9 100644 (file)
@@ -5,12 +5,14 @@
 #include "init.hpp"
 #include "../client/MasterState.hpp"
 #include "../io/filesystem.hpp"
+#include "../io/TokenStreamReader.hpp"
 #include "../io/WorldSave.hpp"
 #include "../server/ServerState.hpp"
 #include "../standalone/MasterState.hpp"
 
 #include <cctype>
 #include <cstdlib>
+#include <ctime>
 #include <fstream>
 #include <iostream>
 #include <SDL.h>
@@ -47,6 +49,76 @@ string default_save_path() {
 
 namespace blank {
 
+void Config::Load(std::istream &is) {
+       TokenStreamReader in(is);
+       std::string name;
+       while (in.HasMore()) {
+               if (in.Peek().type == Token::STRING) {
+                       in.ReadString(name);
+               } else {
+                       in.ReadIdentifier(name);
+               }
+               in.Skip(Token::EQUALS);
+               if (name == "audio.enabled") {
+                       in.ReadBoolean(audio.enabled);
+               } else if (name == "input.keyboard") {
+                       in.ReadBoolean(input.keyboard);
+               } else if (name == "input.mouse") {
+                       in.ReadBoolean(input.mouse);
+               } else if (name == "input.pitch_sensitivity") {
+                       in.ReadNumber(input.pitch_sensitivity);
+               } else if (name == "input.yaw_sensitivity") {
+                       in.ReadNumber(input.yaw_sensitivity);
+               } else if (name == "net.host") {
+                       in.ReadString(net.host);
+               } else if (name == "net.port") {
+                       int port;
+                       in.ReadNumber(port);
+                       net.port = port;
+               } else if (name == "net.cmd_port") {
+                       int port;
+                       in.ReadNumber(port);
+                       net.cmd_port = port;
+               } else if (name == "player.name") {
+                       in.ReadString(player.name);
+               } else if (name == "video.dblbuf") {
+                       in.ReadBoolean(video.dblbuf);
+               } else if (name == "video.vsync") {
+                       in.ReadBoolean(video.vsync);
+               } else if (name == "video.msaa") {
+                       in.ReadNumber(video.msaa);
+               } else if (name == "video.hud") {
+                       in.ReadBoolean(video.hud);
+               } else if (name == "video.world") {
+                       in.ReadBoolean(video.world);
+               } else if (name == "video.debug") {
+                       in.ReadBoolean(video.debug);
+               }
+               if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
+                       in.Skip(Token::SEMICOLON);
+               }
+       }
+}
+
+void Config::Save(std::ostream &out) {
+       out << "audio.enabled = " << (audio.enabled ? "yes" : "no") << ';' << std::endl;
+       out << "input.keyboard = " << (input.keyboard ? "on" : "off") << ';' << std::endl;
+       out << "input.mouse = " << (input.keyboard ? "on" : "off") << ';' << std::endl;
+       out << "input.pitch_sensitivity = " << input.pitch_sensitivity << ';' << std::endl;
+       out << "input.yaw_sensitivity = " << input.yaw_sensitivity << ';' << std::endl;
+       out << "net.host = \"" << net.host << "\";" << std::endl;
+       out << "net.port = " << net.port << ';' << std::endl;
+       out << "net.cmd_port = " << net.cmd_port << ';' << std::endl;
+       out << "player.name = \"" << player.name << "\";" << std::endl;
+       out << "video.dblbuf = " << (video.dblbuf ? "on" : "off") << ';' << std::endl;
+       out << "video.vsync = " << (video.vsync ? "on" : "off") << ';' << std::endl;
+       out << "video.msaa = " << video.msaa << ';' << std::endl;
+       out << "video.hud = " << (video.hud ? "on" : "off") << ';' << std::endl;
+       out << "video.world = " << (video.world ? "on" : "off") << ';' << std::endl;
+       out << "video.debug = " << (video.debug ? "on" : "off") << ';' << std::endl;
+}
+
+
 HeadlessEnvironment::HeadlessEnvironment(const Config &config)
 : config(config)
 , loader(config.asset_path)
@@ -69,7 +141,8 @@ Environment::Environment(Window &win, const Config &config)
 , audio()
 , viewport()
 , window(win)
-, keymap() {
+, keymap()
+, msg_state(*this) {
        viewport.Clear();
        window.Flip();
        keymap.LoadDefault();
@@ -84,6 +157,12 @@ Environment::Environment(Window &win, const Config &config)
        }
 }
 
+void Environment::ShowMessage(const char *msg) {
+       cout << msg << endl;
+       msg_state.SetMessage(msg);
+       state.Push(&msg_state);
+}
+
 
 Runtime::Runtime() noexcept
 : name("blank")
@@ -96,6 +175,13 @@ Runtime::Runtime() noexcept
 }
 
 
+void Runtime::Initialize(int argc, const char *const *argv) {
+       ReadArgs(argc, argv);
+       if (mode == ERROR) return;
+       ReadPreferences();
+       ReadArgs(argc, argv);
+}
+
 void Runtime::ReadArgs(int argc, const char *const *argv) {
        if (argc <= 0) return;
        name = argv[0];
@@ -159,6 +245,14 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                                                } else {
                                                        config.game.net.port = strtoul(argv[i], nullptr, 10);
                                                }
+                                       } else if (strcmp(param, "cmd-port") == 0) {
+                                               ++i;
+                                               if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                       cerr << "missing argument to --cmd-port" << endl;
+                                                       error = true;
+                                               } else {
+                                                       config.game.net.cmd_port = strtoul(argv[i], nullptr, 10);
+                                               }
                                        } else if (strcmp(param, "player-name") == 0) {
                                                ++i;
                                                if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
@@ -251,9 +345,20 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
 
        if (error) {
                mode = ERROR;
-               return;
+       } else if (n > 0) {
+               if (t > 0) {
+                       mode = FIXED_FRAME_LIMIT;
+               } else {
+                       mode = FRAME_LIMIT;
+               }
+       } else if (t > 0) {
+               mode = TIME_LIMIT;
+       } else {
+               mode = NORMAL;
        }
+}
 
+void Runtime::ReadPreferences() {
        if (config.env.asset_path.empty()) {
                config.env.asset_path = default_asset_path();
        } else if (
@@ -271,16 +376,14 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                config.env.save_path += '/';
        }
 
-       if (n > 0) {
-               if (t > 0) {
-                       mode = FIXED_FRAME_LIMIT;
-               } else {
-                       mode = FRAME_LIMIT;
-               }
-       } else if (t > 0) {
-               mode = TIME_LIMIT;
+       string prefs_path = config.env.save_path + "prefs.conf";
+       if (is_file(prefs_path)) {
+               ifstream file(prefs_path);
+               config.game.Load(file);
        } else {
-               mode = NORMAL;
+               make_dirs(config.env.save_path);
+               ofstream file(prefs_path);
+               config.game.Save(file);
        }
 }