#include "Common.h" #include "Types.h" #include "World.h" #include "nbt/NBT.h" #include "RegionFile.h" #include "data/Registry.h" #include #include #include #include namespace fs = std::filesystem; using std::string; using std::stringstream; namespace Feather { const GlobalPalette World::GLOBAL_PALETTE = GlobalPalette( Registry::BLOCK_STATES, new BlockState("minecraft:air") // temp ); static bool CheckPath(fs::path path, bool dir = false) { if (!fs::exists(path) || (dir && !fs::is_directory(path))) { if (dir) Log::Error("Cannot find folder \"{}\"", path.string().c_str()); else Log::Error("Cannot find file \"{}\"", path.string().c_str()); return false; } return true; } World::World(string name) { fs::path worldPath = fs::path(name); if (!CheckPath(worldPath)) return; fs::path regionsPath = worldPath/"region"; if (!CheckPath(regionsPath)) return; fs::path levelDatPath = worldPath/"level.dat"; if (!CheckPath(levelDatPath)) return; Log::Info("Loading world \"{}\"", name.c_str()); NBT::CompoundTag levelDat = NBT::CompoundTag(levelDatPath.string().c_str()); //std::cout << levelDat << "\n"; m_levelData.spawnX = levelDat.Get("SpawnX"); m_levelData.spawnY = levelDat.Get("SpawnY"); m_levelData.spawnZ = levelDat.Get("SpawnZ"); BlockPos spawnPos(m_levelData.spawnX, m_levelData.spawnY, m_levelData.spawnZ); ChunkPos spawnChunk(spawnPos); RegionPos regionPos(spawnChunk); RegionLocalPos regionChunkPos(spawnChunk); stringstream regionFile; regionFile << "r." << regionPos.x << "." << regionPos.z << ".mca"; fs::path mcaFile = regionsPath / regionFile.str(); if (!CheckPath(mcaFile)) return; Log::Trace("Spawn Chunk: Block({} {} {}) -> Chunk({} {}) -> Region({} {}) -> RegionChunk({} {})", spawnPos.x, spawnPos.y, spawnPos.z, spawnChunk.x, spawnChunk.z, regionPos.x, regionPos.z, regionChunkPos.x, regionChunkPos.z ); //Log_Info("Spawn Chunk Region File: %s", mcaFile.string().c_str()); RegionFile region(mcaFile.string()); //int regionOffset = region.GetOffset(regionChunkPos); //int sectorNum = RegionFile::GetSectorNumber(regionOffset); //Log::Trace("Loading chunk from offset: [{}] = {}", regionChunkPos.x + regionChunkPos.z * 32, sectorNum); std::ifstream* chunkStream = region.GetChunkStream(regionChunkPos); Log::Trace("{}", chunkStream->good()); char lengthBytes[5]; chunkStream->read(lengthBytes, 5); int32_t length = 0; std::memcpy(&length, lengthBytes, sizeof(int32_t)); length = ReverseBytes(length); int8_t compressionType = lengthBytes[4]; Log::Trace("Loading chunk: Length: {}. Compression: {}", length, compressionType); char* chunkData = new char[length + 1]; chunkStream->read(chunkData, length); m_chunkNBT = new NBT::CompoundTag(chunkData, length); std::cout << *m_chunkNBT << "\n"; m_chunk = new Chunk(spawnChunk, *m_chunkNBT); /*for (auto& f : fs::directory_iterator(regionsPath)) { std::cout << f.path() << "\n"; }*/ } };