EntryPoint.cpp 3.3 KB

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