1 #include "Application.hpp"
2 #include "Environment.hpp"
6 #include "../client/MasterState.hpp"
7 #include "../io/filesystem.hpp"
8 #include "../io/TokenStreamReader.hpp"
9 #include "../io/WorldSave.hpp"
10 #include "../server/ServerState.hpp"
11 #include "../standalone/MasterState.hpp"
24 string default_asset_path() {
25 char *base = SDL_GetBasePath();
32 string default_save_path() {
34 char *base = SDL_GetBasePath();
40 char *pref = SDL_GetPrefPath("localhorst", "blank");
51 void Config::Load(std::istream &is) {
52 TokenStreamReader in(is);
54 while (in.HasMore()) {
55 if (in.Peek().type == Token::STRING) {
58 in.ReadIdentifier(name);
60 in.Skip(Token::EQUALS);
61 if (name == "audio.enabled") {
62 in.ReadBoolean(audio.enabled);
63 } else if (name == "input.keyboard") {
64 in.ReadBoolean(input.keyboard);
65 } else if (name == "input.mouse") {
66 in.ReadBoolean(input.mouse);
67 } else if (name == "input.pitch_sensitivity") {
68 in.ReadNumber(input.pitch_sensitivity);
69 } else if (name == "input.yaw_sensitivity") {
70 in.ReadNumber(input.yaw_sensitivity);
71 } else if (name == "net.host") {
72 in.ReadString(net.host);
73 } else if (name == "net.port") {
77 } else if (name == "player.name") {
78 in.ReadString(player.name);
79 } else if (name == "video.dblbuf") {
80 in.ReadBoolean(video.dblbuf);
81 } else if (name == "video.vsync") {
82 in.ReadBoolean(video.vsync);
83 } else if (name == "video.msaa") {
84 in.ReadNumber(video.msaa);
85 } else if (name == "video.hud") {
86 in.ReadBoolean(video.hud);
87 } else if (name == "video.world") {
88 in.ReadBoolean(video.world);
89 } else if (name == "video.debug") {
90 in.ReadBoolean(video.debug);
92 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
93 in.Skip(Token::SEMICOLON);
98 void Config::Save(std::ostream &out) {
99 out << "audio.enabled = " << (audio.enabled ? "yes" : "no") << ';' << std::endl;
100 out << "input.keyboard = " << (input.keyboard ? "on" : "off") << ';' << std::endl;
101 out << "input.mouse = " << (input.keyboard ? "on" : "off") << ';' << std::endl;
102 out << "input.pitch_sensitivity = " << input.pitch_sensitivity << ';' << std::endl;
103 out << "input.yaw_sensitivity = " << input.yaw_sensitivity << ';' << std::endl;
104 out << "net.host = \"" << net.host << "\";" << std::endl;
105 out << "net.port = " << net.port << ';' << std::endl;
106 out << "player.name = \"" << player.name << "\";" << std::endl;
107 out << "video.dblbuf = " << (video.dblbuf ? "on" : "off") << ';' << std::endl;
108 out << "video.vsync = " << (video.vsync ? "on" : "off") << ';' << std::endl;
109 out << "video.msaa = " << video.msaa << ';' << std::endl;
110 out << "video.hud = " << (video.hud ? "on" : "off") << ';' << std::endl;
111 out << "video.world = " << (video.world ? "on" : "off") << ';' << std::endl;
112 out << "video.debug = " << (video.debug ? "on" : "off") << ';' << std::endl;
116 HeadlessEnvironment::HeadlessEnvironment(const Config &config)
118 , loader(config.asset_path)
124 string HeadlessEnvironment::Config::GetWorldPath(const string &world_name) const {
125 return save_path + "worlds/" + world_name + '/';
128 string HeadlessEnvironment::Config::GetWorldPath(const string &world_name, const string &host_name) const {
129 return save_path + "cache/" + host_name + '/' + world_name + '/';
132 Environment::Environment(Window &win, const Config &config)
133 : HeadlessEnvironment(config)
142 keymap.LoadDefault();
144 string keys_path = config.save_path + "keys.conf";
145 if (!is_file(keys_path)) {
146 std::ofstream file(keys_path);
149 std::ifstream file(keys_path);
154 void Environment::ShowMessage(const char *msg) {
156 msg_state.SetMessage(msg);
157 state.Push(&msg_state);
161 Runtime::Runtime() noexcept
172 void Runtime::Initialize(int argc, const char *const *argv) {
173 ReadArgs(argc, argv);
174 if (mode == ERROR) return;
176 ReadArgs(argc, argv);
179 void Runtime::ReadArgs(int argc, const char *const *argv) {
180 if (argc <= 0) return;
186 for (int i = 1; i < argc; ++i) {
187 const char *arg = argv[i];
188 if (!arg || arg[0] == '\0') {
189 cerr << "warning: found empty argument at position " << i << endl;
192 if (options && arg[0] == '-') {
193 if (arg[1] == '\0') {
194 cerr << "warning: incomplete option list at position " << i << endl;
195 } else if (arg[1] == '-') {
196 if (arg[2] == '\0') {
200 const char *param = arg + 2;
202 if (strcmp(param, "no-vsync") == 0) {
203 config.game.video.vsync = false;
204 } else if (strcmp(param, "no-keyboard") == 0) {
205 config.game.input.keyboard = false;
206 } else if (strcmp(param, "no-mouse") == 0) {
207 config.game.input.mouse = false;
208 } else if (strcmp(param, "no-hud") == 0) {
209 config.game.video.hud = false;
210 } else if (strcmp(param, "no-audio") == 0) {
211 config.game.audio.enabled = false;
212 } else if (strcmp(param, "standalone") == 0) {
214 } else if (strcmp(param, "server") == 0) {
216 } else if (strcmp(param, "client") == 0) {
218 } else if (strcmp(param, "asset-path") == 0) {
220 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
221 cerr << "missing argument to --asset-path" << endl;
224 config.env.asset_path = argv[i];
226 } else if (strcmp(param, "host") == 0) {
228 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
229 cerr << "missing argument to --host" << endl;
232 config.game.net.host = argv[i];
234 } else if (strcmp(param, "port") == 0) {
236 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
237 cerr << "missing argument to --port" << endl;
240 config.game.net.port = strtoul(argv[i], nullptr, 10);
242 } else if (strcmp(param, "player-name") == 0) {
244 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
245 cerr << "missing argument to --player-name" << endl;
248 config.game.player.name = argv[i];
250 } else if (strcmp(param, "save-path") == 0) {
252 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
253 cerr << "missing argument to --save-path" << endl;
256 config.env.save_path = argv[i];
258 } else if (strcmp(param, "world-name") == 0) {
260 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
261 cerr << "missing argument to --world-name" << endl;
264 config.world.name = argv[i];
267 cerr << "unknown option " << arg << endl;
273 for (int j = 1; arg[j] != '\0'; ++j) {
276 config.game.video.dblbuf = false;
280 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
281 cerr << "missing argument to -m" << endl;
284 config.game.video.msaa = strtoul(argv[i], nullptr, 10);
289 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
290 cerr << "missing argument to -n" << endl;
293 n = strtoul(argv[i], nullptr, 10);
298 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
299 cerr << "missing argument to -s" << endl;
302 config.gen.seed = strtoul(argv[i], nullptr, 10);
307 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
308 cerr << "missing argument to -t" << endl;
311 t = strtoul(argv[i], nullptr, 10);
319 cerr << "unknown option " << arg[j] << endl;
326 cerr << "unable to interpret argument "
327 << i << " (" << arg << ")" << endl;
336 mode = FIXED_FRAME_LIMIT;
347 void Runtime::ReadPreferences() {
348 if (config.env.asset_path.empty()) {
349 config.env.asset_path = default_asset_path();
351 config.env.asset_path[config.env.asset_path.size() - 1] != '/' &&
352 config.env.asset_path[config.env.asset_path.size() - 1] != '\\'
354 config.env.asset_path += '/';
356 if (config.env.save_path.empty()) {
357 config.env.save_path = default_save_path();
359 config.env.save_path[config.env.save_path.size() - 1] != '/' &&
360 config.env.save_path[config.env.save_path.size() - 1] != '\\'
362 config.env.save_path += '/';
365 string prefs_path = config.env.save_path + "prefs.conf";
366 if (is_file(prefs_path)) {
367 ifstream file(prefs_path);
368 config.game.Load(file);
370 make_dirs(config.env.save_path);
371 ofstream file(prefs_path);
372 config.game.Save(file);
376 int Runtime::Execute() {
381 InitHeadless init_headless;
399 void Runtime::RunStandalone() {
400 Init init(config.game.video.dblbuf, config.game.video.msaa);
402 Environment env(init.window, config.env);
403 env.viewport.VSync(config.game.video.vsync);
405 WorldSave save(config.env.GetWorldPath(config.world.name));
407 save.Read(config.world);
408 save.Read(config.gen);
410 save.Write(config.world);
411 save.Write(config.gen);
414 Application app(env);
415 standalone::MasterState world_state(env, config.game, config.gen, config.world, save);
416 app.PushState(&world_state);
420 void Runtime::RunServer() {
421 HeadlessEnvironment env(config.env);
423 WorldSave save(config.env.GetWorldPath(config.world.name));
425 save.Read(config.world);
426 save.Read(config.gen);
428 save.Write(config.world);
429 save.Write(config.gen);
432 HeadlessApplication app(env);
433 server::ServerState server_state(env, config.gen, config.world, save, config.game);
434 app.PushState(&server_state);
438 void Runtime::RunClient() {
439 Init init(config.game.video.dblbuf, config.game.video.msaa);
441 Environment env(init.window, config.env);
442 env.viewport.VSync(config.game.video.vsync);
444 Application app(env);
445 client::MasterState client_state(env, config.game, config.world);
446 app.PushState(&client_state);
450 void Runtime::Run(HeadlessApplication &app) {
462 case FIXED_FRAME_LIMIT: