FactoryClient.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include <Bild.h>
  2. #include <Textur.h>
  3. #include "FactoryClient.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)
  20. disconnect();
  21. }
  22. bool FactoryClient::connect(Text ip, short port, int accountId, Text secret)
  23. {
  24. if (client)
  25. disconnect();
  26. client = new SSLKlient();
  27. if (!client->verbinde(port, ip))
  28. return false;
  29. if (!client->sende("\1", 1))
  30. return false;
  31. if (!client->sende((char*)&accountId, 4))
  32. return false;
  33. unsigned char len = (unsigned char)secret.getLength();
  34. if (!client->sende((char*)&len, 1))
  35. return false;
  36. if (!client->sende(secret, len))
  37. return false;
  38. char result = 0;
  39. if (!client->getNachricht(&result, 1) || result != 1)
  40. return false;
  41. int keyLen;
  42. if (!client->getNachricht((char*)&keyLen, 4))
  43. return false;
  44. char* key = new char[keyLen];
  45. if (!client->getNachricht(key, keyLen))
  46. return false;
  47. int p;
  48. if (!client->getNachricht((char*)&p, 4))
  49. return false;
  50. foreground = new Klient();
  51. if (!foreground->verbinde(p, ip))
  52. return false;
  53. unsigned short l = (unsigned short)keyLen;
  54. if (!foreground->sende((char*)&l, 2))
  55. return false;
  56. if (!foreground->sende(key, keyLen))
  57. return false;
  58. bool bg = 0;
  59. if (!foreground->sende((char*)&bg, 1))
  60. return false;
  61. foregroundReader = new NetworkReader(foreground);
  62. background = new Klient();
  63. if (!background->verbinde(p, ip))
  64. return false;
  65. if (!background->sende((char*)&l, 2))
  66. return false;
  67. if (!background->sende(key, keyLen))
  68. return false;
  69. bg = 1;
  70. if (!background->sende((char*)&bg, 1))
  71. return false;
  72. backgroundReader = new NetworkReader(background);
  73. client->trenne();
  74. // receive type information
  75. for (int i = 0; i < blockTypeCount; i++)
  76. blockTypes[i]->release();
  77. delete[] blockTypes;
  78. for (int i = 0; i < itemTypeCount; i++)
  79. itemTypes[i]->release();
  80. delete[] itemTypes;
  81. for (int i = 0; i < entityTypeCount; i++)
  82. entityTypes[i]->release();
  83. delete[] entityTypes;
  84. foregroundReader->lese((char*)&blockTypeCount, 4);
  85. blockTypes = new BlockType * [blockTypeCount];
  86. for (int i = 0; i < blockTypeCount; i++)
  87. {
  88. int id;
  89. foregroundReader->lese((char*)&id, 4);
  90. bool needsInstance;
  91. foregroundReader->lese((char*)&needsInstance, 1);
  92. int maxHp;
  93. foregroundReader->lese((char*)&maxHp, 4);
  94. blockTypes[i] = new BlockType(id, needsInstance, ModelInfo(foregroundReader), maxHp);
  95. }
  96. foregroundReader->lese((char*)&itemTypeCount, 4);
  97. itemTypes = new ItemType * [itemTypeCount];
  98. for (int i = 0; i < itemTypeCount; i++)
  99. {
  100. int id;
  101. foregroundReader->lese((char*)&id, 4);
  102. itemTypes[i] = new ItemType(id, ModelInfo(foregroundReader));
  103. }
  104. foregroundReader->lese((char*)&entityTypeCount, 4);
  105. entityTypes = new EntityType * [entityTypeCount];
  106. for (int i = 0; i < entityTypeCount; i++)
  107. {
  108. int id;
  109. foregroundReader->lese((char*)&id, 4);
  110. entityTypes[i] = new EntityType(id, ModelInfo(foregroundReader));
  111. }
  112. // pre rendering item models
  113. Kam3D* kam = new Kam3D();
  114. Welt3D* w = new Welt3D();
  115. w->addDiffuseLight(DiffuseLight{ Vec3<float>(0.5f, 0.5f, -1.f), Vec3<float>(1.f, 1.f, 1.f) });
  116. kam->setWelt(w);
  117. kam->setBildschirmPosition(0, 0);
  118. kam->setBildschirmSize(50, 50);
  119. kam->setPosition(Vec3<float>(0, 0, 0));
  120. kam->setRotation({ 2.f, 0, -1.f });
  121. Bild* b = new Bild();
  122. b->neuBild(50, 50, 0);
  123. for (int i = 0; i < itemTypeCount; i++)
  124. {
  125. Model3D* mdl = new Model3D();
  126. mdl->setModelDaten(itemTypes[i]->getItemModel());
  127. mdl->setModelTextur(itemTypes[i]->getItemTextur());
  128. mdl->setPosition(Vec3<float>(-0.8f, -0.45f, -0.4f));
  129. mdl->setSize(2.f);
  130. w->addZeichnung(mdl);
  131. w->tick(0);
  132. window->zBildschirm()->lock();
  133. DX11Textur* t = (DX11Textur*)window->zBildschirm()->zGraphicsApi()->createOrGetTextur(Text("rendered/items/") + i, dynamic_cast<Bild*>(b->getThis()));
  134. window->zBildschirm()->zGraphicsApi()->renderKamera(kam, t);
  135. Bild* result = new Bild();
  136. t->copyToImage(result);
  137. itemIcons->add(result);
  138. t->release();
  139. window->zBildschirm()->unlock();
  140. w->removeZeichnung(mdl);
  141. }
  142. b->release();
  143. kam->release();
  144. return true;
  145. }
  146. void FactoryClient::disconnect()
  147. {
  148. if (client)
  149. {
  150. NetworkReader* fgReader = foregroundReader;
  151. NetworkReader* bgReader = backgroundReader;
  152. backgroundReader = 0;
  153. foregroundReader = 0;
  154. if (foreground)
  155. foreground->trenne();
  156. if (background)
  157. background->trenne();
  158. while (fgReaderUsage > 0 || bgReaderUsage > 0)
  159. Sleep(100);
  160. delete fgReader;
  161. delete bgReader;
  162. client->release();
  163. client = 0;
  164. if (foreground)
  165. foreground->release();
  166. foreground = 0;
  167. if (background)
  168. background->release();
  169. background = 0;
  170. }
  171. }
  172. NetworkReader* FactoryClient::getNextForegroundMessage()
  173. {
  174. fgReaderUsage++;
  175. if (!foreground)
  176. return 0;
  177. if (!foreground->hatNachricht(0))
  178. return 0;
  179. return foregroundReader;
  180. }
  181. NetworkReader* FactoryClient::getNextBackgroundMessage()
  182. {
  183. bgReaderUsage++;
  184. if (!background)
  185. return 0;
  186. if (!background->hatNachricht(0))
  187. return 0;
  188. return backgroundReader;
  189. }
  190. void FactoryClient::endMessageReading(bool bg)
  191. {
  192. if (bg)
  193. bgReaderUsage--;
  194. else
  195. fgReaderUsage--;
  196. }
  197. void FactoryClient::sendPlayerAction(char* data, unsigned short length)
  198. {
  199. if (!foreground)
  200. return;
  201. cs.lock();
  202. length += 1;
  203. foreground->sende((char*)&length, 2);
  204. char msgId = 2;
  205. foreground->sende(&msgId, 1);
  206. foreground->sende((char*)data, length - 1);
  207. cs.unlock();
  208. }
  209. void FactoryClient::sendPlayerMovement(MovementFrame& frame)
  210. {
  211. if (!foreground)
  212. return;
  213. cs.lock();
  214. short length = 38;
  215. foreground->sende((char*)&length, 2);
  216. char msgId = 2; // player message
  217. foreground->sende(&msgId, 1);
  218. foreground->sende(&msgId, 1); // set movement
  219. foreground->sende((char*)&frame.direction.x, 4);
  220. foreground->sende((char*)&frame.direction.y, 4);
  221. foreground->sende((char*)&frame.direction.z, 4);
  222. foreground->sende((char*)&frame.targetPosition.x, 4);
  223. foreground->sende((char*)&frame.targetPosition.y, 4);
  224. foreground->sende((char*)&frame.targetPosition.z, 4);
  225. foreground->sende((char*)&frame.movementFlags, 4);
  226. foreground->sende((char*)&frame.duration, 8);
  227. cs.unlock();
  228. }
  229. void FactoryClient::entityAPIRequest(int entityId, char* message, unsigned short length)
  230. {
  231. if (!foreground)
  232. return;
  233. cs.lock();
  234. length += 5;
  235. foreground->sende((char*)&length, 2);
  236. char msgId = 3;
  237. foreground->sende(&msgId, 1);
  238. foreground->sende((char*)&entityId, 4);
  239. foreground->sende(message, length - 5);
  240. cs.unlock();
  241. }
  242. void FactoryClient::chunkAPIRequest(Punkt center, char* message, unsigned short length)
  243. {
  244. length += 10;
  245. cs.lock();
  246. foreground->sende((char*)&length, 2);
  247. char type = 1;
  248. foreground->sende(&type, 1);
  249. type = 0;
  250. foreground->sende(&type, 1);
  251. foreground->sende((char*)&center.x, 4);
  252. foreground->sende((char*)&center.y, 4);
  253. foreground->sende(message, length - 10);
  254. cs.unlock();
  255. }
  256. void FactoryClient::inventoryAPIRequest(Framework::Either<int, Framework::VecN<int, 4>> target, char* message, unsigned short length)
  257. {
  258. if (!foreground)
  259. return;
  260. cs.lock();
  261. length += target.isA() ? 6 : 18;
  262. foreground->sende((char*)&length, 2);
  263. char msgId = 4;
  264. foreground->sende(&msgId, 1);
  265. bool isEntity = target.isA();
  266. foreground->sende((char*)&isEntity, 1);
  267. if (target.isA())
  268. {
  269. int id = target.getA();
  270. foreground->sende((char*)&id, 4);
  271. }
  272. else
  273. {
  274. for (int i = 0; i < 4; i++)
  275. {
  276. int v = target.getB()[i];
  277. foreground->sende((char*)&v, 4);
  278. }
  279. }
  280. foreground->sende(message, length - (target.isA() ? 6 : 18));
  281. cs.unlock();
  282. }