aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/CustomPayloadPacket.cpp
blob: 46fde5aafcd1fb3a86ff2f6412feac3d77a6bbb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "stdafx.h"
#include "InputOutputStream.h"
#include "PacketListener.h"
#include "BasicTypeContainers.h"
#include "CustomPayloadPacket.h"

// Mojang-defined custom packets
const wstring CustomPayloadPacket::CUSTOM_BOOK_PACKET = L"MC|BEdit";
const wstring CustomPayloadPacket::CUSTOM_BOOK_SIGN_PACKET = L"MC|BSign";
const wstring CustomPayloadPacket::TEXTURE_PACK_PACKET = L"MC|TPack";
const wstring CustomPayloadPacket::TRADER_LIST_PACKET = L"MC|TrList";
const wstring CustomPayloadPacket::TRADER_SELECTION_PACKET = L"MC|TrSel";
const wstring CustomPayloadPacket::SET_ADVENTURE_COMMAND_PACKET = L"MC|AdvCdm";
const wstring CustomPayloadPacket::SET_BEACON_PACKET = L"MC|Beacon";
const wstring CustomPayloadPacket::SET_ITEM_NAME_PACKET = L"MC|ItemName";

CustomPayloadPacket::CustomPayloadPacket()
{
}

CustomPayloadPacket::CustomPayloadPacket(const wstring &identifier, byteArray data)
{
	this->identifier = identifier;
	this->data = data;

	if (data.data != nullptr)
	{
		length = data.length;

		if (length > Short::MAX_VALUE)
		{
			app.DebugPrintf("Payload may not be larger than 32K\n");
#ifndef _CONTENT_PACKAGE
			__debugbreak();
#endif
			//throw new IllegalArgumentException("Payload may not be larger than 32k");
		}
	}
}

void CustomPayloadPacket::read(DataInputStream *dis)
{
	identifier = readUtf(dis, 20);
	length = dis->readShort();

	if (length > 0 && length < Short::MAX_VALUE)
	{
		if(data.data != nullptr)
		{
			delete [] data.data;
		}
		data = byteArray(length);
		dis->readFully(data);
	}
}

void CustomPayloadPacket::write(DataOutputStream *dos)
{
	writeUtf(identifier, dos);
	dos->writeShort(static_cast<short>(length));
	if (data.data != nullptr)
	{
		dos->write(data);
	}
}

void CustomPayloadPacket::handle(PacketListener *listener)
{
	listener->handleCustomPayload( shared_from_this() );
}

int CustomPayloadPacket::getEstimatedSize()
{
	return 2 + identifier.length() * 2 + 2 + length;
}