Start.cpp 7.5 KB

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