summaryrefslogtreecommitdiffstats
path: root/Tools/QtBiomeVisualiser/ChunkSource.cpp
blob: 120b72474c05f109585189d201fcfbf0cb1e461a (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
#include "Globals.h"
#include "ChunkSource.h"
#include <QThread>
#include "src/Generating/BioGen.h"
#include "src/StringCompression.h"
#include "src/WorldStorage/FastNBT.h"
#include "src/IniFile.h"
#include "src/Endianness.h"





////////////////////////////////////////////////////////////////////////////////
// BioGenSource:

BioGenSource::BioGenSource(cIniFilePtr a_IniFile) :
	m_IniFile(a_IniFile),
	m_Mtx(QMutex::Recursive)
{
	reload();
}





void BioGenSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, Chunk & a_DestChunk)
{
	cChunkDef::BiomeMap biomes;
	int tag;
	cBiomeGenPtr biomeGen = getBiomeGen(tag);
	biomeGen->GenBiomes(a_ChunkX, a_ChunkZ, biomes);
	releaseBiomeGen(std::move(biomeGen), tag);
	a_DestChunk.setBiomes(biomes);
}





void BioGenSource::reload()
{
	QMutexLocker lock(&m_Mtx);
	m_CurrentTag += 1;
	m_BiomeGens.clear();
}





cBiomeGenPtr BioGenSource::getBiomeGen(int & a_Tag)
{
	QMutexLocker lock(&m_Mtx);
	a_Tag = m_CurrentTag;
	if (m_BiomeGens.empty())
	{
		// Create a new biogen:
		lock.unlock();
		int seed = m_IniFile->GetValueSetI("Seed", "Seed", 0);
		bool unused;
		cBiomeGenPtr res = cBiomeGen::CreateBiomeGen(*m_IniFile, seed, unused);
		return res;
	}
	else
	{
		// Return an existing biogen:
		cBiomeGenPtr res = m_BiomeGens.back();
		m_BiomeGens.pop_back();
		return res;
	}
}





void BioGenSource::releaseBiomeGen(cBiomeGenPtr && a_BiomeGen, int a_Tag)
{
	QMutexLocker lock(&m_Mtx);

	// If the tag differs, the source has been reloaded and this biogen is old, dispose:
	if (a_Tag != m_CurrentTag)
	{
		return;
	}

	// The tag is the same, put the biogen back to list:
	m_BiomeGens.push_back(std::move(a_BiomeGen));
}





////////////////////////////////////////////////////////////////////////////////
// AnvilSource::AnvilFile

class AnvilSource::AnvilFile
{
public:
	/** Coordinates of the region file. */
	int m_RegionX, m_RegionZ;

	/** True iff the file contains proper data. */
	bool m_IsValid;



	/** Creates a new instance with the specified region coords. Reads the file header. */
	AnvilFile(int a_RegionX, int a_RegionZ, const AString & a_WorldPath) :
		m_RegionX(a_RegionX),
		m_RegionZ(a_RegionZ),
		m_IsValid(false)
	{
		readFile(Printf("%s/r.%d.%d.mca", a_WorldPath.c_str(), a_RegionX, a_RegionZ));
	}



	/** Returns the compressed data of the specified chunk.
	Returns an empty string when chunk not present. */
	AString getChunkData(int a_ChunkX, int a_ChunkZ)
	{
		if (!m_IsValid)
		{
			return "";
		}

		// Translate to local coords:
		int RelChunkX = a_ChunkX - m_RegionX * 32;
		int RelChunkZ = a_ChunkZ - m_RegionZ * 32;
		ASSERT((RelChunkX >= 0) && (RelChunkX < 32));
		ASSERT((RelChunkZ >= 0) && (RelChunkZ < 32));

		// Get the chunk data location:
		UInt32 chunkOffset = m_Header[RelChunkX + 32 * RelChunkZ] >> 8;
		UInt32 numChunkSectors = m_Header[RelChunkX + 32 * RelChunkZ] & 0xff;
		if ((chunkOffset < 2) || (numChunkSectors == 0))
		{
			return "";
		}

		// Get the real data size:
		const char * chunkData = m_FileData.data() + chunkOffset * 4096;
		UInt32 chunkSize = NetworkBufToHost(chunkData);
		if ((chunkSize < 2) || (chunkSize / 4096 > numChunkSectors))
		{
			// Bad data, bail out
			return "";
		}

		// Check the compression method:
		if (chunkData[4] != 2)
		{
			// Chunk is in an unknown compression
			return "";
		}
		chunkSize--;

		// Read the chunk data:
		return m_FileData.substr(chunkOffset * 4096 + 5, chunkSize);
	}

protected:
	AString m_FileData;
	UInt32 m_Header[2048];


