123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- #include <Bild.h>
- #include <Textur.h>
- #include "FactoryClient.h"
- #include "Globals.h"
- using namespace Network;
- using namespace Framework;
- FactoryClient::FactoryClient()
- {
- client = 0;
- background = 0;
- foreground = 0;
- backgroundReader = 0;
- foregroundReader = 0;
- bgReaderUsage = 0;
- fgReaderUsage = 0;
- }
- FactoryClient::~FactoryClient()
- {
- if (client)
- disconnect();
- }
- bool FactoryClient::connect(Text ip, short port, int accountId, Text secret)
- {
- if (client)
- disconnect();
- client = new SSLKlient();
- if (!client->verbinde(port, ip))
- return false;
- if (!client->sende("\1", 1))
- return false;
- if (!client->sende((char*)&accountId, 4))
- return false;
- unsigned char len = (unsigned char)secret.getLength();
- if (!client->sende((char*)&len, 1))
- return false;
- if (!client->sende(secret, len))
- return false;
- char result = 0;
- if (!client->getNachricht(&result, 1) || result != 1)
- return false;
- int keyLen;
- if (!client->getNachricht((char*)&keyLen, 4))
- return false;
- char* key = new char[keyLen];
- if (!client->getNachricht(key, keyLen))
- return false;
- int p;
- if (!client->getNachricht((char*)&p, 4))
- return false;
- foreground = new Klient();
- if (!foreground->verbinde(p, ip))
- return false;
- unsigned short l = (unsigned short)keyLen;
- if (!foreground->sende((char*)&l, 2))
- return false;
- if (!foreground->sende(key, keyLen))
- return false;
- bool bg = 0;
- if (!foreground->sende((char*)&bg, 1))
- return false;
- foregroundReader = new NetworkReader(foreground);
- background = new Klient();
- if (!background->verbinde(p, ip))
- return false;
- if (!background->sende((char*)&l, 2))
- return false;
- if (!background->sende(key, keyLen))
- return false;
- bg = 1;
- if (!background->sende((char*)&bg, 1))
- return false;
- backgroundReader = new NetworkReader(background);
- client->trenne();
- // receive type information
- for (int i = 0; i < blockTypeCount; i++)
- blockTypes[i]->release();
- delete[] blockTypes;
- for (int i = 0; i < itemTypeCount; i++)
- itemTypes[i]->release();
- delete[] itemTypes;
- for (int i = 0; i < entityTypeCount; i++)
- entityTypes[i]->release();
- delete[] entityTypes;
- foregroundReader->lese((char*)&blockTypeCount, 4);
- blockTypes = new BlockType * [blockTypeCount];
- for (int i = 0; i < blockTypeCount; i++)
- {
- int id;
- foregroundReader->lese((char*)&id, 4);
- bool needsInstance;
- foregroundReader->lese((char*)&needsInstance, 1);
- int maxHp;
- foregroundReader->lese((char*)&maxHp, 4);
- blockTypes[i] = new BlockType(id, needsInstance, ModelInfo(foregroundReader), maxHp);
- }
- foregroundReader->lese((char*)&itemTypeCount, 4);
- itemTypes = new ItemType * [itemTypeCount];
- for (int i = 0; i < itemTypeCount; i++)
- {
- int id;
- foregroundReader->lese((char*)&id, 4);
- itemTypes[i] = new ItemType(id, ModelInfo(foregroundReader));
- }
- foregroundReader->lese((char*)&entityTypeCount, 4);
- entityTypes = new EntityType * [entityTypeCount];
- for (int i = 0; i < entityTypeCount; i++)
- {
- int id;
- foregroundReader->lese((char*)&id, 4);
- entityTypes[i] = new EntityType(id, ModelInfo(foregroundReader));
- }
- // pre rendering item models
- Kam3D* kam = new Kam3D();
- Welt3D* w = new Welt3D();
- w->addDiffuseLight(DiffuseLight{ Vec3<float>(0.5f, 0.5f, -1.f), Vec3<float>(1.f, 1.f, 1.f) });
- kam->setWelt(w);
- kam->setBildschirmPosition(0, 0);
- kam->setBildschirmSize(50, 50);
- kam->setPosition(Vec3<float>(0, 0, 0));
- kam->setRotation({ 2.f, 0, -1.f });
- Bild* b = new Bild();
- b->neuBild(50, 50, 0);
- for (int i = 0; i < itemTypeCount; i++)
- {
- Model3D* mdl = new Model3D();
- mdl->setModelDaten(itemTypes[i]->getItemModel());
- mdl->setModelTextur(itemTypes[i]->getItemTextur());
- mdl->setPosition(Vec3<float>(-0.8f, -0.45f, -0.4f));
- mdl->setSize(2.f);
- w->addZeichnung(mdl);
- w->tick(0);
- window->zBildschirm()->lock();
- DX11Textur* t = (DX11Textur*)window->zBildschirm()->zGraphicsApi()->createOrGetTextur(Text("rendered/items/") + i, dynamic_cast<Bild*>(b->getThis()));
- window->zBildschirm()->zGraphicsApi()->renderKamera(kam, t);
- Bild* result = new Bild();
- t->copyToImage(result);
- itemIcons->add(result);
- t->release();
- window->zBildschirm()->unlock();
- w->removeZeichnung(mdl);
- }
- b->release();
- kam->release();
- return true;
- }
- void FactoryClient::disconnect()
- {
- if (client)
- {
- NetworkReader* fgReader = foregroundReader;
- NetworkReader* bgReader = backgroundReader;
- backgroundReader = 0;
- foregroundReader = 0;
- if (foreground)
- foreground->trenne();
- if (background)
- background->trenne();
- while (fgReaderUsage > 0 || bgReaderUsage > 0)
- Sleep(100);
- delete fgReader;
- delete bgReader;
- client->release();
- client = 0;
- if (foreground)
- foreground->release();
- foreground = 0;
- if (background)
- background->release();
- background = 0;
- }
- }
- NetworkReader* FactoryClient::getNextForegroundMessage()
- {
- fgReaderUsage++;
- if (!foreground)
- return 0;
- if (!foreground->hatNachricht(0))
- return 0;
- return foregroundReader;
- }
- NetworkReader* FactoryClient::getNextBackgroundMessage()
- {
- bgReaderUsage++;
- if (!background)
- return 0;
- if (!background->hatNachricht(0))
- return 0;
- return backgroundReader;
- }
- void FactoryClient::endMessageReading(bool bg)
- {
- if (bg)
- bgReaderUsage--;
- else
- fgReaderUsage--;
- }
- void FactoryClient::sendPlayerAction(char* data, unsigned short length)
- {
- if (!foreground)
- return;
- cs.lock();
- length += 1;
- foreground->sende((char*)&length, 2);
- char msgId = 2;
- foreground->sende(&msgId, 1);
- foreground->sende((char*)data, length - 1);
- cs.unlock();
- }
- void FactoryClient::sendPlayerMovement(MovementFrame& frame)
- {
- if (!foreground)
- return;
- cs.lock();
- short length = 38;
- foreground->sende((char*)&length, 2);
- char msgId = 2; // player message
- foreground->sende(&msgId, 1);
- foreground->sende(&msgId, 1); // set movement
- foreground->sende((char*)&frame.direction.x, 4);
- foreground->sende((char*)&frame.direction.y, 4);
- foreground->sende((char*)&frame.direction.z, 4);
- foreground->sende((char*)&frame.targetPosition.x, 4);
- foreground->sende((char*)&frame.targetPosition.y, 4);
- foreground->sende((char*)&frame.targetPosition.z, 4);
- foreground->sende((char*)&frame.movementFlags, 4);
- foreground->sende((char*)&frame.duration, 8);
- cs.unlock();
- }
- void FactoryClient::entityAPIRequest(int entityId, char* message, unsigned short length)
- {
- if (!foreground)
- return;
- cs.lock();
- length += 5;
- foreground->sende((char*)&length, 2);
- char msgId = 3;
- foreground->sende(&msgId, 1);
- foreground->sende((char*)&entityId, 4);
- foreground->sende(message, length - 5);
- cs.unlock();
- }
- void FactoryClient::chunkAPIRequest(Punkt center, char* message, unsigned short length)
- {
- length += 10;
- cs.lock();
- foreground->sende((char*)&length, 2);
- char type = 1;
- foreground->sende(&type, 1);
- type = 0;
- foreground->sende(&type, 1);
- foreground->sende((char*)¢er.x, 4);
- foreground->sende((char*)¢er.y, 4);
- foreground->sende(message, length - 10);
- cs.unlock();
- }
- void FactoryClient::inventoryAPIRequest(Framework::Either<int, Framework::VecN<int, 4>> target, char* message, unsigned short length)
- {
- if (!foreground)
- return;
- cs.lock();
- length += target.isA() ? 6 : 18;
- foreground->sende((char*)&length, 2);
- char msgId = 4;
- foreground->sende(&msgId, 1);
- bool isEntity = target.isA();
- foreground->sende((char*)&isEntity, 1);
- if (target.isA())
- {
- int id = target.getA();
- foreground->sende((char*)&id, 4);
- }
- else
- {
- for (int i = 0; i < 4; i++)
- {
- int v = target.getB()[i];
- foreground->sende((char*)&v, 4);
- }
- }
- foreground->sende(message, length - (target.isA() ? 6 : 18));
- cs.unlock();
- }
|