FactoryClient.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #include "FactoryClient.h"
  2. #include <Bild.h>
  3. #include <Textur.h>
  4. #include "Globals.h"
  5. using namespace Network;
  6. using namespace Framework;
  7. FactoryClient::FactoryClient()
  8. {
  9. client = 0;
  10. background = 0;
  11. foreground = 0;
  12. backgroundReader = 0;
  13. foregroundReader = 0;
  14. bgReaderUsage = 0;
  15. fgReaderUsage = 0;
  16. }
  17. FactoryClient::~FactoryClient()
  18. {
  19. if (client) disconnect();
  20. }
  21. void FactoryClient::loadServerInfo()
  22. {
  23. std::cout << "downloading server type information\n";
  24. // receive type information
  25. for (int i = 0; i < blockTypeCount; i++)
  26. blockTypes[i]->release();
  27. delete[] blockTypes;
  28. for (int i = 0; i < itemTypeCount; i++)
  29. itemTypes[i]->release();
  30. delete[] itemTypes;
  31. for (int i = 0; i < entityTypeCount; i++)
  32. entityTypes[i]->release();
  33. delete[] entityTypes;
  34. foregroundReader->lese((char*)&blockTypeCount, 4);
  35. blockTypes = new BlockType*[blockTypeCount];
  36. for (int i = 0; i < blockTypeCount; i++)
  37. {
  38. int id;
  39. foregroundReader->lese((char*)&id, 4);
  40. bool needsInstance;
  41. foregroundReader->lese((char*)&needsInstance, 1);
  42. bool needsSubscription;
  43. foregroundReader->lese((char*)&needsSubscription, 1);
  44. int maxHp;
  45. foregroundReader->lese((char*)&maxHp, 4);
  46. blockTypes[i] = new BlockType(id,
  47. needsInstance,
  48. ModelInfo(foregroundReader),
  49. maxHp,
  50. needsSubscription);
  51. }
  52. foregroundReader->lese((char*)&itemTypeCount, 4);
  53. itemTypes = new ItemType*[itemTypeCount];
  54. for (int i = 0; i < itemTypeCount; i++)
  55. {
  56. int id;
  57. foregroundReader->lese((char*)&id, 4);
  58. char len;
  59. foregroundReader->lese((char*)&len, 1);
  60. char* name = new char[len + 1];
  61. foregroundReader->lese(name, len);
  62. name[len] = 0;
  63. short tlen;
  64. foregroundReader->lese((char*)&tlen, 2);
  65. char* tooltipUIML = new char[tlen + 1];
  66. foregroundReader->lese(tooltipUIML, tlen);
  67. tooltipUIML[tlen] = 0;
  68. itemTypes[i] = new ItemType(
  69. id, ModelInfo(foregroundReader), Text(name), Text(tooltipUIML));
  70. delete[] name;
  71. delete[] tooltipUIML;
  72. }
  73. foregroundReader->lese((char*)&entityTypeCount, 4);
  74. entityTypes = new EntityType*[entityTypeCount];
  75. for (int i = 0; i < entityTypeCount; i++)
  76. {
  77. int id;
  78. foregroundReader->lese((char*)&id, 4);
  79. entityTypes[i] = new EntityType(id, ModelInfo(foregroundReader));
  80. }
  81. // pre rendering item models
  82. Kam3D* kam = new Kam3D();
  83. Welt3D* w = new Welt3D();
  84. w->addDiffuseLight(DiffuseLight{
  85. Vec3<float>(0.5f, 0.5f, -1.f), Vec3<float>(1.f, 1.f, 1.f)});
  86. kam->setWelt(w);
  87. kam->setBildschirmPosition(0, 0);
  88. kam->setBildschirmSize(50, 50);
  89. kam->setPosition(Vec3<float>(0, 0, 0));
  90. kam->setRotation(
  91. {(float)PI / 2.f, 0.f, std::atan2(0.f, -1.f) + (float)PI / 2});
  92. Bild* b = new Bild();
  93. b->neuBild(50, 50, 0);
  94. for (int i = 0; i < itemTypeCount; i++)
  95. {
  96. Model3D* mdl = new Model3D();
  97. Model3DData* data = itemTypes[i]->getItemModel();
  98. if (data)
  99. {
  100. Vec3<float> min = data->getMinPos();
  101. Vec3<float> max = data->getMaxPos();
  102. float maxX = MAX(
  103. MAX(MAX(abs(min.x), abs(max.x)), MAX(abs(min.y), abs(max.y))),
  104. MAX(abs(min.z), abs(max.z)));
  105. kam->setPosition(Vec3<float>(maxX * 5, 0.f, 0.f));
  106. }
  107. mdl->setModelDaten(data);
  108. mdl->setModelTextur(itemTypes[i]->getItemTextur());
  109. mdl->setPosition(Vec3<float>(0.f, 0.f, 0.f));
  110. mdl->setDrehung(0.25f, 0.25f, 0.55f);
  111. w->addZeichnung(mdl);
  112. w->tick(0);
  113. window->zBildschirm()->lock();
  114. DX11Textur* t = (DX11Textur*)window->zBildschirm()
  115. ->zGraphicsApi()
  116. ->createOrGetTextur(
  117. Text("rendered/items/") + itemTypes[i]->getId(),
  118. dynamic_cast<Bild*>(b->getThis()));
  119. window->zBildschirm()->zGraphicsApi()->renderKamera(kam, t);
  120. Bild* result = new Bild();
  121. t->copyToImage(result);
  122. itemTypes[i]->setBild(result);
  123. t->release();
  124. window->zBildschirm()->unlock();
  125. w->removeZeichnung(mdl);
  126. }
  127. b->release();
  128. kam->release();
  129. }
  130. bool FactoryClient::connect(Text ip, unsigned short sslPort)
  131. {
  132. if (client) disconnect();
  133. client = new SSLKlient();
  134. if (!client->verbinde(sslPort, ip)) return false;
  135. char c;
  136. while (client->hatNachricht(1))
  137. client->getNachricht(&c, 1);
  138. this->ip = ip;
  139. return 1;
  140. }
  141. int FactoryClient::ping()
  142. {
  143. ZeitMesser zm;
  144. zm.messungStart();
  145. if (!client->sende("\3", 1)) return -1;
  146. char c;
  147. client->getNachricht(&c, 1);
  148. zm.messungEnde();
  149. return (int)(zm.getSekunden() * 1000);
  150. }
  151. int FactoryClient::status(Framework::Text name, Framework::Text secret)
  152. {
  153. if (!client->sende("\4", 1)) return 404;
  154. char c;
  155. client->getNachricht(&c, 1);
  156. if (c == 1)
  157. {
  158. char len = (char)name.getLength();
  159. client->sende(&len, 1);
  160. client->sende(name, len);
  161. short sLen = (short)secret.getLength();
  162. client->sende((char*)&sLen, 2);
  163. client->sende(secret, sLen);
  164. char res;
  165. client->getNachricht(&res, 1);
  166. if (res == 1) return 200;
  167. if (res == 0) return 403;
  168. }
  169. return 404;
  170. }
  171. int FactoryClient::join(
  172. Framework::Text name, Framework::Text& secret, unsigned short port)
  173. {
  174. client->sende("\1", 1);
  175. char len = (char)name.getLength();
  176. client->sende(&len, 1);
  177. client->sende(name, len);
  178. short sLen = (short)secret.getLength();
  179. client->sende((char*)&sLen, 2);
  180. client->sende(secret, sLen);
  181. char res;
  182. client->getNachricht(&res, 1);
  183. if (res == 1 || res == 2)
  184. {
  185. if (res == 2)
  186. {
  187. client->getNachricht((char*)&sLen, 2);
  188. char* buffer = new char[sLen + 1];
  189. client->getNachricht(buffer, sLen);
  190. buffer[sLen] = 0;
  191. secret = buffer;
  192. delete[] buffer;
  193. }
  194. short keyLen;
  195. client->getNachricht((char*)&keyLen, 2);
  196. char* key = new char[keyLen];
  197. client->getNachricht(key, keyLen);
  198. foreground = new Klient();
  199. if (!foreground->verbinde(port, ip))
  200. {
  201. delete[] key;
  202. return false;
  203. }
  204. if (!foreground->sende((char*)&keyLen, 2))
  205. {
  206. delete[] key;
  207. return false;
  208. }
  209. if (!foreground->sende(key, keyLen))
  210. {
  211. delete[] key;
  212. return false;
  213. }
  214. background = new Klient();
  215. if (!background->verbinde(port, ip))
  216. {
  217. delete[] key;
  218. foreground->release();
  219. foreground = 0;
  220. background->release();
  221. background = 0;
  222. return false;
  223. }
  224. if (!background->sende((char*)&keyLen, 2))
  225. {
  226. delete[] key;
  227. foreground->release();
  228. foreground = 0;
  229. background->release();
  230. background = 0;
  231. return false;
  232. }
  233. if (!background->sende(key, keyLen))
  234. {
  235. delete[] key;
  236. foreground->release();
  237. foreground = 0;
  238. background->release();
  239. background = 0;
  240. return false;
  241. }
  242. delete[] key;
  243. bool bg = 0;
  244. if (!foreground->sende((char*)&bg, 1))
  245. {
  246. delete[] key;
  247. return 201;
  248. }
  249. foregroundReader = new NetworkReader(foreground);
  250. bg = 1;
  251. if (!background->sende((char*)&bg, 1)) return 201;
  252. backgroundReader = new NetworkReader(background);
  253. char res;
  254. foregroundReader->lese(&res, 1);
  255. if (res != 1) return 403;
  256. backgroundReader->lese(&res, 1);
  257. if (res != 1) return 403;
  258. client->trenne();
  259. loadServerInfo();
  260. return 200;
  261. }
  262. if (res == 0) return 403;
  263. return 500;
  264. }
  265. void FactoryClient::disconnect()
  266. {
  267. if (client)
  268. {
  269. NetworkReader* fgReader = foregroundReader;
  270. NetworkReader* bgReader = backgroundReader;
  271. backgroundReader = 0;
  272. foregroundReader = 0;
  273. if (foreground) foreground->trenne();
  274. if (background) background->trenne();
  275. while (fgReaderUsage > 0 || bgReaderUsage > 0)
  276. Sleep(100);
  277. delete fgReader;
  278. delete bgReader;
  279. client->release();
  280. client = 0;
  281. if (foreground) foreground->release();
  282. foreground = 0;
  283. if (background) background->release();
  284. background = 0;
  285. }
  286. }
  287. NetworkReader* FactoryClient::getNextForegroundMessage()
  288. {
  289. fgReaderUsage++;
  290. if (!foreground) return 0;
  291. if (!foreground->isConnected()) return 0;
  292. if (!foreground->hatNachricht(0)) return 0;
  293. return foregroundReader;
  294. }
  295. NetworkReader* FactoryClient::getNextBackgroundMessage()
  296. {
  297. bgReaderUsage++;
  298. if (!background) return 0;
  299. if (!background->isConnected()) return 0;
  300. if (!background->hatNachricht(0)) return 0;
  301. return backgroundReader;
  302. }
  303. void FactoryClient::endMessageReading(bool bg)
  304. {
  305. if (bg)
  306. bgReaderUsage--;
  307. else
  308. fgReaderUsage--;
  309. }
  310. void FactoryClient::sendPlayerAction(char* data, unsigned short length)
  311. {
  312. if (!foreground) return;
  313. cs.lock();
  314. length += 1;
  315. foreground->sende((char*)&length, 2);
  316. char msgId = 2;
  317. foreground->sende(&msgId, 1);
  318. foreground->sende((char*)data, length - 1);
  319. cs.unlock();
  320. }
  321. void FactoryClient::sendPlayerMovement(MovementFrame& frame)
  322. {
  323. if (!foreground) return;
  324. cs.lock();
  325. short length = 38;
  326. foreground->sende((char*)&length, 2);
  327. char msgId = 2; // player message
  328. foreground->sende(&msgId, 1);
  329. foreground->sende(&msgId, 1); // set movement
  330. foreground->sende((char*)&frame.direction.x, 4);
  331. foreground->sende((char*)&frame.direction.y, 4);
  332. foreground->sende((char*)&frame.direction.z, 4);
  333. foreground->sende((char*)&frame.targetPosition.x, 4);
  334. foreground->sende((char*)&frame.targetPosition.y, 4);
  335. foreground->sende((char*)&frame.targetPosition.z, 4);
  336. foreground->sende((char*)&frame.movementFlags, 4);
  337. foreground->sende((char*)&frame.duration, 8);
  338. cs.unlock();
  339. }
  340. void FactoryClient::entityAPIRequest(
  341. int entityId, char* message, unsigned short length)
  342. {
  343. if (!foreground) return;
  344. cs.lock();
  345. length += 5;
  346. foreground->sende((char*)&length, 2);
  347. char msgId = 3;
  348. foreground->sende(&msgId, 1);
  349. foreground->sende((char*)&entityId, 4);
  350. foreground->sende(message, length - 5);
  351. cs.unlock();
  352. }
  353. void FactoryClient::blockAPIRequest(
  354. Vec3<int> pos, char* message, unsigned short length)
  355. {
  356. if (!foreground) return;
  357. cs.lock();
  358. length += 14;
  359. foreground->sende((char*)&length, 2);
  360. char msgId = 1;
  361. foreground->sende(&msgId, 1);
  362. foreground->sende(&msgId, 1);
  363. foreground->sende((char*)&pos.x, 4);
  364. foreground->sende((char*)&pos.y, 4);
  365. foreground->sende((char*)&pos.z, 4);
  366. foreground->sende(message, length - 14);
  367. cs.unlock();
  368. }
  369. void FactoryClient::blockAPIRequest(
  370. int dimensionId, Vec3<int> pos, char* message, unsigned short length)
  371. {
  372. if (!foreground) return;
  373. cs.lock();
  374. length += 18;
  375. foreground->sende((char*)&length, 2);
  376. char msgId = 7;
  377. foreground->sende(&msgId, 1);
  378. foreground->sende((char*)&dimensionId, 4);
  379. msgId = 1;
  380. foreground->sende(&msgId, 1);
  381. foreground->sende((char*)&pos.x, 4);
  382. foreground->sende((char*)&pos.y, 4);
  383. foreground->sende((char*)&pos.z, 4);
  384. foreground->sende(message, length - 18);
  385. cs.unlock();
  386. }
  387. void FactoryClient::chunkAPIRequest(
  388. Punkt center, char* message, unsigned short length)
  389. {
  390. if (!foreground) return;
  391. length += 10;
  392. cs.lock();
  393. foreground->sende((char*)&length, 2);
  394. char type = 1;
  395. foreground->sende(&type, 1);
  396. type = 0;
  397. foreground->sende(&type, 1);
  398. foreground->sende((char*)&center.x, 4);
  399. foreground->sende((char*)&center.y, 4);
  400. foreground->sende(message, length - 10);
  401. cs.unlock();
  402. }
  403. void FactoryClient::dimensionAPIRequest(char* message, unsigned short length)
  404. {
  405. if (!foreground) return;
  406. length += 1;
  407. cs.lock();
  408. foreground->sende((char*)&length, 2);
  409. char type = 1;
  410. foreground->sende(&type, 1);
  411. foreground->sende(message, length - 1);
  412. cs.unlock();
  413. }
  414. void FactoryClient::inventoryAPIRequest(
  415. Framework::Either<int, Framework::VecN<int, 4>> target,
  416. char* message,
  417. unsigned short length)
  418. {
  419. if (!foreground) return;
  420. cs.lock();
  421. length += target.isA() ? 6 : 18;
  422. foreground->sende((char*)&length, 2);
  423. char msgId = 4;
  424. foreground->sende(&msgId, 1);
  425. bool isEntity = target.isA();
  426. foreground->sende((char*)&isEntity, 1);
  427. if (target.isA())
  428. {
  429. int id = target.getA();
  430. foreground->sende((char*)&id, 4);
  431. }
  432. else
  433. {
  434. for (int i = 0; i < 4; i++)
  435. {
  436. int v = target.getB()[i];
  437. foreground->sende((char*)&v, 4);
  438. }
  439. }
  440. foreground->sende(message, length - (target.isA() ? 6 : 18));
  441. cs.unlock();
  442. }
  443. void FactoryClient::craftingUIMLRequest(int itemTypeId)
  444. {
  445. if (!foreground) return;
  446. cs.lock();
  447. short length = 5;
  448. foreground->sende((char*)&length, 2);
  449. char msgId = 5;
  450. foreground->sende(&msgId, 1);
  451. foreground->sende((char*)&itemTypeId, 4);
  452. cs.unlock();
  453. }
  454. void FactoryClient::sendChatMessage(Framework::Text message)
  455. {
  456. if (!background) return;
  457. cs.lock();
  458. short length = message.getLength() + 4;
  459. background->sende((char*)&length, 2);
  460. char msgId = 6;
  461. background->sende(&msgId, 1);
  462. msgId = 0;
  463. background->sende(&msgId, 1);
  464. length = message.getLength();
  465. background->sende((char*)&length, 2);
  466. background->sende(message.getText(), message.getLength());
  467. cs.unlock();
  468. }
  469. void FactoryClient::chatAPIRequest(char* data, unsigned short length)
  470. {
  471. if (!background) return;
  472. cs.lock();
  473. short totalLength = length + 1;
  474. background->sende((char*)&totalLength, 2);
  475. char msgId = 6;
  476. background->sende(&msgId, 1);
  477. background->sende(data, length);
  478. cs.unlock();
  479. }
  480. bool FactoryClient::isConnected()
  481. {
  482. return foreground && background && foreground->isConnected()
  483. && background->isConnected();
  484. }
  485. void FactoryClient::leaveGame()
  486. {
  487. cs.lock();
  488. disconnect();
  489. cs.unlock();
  490. }