summaryrefslogtreecommitdiffstats
path: root/main.c
blob: d2063eca05c3fc95c804cf98512d81dd915ddfd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <getopt.h>

/* Required by event.h. */
#include <sys/time.h>
#include <sys/syslimits.h>

#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

#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;
}