#include #include #include #include #include /* Required by event.h. */ #include #include #include #include #include #include #include #include #include #include #include "main.h" #include "socket.h" #include "world.h" mc_config_t *config; int read_config(char *filename) { FILE *f; nbt_node *cur; fprintf(stderr, "configuring...\n"); config = malloc(sizeof(mc_config_t)); if (config == NULL) { return -ENOMEM; } f = fopen(filename, "rb"); if (f == NULL) { fprintf(stderr, "could not open '%s': %s\n", filename, strerror(errno)); return -errno; } config->root = nbt_parse_file(f); fclose(f); if (errno != NBT_OK) { fprintf(stderr, "Config parsing error\n"); return -errno; } // port cur = nbt_find_by_path(config->root, "config.port"); if (cur == NULL || cur->type != TAG_SHORT) { fprintf(stderr, "Port not found\n"); config->port = 25565; } else { config->port = cur->payload.tag_short; } cur = nbt_find_by_path(config->root, "config.motd"); if (cur == NULL || cur->type != TAG_STRING) { fprintf(stderr, "Motd not found\n"); config->motd = "Not a Minecraft server"; } else { config->motd = cur->payload.tag_string; } cur = nbt_find_by_path(config->root, "config.max_users"); if (cur == NULL || cur->type != TAG_INT) { fprintf(stderr, "max_users nof found\n"); config->max_user = 20; } else { config->max_user = cur->payload.tag_int; } fprintf(stderr, "configured\n"); return 0; } int start_server() { int err; /*mc_dimension_registry_element_t *el = mc_dim_registry_element_default_overworld(); nbt_node *cctag = mc_dimension_registry_element_to_nbt(el); if (cctag == NULL) { printf("error: %s\n", strerror(-errno)); return -1; } char* str = nbt_dump_ascii(cctag); nbt_free(cctag); printf("nbt: %s\n", str);*/ srand(time(NULL)); printf("starting on port %d\n", config->port); evbase = event_base_new(); if ((err = open_socket(config->port)) != 0) { printf("could not open socket\n"); return -err; } printf("generating world...\n"); err = init_world(); if (err < 0) { printf("failed to load world: %s\n", strerror(err)); return err; } printf("dispatching...\n"); event_base_dispatch(evbase); printf("after"); return 0; } int main(int argc, char **argv) { int c, option_index; char *configfile; char *datadir; datadir = malloc(PATH_MAX); if (datadir == NULL) fprintf(stderr, "could not alloc datadir\n"); static struct option long_options[] = { {"version", no_argument, NULL, 'v'}, {NULL, no_argument, NULL, 0} }; for (;;) { if ((c = getopt_long(argc, argv, "v", long_options, &option_index)) < 0) break; switch (c) { case 0: if (long_options[option_index].flag != 0) break; break; case 'v': printf("%s %d.%d\n", argv[0], mcc_VERSION_MAJOR, mcc_VERSION_MINOR); return 0; case '?': break; } } // set pwd to path of binary strncpy(datadir, argv[0], PATH_MAX); fprintf(stderr, "argv[0]: %s\n", datadir); fprintf(stderr, "argv[0]: %s\n", argv[0]); datadir = dirname(datadir); strncat(datadir, "/" mcc_RESOURCE_PATH, PATH_MAX - 1); fprintf(stderr, "chdir to %s\n", datadir); if (chdir(datadir) < 0) { fprintf(stderr, "coud not set cwd: %s", strerror(errno)); } if (optind < argc) { configfile = argv[optind]; } else { configfile = "config.dat"; } if ((errno = read_config(configfile)) < 0) { fprintf(stderr, "could not read config: %s", strerror(-errno)); return -errno; } if ((errno = start_server()) < 0) { fprintf(stderr, "could not start server: %s", strerror(-errno)); return -errno; } fprintf(stderr, "usage: %s FILE\n", argv[0]); return -1; }