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 = " << net.port << ';' << 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.world ? "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::ReadArgs(int argc, const char *const *argv) {
173 if (argc <= 0) return;
179 for (int i = 1; i < argc; ++i) {
180 const char *arg = argv[i];
181 if (!arg || arg[0] == '\0') {
182 cerr << "warning: found empty argument at position " << i << endl;
185 if (options && arg[0] == '-') {
186 if (arg[1] == '\0') {
187 cerr << "warning: incomplete option list at position " << i << endl;
188 } else if (arg[1] == '-') {
189 if (arg[2] == '\0') {
193 const char *param = arg + 2;
195 if (strcmp(param, "no-vsync") == 0) {
196 config.game.video.vsync = false;
197 } else if (strcmp(param, "no-keyboard") == 0) {
198 config.game.input.keyboard = false;
199 } else if (strcmp(param, "no-mouse") == 0) {
200 config.game.input.mouse = false;
201 } else if (strcmp(param, "no-hud") == 0) {
202 config.game.video.hud = false;
203 } else if (strcmp(param, "no-audio") == 0) {
204 config.game.audio.enabled = false;
205 } else if (strcmp(param, "standalone") == 0) {
207 } else if (strcmp(param, "server") == 0) {
209 } else if (strcmp(param, "client") == 0) {
211 } else if (strcmp(param, "asset-path") == 0) {
213 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
214 cerr << "missing argument to --asset-path" << endl;
217 config.env.asset_path = argv[i];
219 } else if (strcmp(param, "host") == 0) {
221 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
222 cerr << "missing argument to --host" << endl;
225 config.game.net.host = argv[i];
227 } else if (strcmp(param, "port") == 0) {
229 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
230 cerr << "missing argument to --port" << endl;
233 config.game.net.port = strtoul(argv[i], nullptr, 10);
235 } else if (strcmp(param, "player-name") == 0) {
237 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
238 cerr << "missing argument to --player-name" << endl;
241 config.game.player.name = argv[i];
243 } else if (strcmp(param, "save-path") == 0) {
245 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
246 cerr << "missing argument to --save-path" << endl;
249 config.env.save_path = argv[i];
251 } else if (strcmp(param, "world-name") == 0) {
253 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
254 cerr << "missing argument to --world-name" << endl;
257 config.world.name = argv[i];
260 cerr << "unknown option " << arg << endl;
266 for (int j = 1; arg[j] != '\0'; ++j) {
269 config.game.video.dblbuf = false;
273 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
274 cerr << "missing argument to -m" << endl;
277 config.game.video.msaa = strtoul(argv[i], nullptr, 10);
282 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
283 cerr << "missing argument to -n" << endl;
286 n = strtoul(argv[i], nullptr, 10);
291 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
292 cerr << "missing argument to -s" << endl;
295 config.gen.seed = strtoul(argv[i], nullptr, 10);
300 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
301 cerr << "missing argument to -t" << endl;
304 t = strtoul(argv[i], nullptr, 10);
312 cerr << "unknown option " << arg[j] << endl;
319 cerr << "unable to interpret argument "
320 << i << " (" << arg << ")" << endl;
330 if (config.env.asset_path.empty()) {
331 config.env.asset_path = default_asset_path();
333 config.env.asset_path[config.env.asset_path.size() - 1] != '/' &&
334 config.env.asset_path[config.env.asset_path.size() - 1] != '\\'
336 config.env.asset_path += '/';
338 if (config.env.save_path.empty()) {
339 config.env.save_path = default_save_path();
341 config.env.save_path[config.env.save_path.size() - 1] != '/' &&
342 config.env.save_path[config.env.save_path.size() - 1] != '\\'
344 config.env.save_path += '/';
349 mode = FIXED_FRAME_LIMIT;
360 int Runtime::Execute() {
365 InitHeadless init_headless;
383 void Runtime::RunStandalone() {
384 Init init(config.game.video.dblbuf, config.game.video.msaa);
386 Environment env(init.window, config.env);
387 env.viewport.VSync(config.game.video.vsync);
389 WorldSave save(config.env.GetWorldPath(config.world.name));
391 save.Read(config.world);
392 save.Read(config.gen);
394 save.Write(config.world);
395 save.Write(config.gen);
398 Application app(env);
399 standalone::MasterState world_state(env, config.game, config.gen, config.world, save);
400 app.PushState(&world_state);
404 void Runtime::RunServer() {
405 HeadlessEnvironment env(config.env);
407 WorldSave save(config.env.GetWorldPath(config.world.name));
409 save.Read(config.world);
410 save.Read(config.gen);
412 save.Write(config.world);
413 save.Write(config.gen);
416 HeadlessApplication app(env);
417 server::ServerState server_state(env, config.gen, config.world, save, config.game);
418 app.PushState(&server_state);
422 void Runtime::RunClient() {
423 Init init(config.game.video.dblbuf, config.game.video.msaa);
425 Environment env(init.window, config.env);
426 env.viewport.VSync(config.game.video.vsync);
428 Application app(env);
429 client::MasterState client_state(env, config.game, config.world);
430 app.PushState(&client_state);
434 void Runtime::Run(HeadlessApplication &app) {
446 case FIXED_FRAME_LIMIT: