Start.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <AsynchronCall.h>
  12. #include <Text.h>
  13. #include <Zeit.h>
  14. #include "ChunkMap.h"
  15. #include "Server.h"
  16. FactoryCraftServer* mserver = 0;
  17. void exit()
  18. {
  19. if (mserver)
  20. {
  21. std::cout << "The server terminated unexpectedly. Trying to save game "
  22. "progress.\n";
  23. mserver->close();
  24. }
  25. }
  26. class DuplicatingStreamBuf : public std::stringbuf
  27. {
  28. private:
  29. std::ostream& console;
  30. std::ostream& file;
  31. bool hasFile = 0;
  32. Critical cs;
  33. int infoLength;
  34. public:
  35. DuplicatingStreamBuf(std::ostream& console, std::ostream& file)
  36. : std::stringbuf(),
  37. console(console),
  38. file(file),
  39. hasFile(1),
  40. infoLength(0)
  41. {}
  42. DuplicatingStreamBuf(std::ostream& console)
  43. : std::stringbuf(),
  44. console(console),
  45. file(console),
  46. hasFile(0),
  47. infoLength(0)
  48. {}
  49. int sync() override
  50. {
  51. cs.lock();
  52. if (str().length() == 0 && !Game::INSTANCE)
  53. {
  54. cs.unlock();
  55. return 0;
  56. }
  57. if (infoLength > 0)
  58. {
  59. console << /* store cursor position */ "\033[s"
  60. << /* move cursor a line up*/ "\033[1A"
  61. << /* clear the line */ "\x1b[2K"
  62. << /* return to beginning of line */ "\r";
  63. }
  64. int newLines = Text(str().c_str()).anzahlVon('\n');
  65. if (str().length() > 0)
  66. {
  67. console << str();
  68. if (str().c_str()[str().length() - 1] != '\n')
  69. {
  70. console << "\n";
  71. newLines++;
  72. }
  73. }
  74. if (Game::INSTANCE)
  75. {
  76. int tps = Game::INSTANCE->getTicksPerSecond();
  77. Framework::Text infoLine = "";
  78. infoLine.append()
  79. << "Players: " << Game::INSTANCE->getPlayerCount()
  80. << "\tChunks: " << Game::INSTANCE->getChunkCount() << "\ttps: ";
  81. if (tps < 15)
  82. { // red
  83. infoLine += "\033[1;31m";
  84. }
  85. else if (tps < 20)
  86. { // yellow
  87. infoLine += "\033[1;33m";
  88. }
  89. else
  90. { // green
  91. infoLine += "\033[1;32m";
  92. }
  93. infoLine.append() << tps << /* reset color */ "\033[0m"
  94. << "\tAverage Tick Time: "
  95. << Game::INSTANCE->getAverageTickTime() << "\n";
  96. if (infoLength > 0)
  97. {
  98. infoLine.append()
  99. << /* restore cursor position */ "\033[u"
  100. << /* set cursor down by amount of new lines */
  101. (newLines > 0 ? (Text("\033[") + newLines + "B").getText()
  102. : "");
  103. // << "\x1b[0K";
  104. }
  105. infoLength = infoLine.getLength();
  106. console << infoLine << std::flush;
  107. }
  108. if (hasFile) file << str() << std::flush;
  109. str("");
  110. cs.unlock();
  111. return 0;
  112. }
  113. };
  114. int main()
  115. {
  116. Framework::initFramework();
  117. initializeBlockTypes();
  118. initializeItemTypes();
  119. initializeEntityTypes();
  120. initializeDimensions();
  121. initializeMultiblockTypes();
  122. #ifndef _WINDOWS
  123. struct rlimit core_limits;
  124. core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
  125. setrlimit(RLIMIT_CORE, &core_limits);
  126. #endif
  127. Zeit* z = getZeit();
  128. Text* pfad = new Text("log/");
  129. pfad->append(z->getZeit("y-m-d_h-i-s.log"));
  130. z->release();
  131. DateiPfadErstellen(pfad->getText());
  132. std::ofstream file;
  133. std::streambuf* sbuf = std::cout.rdbuf();
  134. #ifndef _DEBUG
  135. file.open(pfad->getText());
  136. std::ostream newCout(sbuf);
  137. DuplicatingStreamBuf duplicator(newCout, file);
  138. std::cout.rdbuf(&duplicator);
  139. #else
  140. std::ostream newCout(sbuf);
  141. DuplicatingStreamBuf duplicator(newCout);
  142. std::cout.rdbuf(&duplicator);
  143. #endif
  144. pfad->release();
  145. std::cout << "Starting...\n";
  146. std::cout << "Loading config file fcInit.ini ...\n";
  147. InitDatei* dat = new InitDatei("fcInit.ini");
  148. if (!dat->laden())
  149. {
  150. std::cout << "error: Datei konnte nicht gelesen werden. Das Programm "
  151. "wird geschlossen.\n";
  152. dat->release();
  153. #ifndef _DEBUG
  154. file.close();
  155. #endif
  156. std::cout.rdbuf(sbuf);
  157. exit(1);
  158. }
  159. const char* wichtig[]
  160. = {"SSLPort", "SSLCert", "SSLKey", "SSLPasswort", "Port"};
  161. for (const char* w : wichtig)
  162. {
  163. if (!dat->wertExistiert(w))
  164. {
  165. std::cout << "error: The value '" << w
  166. << "' was not specified. The Server can not start.\n";
  167. dat->release();
  168. file.close();
  169. std::cout.rdbuf(sbuf);
  170. exit(1);
  171. }
  172. }
  173. mserver = new FactoryCraftServer(dat);
  174. std::atexit(exit);
  175. signal(SIGTERM, exit);
  176. signal(SIGSEGV, exit);
  177. signal(SIGILL, exit);
  178. signal(SIGABRT, exit);
  179. signal(SIGFPE, exit);
  180. signal(SIGINT, exit);
  181. new Framework::AsynchronCall("Commander", []() {
  182. while (mserver)
  183. {
  184. std::string line;
  185. std::getline(std::cin, line);
  186. if (!mserver) return;
  187. std::cout << std::flush << "\033[1A"
  188. << "\x1b[2K"
  189. << "\x1b[0G" << line << "\n\033[1A\033[s" << std::flush;
  190. if (Text(line.c_str()) == Text("exit"))
  191. {
  192. std::cout << "The server will be terminated and the game "
  193. "progress will be saved.\n";
  194. mserver->close();
  195. return;
  196. }
  197. else if (Game::INSTANCE)
  198. {
  199. Game::INSTANCE->zChat()->zCommandExecutor()->execute(
  200. line.c_str(), 0);
  201. }
  202. }
  203. });
  204. std::cout << "The Server is now running.\n";
  205. mserver->run();
  206. mserver->release();
  207. mserver = 0;
  208. if (Game::INSTANCE)
  209. {
  210. Game::INSTANCE->warteAufThread(100000000);
  211. Game::INSTANCE->release();
  212. Game::INSTANCE = 0;
  213. }
  214. dat->release();
  215. std::cout << "The server was shut down successfully.\n" << std::flush;
  216. #ifndef _DEBUG
  217. file.close();
  218. #endif
  219. std::cout.rdbuf(sbuf);
  220. Framework::releaseFramework();
  221. return 0;
  222. }