Start.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <csignal>
  2. #include <cstdlib>
  3. #include <Datei.h>
  4. #include <fstream>
  5. #include <Globals.h>
  6. #include <iostream>
  7. #include <Klient.h>
  8. #ifndef _WINDOWS
  9. #include <sys/resource.h>
  10. #endif
  11. #include <Text.h>
  12. #include <Zeit.h>
  13. #include <AsynchronCall.h>
  14. #include "Server.h"
  15. #include "ChunkMap.h"
  16. FactoryCraftServer* mserver = 0;
  17. void exit()
  18. {
  19. if (mserver)
  20. {
  21. std::cout << "The server terminated unexpectedly. Trying to save game progress.\n";
  22. mserver->close();
  23. }
  24. }
  25. int main()
  26. {
  27. Framework::initFramework();
  28. initializeBlockTypes();
  29. initializeItemTypes();
  30. initializeEntityTypes();
  31. initializeDimensions();
  32. initializeMultiblockTypes();
  33. #ifndef _WINDOWS
  34. struct rlimit core_limits;
  35. core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
  36. setrlimit(RLIMIT_CORE, &core_limits);
  37. #endif
  38. Zeit* z = getZeit();
  39. Text* pfad = new Text("log/");
  40. pfad->append(z->getZeit("y-m-d_h-i-s.log"));
  41. z->release();
  42. DateiPfadErstellen(pfad->getText());
  43. std::ofstream file;
  44. file.open(pfad->getText());
  45. std::streambuf* sbuf = std::cout.rdbuf();
  46. #ifndef _DEBUG
  47. std::cout.rdbuf( file.rdbuf() );
  48. #endif
  49. pfad->release();
  50. std::cout << "Starting...\n";
  51. std::cout << "Loading config file fcInit.ini ...\n";
  52. InitDatei* dat = new InitDatei("fcInit.ini");
  53. if (!dat->laden())
  54. {
  55. std::cout << "error: Datei konnte nicht gelesen werden. Das Programm "
  56. "wird geschlossen.\n";
  57. dat->release();
  58. file.close();
  59. std::cout.rdbuf(sbuf);
  60. exit(1);
  61. }
  62. const char* wichtig[]
  63. = {"SSLPort", "SSLCert", "SSLKey", "SSLPasswort", "Port"};
  64. for (const char* w : wichtig)
  65. {
  66. if (!dat->wertExistiert(w))
  67. {
  68. std::cout
  69. << "error: The value '" << w
  70. << "' was not specified. The Server can not start.\n";
  71. dat->release();
  72. file.close();
  73. std::cout.rdbuf(sbuf);
  74. exit(1);
  75. }
  76. }
  77. mserver = new FactoryCraftServer(dat);
  78. std::atexit(exit);
  79. signal(SIGTERM, exit);
  80. signal(SIGSEGV, exit);
  81. signal(SIGILL, exit);
  82. signal(SIGABRT, exit);
  83. signal(SIGFPE, exit);
  84. signal(SIGINT, exit);
  85. new Framework::AsynchronCall("Commander", []() {
  86. while (mserver)
  87. {
  88. std::string line;
  89. std::getline(std::cin, line);
  90. if (Text(line.c_str()) == Text("exit"))
  91. {
  92. std::cout << "The server will be terminated and the game progress will be saved.\n";
  93. mserver->close();
  94. return;
  95. }
  96. else if (Game::INSTANCE)
  97. {
  98. Game::INSTANCE->zChat()->zCommandExecutor()->execute(
  99. line.c_str(), 0);
  100. }
  101. }
  102. });
  103. std::cout << "The Server is now running.\n";
  104. mserver->run();
  105. mserver->release();
  106. mserver = 0;
  107. if (Game::INSTANCE)
  108. {
  109. Game::INSTANCE->warteAufThread(100000000);
  110. Game::INSTANCE->release();
  111. Game::INSTANCE = 0;
  112. }
  113. dat->release();
  114. std::cout << "The server was shut down successfully.\n";
  115. file.close();
  116. std::cout.rdbuf(sbuf);
  117. Framework::releaseFramework();
  118. return 0;
  119. }