21
|
1 |
|
|
2 |
#include "Engine.hh"
|
|
3 |
|
|
4 |
#include <stdexcept>
|
|
5 |
#include <cassert>
|
|
6 |
|
|
7 |
#include <ClanLib/core.h>
|
|
8 |
#include <ClanLib/application.h>
|
|
9 |
|
|
10 |
class ArgumentError : public std::exception {
|
|
11 |
private:
|
|
12 |
const char *message;
|
|
13 |
|
|
14 |
public:
|
|
15 |
ArgumentError (const std::string &message) : message(message.c_str()) { }
|
|
16 |
|
|
17 |
virtual const char* what() const throw() {
|
|
18 |
return message;
|
|
19 |
}
|
|
20 |
};
|
|
21 |
|
|
22 |
class Main : public CL_ClanApplication {
|
|
23 |
private:
|
|
24 |
// arguments
|
|
25 |
CL_CommandLine args;
|
|
26 |
|
|
27 |
std::string arg_port;
|
|
28 |
bool arg_dedicated;
|
|
29 |
std::string arg_connect;
|
|
30 |
|
|
31 |
void parse_args (int argc, char **argv) {
|
|
32 |
// set up the options
|
|
33 |
args.add_option('p', "port", "PORT", "set network port used", true);
|
|
34 |
args.add_option('D', "dedicated", "", "act as a network server", true);
|
|
35 |
args.add_option('c', "connect", "SERVERHOST", "act as a network client", true);
|
|
36 |
|
|
37 |
// set defaults
|
|
38 |
arg_port = NETWORK_PORT_STR;
|
|
39 |
arg_dedicated = false;
|
|
40 |
arg_connect = "";
|
|
41 |
|
|
42 |
try {
|
|
43 |
// parse args
|
|
44 |
args.parse_args(argc, argv);
|
|
45 |
} catch (CL_Error &e) {
|
|
46 |
throw ArgumentError(e.message);
|
|
47 |
}
|
|
48 |
|
|
49 |
while (args.next()) {
|
|
50 |
switch (args.get_key()) {
|
|
51 |
case 'p':
|
|
52 |
arg_port = args.get_argument();
|
|
53 |
break;
|
|
54 |
|
|
55 |
case 'D':
|
|
56 |
arg_dedicated = true;
|
|
57 |
break;
|
|
58 |
|
|
59 |
case 'c':
|
|
60 |
arg_connect = args.get_argument();
|
|
61 |
break;
|
|
62 |
|
|
63 |
case CL_CommandLine::REST_ARG:
|
|
64 |
throw ArgumentError(args.get_argument());
|
|
65 |
|
|
66 |
default:
|
|
67 |
throw ArgumentError(std::string(1, (char) args.get_key()));
|
|
68 |
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
// check for valid combinations of arugments
|
|
73 |
if (arg_dedicated && !arg_connect.empty())
|
|
74 |
throw ArgumentError("-D and -c are mutually exclusive");
|
|
75 |
|
|
76 |
if (!arg_dedicated && arg_connect.empty())
|
|
77 |
throw ArgumentError("nothing to do");
|
|
78 |
}
|
|
79 |
|
|
80 |
public:
|
|
81 |
virtual int main (int argc, char **argv) {
|
|
82 |
// initialize core
|
|
83 |
CL_SetupCore setup_core;
|
|
84 |
|
|
85 |
try {
|
|
86 |
// parse arugments
|
|
87 |
parse_args(argc, argv);
|
|
88 |
|
|
89 |
// run engine based on args
|
|
90 |
if (arg_dedicated) {
|
|
91 |
Engine::runNetworkServer(arg_port);
|
|
92 |
|
|
93 |
} else if (!arg_connect.empty()) {
|
|
94 |
Engine::runNetworkClient(arg_connect, arg_port);
|
|
95 |
|
|
96 |
} else
|
|
97 |
assert(false);
|
|
98 |
|
|
99 |
// succesful return
|
|
100 |
return 0;
|
|
101 |
|
|
102 |
} catch (ArgumentError &e) {
|
|
103 |
std::cerr << e.what() << std::endl;
|
|
104 |
args.print_help();
|
|
105 |
|
|
106 |
// XXX: handle --help
|
|
107 |
|
|
108 |
} catch (std::exception &e) {
|
|
109 |
std::cerr << e.what() << std::endl;
|
|
110 |
|
|
111 |
return 1;
|
|
112 |
}
|
|
113 |
}
|
|
114 |
} app;
|
|
115 |
|