FactoryClient.cpp 16 KB

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