1 module nbtd;
2 
3 public
4 {
5 	import nbtd.INBTItem;
6 	import nbtd.NBTCommon;
7 	import nbtd.NBTEnd;
8 	import nbtd.NBTList;
9 	import nbtd.NBTCompound;
10 
11 	ubyte[] uncompressGZip(ubyte[] data)
12 	{
13 		import std.zlib;
14 
15 		UnCompress uncompressor = new UnCompress(HeaderFormat.gzip);
16 		ubyte[] result;
17 		while(data.length > 1024)
18 		{
19 			result ~= cast(ubyte[])uncompressor.uncompress(data[0 .. 1024]);
20 			data = data[1024 .. $];
21 		}
22 		result ~= cast(ubyte[])uncompressor.uncompress(data);
23 		result ~= cast(ubyte[])uncompressor.flush();
24 		return result;
25 	}
26 
27 	ubyte[] compressGZip(ubyte[] data)
28 	{
29 		import std.zlib;
30 
31 		Compress compressor = new Compress(HeaderFormat.gzip);
32 		ubyte[] result;
33 		while(data.length > 1024)
34 		{
35 			result ~= cast(ubyte[])compressor.compress(data[0 .. 1024]);
36 			data = data[1024 .. $];
37 		}
38 		result ~= cast(ubyte[])compressor.compress(data);
39 		result ~= cast(ubyte[])compressor.flush();
40 		return result;
41 	}
42 
43 	INBTItem parseElement(ref ubyte[] stream, bool hasName = true)
44 	{
45 		import std.conv;
46 
47 		switch(stream[0])
48 		{
49 		case 0:
50 			if(hasName)
51 				stream = stream[1 .. $];
52 			return new NBTEnd();
53 		case 1:
54 		{
55 			auto value = new NBTByte();
56 			value.read(stream, hasName);
57 			return value;
58 		}
59 		case 2:
60 		{
61 			auto value = new NBTShort();
62 			value.read(stream, hasName);
63 			return value;
64 		}
65 		case 3:
66 		{
67 			auto value = new NBTInt();
68 			value.read(stream, hasName);
69 			return value;
70 		}
71 		case 4:
72 		{
73 			auto value = new NBTLong();
74 			value.read(stream, hasName);
75 			return value;
76 		}
77 		case 5:
78 		{
79 			auto value = new NBTFloat();
80 			value.read(stream, hasName);
81 			return value;
82 		}
83 		case 6:
84 		{
85 			auto value = new NBTDouble();
86 			value.read(stream, hasName);
87 			return value;
88 		}
89 		case 7:
90 		{
91 			auto value = new NBTByteArray();
92 			value.read(stream, hasName);
93 			return value;
94 		}
95 		case 8:
96 		{
97 			auto value = new NBTString();
98 			value.read(stream, hasName);
99 			return value;
100 		}
101 		case 9:
102 		{
103 			auto value = new NBTList();
104 			value.read(stream, hasName);
105 			return value;
106 		}
107 		case 10:
108 		{
109 			auto value = new NBTCompound();
110 			value.read(stream, hasName);
111 			return value;
112 		}
113 		case 11:
114 		{
115 			auto value = new NBTIntArray();
116 			value.read(stream, hasName);
117 			return value;
118 		}
119 		default:
120 			throw new Exception("Invalid NBT-TAG(" ~ to!string(stream[0]) ~ ")!");
121 		}
122 	}
123 
124 	INBTItem parseElement(NBTType type, ref ubyte[] stream, bool hasName = true)
125 	{
126 		import std.conv;
127 
128 		switch(type)
129 		{
130 		case 0:
131 			if(hasName)
132 				stream = stream[1 .. $];
133 			return new NBTEnd();
134 		case 1:
135 		{
136 			auto value = new NBTByte();
137 			value.read(stream, hasName);
138 			return value;
139 		}
140 		case 2:
141 		{
142 			auto value = new NBTShort();
143 			value.read(stream, hasName);
144 			return value;
145 		}
146 		case 3:
147 		{
148 			auto value = new NBTInt();
149 			value.read(stream, hasName);
150 			return value;
151 		}
152 		case 4:
153 		{
154 			auto value = new NBTLong();
155 			value.read(stream, hasName);
156 			return value;
157 		}
158 		case 5:
159 		{
160 			auto value = new NBTFloat();
161 			value.read(stream, hasName);
162 			return value;
163 		}
164 		case 6:
165 		{
166 			auto value = new NBTDouble();
167 			value.read(stream, hasName);
168 			return value;
169 		}
170 		case 7:
171 		{
172 			auto value = new NBTByteArray();
173 			value.read(stream, hasName);
174 			return value;
175 		}
176 		case 8:
177 		{
178 			auto value = new NBTString();
179 			value.read(stream, hasName);
180 			return value;
181 		}
182 		case 9:
183 		{
184 			auto value = new NBTList();
185 			value.read(stream, hasName);
186 			return value;
187 		}
188 		case 10:
189 		{
190 			auto value = new NBTCompound();
191 			value.read(stream, hasName);
192 			return value;
193 		}
194 		case 11:
195 		{
196 			auto value = new NBTIntArray();
197 			value.read(stream, hasName);
198 			return value;
199 		}
200 		default:
201 			throw new Exception("Invalid NBT-TAG(" ~ to!string(type) ~ ")!");
202 		}
203 	}
204 }