aboutsummaryrefslogtreecommitdiff
path: root/src/client/main.cc
blob: 858c6df5f54f3dab64f79e51088574ebaf801d71 (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
#include "client/main.hh"

static bool& get_has_address() noexcept {
    static bool ret = false;
    return ret;
}

static bool& get_has_port() noexcept {
    static bool ret = false;
    return ret;
}

static std::string& get_address() noexcept {
    static std::string address{shared::DEFAULT_ADDRESS};
    return address;
}

static std::string& get_port() noexcept {
    static std::string port{shared::DEFAULT_PORT};
    return port;
}

static bool& get_is_headless() noexcept {
    static bool is_headless = false;
    return is_headless;
}

static bool parse_arg(const int& c, const char* const arg) {
    switch (c) {
    case 'a':
        get_address() = boost::lexical_cast<std::string>(arg);
        get_has_address() = true;
        break;
    case 'p':
        get_port() = boost::lexical_cast<std::string>(arg);
        get_has_port() = true;
        break;
    case 'H':
        get_is_headless() = true;
        break;
    case 's':
        get_address() = "localhost";
        break;
    default:
        return false;
    }
    return true;
}

static const shared::args_t& get_options() {
    static shared::args_t ret = []() -> shared::args_t {
        shared::args_t ret{
            {.name = "address", .desc = "connect elsewhere", .val = "a:"},
            {.name = "port", .desc = "override the default port", .val = "p:"},
            {.name = "headless", .desc = "run without a client ", .val = "H"},
            {.name = "singleplayer",
             .desc = "avoid hosting a lan server",
             .val = "s"}};
        std::ranges::copy(shared::get_options(), std::back_inserter(ret));
        std::ranges::copy(server::get_options(), std::back_inserter(ret));
        return ret;
    }();

    return ret;
}

int main(const int argc, char* const* argv) {
    shared::init();

    shared::parse_args(argc, argv, get_options(),
                       {&parse_arg, &shared::parse_arg, &server::parse_arg});

    if (get_has_address()) {
        shared::try_main(client::main, get_address(), get_port());
        return EXIT_SUCCESS;
    }

    if (get_is_headless()) {
        shared::try_main(server::main, get_address(), get_port());
        return EXIT_SUCCESS;
    }

    const std::jthread server_thread{shared::try_main, server::main,
                                     get_address(), get_port()};
    while (!server::has_initialised) {
        continue;
    }
    shared::try_main(client::main, get_address(), get_port());
    return EXIT_SUCCESS;
}