summaryrefslogtreecommitdiffstats
path: root/Tools/BlockZapper/Zapper.cpp
blob: 83c9657cecd95388eb0d5d64e208edf6ceeca3a5 (plain) (blame)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436

// Zapper.cpp

// Implements the cZapper class representing the processor that actually zaps blocks and entities

#include "Globals.h"

#include "WorldStorage/FastNBT.h"
#include "StringCompression.h"
#include "zlib/zlib.h"

#include "Zapper.h"





/** The maximum size of an inflated chunk; raw chunk data is 192 KiB, allow 64 KiB more of entities */
#define CHUNK_INFLATE_MAX 256 KiB





cZapper::cZapper(const AString & a_MCAFolder) :
	m_MCAFolder(a_MCAFolder)
{
}





void cZapper::ZapRegions(const cRegionVector & a_Regions)
{
	for (cRegionVector::const_iterator itr = a_Regions.begin(), end = a_Regions.end(); itr != end; ++itr)
	{
		int MinAnvX, MinAnvZ;
		int MaxAnvX, MaxAnvZ;
		BlockToMCA(itr->m_MinX, itr->m_MinZ, MinAnvX, MinAnvZ);
		BlockToMCA(itr->m_MaxX, itr->m_MaxZ, MaxAnvX, MaxAnvZ);
		for (int x = MinAnvX; x <= MaxAnvX; x++)
		{
			for (int z = MinAnvZ; z <= MaxAnvZ; z++)
			{
				ZapRegionInMCAFile(*itr, x, z);
			}
		}
	}  // for itr - a_Regions
}





void cZapper::BlockToMCA(int a_BlockX, int a_BlockZ, int & a_MCAX, int & a_MCAZ)
{
	// These need to be arithmetic shifts, consult your compiler documentation to see if it's so
	// MSVC and GCC both use arithmetic shifts
	a_MCAX = a_BlockX >> 10;
	a_MCAZ = a_BlockZ >> 10;
}





void cZapper::BlockToChunk(int a_BlockX, int a_BlockZ, int & a_ChunkX, int & a_ChunkZ)
{
	// These need to be arithmetic shifts, consult your compiler documentation to see if it's so
	// MSVC and GCC both use arithmetic shifts
	a_ChunkX = a_BlockX >> 4;
	a_ChunkZ = a_BlockZ >> 4;
}





void cZapper::ZapRegionInMCAFile(const cRegion & a_Region, int a_MCAX, int a_MCAZ)
{
	cFile fIn;
	AString FileNameIn = Printf("%s/r.%d.%d.mca", m_MCAFolder.c_str(), a_MCAX, a_MCAZ);
	if (!fIn.Open(FileNameIn, cFile::fmRead))
	{
		return;
	}
	cFile fOut;
	AString FileNameOut = Printf("%s/r.%d.%d.zap", m_MCAFolder.c_str(), a_MCAX, a_MCAZ);
	if (!fOut.Open(FileNameOut, cFile::fmWrite))
	{
		fprintf(stderr, "Cannot open temporary file \"%s\" for writing, skipping file \"%s\".", FileNameOut.c_str(), FileNameIn.c_str());
		return;
	}

	AString DataOut;
	DataOut.reserve(fIn.GetSize());

	int HeaderIn[2048];
	if (fIn.Read(HeaderIn, sizeof(HeaderIn)) != sizeof(HeaderIn))
	{
		fprintf(stderr, "Cannot read header from file \"%s\", skipping file.", FileNameIn.c_str());
	}
	int HeaderOut[2048];
	for (int i = 0; i < 1024; i++)
	{
		if (HeaderIn[i] == 0)
		{
			// Chunk not present
			HeaderOut[i] = 0;
			continue;
		}
		AString ChunkData;
		int ChunkX = a_MCAX * ChunksPerMCAX + (i % ChunksPerMCAX);
		int ChunkZ = a_MCAZ * ChunksPerMCAZ + (i / ChunksPerMCAX);

		LoadChunkData(fIn, HeaderIn[i], ChunkData, ChunkX, ChunkZ);

		if (a_Region.TouchesChunk(ChunkX, ChunkZ))
		{
			ZapRegionInRawChunkData(a_Region, ChunkData, ChunkX, ChunkZ);
		}
		unsigned char ChunkHeader[5];
		size_t DataSize = ChunkData.size() + 1;
		ChunkHeader[0] = (DataSize >> 24) & 0xff;
		ChunkHeader[1] = (DataSize >> 16) & 0xff;
		ChunkHeader[2] = (DataSize >> 8)  & 0xff;
		ChunkHeader[3] = DataSize & 0xff;
		ChunkHeader[4] = 2;  // zlib compression
		size_t Alignment = 4096 - (ChunkData.size() + 5) % 4096;  // 5 bytes of the header are appended outside of ChunkData
		if (Alignment > 0)
		{
			ChunkData.append(Alignment, (char)0);
		}
		HeaderOut[i] = htonl(((DataOut.size() / 4096 + 2) << 8) | ((ChunkData.size() + 5) / 4096));
		DataOut.append((const char *)ChunkHeader, sizeof(ChunkHeader));
		DataOut.append(ChunkData);
	}  // for i - chunks in fIn
	for (int i = 1024; i < 2048; i++)
	{
		HeaderOut[i] = HeaderIn[i];
	}
	fIn.Close();
	fOut.Write(HeaderOut, sizeof(HeaderOut));
	fOut.Write(DataOut.data(), DataOut.size());
	fOut.Close();
	cFile::Delete(FileNameIn);
	cFile::Rename(FileNameOut, FileNameIn);
}