	/** Reads the whole specified file contents and parses the header. */
	void readFile(const AString & a_FileName)
	{
		// Read the entire file:
		m_FileData = cFile::ReadWholeFile(a_FileName);
		if (m_FileData.size() < sizeof(m_Header))
		{
			return;
		}

		// Parse the header - change endianness:
		const char * hdr = m_FileData.data();
		for (size_t i = 0; i < ARRAYCOUNT(m_Header); i++)
		{
			m_Header[i] = NetworkBufToHost(hdr + 4 * i);
		}
		m_IsValid = true;
	}
};





////////////////////////////////////////////////////////////////////////////////
// AnvilSource:

AnvilSource::AnvilSource(QString a_WorldRegionFolder) :
	m_WorldRegionFolder(a_WorldRegionFolder)
{
}





void AnvilSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, Chunk & a_DestChunk)
{
	// Load the compressed data:
	AString compressedChunkData = getCompressedChunkData(a_ChunkX, a_ChunkZ);
	if (compressedChunkData.empty())
	{
		return;
	}

	// Uncompress the chunk data:
	AString uncompressed;
	int res = InflateString(compressedChunkData.data(), compressedChunkData.size(), uncompressed);
	if (res != Z_OK)
	{
		return;
	}

	// Parse the NBT data:
	cParsedNBT nbt(uncompressed.data(), uncompressed.size());
	if (!nbt.IsValid())
	{
		return;
	}

	// Get the biomes out of the NBT:
	int Level = nbt.FindChildByName(0, "Level");
	if (Level < 0)
	{
		return;
	}
	cChunkDef::BiomeMap biomeMap;
	int mcsBiomes = nbt.FindChildByName(Level, "MCSBiomes");
	if ((mcsBiomes >= 0) && (nbt.GetDataLength(mcsBiomes) == sizeof(biomeMap)))
	{
		// Convert the biomes from BigEndian to platform native numbers:
		const char * beBiomes = nbt.GetData(mcsBiomes);
		for (size_t i = 0; i < ARRAYCOUNT(biomeMap); i++)
		{
			biomeMap[i] = (EMCSBiome)NetworkBufToHost<Int32>(beBiomes + 4 * i);
		}
		a_DestChunk.setBiomes(biomeMap);
		return;
	}

	// MCS biomes not found, load Vanilla biomes instead:
	int biomes = nbt.FindChildByName(Level, "Biomes");
	if ((biomes < 0) || (nbt.GetDataLength(biomes) != ARRAYCOUNT(biomeMap)))
	{
		return;
	}
	// Convert the biomes from Vanilla to EMCSBiome:
	const char * vanillaBiomes = nbt.GetData(biomes);
	for (size_t i = 0; i < ARRAYCOUNT(biomeMap); i++)
	{
		biomeMap[i] = EMCSBiome(vanillaBiomes[i]);
	}
	a_DestChunk.setBiomes(biomeMap);
}





void AnvilSource::reload()
{
	// Remove all files from the cache:
	QMutexLocker lock(&m_Mtx);
	m_Files.clear();
}





void AnvilSource::chunkToRegion(int a_ChunkX, int a_ChunkZ, int & a_RegionX, int & a_RegionZ)
{
	a_RegionX = a_ChunkX >> 5;
	a_RegionZ = a_ChunkZ >> 5;
}





AString AnvilSource::getCompressedChunkData(int a_ChunkX, int a_ChunkZ)
{
	return getAnvilFile(a_ChunkX, a_ChunkZ)->getChunkData(a_ChunkX, a_ChunkZ);
}





AnvilSource::AnvilFilePtr AnvilSource::getAnvilFile(int a_ChunkX, int a_ChunkZ)
{
	int RegionX, RegionZ;
	chunkToRegion(a_ChunkX, a_ChunkZ, RegionX, RegionZ);

	// Search the cache for the file:
	QMutexLocker lock(&m_Mtx);
	for (auto itr = m_Files.begin(), end = m_Files.end(); itr != end; ++itr)
	{
		if (((*itr)->m_RegionX == RegionX) && ((*itr)->m_RegionZ == RegionZ))
		{
			// Found the file in the cache, move it to front and return it:
			AnvilFilePtr file(*itr);
			m_Files.erase(itr);
			m_Files.push_front(file);
			return file;
		}
	}

	// File not in cache, create it:
	AnvilFilePtr file(new AnvilFile(RegionX, RegionZ, m_WorldRegionFolder.toStdString()));
	m_Files.push_front(file);
	return file;
}