2
0

EntryPoint.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * EntryPoint.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "../server/CVCMIServer.h"
  12. #include "../lib/CConsoleHandler.h"
  13. #include "../lib/logging/CBasicLogConfigurator.h"
  14. #include "../lib/VCMIDirs.h"
  15. #include "../lib/VCMI_Lib.h"
  16. #include <boost/program_options.hpp>
  17. static const std::string SERVER_NAME_AFFIX = "server";
  18. static const std::string SERVER_NAME = GameConstants::VCMI_VERSION + std::string(" (") + SERVER_NAME_AFFIX + ')';
  19. static void handleCommandOptions(int argc, const char * argv[], boost::program_options::variables_map & options)
  20. {
  21. boost::program_options::options_description opts("Allowed options");
  22. opts.add_options()
  23. ("help,h", "display help and exit")
  24. ("version,v", "display version information and exit")
  25. ("run-by-client", "indicate that server launched by client on same machine")
  26. ("port", boost::program_options::value<ui16>(), "port at which server will listen to connections from client")
  27. ("lobby", "start server in lobby mode in which server connects to a global lobby");
  28. if(argc > 1)
  29. {
  30. try
  31. {
  32. boost::program_options::store(boost::program_options::parse_command_line(argc, argv, opts), options);
  33. }
  34. catch(boost::program_options::error & e)
  35. {
  36. std::cerr << "Failure during parsing command-line options:\n" << e.what() << std::endl;
  37. }
  38. }
  39. boost::program_options::notify(options);
  40. if(options.count("help"))
  41. {
  42. auto time = std::time(nullptr);
  43. printf("%s - A Heroes of Might and Magic 3 clone\n", GameConstants::VCMI_VERSION.c_str());
  44. printf("Copyright (C) 2007-%d VCMI dev team - see AUTHORS file\n", std::localtime(&time)->tm_year + 1900);
  45. printf("This is free software; see the source for copying conditions. There is NO\n");
  46. printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
  47. printf("\n");
  48. std::cout << opts;
  49. exit(0);
  50. }
  51. if(options.count("version"))
  52. {
  53. printf("%s\n", GameConstants::VCMI_VERSION.c_str());
  54. std::cout << VCMIDirs::get().genHelpString();
  55. exit(0);
  56. }
  57. }
  58. int main(int argc, const char * argv[])
  59. {
  60. // Correct working dir executable folder (not bundle folder) so we can use executable relative paths
  61. boost::filesystem::current_path(boost::filesystem::system_complete(argv[0]).parent_path());
  62. console = new CConsoleHandler();
  63. CBasicLogConfigurator logConfig(VCMIDirs::get().userLogsPath() / "VCMI_Server_log.txt", console);
  64. logConfig.configureDefault();
  65. logGlobal->info(SERVER_NAME);
  66. boost::program_options::variables_map opts;
  67. handleCommandOptions(argc, argv, opts);
  68. preinitDLL(console, false);
  69. logConfig.configure();
  70. loadDLLClasses();
  71. std::srand(static_cast<uint32_t>(time(nullptr)));
  72. {
  73. bool connectToLobby = opts.count("lobby");
  74. bool runByClient = opts.count("runByClient");
  75. uint16_t port = 3030;
  76. if(opts.count("port"))
  77. port = opts["port"].as<uint16_t>();
  78. CVCMIServer server(port, connectToLobby, runByClient);
  79. server.run();
  80. // CVCMIServer destructor must be called here - before VLC cleanup
  81. }
  82. logConfig.deconfigure();
  83. vstd::clear_pointer(VLC);
  84. return 0;
  85. }