void cZapper::LoadChunkData(cFile & a_InFile, int a_ChunkHeaderValue, AString & a_ChunkData, int a_ChunkX, int a_ChunkZ)
{
	a_ChunkHeaderValue = ntohl(a_ChunkHeaderValue);  // Convert from big-endian to system-endian
	int ChunkOffset = (a_ChunkHeaderValue >> 8) * 4096;
	int ChunkSize = (a_ChunkHeaderValue & 0xff) * 4096;
	a_InFile.Seek(ChunkOffset);
	unsigned char ChunkHeader[5];
	a_InFile.Read(ChunkHeader, sizeof(ChunkHeader));
	if (ChunkHeader[4] != 2)
	{
		fprintf(stderr, "Chunk [%d, %d] is compressed in an unknown scheme (%d), skipping", a_ChunkX, a_ChunkZ, ChunkHeader[5]);
		return;
	}
	int ActualSize = (ChunkHeader[0] << 24) |  (ChunkHeader[1] << 16) | (ChunkHeader[2] << 8) | ChunkHeader[3];
	ActualSize -= 1;  // Compression took 1 byte
	a_ChunkData.resize(ActualSize);
	int BytesRead = a_InFile.Read((void *)(a_ChunkData.data()), ActualSize);
	if (BytesRead != ActualSize)
	{
		fprintf(stderr, "Chunk is truncated in file (%d bytes out of %d), skipping.", BytesRead, ActualSize);
		a_ChunkData.clear();
		return;
	}
}





void cZapper::ZapRegionInRawChunkData(const cRegion & a_Region, AString & a_ChunkData, int a_ChunkX, int a_ChunkZ)
{
	// Decompress the data:
	char Uncompressed[CHUNK_INFLATE_MAX];
	z_stream strm;
	strm.zalloc = (alloc_func)NULL;
	strm.zfree = (free_func)NULL;
	strm.opaque = NULL;
	inflateInit(&strm);
	strm.next_out  = (Bytef *)Uncompressed;
	strm.avail_out = sizeof(Uncompressed);
	strm.next_in   = (Bytef *)a_ChunkData.data();
	strm.avail_in  = a_ChunkData.size();
	int res = inflate(&strm, Z_FINISH);
	inflateEnd(&strm);
	if (res != Z_STREAM_END)
	{
		fprintf(stderr, "Chunk [%d, %d] failed to decompress: error %d. Skipping chunk.", a_ChunkX, a_ChunkZ, res);
		return;
	}

	/*
	// DEBUG: Output src to a file:
	cFile f1;
	if (f1.Open(Printf("chunk_%d_%d_in.nbt", a_ChunkX, a_ChunkZ), cFile::fmWrite))
	{
		f1.Write(Uncompressed, strm.total_out);
	}
	//*/

	cParsedNBT NBT(Uncompressed, strm.total_out);
	if (!NBT.IsValid())
	{
		fprintf(stderr, "Chunk [%d, %d] failed to parse. Skipping chunk.", a_ChunkX, a_ChunkZ);
		return;
	}
	ZapRegionInNBTChunk(a_Region, NBT, a_ChunkX, a_ChunkZ);

	cFastNBTWriter Writer;
	for (int ch = NBT.GetFirstChild(0); ch >= 0; ch = NBT.GetNextSibling(ch))
	{
		SerializeNBTTag(NBT, ch, Writer);
	}
	Writer.Finish();

	/*
	// DEBUG: Output dst to a file:
	cFile f2;
	if (f2.Open(Printf("chunk_%d_%d_out.nbt", a_ChunkX, a_ChunkZ), cFile::fmWrite))
	{
		f2.Write(Writer.GetResult().data(), Writer.GetResult().size());
	}
	//*/

	// Compress the serialized data into "Uncompressed" (reuse buffer)
	CompressString(Writer.GetResult().data(), Writer.GetResult().size(), a_ChunkData);
}





void cZapper::ZapRegionInNBTChunk(const cRegion & a_Region, cParsedNBT & a_NBT, int a_ChunkX, int a_ChunkZ)
{
	int LevelTag = a_NBT.FindChildByName(a_NBT.GetRoot(), "Level");
	if (LevelTag < 0)
	{
		fprintf(stderr, "Cannot find Level tag in chunk [%d, %d]'s NBT. Skipping chunk.", a_ChunkX, a_ChunkZ);
		return;
	}

	// Create a copy of the region and limit it to the current chunk:
	int BlockX = a_ChunkX * 16;
	int BlockZ = a_ChunkZ * 16;
	cRegion Local;
	Local.m_MinX = std::max(0, a_Region.m_MinX - BlockX);
	Local.m_MaxX = std::min(15, a_Region.m_MaxX - BlockX);
	Local.m_MinY = a_Region.m_MinY;
	Local.m_MaxY = a_Region.m_MaxY;
	Local.m_MinZ = std::max(0, a_Region.m_MinZ - BlockZ);
	Local.m_MaxZ = std::min(15, a_Region.m_MaxZ - BlockZ);

	if (a_Region.m_ShouldZapBlocks)
	{
		int SectionsTag = a_NBT.FindChildByName(LevelTag, "Sections");
		if (SectionsTag < 0)
		{
			fprintf(stderr, "Cannot find the Sections tag in the Level tag in chunk [%d, %d]'s NBT. Skipping block-zapping in chunk.", a_ChunkX, a_ChunkZ);
			return;
		}
		ZapRegionBlocksInNBT(Local, a_NBT, SectionsTag);
	}

	if (a_Region.m_ShouldZapEntities)
	{
		int EntitiesTag = a_NBT.FindChildByName(LevelTag, "Entities");
		if (EntitiesTag < 0)
		{
			fprintf(stderr, "Cannot find the Entities tag in the Level tag in chunk [%d, %d]'s NBT. Skipping entity-zapping in chunk.", a_ChunkX, a_ChunkZ);
			return;
		}
		ZapRegionEntitiesInNBT(Local, a_NBT, EntitiesTag);
	}
}





void cZapper::ZapRegionBlocksInNBT(const cRegion & a_Region, cParsedNBT & a_NBT, int a_SectionsTag)
{
	for (int Child = a_NBT.GetFirstChild(a_SectionsTag); Child >= 0; Child = a_NBT.GetNextSibling(Child))
	{
		int y = 0;
		int SectionY = a_NBT.FindChildByName(Child, "Y");
		if ((SectionY < 0) || (a_NBT.GetType(SectionY) != TAG_Byte))
		{
			continue;
		}
		y = a_NBT.GetByte(SectionY);
		if ((y * 16 > a_Region.m_MaxY) || (y * 16 + 16 < a_Region.m_MinY))
		{
			continue;
		}
		int BlockDataTag = a_NBT.FindChildByName(Child, "Blocks");
		int BlockMetaTag = a_NBT.FindChildByName(Child, "Data");
		int BlockAddTag  = a_NBT.FindChildByName(Child, "Add");
		if (BlockDataTag > 0)
		{
			ZapRegionInNBTSectionBytes(a_Region, y, (unsigned char *)(a_NBT.GetData(BlockDataTag)));
		}
		if (BlockMetaTag > 0)
		{
			ZapRegionInNBTSectionNibbles(a_Region, y, (unsigned char *)(a_NBT.GetData(BlockMetaTag)));
		}
		if (BlockAddTag > 0)
		{
			ZapRegionInNBTSectionNibbles(a_Region, y, (unsigned char *)(a_NBT.GetData(BlockAddTag)));
		}
	}  // for Child - Level / Sections / []
}





