FeatherMC/src/world/World.cpp

72 lines
1.7 KiB
C++

#include "Common.h"
#include "Types.h"
#include "World.h"
#include "nbt/NBT.h"
#include "RegionFile.h"
#include <iostream>
#include <fstream>
#include <filesystem>
#include <sstream>
namespace fs = std::filesystem;
using std::string;
using std::stringstream;
namespace Feather
{
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 \"%s\"", path.string().c_str());
else Log_Error("Cannot find file \"%s\"", 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 \"%s\"", name.c_str());
NBT::CompoundTag levelDat = NBT::CompoundTag(levelDatPath.string().c_str());
//std::cout << levelDat << "\n";
m_levelData.spawnX = levelDat.Get<int32_t>("SpawnX");
m_levelData.spawnY = levelDat.Get<int32_t>("SpawnY");
m_levelData.spawnZ = levelDat.Get<int32_t>("SpawnZ");
ChunkPos spawnChunk(BlockPos(m_levelData.spawnX, m_levelData.spawnY, m_levelData.spawnZ));
RegionPos regionPos(spawnChunk);
stringstream regionFile;
regionFile << "r." << regionPos.x << "." << regionPos.z << ".mca";
fs::path mcaFile = regionsPath / regionFile.str();
if (!CheckPath(mcaFile)) return;
//Log_Info("Spawn Chunk Region File: %s", mcaFile.string().c_str());
RegionFile region(mcaFile.string());
//std::cout << region << "\n";
/*for (auto& f : fs::directory_iterator(regionsPath))
{
std::cout << f.path() << "\n";
}*/
}
};