X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2Fruntime.cpp;h=f51a0cd620d48921fa01c9bd1b53070a0a5ed925;hb=ca74cd8cdaf25b5ae778bef1dbabad21cea13b2d;hp=5dce72159a439203843581f84cb14fc86fdda08a;hpb=34a7fafb4c2d4ed95eb990eda5c02d5c79d3411b;p=blank.git diff --git a/src/app/runtime.cpp b/src/app/runtime.cpp index 5dce721..f51a0cd 100644 --- a/src/app/runtime.cpp +++ b/src/app/runtime.cpp @@ -1,13 +1,19 @@ #include "Application.hpp" #include "Environment.hpp" -#include "PreloadState.hpp" #include "Runtime.hpp" -#include "WorldState.hpp" #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 #include +#include +#include #include #include @@ -43,19 +49,125 @@ string default_save_path() { namespace blank { -Environment::Environment(Window &win, const string &asset_path) -: audio() +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) +, counter() +, state() { + +} + +string HeadlessEnvironment::Config::GetWorldPath(const string &world_name) const { + return save_path + "worlds/" + world_name + '/'; +} + +string HeadlessEnvironment::Config::GetWorldPath(const string &world_name, const string &host_name) const { + return save_path + "cache/" + host_name + '/' + world_name + '/'; +} + +Environment::Environment(Window &win, const Config &config) +: HeadlessEnvironment(config) +, assets(loader) +, audio() , viewport() , window(win) -, assets(asset_path) -, counter() { +, keymap() +, msg_state(*this) { + viewport.Clear(); + window.Flip(); + keymap.LoadDefault(); + + string keys_path = config.save_path + "keys.conf"; + if (!is_file(keys_path)) { + std::ofstream file(keys_path); + keymap.Save(file); + } else { + std::ifstream file(keys_path); + keymap.Load(file); + } +} +void Environment::ShowMessage(const char *msg) { + cout << msg << endl; + msg_state.SetMessage(msg); + state.Push(&msg_state); } Runtime::Runtime() noexcept : name("blank") , mode(NORMAL) +, target(STANDALONE) , n(0) , t(0) , config() { @@ -63,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]; @@ -87,22 +206,60 @@ void Runtime::ReadArgs(int argc, const char *const *argv) { const char *param = arg + 2; // long option if (strcmp(param, "no-vsync") == 0) { - config.vsync = false; + config.game.video.vsync = false; } else if (strcmp(param, "no-keyboard") == 0) { - config.interface.keyboard_disabled = true; + config.game.input.keyboard = false; } else if (strcmp(param, "no-mouse") == 0) { - config.interface.mouse_disabled = true; + config.game.input.mouse = false; } else if (strcmp(param, "no-hud") == 0) { - config.interface.visual_disabled = true; + config.game.video.hud = false; } else if (strcmp(param, "no-audio") == 0) { - config.interface.audio_disabled = true; + config.game.audio.enabled = false; + } else if (strcmp(param, "standalone") == 0) { + target = STANDALONE; + } else if (strcmp(param, "server") == 0) { + target = SERVER; + } else if (strcmp(param, "client") == 0) { + target = CLIENT; } else if (strcmp(param, "asset-path") == 0) { ++i; if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') { cerr << "missing argument to --asset-path" << endl; error = true; } else { - config.asset_path = argv[i]; + config.env.asset_path = argv[i]; + } + } else if (strcmp(param, "host") == 0) { + ++i; + if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') { + cerr << "missing argument to --host" << endl; + error = true; + } else { + config.game.net.host = argv[i]; + } + } else if (strcmp(param, "port") == 0) { + ++i; + if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') { + cerr << "missing argument to --port" << endl; + error = true; + } 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') { + cerr << "missing argument to --player-name" << endl; + error = true; + } else { + config.game.player.name = argv[i]; } } else if (strcmp(param, "save-path") == 0) { ++i; @@ -110,7 +267,15 @@ void Runtime::ReadArgs(int argc, const char *const *argv) { cerr << "missing argument to --save-path" << endl; error = true; } else { - config.save_path = argv[i]; + config.env.save_path = argv[i]; + } + } else if (strcmp(param, "world-name") == 0) { + ++i; + if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') { + cerr << "missing argument to --world-name" << endl; + error = true; + } else { + config.world.name = argv[i]; } } else { cerr << "unknown option " << arg << endl; @@ -122,7 +287,7 @@ void Runtime::ReadArgs(int argc, const char *const *argv) { for (int j = 1; arg[j] != '\0'; ++j) { switch (arg[j]) { case 'd': - config.doublebuf = false; + config.game.video.dblbuf = false; break; case 'm': ++i; @@ -130,7 +295,7 @@ void Runtime::ReadArgs(int argc, const char *const *argv) { cerr << "missing argument to -m" << endl; error = true; } else { - config.multisampling = strtoul(argv[i], nullptr, 10); + config.game.video.msaa = strtoul(argv[i], nullptr, 10); } break; case 'n': @@ -148,8 +313,7 @@ void Runtime::ReadArgs(int argc, const char *const *argv) { cerr << "missing argument to -s" << endl; error = true; } else { - config.world.gen.solid_seed = strtoul(argv[i], nullptr, 10); - config.world.gen.type_seed = config.world.gen.solid_seed; + config.gen.seed = strtoul(argv[i], nullptr, 10); } break; case 't': @@ -181,36 +345,45 @@ 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; } - if (config.asset_path.empty()) { - config.asset_path = default_asset_path(); + if (config.env.asset_path.empty()) { + config.env.asset_path = default_asset_path(); } else if ( - config.asset_path[config.asset_path.size() - 1] != '/' && - config.asset_path[config.asset_path.size() - 1] != '\\' + config.env.asset_path.back() != '/' && + config.env.asset_path.back() != '\\' ) { - config.asset_path += '/'; + config.env.asset_path += '/'; } - if (config.save_path.empty()) { - config.save_path = default_save_path(); + if (config.env.save_path.empty()) { + config.env.save_path = default_save_path(); } else if ( - config.save_path[config.save_path.size() - 1] != '/' && - config.save_path[config.save_path.size() - 1] != '\\' + config.env.save_path.back() != '/' && + config.env.save_path.back() != '\\' ) { - config.save_path += '/'; + 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; +void Runtime::ReadPreferences() { + 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); } } @@ -219,19 +392,76 @@ int Runtime::Execute() { return 1; } - Init init(config.doublebuf, config.multisampling); + InitHeadless init_headless; + + switch (target) { + default: + case STANDALONE: + RunStandalone(); + break; + case SERVER: + RunServer(); + break; + case CLIENT: + RunClient(); + break; + } + + return 0; +} - Environment env(init.window, config.asset_path); - env.viewport.VSync(config.vsync); +void Runtime::RunStandalone() { + Init init(config.game.video.dblbuf, config.game.video.msaa); - Application app(env); + Environment env(init.window, config.env); + env.viewport.VSync(config.game.video.vsync); - WorldState world_state(env, config.interface, config.world); + WorldSave save(config.env.GetWorldPath(config.world.name)); + if (save.Exists()) { + save.Read(config.world); + save.Read(config.gen); + } else { + save.Write(config.world); + save.Write(config.gen); + } + + Application app(env); + standalone::MasterState world_state(env, config.game, config.gen, config.world, save); app.PushState(&world_state); + Run(app); +} - PreloadState preloader(env, world_state.GetWorld().Loader()); - app.PushState(&preloader); +void Runtime::RunServer() { + HeadlessEnvironment env(config.env); + WorldSave save(config.env.GetWorldPath(config.world.name)); + if (save.Exists()) { + save.Read(config.world); + save.Read(config.gen); + } else { + save.Write(config.world); + save.Write(config.gen); + } + + HeadlessApplication app(env); + server::ServerState server_state(env, config.gen, config.world, save, config.game); + app.PushState(&server_state); + Run(app); +} + +void Runtime::RunClient() { + Init init(config.game.video.dblbuf, config.game.video.msaa); + + Environment env(init.window, config.env); + env.viewport.VSync(config.game.video.vsync); + + Application app(env); + client::MasterState client_state(env, config.game, config.world); + app.PushState(&client_state); + Run(app); +} + +void Runtime::Run(HeadlessApplication &app) { switch (mode) { default: case NORMAL: @@ -247,8 +477,6 @@ int Runtime::Execute() { app.RunS(n, t); break; } - - return 0; } }