terom@21: terom@21: #include "Engine.hh" terom@21: terom@21: #include terom@21: #include terom@21: terom@21: #include terom@21: #include terom@21: terom@21: class ArgumentError : public std::exception { terom@21: private: terom@21: const char *message; terom@21: terom@21: public: terom@21: ArgumentError (const std::string &message) : message(message.c_str()) { } terom@21: terom@21: virtual const char* what() const throw() { terom@21: return message; terom@21: } terom@21: }; terom@21: terom@21: class Main : public CL_ClanApplication { terom@21: private: terom@21: // arguments terom@21: CL_CommandLine args; terom@21: terom@21: std::string arg_port; terom@21: bool arg_dedicated; terom@21: std::string arg_connect; terom@21: terom@21: void parse_args (int argc, char **argv) { terom@21: // set up the options terom@21: args.add_option('p', "port", "PORT", "set network port used", true); terom@21: args.add_option('D', "dedicated", "", "act as a network server", true); terom@21: args.add_option('c', "connect", "SERVERHOST", "act as a network client", true); terom@21: terom@21: // set defaults terom@21: arg_port = NETWORK_PORT_STR; terom@21: arg_dedicated = false; terom@21: arg_connect = ""; terom@21: terom@21: try { terom@21: // parse args terom@21: args.parse_args(argc, argv); terom@21: } catch (CL_Error &e) { terom@21: throw ArgumentError(e.message); terom@21: } terom@21: terom@21: while (args.next()) { terom@21: switch (args.get_key()) { terom@21: case 'p': terom@21: arg_port = args.get_argument(); terom@21: break; terom@21: terom@21: case 'D': terom@21: arg_dedicated = true; terom@21: break; terom@21: terom@21: case 'c': terom@21: arg_connect = args.get_argument(); terom@21: break; terom@21: terom@21: case CL_CommandLine::REST_ARG: terom@21: throw ArgumentError(args.get_argument()); terom@21: terom@21: default: terom@21: throw ArgumentError(std::string(1, (char) args.get_key())); terom@21: terom@21: } terom@21: } terom@21: terom@21: // check for valid combinations of arugments terom@21: if (arg_dedicated && !arg_connect.empty()) terom@21: throw ArgumentError("-D and -c are mutually exclusive"); terom@21: terom@21: if (!arg_dedicated && arg_connect.empty()) terom@21: throw ArgumentError("nothing to do"); terom@21: } terom@21: terom@21: public: terom@21: virtual int main (int argc, char **argv) { terom@21: // initialize core terom@21: CL_SetupCore setup_core; terom@21: terom@21: try { terom@21: // parse arugments terom@21: parse_args(argc, argv); terom@21: terom@21: // run engine based on args terom@21: if (arg_dedicated) { terom@21: Engine::runNetworkServer(arg_port); terom@21: terom@21: } else if (!arg_connect.empty()) { terom@21: Engine::runNetworkClient(arg_connect, arg_port); terom@21: terom@21: } else terom@21: assert(false); terom@21: terom@21: // succesful return terom@21: return 0; terom@21: terom@21: } catch (ArgumentError &e) { terom@21: std::cerr << e.what() << std::endl; terom@21: args.print_help(); terom@21: terom@21: // XXX: handle --help terom@21: terom@21: } catch (std::exception &e) { terom@21: std::cerr << e.what() << std::endl; terom@21: terom@21: return 1; terom@21: } terom@21: } terom@21: } app; terom@21: