EntryPoint.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. // EntryPoint.cpp : Defines the entry point for the console application.
  11. #include "StdInc.h"
  12. #include "../Global.h"
  13. #include "../client/ClientCommandManager.h"
  14. #include "../client/CMT.h"
  15. #include "../client/CPlayerInterface.h"
  16. #include "../client/CServerHandler.h"
  17. #include "../client/GameEngine.h"
  18. #include "../client/GameInstance.h"
  19. #include "../client/gui/CursorHandler.h"
  20. #include "../client/gui/WindowHandler.h"
  21. #include "../client/mainmenu/CMainMenu.h"
  22. #include "../client/render/Graphics.h"
  23. #include "../client/render/IRenderHandler.h"
  24. #include "../client/windows/CMessage.h"
  25. #include "../client/windows/InfoWindows.h"
  26. #include "../lib/AsyncRunner.h"
  27. #include "../lib/CConsoleHandler.h"
  28. #include "../lib/CConfigHandler.h"
  29. #include "../lib/CThreadHelper.h"
  30. #include "../lib/ExceptionsCommon.h"
  31. #include "../lib/filesystem/Filesystem.h"
  32. #include "../lib/logging/CBasicLogConfigurator.h"
  33. #include "../lib/modding/IdentifierStorage.h"
  34. #include "../lib/modding/CModHandler.h"
  35. #include "../lib/modding/ModDescription.h"
  36. #include "../lib/texts/MetaString.h"
  37. #include "../lib/GameLibrary.h"
  38. #include "../lib/VCMIDirs.h"
  39. #include <boost/program_options.hpp>
  40. #include <vstd/StringUtils.h>
  41. #include <SDL_main.h>
  42. #include <SDL.h>
  43. #ifdef VCMI_ANDROID
  44. #include "../lib/CAndroidVMHelper.h"
  45. #include <SDL_system.h>
  46. #endif
  47. #if __MINGW32__
  48. #undef main
  49. #endif
  50. namespace po = boost::program_options;
  51. namespace po_style = boost::program_options::command_line_style;
  52. static std::atomic<bool> headlessQuit = false;
  53. static std::optional<std::string> criticalInitializationError;
  54. static void init()
  55. {
  56. try
  57. {
  58. CStopWatch tmh;
  59. LIBRARY->initializeLibrary();
  60. logGlobal->info("Initializing VCMI_Lib: %d ms", tmh.getDiff());
  61. }
  62. catch (const DataLoadingException & e)
  63. {
  64. criticalInitializationError = e.what();
  65. return;
  66. }
  67. // Debug code to load all maps on start
  68. //ClientCommandManager commandController;
  69. //commandController.processCommand("translate maps", false);
  70. }
  71. static void checkForModLoadingFailure()
  72. {
  73. const auto & brokenMods = LIBRARY->identifiersHandler->getModsWithFailedRequests();
  74. if (!brokenMods.empty())
  75. {
  76. MetaString messageText;
  77. messageText.appendTextID("vcmi.client.errors.modLoadingFailure");
  78. for (const auto & modID : brokenMods)
  79. {
  80. messageText.appendRawString(LIBRARY->modh->getModInfo(modID).getName());
  81. messageText.appendEOL();
  82. }
  83. CInfoWindow::showInfoDialog(messageText.toString(), {});
  84. }
  85. }
  86. static void prog_version()
  87. {
  88. printf("%s\n", GameConstants::VCMI_VERSION.c_str());
  89. std::cout << VCMIDirs::get().genHelpString();
  90. }
  91. static void prog_help(const po::options_description &opts)
  92. {
  93. auto time = std::time(nullptr);
  94. printf("%s - A Heroes of Might and Magic 3 clone\n", GameConstants::VCMI_VERSION.c_str());
  95. printf("Copyright (C) 2007-%d VCMI dev team - see AUTHORS file\n", std::localtime(&time)->tm_year + 1900);
  96. printf("This is free software; see the source for copying conditions. There is NO\n");
  97. printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
  98. printf("\n");
  99. std::cout << opts;
  100. }
  101. #if defined(VCMI_WINDOWS) && !defined(__GNUC__) && defined(VCMI_WITH_DEBUG_CONSOLE)
  102. int wmain(int argc, wchar_t* argv[])
  103. #elif defined(VCMI_MOBILE)
  104. int SDL_main(int argc, char *argv[])
  105. #else
  106. int main(int argc, char * argv[])
  107. #endif
  108. {
  109. #ifdef VCMI_ANDROID
  110. CAndroidVMHelper::initClassloader(SDL_AndroidGetJNIEnv());
  111. // boost will crash without this
  112. setenv("LANG", "C", 1);
  113. #endif
  114. #if !defined(VCMI_MOBILE)
  115. // Correct working dir executable folder (not bundle folder) so we can use executable relative paths
  116. boost::filesystem::current_path(boost::filesystem::system_complete(argv[0]).parent_path());
  117. #endif
  118. std::cout << "Starting... " << std::endl;
  119. po::options_description opts("Allowed options");
  120. po::variables_map vm;
  121. opts.add_options()
  122. ("help,h", "display help and exit")
  123. ("version,v", "display version information and exit")
  124. ("testmap", po::value<std::string>(), "")
  125. ("testsave", po::value<std::string>(), "")
  126. ("logLocation", po::value<std::string>(), "new location for log files")
  127. ("spectate,s", "enable spectator interface for AI-only games")
  128. ("spectate-ignore-hero", "wont follow heroes on adventure map")
  129. ("spectate-hero-speed", po::value<int>(), "hero movement speed on adventure map")
  130. ("spectate-battle-speed", po::value<int>(), "battle animation speed for spectator")
  131. ("spectate-skip-battle", "skip battles in spectator view")
  132. ("spectate-skip-battle-result", "skip battle result window")
  133. ("onlyAI", "allow one to run without human player, all players will be default AI")
  134. ("headless", "runs without GUI, implies --onlyAI")
  135. ("ai", po::value<std::vector<std::string>>(), "AI to be used for the player, can be specified several times for the consecutive players")
  136. ("oneGoodAI", "puts one default AI and the rest will be EmptyAI")
  137. ("autoSkip", "automatically skip turns in GUI")
  138. ("disable-video", "disable video player")
  139. ("nointro,i", "skips intro movies")
  140. ("donotstartserver,d","do not attempt to start server and just connect to it instead server")
  141. ("serverport", po::value<si64>(), "override port specified in config file")
  142. ("savefrequency", po::value<si64>(), "limit auto save creation to each N days");
  143. if(argc > 1)
  144. {
  145. try
  146. {
  147. po::store(po::parse_command_line(argc, argv, opts, po_style::unix_style|po_style::case_insensitive), vm);
  148. }
  149. catch(boost::program_options::error &e)
  150. {
  151. std::cerr << "Failure during parsing command-line options:\n" << e.what() << std::endl;
  152. }
  153. }
  154. po::notify(vm);
  155. if(vm.count("help"))
  156. {
  157. prog_help(opts);
  158. #ifdef VCMI_IOS
  159. exit(0);
  160. #else
  161. return 0;
  162. #endif
  163. }
  164. if(vm.count("version"))
  165. {
  166. prog_version();
  167. #ifdef VCMI_IOS
  168. exit(0);
  169. #else
  170. return 0;
  171. #endif
  172. }
  173. // Init old logging system and new (temporary) logging system
  174. CStopWatch total;
  175. CStopWatch pomtime;
  176. std::cout.flags(std::ios::unitbuf);
  177. setThreadNameLoggingOnly("MainGUI");
  178. boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "VCMI_Client_log.txt";
  179. if(vm.count("logLocation"))
  180. logPath = vm["logLocation"].as<std::string>() + "/VCMI_Client_log.txt";
  181. #ifndef VCMI_IOS
  182. auto callbackFunction = [](std::string buffer, bool calledFromIngameConsole)
  183. {
  184. ClientCommandManager commandController;
  185. commandController.processCommand(buffer, calledFromIngameConsole);
  186. };
  187. CConsoleHandler console(callbackFunction);
  188. console.start();
  189. CBasicLogConfigurator logConfigurator(logPath, &console);
  190. #else
  191. CBasicLogConfigurator logConfigurator(logPath, nullptr);
  192. #endif
  193. logConfigurator.configureDefault();
  194. logGlobal->info("Starting client of '%s'", GameConstants::VCMI_VERSION);
  195. logGlobal->info("Creating console and configuring logger: %d ms", pomtime.getDiff());
  196. logGlobal->info("The log file will be saved to %s", logPath);
  197. // Init filesystem and settings
  198. try
  199. {
  200. LIBRARY = new GameLibrary;
  201. LIBRARY->initializeFilesystem(false);
  202. }
  203. catch (const DataLoadingException & e)
  204. {
  205. handleFatalError(e.what(), true);
  206. }
  207. Settings session = settings.write["session"];
  208. auto setSettingBool = [&](const std::string & key, const std::string & arg) {
  209. Settings s = settings.write(vstd::split(key, "/"));
  210. if(vm.count(arg))
  211. s->Bool() = true;
  212. else if(s->isNull())
  213. s->Bool() = false;
  214. };
  215. auto setSettingInteger = [&](const std::string & key, const std::string & arg, si64 defaultValue) {
  216. Settings s = settings.write(vstd::split(key, "/"));
  217. if(vm.count(arg))
  218. s->Integer() = vm[arg].as<si64>();
  219. else if(s->isNull())
  220. s->Integer() = defaultValue;
  221. };
  222. setSettingBool("session/onlyai", "onlyAI");
  223. setSettingBool("session/disableVideo", "disable-video");
  224. if(vm.count("headless"))
  225. {
  226. session["headless"].Bool() = true;
  227. session["onlyai"].Bool() = true;
  228. }
  229. else if(vm.count("spectate"))
  230. {
  231. session["spectate"].Bool() = true;
  232. session["spectate-ignore-hero"].Bool() = vm.count("spectate-ignore-hero");
  233. session["spectate-skip-battle"].Bool() = vm.count("spectate-skip-battle");
  234. session["spectate-skip-battle-result"].Bool() = vm.count("spectate-skip-battle-result");
  235. if(vm.count("spectate-hero-speed"))
  236. session["spectate-hero-speed"].Integer() = vm["spectate-hero-speed"].as<int>();
  237. if(vm.count("spectate-battle-speed"))
  238. session["spectate-battle-speed"].Float() = vm["spectate-battle-speed"].as<int>();
  239. }
  240. // Server settings
  241. setSettingBool("session/donotstartserver", "donotstartserver");
  242. // Init special testing settings
  243. setSettingInteger("session/serverport", "serverport", 0);
  244. setSettingInteger("general/saveFrequency", "savefrequency", 1);
  245. // Initialize logging based on settings
  246. logConfigurator.configure();
  247. logGlobal->debug("settings = %s", settings.toJsonNode().toString());
  248. // Some basic data validation to produce better error messages in cases of incorrect install
  249. auto testFile = [](const std::string & filename, const std::string & message)
  250. {
  251. if (!CResourceHandler::get()->existsResource(ResourcePath(filename)))
  252. handleFatalError(message, false);
  253. };
  254. testFile("DATA/HELP.TXT", "VCMI requires Heroes III: Shadow of Death or Heroes III: Complete data files to run!");
  255. testFile("DATA/TENTCOLR.TXT", "Heroes III: Restoration of Erathia (including HD Edition) data files are not supported!");
  256. testFile("MODS/VCMI/MOD.JSON", "VCMI installation is corrupted!\nBuilt-in mod was not found!");
  257. testFile("DATA/NOTOSERIF-MEDIUM.TTF", "VCMI installation is corrupted!\nBuilt-in font was not found!\nManually deleting '" + VCMIDirs::get().userDataPath().string() + "/Mods/VCMI' directory (if it exists)\nor clearing app data and reimporting Heroes III files may fix this problem.");
  258. testFile("DATA/PLAYERS.PAL", "Heroes III data files (Data/H3Bitmap.lod) are incomplete or corruped!\n Please reinstall them.");
  259. testFile("SPRITES/DEFAULT.DEF", "Heroes III data files (Data/H3Sprite.lod) are incomplete or corruped!\n Please reinstall them.");
  260. srand ( (unsigned int)time(nullptr) );
  261. if(!settings["session"]["headless"].Bool())
  262. ENGINE = std::make_unique<GameEngine>();
  263. GAME = std::make_unique<GameInstance>();
  264. if (ENGINE)
  265. ENGINE->setEngineUser(GAME.get());
  266. #ifndef VCMI_NO_THREADED_LOAD
  267. //we can properly play intro only in the main thread, so we have to move loading to the separate thread
  268. std::thread loading([]()
  269. {
  270. setThreadName("initialize");
  271. init();
  272. });
  273. #else
  274. init();
  275. #endif
  276. #ifndef VCMI_NO_THREADED_LOAD
  277. #ifdef VCMI_ANDROID // android loads the data quite slowly so we display native progressbar to prevent having only black screen for few seconds
  278. {
  279. CAndroidVMHelper vmHelper;
  280. vmHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "showProgress");
  281. #endif // ANDROID
  282. loading.join();
  283. #ifdef VCMI_ANDROID
  284. vmHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "hideProgress");
  285. }
  286. #endif // ANDROID
  287. #endif // THREADED
  288. if (criticalInitializationError.has_value())
  289. {
  290. handleFatalError(criticalInitializationError.value(), false);
  291. }
  292. if (ENGINE)
  293. {
  294. pomtime.getDiff();
  295. graphics = new Graphics(); // should be before curh
  296. ENGINE->renderHandler().onLibraryLoadingFinished(LIBRARY);
  297. CMessage::init();
  298. logGlobal->info("Message handler: %d ms", pomtime.getDiff());
  299. ENGINE->cursor().init();
  300. ENGINE->cursor().show();
  301. }
  302. logGlobal->info("Initialization of VCMI (together): %d ms", total.getDiff());
  303. session["autoSkip"].Bool() = vm.count("autoSkip");
  304. session["oneGoodAI"].Bool() = vm.count("oneGoodAI");
  305. session["aiSolo"].Bool() = false;
  306. if(vm.count("testmap"))
  307. {
  308. session["testmap"].String() = vm["testmap"].as<std::string>();
  309. session["onlyai"].Bool() = true;
  310. GAME->server().debugStartTest(session["testmap"].String(), false);
  311. }
  312. else if(vm.count("testsave"))
  313. {
  314. session["testsave"].String() = vm["testsave"].as<std::string>();
  315. session["onlyai"].Bool() = true;
  316. GAME->server().debugStartTest(session["testsave"].String(), true);
  317. }
  318. else if (!settings["session"]["headless"].Bool())
  319. {
  320. GAME->mainmenu()->makeActiveInterface();
  321. bool playIntroVideo = !vm.count("battle") && !vm.count("nointro") && settings["video"]["showIntro"].Bool();
  322. if(playIntroVideo)
  323. GAME->mainmenu()->playIntroVideos();
  324. else
  325. GAME->mainmenu()->playMusic();
  326. }
  327. #ifndef VCMI_UNIX
  328. // on Linux, name of main thread is also name of our process. Which we don't want to change
  329. setThreadName("MainGUI");
  330. #endif
  331. try
  332. {
  333. if (ENGINE)
  334. {
  335. checkForModLoadingFailure();
  336. ENGINE->mainLoop();
  337. }
  338. else
  339. {
  340. while(!headlessQuit)
  341. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  342. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  343. }
  344. }
  345. catch (const GameShutdownException & )
  346. {
  347. // no-op - just break out of main loop
  348. logGlobal->info("Main loop termination requested");
  349. }
  350. GAME->server().endNetwork();
  351. if(!settings["session"]["headless"].Bool())
  352. {
  353. if(GAME->server().client)
  354. GAME->server().endGameplay();
  355. if (ENGINE)
  356. ENGINE->windows().clear();
  357. }
  358. GAME.reset();
  359. if(!settings["session"]["headless"].Bool())
  360. {
  361. CMessage::dispose();
  362. delete graphics;
  363. graphics = nullptr;
  364. }
  365. // must be executed before reset - since unique_ptr resets pointer to null before calling destructor
  366. ENGINE->async().wait();
  367. ENGINE.reset();
  368. delete LIBRARY;
  369. LIBRARY = nullptr;
  370. logConfigurator.deconfigure();
  371. std::cout << "Ending...\n";
  372. return 0;
  373. }
  374. /// Notify user about encountered fatal error and terminate the game
  375. /// TODO: decide on better location for this method
  376. void handleFatalError(const std::string & message, bool terminate)
  377. {
  378. logGlobal->error("FATAL ERROR ENCOUNTERED, VCMI WILL NOW TERMINATE");
  379. logGlobal->error("Reason: %s", message);
  380. std::string messageToShow = "Fatal error! " + message;
  381. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error!", messageToShow.c_str(), nullptr);
  382. if (terminate)
  383. throw std::runtime_error(message);
  384. else
  385. ::exit(1);
  386. }