DarkflameServer/dNet/WorldPackets.cpp

234 lines
8.8 KiB
C++
Raw Normal View History

#include "dCommonVars.h"
#include "WorldPackets.h"
#include "BitStream.h"
#include "BitstreamUtils.h"
#include "GeneralUtils.h"
#include "User.h"
#include "Character.h"
#include "dLogger.h"
#include <iostream>
#include "Game.h"
#include "LDFFormat.h"
#include "dServer.h"
#include "dZoneManager.h"
#include "CharacterComponent.h"
#include "ZCompression.h"
#include "eConnectionType.h"
void WorldPackets::SendLoadStaticZone(const SystemAddress& sysAddr, float x, float y, float z, uint32_t checksum) {
2022-07-28 13:39:57 +00:00
RakNet::BitStream bitStream;
BitstreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::LOAD_STATIC_ZONE);
auto zone = Game::zoneManager->GetZone()->GetZoneID();
bitStream.Write(static_cast<uint16_t>(zone.GetMapID()));
bitStream.Write(static_cast<uint16_t>(zone.GetInstanceID()));
//bitStream.Write(static_cast<uint32_t>(zone.GetCloneID()));
bitStream.Write(0);
2022-07-28 13:39:57 +00:00
bitStream.Write(checksum);
bitStream.Write(static_cast<uint16_t>(0)); // ??
2022-07-28 13:39:57 +00:00
bitStream.Write(x);
bitStream.Write(y);
bitStream.Write(z);
2022-07-28 13:39:57 +00:00
bitStream.Write(static_cast<uint32_t>(0)); // Change this to eventually use 4 on activity worlds
2022-08-05 13:40:12 +00:00
SEND_PACKET;
}
2022-07-28 13:39:57 +00:00
void WorldPackets::SendCharacterList(const SystemAddress& sysAddr, User* user) {
if (!user) return;
RakNet::BitStream bitStream;
BitstreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::CHARACTER_LIST_RESPONSE);
std::vector<Character*> characters = user->GetCharacters();
bitStream.Write(static_cast<uint8_t>(characters.size()));
2022-07-28 13:39:57 +00:00
bitStream.Write(static_cast<uint8_t>(0)); //character index in front, just picking 0
for (uint32_t i = 0; i < characters.size(); ++i) {
bitStream.Write(characters[i]->GetObjectID());
2022-07-28 13:39:57 +00:00
bitStream.Write(static_cast<uint32_t>(0));
bitStream.Write(LUWString(characters[i]->GetName()));
bitStream.Write(LUWString(characters[i]->GetUnapprovedName()));
bitStream.Write(static_cast<uint8_t>(characters[i]->GetNameRejected()));
2022-07-28 13:39:57 +00:00
bitStream.Write(static_cast<uint8_t>(false));
bitStream.Write(LUString("", 10));
bitStream.Write(characters[i]->GetShirtColor());
2022-07-28 13:39:57 +00:00
bitStream.Write(characters[i]->GetShirtStyle());
bitStream.Write(characters[i]->GetPantsColor());
bitStream.Write(characters[i]->GetHairStyle());
bitStream.Write(characters[i]->GetHairColor());
bitStream.Write(characters[i]->GetLeftHand());
bitStream.Write(characters[i]->GetRightHand());
bitStream.Write(characters[i]->GetEyebrows());
bitStream.Write(characters[i]->GetEyes());
bitStream.Write(characters[i]->GetMouth());
bitStream.Write(static_cast<uint32_t>(0));
bitStream.Write(static_cast<uint16_t>(characters[i]->GetZoneID()));
bitStream.Write(static_cast<uint16_t>(characters[i]->GetZoneInstance()));
bitStream.Write(characters[i]->GetZoneClone());
bitStream.Write(characters[i]->GetLastLogin());
const auto& equippedItems = characters[i]->GetEquippedItems();
bitStream.Write(static_cast<uint16_t>(equippedItems.size()));
for (uint32_t j = 0; j < equippedItems.size(); ++j) {
bitStream.Write(equippedItems[j]);
}
}
2022-08-05 13:40:12 +00:00
SEND_PACKET;
}
void WorldPackets::SendCharacterCreationResponse(const SystemAddress& sysAddr, eCharacterCreationResponse response) {
RakNet::BitStream bitStream;
BitstreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::CHARACTER_CREATE_RESPONSE);
bitStream.Write(response);
2022-08-05 13:40:12 +00:00
SEND_PACKET;
}
2022-07-28 13:39:57 +00:00
void WorldPackets::SendCharacterRenameResponse(const SystemAddress& sysAddr, eRenameResponse response) {
RakNet::BitStream bitStream;
BitstreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::CHARACTER_RENAME_RESPONSE);
bitStream.Write(response);
2022-08-05 13:40:12 +00:00
SEND_PACKET;
}
void WorldPackets::SendCharacterDeleteResponse(const SystemAddress& sysAddr, bool response) {
2022-07-28 13:39:57 +00:00
RakNet::BitStream bitStream;
BitstreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::DELETE_CHARACTER_RESPONSE);
2022-07-28 13:39:57 +00:00
bitStream.Write(static_cast<uint8_t>(response));
2022-08-05 13:40:12 +00:00
SEND_PACKET;
}
2022-07-28 13:39:57 +00:00
void WorldPackets::SendTransferToWorld(const SystemAddress& sysAddr, const std::string& serverIP, uint32_t serverPort, bool mythranShift) {
RakNet::BitStream bitStream;
BitstreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::TRANSFER_TO_WORLD);
bitStream.Write(LUString(serverIP));
bitStream.Write(static_cast<uint16_t>(serverPort));
bitStream.Write(static_cast<uint8_t>(mythranShift));
2022-08-05 13:40:12 +00:00
SEND_PACKET;
}
2022-07-28 13:39:57 +00:00
void WorldPackets::SendServerState(const SystemAddress& sysAddr) {
RakNet::BitStream bitStream;
BitstreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::SERVER_STATES);
bitStream.Write(static_cast<uint8_t>(1)); //If the server is receiving this request, it probably is ready anyway.
2022-08-05 13:40:12 +00:00
SEND_PACKET;
}
void WorldPackets::SendCreateCharacter(const SystemAddress& sysAddr, Entity* entity, const std::string& xmlData, const std::u16string& username, eGameMasterLevel gm) {
2022-07-28 13:39:57 +00:00
RakNet::BitStream bitStream;
BitstreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::CREATE_CHARACTER);
2022-07-28 13:39:57 +00:00
RakNet::BitStream data;
data.Write<uint32_t>(7); //LDF key count
auto character = entity->GetComponent<CharacterComponent>();
if (!character) {
Game::logger->Log("WorldPackets", "Entity is not a character?? what??");
return;
}
LDFData<LWOOBJID>* objid = new LDFData<LWOOBJID>(u"objid", entity->GetObjectID());
LDFData<LOT>* lot = new LDFData<LOT>(u"template", 1);
LDFData<std::string>* xmlConfigData = new LDFData<std::string>(u"xmlData", xmlData);
LDFData<std::u16string>* name = new LDFData<std::u16string>(u"name", username);
LDFData<int32_t>* gmlevel = new LDFData<int32_t>(u"gmlevel", static_cast<int32_t>(gm));
LDFData<int32_t>* chatmode = new LDFData<int32_t>(u"chatmode", static_cast<int32_t>(gm));
2022-07-28 13:39:57 +00:00
LDFData<int64_t>* reputation = new LDFData<int64_t>(u"reputation", character->GetReputation());
objid->WriteToPacket(&data);
lot->WriteToPacket(&data);
name->WriteToPacket(&data);
gmlevel->WriteToPacket(&data);
chatmode->WriteToPacket(&data);
xmlConfigData->WriteToPacket(&data);
reputation->WriteToPacket(&data);
delete objid;
delete lot;
delete xmlConfigData;
delete gmlevel;
delete chatmode;
delete name;
delete reputation;
2022-03-31 23:12:36 +00:00
2022-07-28 13:39:57 +00:00
//Compress the data before sending:
const uint32_t reservedSize = ZCompression::GetMaxCompressedLength(data.GetNumberOfBytesUsed());
uint8_t* compressedData = new uint8_t[reservedSize];
// TODO There should be better handling here for not enough memory...
if (!compressedData) return;
2022-07-28 13:39:57 +00:00
size_t size = ZCompression::Compress(data.GetData(), data.GetNumberOfBytesUsed(), compressedData, reservedSize);
assert(size <= reservedSize);
2022-07-28 13:39:57 +00:00
bitStream.Write<uint32_t>(size + 9); //size of data + header bytes (8)
bitStream.Write<uint8_t>(1); //compressed boolean, true
bitStream.Write<uint32_t>(data.GetNumberOfBytesUsed());
bitStream.Write<uint32_t>(size);
/**
* In practice, this warning serves no purpose for us. We allocate the max memory needed on the heap
* and then compress the data. In the off chance that the compression actually increases the size,
* an assertion is done to prevent bad data from being saved or sent.
*/
#pragma warning(disable:6385) // C6385 Reading invalid data from 'compressedData'.
2022-07-28 13:39:57 +00:00
for (size_t i = 0; i < size; i++)
bitStream.Write(compressedData[i]);
#pragma warning(default:6385)
// PacketUtils::SavePacket("chardata.bin", (const char*)bitStream.GetData(), static_cast<uint32_t>(bitStream.GetNumberOfBytesUsed()));
2022-08-05 13:40:12 +00:00
SEND_PACKET;
delete[] compressedData;
2022-08-05 13:40:12 +00:00
Game::logger->Log("WorldPackets", "Sent CreateCharacter for ID: %llu", entity->GetObjectID());
}
2022-07-18 09:01:43 +00:00
void WorldPackets::SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::vector<std::pair<uint8_t, uint8_t>> unacceptedItems) {
2022-08-05 13:40:12 +00:00
CBITSTREAM;
BitstreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::CHAT_MODERATION_STRING);
2022-07-28 13:39:57 +00:00
bitStream.Write<uint8_t>(unacceptedItems.empty()); // Is sentence ok?
bitStream.Write<uint16_t>(0x16); // Source ID, unknown
2022-07-28 13:39:57 +00:00
bitStream.Write(static_cast<uint8_t>(requestID)); // request ID
bitStream.Write(static_cast<char>(0)); // chat mode
2022-07-17 06:54:36 +00:00
bitStream.Write(LUWString(receiver, 42)); // receiver name
2022-07-28 13:39:57 +00:00
for (auto it : unacceptedItems) {
bitStream.Write<uint8_t>(it.first); // start index
bitStream.Write<uint8_t>(it.second); // length
}
2022-07-28 13:39:57 +00:00
for (int i = unacceptedItems.size(); 64 > i; i++) {
bitStream.Write<uint16_t>(0);
}
2022-07-17 06:54:36 +00:00
2022-08-05 13:40:12 +00:00
SEND_PACKET;
}
void WorldPackets::SendGMLevelChange(const SystemAddress& sysAddr, bool success, eGameMasterLevel highestLevel, eGameMasterLevel prevLevel, eGameMasterLevel newLevel) {
2022-08-05 13:40:12 +00:00
CBITSTREAM;
BitstreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::MAKE_GM_RESPONSE);
2022-07-28 13:39:57 +00:00
bitStream.Write<uint8_t>(success);
bitStream.Write(static_cast<uint16_t>(highestLevel));
bitStream.Write(static_cast<uint16_t>(prevLevel));
bitStream.Write(static_cast<uint16_t>(newLevel));
2022-08-05 13:40:12 +00:00
SEND_PACKET;
}