FactoryClient.cpp 16 KB

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