FactoryClient.cpp 15 KB

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