void cZapper::ZapRegionInNBTSectionBytes(const cRegion & a_Region, int a_SectionY, unsigned char * a_BlockBytes)
{
	int MinY = std::max(0, a_Region.m_MinY - a_SectionY * 16);
	int MaxY = std::min(15, a_Region.m_MaxY - a_SectionY * 16);
	ASSERT(MinY >= 0);
	ASSERT(MaxY >= 0);
	for (int y = MinY; y <= MaxY; y++)
	{
		for (int z = a_Region.m_MinZ; z <= a_Region.m_MaxZ; z++)
		{
			for (int x = a_Region.m_MinX; x <= a_Region.m_MaxX; x++)
			{
				a_BlockBytes[x + z * 16 + y * 16 * 16] = 0;
			}
		}
	}
}





void cZapper::ZapRegionInNBTSectionNibbles(const cRegion & a_Region, int a_SectionY, unsigned char * a_BlockNibbles)
{
	int MinY = std::max(0, a_Region.m_MinY - a_SectionY * 16);
	int MaxY = std::min(15, a_Region.m_MaxY - a_SectionY * 16);
	ASSERT(MinY >= 0);
	ASSERT(MaxY >= 0);
	for (int y = MinY; y <= MaxY; y++)
	{
		for (int z = a_Region.m_MinZ; z < a_Region.m_MaxZ; z++)
		{
			for (int x = a_Region.m_MinX; x < a_Region.m_MaxX; x++)
			{
				cChunkDef::SetNibble(a_BlockNibbles, x, y, z, 0);
			}
		}
	}
}





void cZapper::ZapRegionEntitiesInNBT(const cRegion & a_Region, cParsedNBT & a_NBT, int a_EntitiesTag)
{
	// TODO
}





void cZapper::SerializeNBTTag(const cParsedNBT & a_NBT, int a_Tag, cFastNBTWriter & a_Writer)
{
	switch (a_NBT.GetType(a_Tag))
	{
		case TAG_Byte:      a_Writer.AddByte     (a_NBT.GetName(a_Tag), a_NBT.GetByte  (a_Tag)); break;
		case TAG_Short:     a_Writer.AddShort    (a_NBT.GetName(a_Tag), a_NBT.GetShort (a_Tag)); break;
		case TAG_Int:       a_Writer.AddInt      (a_NBT.GetName(a_Tag), a_NBT.GetInt   (a_Tag)); break;
		case TAG_Long:      a_Writer.AddLong     (a_NBT.GetName(a_Tag), a_NBT.GetLong  (a_Tag)); break;
		case TAG_Float:     a_Writer.AddFloat    (a_NBT.GetName(a_Tag), a_NBT.GetFloat (a_Tag)); break;
		case TAG_Double:    a_Writer.AddDouble   (a_NBT.GetName(a_Tag), a_NBT.GetDouble(a_Tag)); break;
		case TAG_ByteArray: a_Writer.AddByteArray(a_NBT.GetName(a_Tag), a_NBT.GetData  (a_Tag), a_NBT.GetDataLength(a_Tag)); break;
		case TAG_String:    a_Writer.AddString   (a_NBT.GetName(a_Tag), a_NBT.GetString(a_Tag)); break;
		case TAG_IntArray:
		{
			std::vector<int> Data;
			int NumInts = a_NBT.GetDataLength(a_Tag) / 4;
			Data.reserve(NumInts);
			int * OrigData = (int *)(a_NBT.GetData(a_Tag));
			for (int i = 0; i < NumInts; i++)
			{
				Data.push_back(ntohl(OrigData[i]));
			}
			a_Writer.AddIntArray (a_NBT.GetName(a_Tag), &Data.front(), Data.size()); break;
		}

		case TAG_List:
		{
			a_Writer.BeginList(a_NBT.GetName(a_Tag), a_NBT.GetChildrenType(a_Tag));
			for (int ch = a_NBT.GetFirstChild(a_Tag); ch >= 0; ch = a_NBT.GetNextSibling(ch))
			{
				SerializeNBTTag(a_NBT, ch, a_Writer);
			}  // for ch - children[]
			a_Writer.EndList();
			break;
		}

		case TAG_Compound:
		{
			a_Writer.BeginCompound(a_NBT.GetName(a_Tag));
			for (int ch = a_NBT.GetFirstChild(a_Tag); ch >= 0; ch = a_NBT.GetNextSibling(ch))
			{
				SerializeNBTTag(a_NBT, ch, a_Writer);
			}  // for ch - children[]
			a_Writer.EndCompound();
			break;
		}

		default:
		{
			ASSERT(!"Unknown NBT tag");
			break;
		}
	}
}