aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/Tag.cpp
blob: eafdc9e87d28f00d949219e90e5d6da9b6d73b16 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "stdafx.h"
#include "Tag.h"
#include "EndTag.h"
#include "ByteTag.h"
#include "ByteArrayTag.h"
#include "DoubleTag.h"
#include "FloatTag.h"
#include "IntTag.h"
#include "LongTag.h"
#include "ShortTag.h"
#include "StringTag.h"
#include "ListTag.h"
#include "CompoundTag.h"

Tag::Tag(const wstring &name)
{
	if (name.empty())
	{
		this->name = L"";
	}
	else
	{
		this->name = name;
	}
}

// 4J - Was Object obj
bool Tag::equals(Tag *obj)
{
	if (obj == nullptr )// || !(obj instanceof Tag))
	{
		return false;
	}
	Tag *o = (Tag *) obj;
	if (getId() != o->getId())
	{
		return false;
	}
	if ( (name.empty() && !o->name.empty()) || (!name.empty() && o->name.empty()))
	{
		return false;
	}
	if (!name.empty() && name.compare(o->name) != 0)
	{
		return false;
	}
	return true;
}

void Tag::print(ostream out)
{
	out << "";
}

void Tag::print(char *prefix, wostream out)
{
	wstring name = getName();

	out << prefix;
	out << getTagName(getId());
	if ( name.length() > 0)
	{
		out << L"(\"" << name << L"\")";
	}
	out << L": ";
	out << toString() << endl;
}

wstring Tag::getName()
{
	return name;
}

Tag *Tag::setName(const wstring& name)
{
	this->name = name;
	return this;
}

Tag *Tag::readNamedTag(DataInput *dis)
{
	return readNamedTag(dis,0);
}

Tag *Tag::readNamedTag(DataInput *dis, int tagDepth)
{
    static __declspec(thread) int depth = 0;
    static __declspec(thread) int totalTagCount = 0;

    if (depth == 0)
    {
        totalTagCount = 0;
    }

    depth++;

    if (depth > 256)
    {
        depth--;
        return new EndTag();
    }

    totalTagCount++;
    const int MAX_TOTAL_TAGS = 32768;
    if (totalTagCount > MAX_TOTAL_TAGS)
    {
        depth--;
        return new EndTag();
    }

	byte type = dis->readByte();
	if (type == 0) { 
		depth--; 
		return new EndTag(); 
	}

	// 4J Stu - readByte can return -1, so if it's that then also mark as the end tag
	if(type == 255)
	{
        depth--;
		return new EndTag();
	}

	wstring name = dis->readUTF();//new String(bytes, "UTF-8");

	Tag *tag = newTag(type, name);
	if (tag == nullptr) { 
		depth--; 
		return new EndTag();
	}
	//        short length = dis.readShort();
	//        byte[] bytes = new byte[length];
	//        dis.readFully(bytes);

	tag->load(dis, tagDepth);
    depth--;
	return tag;
}

void Tag::writeNamedTag(Tag *tag, DataOutput *dos)
{
	dos->writeByte(tag->getId());
	if (tag->getId() == Tag::TAG_End) return;

	//        byte[] bytes = tag.getName().getBytes("UTF-8");
	//        dos.writeShort(bytes.length);
	//        dos.write(bytes);
	dos->writeUTF(tag->getName());

	tag->write(dos);
}

Tag *Tag::newTag(byte type, const wstring &name)
{
	switch (type)
	{
	case TAG_End:
		return new EndTag(name);
	case TAG_Byte:
		return new ByteTag(name);
	case TAG_Short:
		return new ShortTag(name);
	case TAG_Int:
		return new IntTag(name);
	case TAG_Long:
		return new LongTag(name);
	case TAG_Float:
		return new FloatTag(name);
	case TAG_Double:
		return new DoubleTag(name);
	case TAG_Byte_Array:
		return new ByteArrayTag(name);
	case TAG_Int_Array:
		return new IntArrayTag(name);
	case TAG_String:
		return new StringTag(name);
	case TAG_List:
		return new ListTag<Tag>(name);
	case TAG_Compound:
		return new CompoundTag(name);
	}
	return nullptr;
}

const wchar_t *Tag::getTagName(byte type)
{
	switch (type)
	{
	case TAG_End:
		return L"TAG_End";
	case TAG_Byte:
		return L"TAG_Byte";
	case TAG_Short:
		return L"TAG_Short";
	case TAG_Int:
		return L"TAG_Int";
	case TAG_Long:
		return L"TAG_Long";
	case TAG_Float:
		return L"TAG_Float";
	case TAG_Double:
		return L"TAG_Double";
	case TAG_Byte_Array:
		return L"TAG_Byte_Array";
	case TAG_Int_Array:
		return L"TAG_Int_Array";
	case TAG_String:
		return L"TAG_String";
	case TAG_List:
		return L"TAG_List";
	case TAG_Compound:
		return L"TAG_Compound";
	}
	return L"UNKNOWN";
}