|
| 1 | + |
| 2 | +#ifndef WORLD_CHUNKPARSER_HPP |
| 3 | +#define WORLD_CHUNKPARSER_HPP |
| 4 | + |
| 5 | +#include "fstream" |
| 6 | +#include "nbt.hpp" |
| 7 | +#include "world.hpp" |
| 8 | + |
| 9 | +#include <cstdint> |
| 10 | +#include <filesystem> |
| 11 | +#include <map> |
| 12 | +#include <memory> |
| 13 | +#include <string> |
| 14 | +#include <utility> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +namespace World { |
| 18 | + |
| 19 | + class DirectChunkParser { |
| 20 | + public: |
| 21 | + struct ParseConfig { |
| 22 | + int worldHeight = 384; |
| 23 | + int minY = -64; |
| 24 | + bool loadLighting = true; |
| 25 | + bool optimizePalettes = true; |
| 26 | + bool validateData = true; |
| 27 | + bool loadBlockEntities = true; |
| 28 | + |
| 29 | + // Performance options |
| 30 | + bool useMemoryMapping = true; |
| 31 | + size_t cacheSize = 64; // Number of chunks to cache |
| 32 | + }; |
| 33 | + |
| 34 | + private: |
| 35 | + ParseConfig _config; |
| 36 | + |
| 37 | + // Region file handling |
| 38 | + struct RegionFile { |
| 39 | + std::filesystem::path path; |
| 40 | + std::unique_ptr<std::ifstream> stream; |
| 41 | + std::vector<uint32_t> chunkOffsets; |
| 42 | + std::vector<uint32_t> chunkTimestamps; |
| 43 | + bool isLoaded = false; |
| 44 | + }; |
| 45 | + |
| 46 | + mutable std::map<std::pair<int32_t, int32_t>, std::unique_ptr<RegionFile>> _regionCache; |
| 47 | + |
| 48 | + public: |
| 49 | + explicit DirectChunkParser(const ParseConfig& config); |
| 50 | + ~DirectChunkParser(); |
| 51 | + |
| 52 | + // Main parsing methods |
| 53 | + World::ChunkData parseChunkFromRegion(const std::filesystem::path& regionPath, int32_t chunkX, int32_t chunkZ); |
| 54 | + |
| 55 | + World::ChunkData parseChunkFromNBT(const nbt::NBT& chunkNBT, int32_t chunkX, int32_t chunkZ); |
| 56 | + |
| 57 | + // Batch operations |
| 58 | + std::vector<World::ChunkData> parseChunkBatch(const std::filesystem::path& regionPath, |
| 59 | + const std::vector<std::pair<int32_t, int32_t>>& coordinates); |
| 60 | + |
| 61 | + // Utility methods |
| 62 | + bool chunkExistsInRegion(const std::filesystem::path& regionPath, int32_t chunkX, int32_t chunkZ); |
| 63 | + std::vector<std::pair<int32_t, int32_t>> getAvailableChunks(const std::filesystem::path& regionPath); |
| 64 | + |
| 65 | + // Configuration |
| 66 | + void setConfig(const ParseConfig& config) { _config = config; } |
| 67 | + const ParseConfig& getConfig() const { return _config; } |
| 68 | + |
| 69 | + private: |
| 70 | + // Region file operations |
| 71 | + RegionFile* loadRegionFile(const std::filesystem::path& regionPath) const; |
| 72 | + std::vector<uint8_t> readChunkData(RegionFile* region, int32_t chunkX, int32_t chunkZ) const; |
| 73 | + void parseRegionHeader(RegionFile* region) const; |
| 74 | + uint32_t getBlockIdFromName(const std::string& blockName); |
| 75 | + |
| 76 | + // NBT parsing methods |
| 77 | + void parseHeightMaps(const nbt::TagCompound& heightmaps, World::ChunkData& chunk); |
| 78 | + void parseSections(const nbt::TagList& sections, World::ChunkData& chunk); |
| 79 | + void parseBlockEntities(const nbt::TagList& blockEntities, World::ChunkData& chunk); |
| 80 | + void parseChunkMetadata(const nbt::TagCompound& level, World::ChunkData& chunk); |
| 81 | + |
| 82 | + // Section parsing |
| 83 | + void parseBlockStates(const nbt::TagCompound& blockStates, ChunkSection& section); |
| 84 | + void parseBiomes(const nbt::TagCompound& biomes, ChunkSection& section); |
| 85 | + void parseLighting(const nbt::TagCompound& section, ChunkSection& chunkSection); |
| 86 | + |
| 87 | + // Paletted data unpacking |
| 88 | + std::vector<uint32_t> |
| 89 | + unpackPalettedData(const nbt::TagLongArray& data, const nbt::TagList* palette, size_t expectedSize, uint32_t defaultValue = 0); |
| 90 | + |
| 91 | + std::vector<uint32_t> unpackDirectPalettedData(const nbt::TagLongArray& data, uint8_t bitsPerEntry, size_t expectedSize); |
| 92 | + |
| 93 | + // Heightmap processing |
| 94 | + std::vector<uint16_t> unpackHeightmapData(const nbt::TagLongArray& data, int worldHeight); |
| 95 | + |
| 96 | + // Validation methods |
| 97 | + void validateChunkData(const World::ChunkData& chunk) const; |
| 98 | + void validateSection(const ChunkSection& section, int sectionY) const; |
| 99 | + |
| 100 | + // Utility methods |
| 101 | + std::pair<int32_t, int32_t> getRegionCoords(int32_t chunkX, int32_t chunkZ) const; |
| 102 | + size_t getChunkIndex(int32_t chunkX, int32_t chunkZ) const; |
| 103 | + uint32_t readBigEndianInt(const uint8_t* data) const; |
| 104 | + |
| 105 | + // NBT helper methods |
| 106 | + template <typename T> T getTagValue(const nbt::TagCompound& compound, const std::string& key, T defaultValue = T{}) const; |
| 107 | + |
| 108 | + bool hasTag(const nbt::TagCompound& compound, const std::string& key) const; |
| 109 | + }; |
| 110 | + |
| 111 | +}; // namespace World |
| 112 | + |
| 113 | +#endif // WORLD_CHUNKPARSER_HPP |
0 commit comments