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)
123 #ifdef BLANK_PROFILING
129 for (int i = 0; i < 4; ++i) {
134 string HeadlessEnvironment::Config::GetWorldPath(const string &world_name) const {
135 return save_path + "worlds/" + world_name + '/';
138 string HeadlessEnvironment::Config::GetWorldPath(const string &world_name, const string &host_name) const {
139 return save_path + "cache/" + host_name + '/' + world_name + '/';
142 Environment::Environment(Window &win, const Config &config)
143 : HeadlessEnvironment(config)
152 keymap.LoadDefault();
154 string keys_path = config.save_path + "keys.conf";
155 if (!is_file(keys_path)) {
156 std::ofstream file(keys_path);
159 std::ifstream file(keys_path);
164 void Environment::ShowMessage(const char *msg) {
166 msg_state.SetMessage(msg);
167 state.Push(&msg_state);
171 Runtime::Runtime() noexcept
182 void Runtime::Initialize(int argc, const char *const *argv) {
183 ReadArgs(argc, argv);
184 if (mode == ERROR) return;
186 ReadArgs(argc, argv);
189 void Runtime::ReadArgs(int argc, const char *const *argv) {
190 if (argc <= 0) return;
196 for (int i = 1; i < argc; ++i) {
197 const char *arg = argv[i];
198 if (!arg || arg[0] == '\0') {
199 cerr << "warning: found empty argument at position " << i << endl;
202 if (options && arg[0] == '-') {
203 if (arg[1] == '\0') {
204 cerr << "warning: incomplete option list at position " << i << endl;
205 } else if (arg[1] == '-') {
206 if (arg[2] == '\0') {
210 const char *param = arg + 2;
212 if (strcmp(param, "no-vsync") == 0) {
213 config.game.video.vsync = false;
214 } else if (strcmp(param, "no-keyboard") == 0) {
215 config.game.input.keyboard = false;
216 } else if (strcmp(param, "no-mouse") == 0) {
217 config.game.input.mouse = false;
218 } else if (strcmp(param, "no-hud") == 0) {
219 config.game.video.hud = false;
220 } else if (strcmp(param, "no-audio") == 0) {
221 config.game.audio.enabled = false;
222 } else if (strcmp(param, "standalone") == 0) {
224 } else if (strcmp(param, "server") == 0) {
226 } else if (strcmp(param, "client") == 0) {
228 } else if (strcmp(param, "asset-path") == 0) {
230 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
231 cerr << "missing argument to --asset-path" << endl;
234 config.env.asset_path = argv[i];
236 } else if (strcmp(param, "host") == 0) {
238 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
239 cerr << "missing argument to --host" << endl;
242 config.game.net.host = argv[i];
244 } else if (strcmp(param, "port") == 0) {
246 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
247 cerr << "missing argument to --port" << endl;
250 config.game.net.port = strtoul(argv[i], nullptr, 10);
252 } else if (strcmp(param, "player-name") == 0) {
254 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
255 cerr << "missing argument to --player-name" << endl;
258 config.game.player.name = argv[i];
260 } else if (strcmp(param, "save-path") == 0) {
262 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
263 cerr << "missing argument to --save-path" << endl;
266 config.env.save_path = argv[i];
268 } else if (strcmp(param, "world-name") == 0) {
270 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
271 cerr << "missing argument to --world-name" << endl;
274 config.world.name = argv[i];
277 cerr << "unknown option " << arg << endl;
283 for (int j = 1; arg[j] != '\0'; ++j) {
286 config.game.video.dblbuf = false;
290 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
291 cerr << "missing argument to -m" << endl;
294 config.game.video.msaa = strtoul(argv[i], nullptr, 10);
299 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
300 cerr << "missing argument to -n" << endl;
303 n = strtoul(argv[i], nullptr, 10);
308 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
309 cerr << "missing argument to -s" << endl;
312 config.gen.seed = strtoul(argv[i], nullptr, 10);
317 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
318 cerr << "missing argument to -t" << endl;
321 t = strtoul(argv[i], nullptr, 10);
329 cerr << "unknown option " << arg[j] << endl;
336 cerr << "unable to interpret argument "
337 << i << " (" << arg << ")" << endl;
346 mode = FIXED_FRAME_LIMIT;
357 void Runtime::ReadPreferences() {
358 if (config.env.asset_path.empty()) {
359 config.env.asset_path = default_asset_path();
361 config.env.asset_path[config.env.asset_path.size() - 1] != '/' &&
362 config.env.asset_path[config.env.asset_path.size() - 1] != '\\'
364 config.env.asset_path += '/';
366 if (config.env.save_path.empty()) {
367 config.env.save_path = default_save_path();
369 config.env.save_path[config.env.save_path.size() - 1] != '/' &&
370 config.env.save_path[config.env.save_path.size() - 1] != '\\'
372 config.env.save_path += '/';
375 string prefs_path = config.env.save_path + "prefs.conf";
376 if (is_file(prefs_path)) {
377 ifstream file(prefs_path);
378 config.game.Load(file);
380 make_dirs(config.env.save_path);
381 ofstream file(prefs_path);
382 config.game.Save(file);
386 int Runtime::Execute() {
391 InitHeadless init_headless;
409 void Runtime::RunStandalone() {
410 Init init(config.game.video.dblbuf, config.game.video.msaa);
412 Environment env(init.window, config.env);
413 env.viewport.VSync(config.game.video.vsync);
415 WorldSave save(config.env.GetWorldPath(config.world.name));
417 save.Read(config.world);
418 save.Read(config.gen);
420 save.Write(config.world);
421 save.Write(config.gen);
424 Application app(env);
425 standalone::MasterState world_state(env, config.game, config.gen, config.world, save);
426 app.PushState(&world_state);
430 void Runtime::RunServer() {
431 HeadlessEnvironment env(config.env);
433 WorldSave save(config.env.GetWorldPath(config.world.name));
435 save.Read(config.world);
436 save.Read(config.gen);
438 save.Write(config.world);
439 save.Write(config.gen);
442 HeadlessApplication app(env);
443 server::ServerState server_state(env, config.gen, config.world, save, config.game);
444 app.PushState(&server_state);
448 void Runtime::RunClient() {
449 Init init(config.game.video.dblbuf, config.game.video.msaa);
451 Environment env(init.window, config.env);
452 env.viewport.VSync(config.game.video.vsync);
454 Application app(env);
455 client::MasterState client_state(env, config.game, config.world);
456 app.PushState(&client_state);
460 void Runtime::Run(HeadlessApplication &app) {
472 case FIXED_FRAME_LIMIT: