Start.cpp 6.7 KB

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