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"
25 string default_asset_path() {
26 char *base = SDL_GetBasePath();
33 string default_save_path() {
35 char *base = SDL_GetBasePath();
41 char *pref = SDL_GetPrefPath("localhorst", "blank");
52 void Config::Load(std::istream &is) {
53 TokenStreamReader in(is);
55 while (in.HasMore()) {
56 if (in.Peek().type == Token::STRING) {
59 in.ReadIdentifier(name);
61 in.Skip(Token::EQUALS);
62 if (name == "audio.enabled") {
63 in.ReadBoolean(audio.enabled);
64 } else if (name == "input.keyboard") {
65 in.ReadBoolean(input.keyboard);
66 } else if (name == "input.mouse") {
67 in.ReadBoolean(input.mouse);
68 } else if (name == "input.pitch_sensitivity") {
69 in.ReadNumber(input.pitch_sensitivity);
70 } else if (name == "input.yaw_sensitivity") {
71 in.ReadNumber(input.yaw_sensitivity);
72 } else if (name == "net.host") {
73 in.ReadString(net.host);
74 } else if (name == "net.port") {
78 } else if (name == "player.name") {
79 in.ReadString(player.name);
80 } else if (name == "video.dblbuf") {
81 in.ReadBoolean(video.dblbuf);
82 } else if (name == "video.vsync") {
83 in.ReadBoolean(video.vsync);
84 } else if (name == "video.msaa") {
85 in.ReadNumber(video.msaa);
86 } else if (name == "video.hud") {
87 in.ReadBoolean(video.hud);
88 } else if (name == "video.world") {
89 in.ReadBoolean(video.world);
90 } else if (name == "video.debug") {
91 in.ReadBoolean(video.debug);
93 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
94 in.Skip(Token::SEMICOLON);
99 void Config::Save(std::ostream &out) {
100 out << "audio.enabled = " << (audio.enabled ? "yes" : "no") << ';' << std::endl;
101 out << "input.keyboard = " << (input.keyboard ? "on" : "off") << ';' << std::endl;
102 out << "input.mouse = " << (input.keyboard ? "on" : "off") << ';' << std::endl;
103 out << "input.pitch_sensitivity = " << input.pitch_sensitivity << ';' << std::endl;
104 out << "input.yaw_sensitivity = " << input.yaw_sensitivity << ';' << std::endl;
105 out << "net.host = \"" << net.host << "\";" << std::endl;
106 out << "net.port = " << net.port << ';' << std::endl;
107 out << "player.name = \"" << player.name << "\";" << std::endl;
108 out << "video.dblbuf = " << (video.dblbuf ? "on" : "off") << ';' << std::endl;
109 out << "video.vsync = " << (video.vsync ? "on" : "off") << ';' << std::endl;
110 out << "video.msaa = " << video.msaa << ';' << std::endl;
111 out << "video.hud = " << (video.hud ? "on" : "off") << ';' << std::endl;
112 out << "video.world = " << (video.world ? "on" : "off") << ';' << std::endl;
113 out << "video.debug = " << (video.debug ? "on" : "off") << ';' << std::endl;
117 HeadlessEnvironment::HeadlessEnvironment(const Config &config)
119 , loader(config.asset_path)
125 string HeadlessEnvironment::Config::GetWorldPath(const string &world_name) const {
126 return save_path + "worlds/" + world_name + '/';
129 string HeadlessEnvironment::Config::GetWorldPath(const string &world_name, const string &host_name) const {
130 return save_path + "cache/" + host_name + '/' + world_name + '/';
133 Environment::Environment(Window &win, const Config &config)
134 : HeadlessEnvironment(config)
143 keymap.LoadDefault();
145 string keys_path = config.save_path + "keys.conf";
146 if (!is_file(keys_path)) {
147 std::ofstream file(keys_path);
150 std::ifstream file(keys_path);
155 void Environment::ShowMessage(const char *msg) {
157 msg_state.SetMessage(msg);
158 state.Push(&msg_state);
162 Runtime::Runtime() noexcept
173 void Runtime::Initialize(int argc, const char *const *argv) {
174 ReadArgs(argc, argv);
175 if (mode == ERROR) return;
177 ReadArgs(argc, argv);
180 void Runtime::ReadArgs(int argc, const char *const *argv) {
181 if (argc <= 0) return;
187 for (int i = 1; i < argc; ++i) {
188 const char *arg = argv[i];
189 if (!arg || arg[0] == '\0') {
190 cerr << "warning: found empty argument at position " << i << endl;
193 if (options && arg[0] == '-') {
194 if (arg[1] == '\0') {
195 cerr << "warning: incomplete option list at position " << i << endl;
196 } else if (arg[1] == '-') {
197 if (arg[2] == '\0') {
201 const char *param = arg + 2;
203 if (strcmp(param, "no-vsync") == 0) {
204 config.game.video.vsync = false;
205 } else if (strcmp(param, "no-keyboard") == 0) {
206 config.game.input.keyboard = false;
207 } else if (strcmp(param, "no-mouse") == 0) {
208 config.game.input.mouse = false;
209 } else if (strcmp(param, "no-hud") == 0) {
210 config.game.video.hud = false;
211 } else if (strcmp(param, "no-audio") == 0) {
212 config.game.audio.enabled = false;
213 } else if (strcmp(param, "standalone") == 0) {
215 } else if (strcmp(param, "server") == 0) {
217 } else if (strcmp(param, "client") == 0) {
219 } else if (strcmp(param, "asset-path") == 0) {
221 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
222 cerr << "missing argument to --asset-path" << endl;
225 config.env.asset_path = argv[i];
227 } else if (strcmp(param, "host") == 0) {
229 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
230 cerr << "missing argument to --host" << endl;
233 config.game.net.host = argv[i];
235 } else if (strcmp(param, "port") == 0) {
237 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
238 cerr << "missing argument to --port" << endl;
241 config.game.net.port = strtoul(argv[i], nullptr, 10);
243 } else if (strcmp(param, "player-name") == 0) {
245 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
246 cerr << "missing argument to --player-name" << endl;
249 config.game.player.name = argv[i];
251 } else if (strcmp(param, "save-path") == 0) {
253 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
254 cerr << "missing argument to --save-path" << endl;
257 config.env.save_path = argv[i];
259 } else if (strcmp(param, "world-name") == 0) {
261 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
262 cerr << "missing argument to --world-name" << endl;
265 config.world.name = argv[i];
268 cerr << "unknown option " << arg << endl;
274 for (int j = 1; arg[j] != '\0'; ++j) {
277 config.game.video.dblbuf = false;
281 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
282 cerr << "missing argument to -m" << endl;
285 config.game.video.msaa = strtoul(argv[i], nullptr, 10);
290 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
291 cerr << "missing argument to -n" << endl;
294 n = strtoul(argv[i], nullptr, 10);
299 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
300 cerr << "missing argument to -s" << endl;
303 config.gen.seed = strtoul(argv[i], nullptr, 10);
308 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
309 cerr << "missing argument to -t" << endl;
312 t = strtoul(argv[i], nullptr, 10);
320 cerr << "unknown option " << arg[j] << endl;
327 cerr << "unable to interpret argument "
328 << i << " (" << arg << ")" << endl;
337 mode = FIXED_FRAME_LIMIT;
348 void Runtime::ReadPreferences() {
349 if (config.env.asset_path.empty()) {
350 config.env.asset_path = default_asset_path();
352 config.env.asset_path[config.env.asset_path.size() - 1] != '/' &&
353 config.env.asset_path[config.env.asset_path.size() - 1] != '\\'
355 config.env.asset_path += '/';
357 if (config.env.save_path.empty()) {
358 config.env.save_path = default_save_path();
360 config.env.save_path[config.env.save_path.size() - 1] != '/' &&
361 config.env.save_path[config.env.save_path.size() - 1] != '\\'
363 config.env.save_path += '/';
366 string prefs_path = config.env.save_path + "prefs.conf";
367 if (is_file(prefs_path)) {
368 ifstream file(prefs_path);
369 config.game.Load(file);
371 make_dirs(config.env.save_path);
372 ofstream file(prefs_path);
373 config.game.Save(file);
377 int Runtime::Execute() {
382 InitHeadless init_headless;
400 void Runtime::RunStandalone() {
401 Init init(config.game.video.dblbuf, config.game.video.msaa);
403 Environment env(init.window, config.env);
404 env.viewport.VSync(config.game.video.vsync);
406 WorldSave save(config.env.GetWorldPath(config.world.name));
408 save.Read(config.world);
409 save.Read(config.gen);
411 save.Write(config.world);
412 save.Write(config.gen);
415 Application app(env);
416 standalone::MasterState world_state(env, config.game, config.gen, config.world, save);
417 app.PushState(&world_state);
421 void Runtime::RunServer() {
422 HeadlessEnvironment env(config.env);
424 WorldSave save(config.env.GetWorldPath(config.world.name));
426 save.Read(config.world);
427 save.Read(config.gen);
429 save.Write(config.world);
430 save.Write(config.gen);
433 HeadlessApplication app(env);
434 server::ServerState server_state(env, config.gen, config.world, save, config.game);
435 app.PushState(&server_state);
439 void Runtime::RunClient() {
440 Init init(config.game.video.dblbuf, config.game.video.msaa);
442 Environment env(init.window, config.env);
443 env.viewport.VSync(config.game.video.vsync);
445 Application app(env);
446 client::MasterState client_state(env, config.game, config.world);
447 app.PushState(&client_state);
451 void Runtime::Run(HeadlessApplication &app) {
463 case FIXED_FRAME_LIMIT: