EntryPoint.cpp 3.4 KB

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