From 66ef05c765a7e66ec3d77cd1a1eaff4f03b28688 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 20 Sep 2014 18:41:21 +0200 Subject: QtBiomeVisualiser: Added support for loading Anvil worlds. --- Tools/QtBiomeVisualiser/ChunkSource.cpp | 241 ++++++++++++++++++++++++++ Tools/QtBiomeVisualiser/ChunkSource.h | 34 +++- Tools/QtBiomeVisualiser/MainWindow.cpp | 12 +- Tools/QtBiomeVisualiser/QtBiomeVisualiser.pro | 38 +++- 4 files changed, 320 insertions(+), 5 deletions(-) diff --git a/Tools/QtBiomeVisualiser/ChunkSource.cpp b/Tools/QtBiomeVisualiser/ChunkSource.cpp index 2235816bc..54da2afe5 100644 --- a/Tools/QtBiomeVisualiser/ChunkSource.cpp +++ b/Tools/QtBiomeVisualiser/ChunkSource.cpp @@ -3,6 +3,8 @@ #include #include "Generating/BioGen.h" #include "inifile/iniFile.h" +#include "StringCompression.h" +#include "WorldStorage/FastNBT.h" @@ -182,3 +184,242 @@ void BioGenSource::reload() + +//////////////////////////////////////////////////////////////////////////////// +// 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 = GetBEInt(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] = GetBEInt(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, ChunkPtr 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)GetBEInt(beBiomes + 4 * i); + } + // Render the biomes: + Chunk::Image img; + biomesToImage(biomeMap, img); + a_DestChunk->setImage(img); + 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]); + } + // Render the biomes: + Chunk::Image img; + biomesToImage(biomeMap, img); + a_DestChunk->setImage(img); +} + + + + + +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.cbegin(), end = m_Files.cend(); 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; +} + + + + + diff --git a/Tools/QtBiomeVisualiser/ChunkSource.h b/Tools/QtBiomeVisualiser/ChunkSource.h index 868e4a144..a5612da01 100644 --- a/Tools/QtBiomeVisualiser/ChunkSource.h +++ b/Tools/QtBiomeVisualiser/ChunkSource.h @@ -1,4 +1,5 @@ #pragma once +#include "Globals.h" #include #include #include "Chunk.h" @@ -64,11 +65,40 @@ class AnvilSource : public ChunkSource { public: - // TODO + /** Constructs a new AnvilSource based on the world path. */ + AnvilSource(QString a_WorldRegionFolder); // ChunkSource overrides: virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk) override; - virtual void reload() override {} + virtual void reload() override; + +protected: + class AnvilFile; + typedef std::shared_ptr AnvilFilePtr; + + + /** Folder where the individual Anvil Region files are located. */ + QString m_WorldRegionFolder; + + /** List of currently loaded files. Acts as a cache so that a file is not opened and closed over and over again. + Protected against multithreaded access by m_Mtx. */ + std::list m_Files; + + /** Guards m_Files agains multithreaded access. */ + QMutex m_Mtx; + + + /** Converts chunk coords to region coords. */ + void chunkToRegion(int a_ChunkX, int a_ChunkZ, int & a_RegionX, int & a_RegionZ); + + /** Returns the compressed data of the specified chunk. + Returns an empty string if the chunk is not available. */ + AString getCompressedChunkData(int a_ChunkX, int a_ChunkZ); + + /** Returns the file object that contains the specified chunk. + The file is taken from the cache if available there, otherwise it is created anew. */ + AnvilFilePtr getAnvilFile(int a_ChunkX, int a_ChunkZ); + }; diff --git a/Tools/QtBiomeVisualiser/MainWindow.cpp b/Tools/QtBiomeVisualiser/MainWindow.cpp index 65d0ccf5e..b6db806f9 100644 --- a/Tools/QtBiomeVisualiser/MainWindow.cpp +++ b/Tools/QtBiomeVisualiser/MainWindow.cpp @@ -39,6 +39,10 @@ MainWindow::~MainWindow() void MainWindow::generate() { QString worldIni = QFileDialog::getOpenFileName(this, tr("Open world.ini"), QString(), tr("world.ini (world.ini)")); + if (worldIni.isEmpty()) + { + return; + } m_BiomeView->setChunkSource(std::shared_ptr(new BioGenSource(worldIni))); m_BiomeView->redraw(); } @@ -49,7 +53,13 @@ void MainWindow::generate() void MainWindow::open() { - // TODO + QString regionFolder = QFileDialog::getExistingDirectory(this, tr("Select the region folder"), QString()); + if (regionFolder.isEmpty()) + { + return; + } + m_BiomeView->setChunkSource(std::shared_ptr(new AnvilSource(regionFolder))); + m_BiomeView->redraw(); } diff --git a/Tools/QtBiomeVisualiser/QtBiomeVisualiser.pro b/Tools/QtBiomeVisualiser/QtBiomeVisualiser.pro index e6b65e628..283e180f8 100644 --- a/Tools/QtBiomeVisualiser/QtBiomeVisualiser.pro +++ b/Tools/QtBiomeVisualiser/QtBiomeVisualiser.pro @@ -29,7 +29,24 @@ SOURCES += main.cpp\ ChunkCache.cpp \ Chunk.cpp \ ChunkSource.cpp \ - ChunkLoader.cpp + ChunkLoader.cpp \ + ../../src/StringCompression.cpp \ + ../../src/WorldStorage/FastNBT.cpp \ + ../../lib/zlib/adler32.c \ + ../../lib/zlib/compress.c \ + ../../lib/zlib/crc32.c \ + ../../lib/zlib/deflate.c \ + ../../lib/zlib/gzclose.c \ + ../../lib/zlib/gzlib.c \ + ../../lib/zlib/gzread.c \ + ../../lib/zlib/gzwrite.c \ + ../../lib/zlib/infback.c \ + ../../lib/zlib/inffast.c \ + ../../lib/zlib/inflate.c \ + ../../lib/zlib/inftrees.c \ + ../../lib/zlib/trees.c \ + ../../lib/zlib/uncompr.c \ + ../../lib/zlib/zutil.c HEADERS += MainWindow.h \ Globals.h \ @@ -48,7 +65,20 @@ HEADERS += MainWindow.h \ ChunkCache.h \ Chunk.h \ ChunkSource.h \ - ChunkLoader.h + ChunkLoader.h \ + ../../src/StringCompression.h \ + ../../src/WorldStorage/FastNBT.h \ + ../../lib/zlib/crc32.h \ + ../../lib/zlib/deflate.h \ + ../../lib/zlib/gzguts.h \ + ../../lib/zlib/inffast.h \ + ../../lib/zlib/inffixed.h \ + ../../lib/zlib/inflate.h \ + ../../lib/zlib/inftrees.h \ + ../../lib/zlib/trees.h \ + ../../lib/zlib/zconf.h \ + ../../lib/zlib/zlib.h \ + ../../lib/zlib/zutil.h INCLUDEPATH += $$_PRO_FILE_PWD_ \ $$_PRO_FILE_PWD_/../../src \ @@ -57,4 +87,8 @@ INCLUDEPATH += $$_PRO_FILE_PWD_ \ CONFIG += C++11 +OTHER_FILES += \ + ../../lib/zlib/example.c.txt \ + ../../lib/zlib/minigzip.c.txt + -- cgit v1.2.3