Start.cpp 6.4 KB

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