private Chunk _LoadChunk(int x, int z)
{
//CurrentBlock.X = x;
//CurrentBlock.Z = z;
Chunk c = new Chunk(this);
c.Loading = true;
c.Filename = GetChunkFilename(x, z);
c.CreationDate = File.GetCreationTime(c.Filename);
c.Creator = "?";
c.Size = ChunkScale;
if (!File.Exists(c.Filename))
{
if(_DEBUG) Console.WriteLine("! {0}", c.Filename);
return null;
}
#if !DEBUG || CATCH_CHUNK_ERRORS
try
{
#endif
mChunk = new NbtFile(c.Filename);
mChunk.LoadFile();
NbtCompound level = mChunk.RootTag.Get<NbtCompound>("Level");
c.Position = new Vector3i(
level.Get<NbtInt>("xPos").Value,
0,
level.Get<NbtInt>("zPos").Value);
if ((int)c.Position.X != x || (int)c.Position.Z != z)
{
throw new Exception(string.Format("Chunk pos is wrong. {0}!={1}", c.Filename, c.Position));
}
NbtList TileEntities = (NbtList)level["TileEntities"];
if (TileEntities.Tags.Count > 0)
{
//if(_DEBUG) Console.WriteLine("*** Found TileEntities.");
LoadTileEnts(ref c, (int)x, (int)z, TileEntities);
}
NbtList Entities = (NbtList)level["Entities"];
//if(_DEBUG) Console.WriteLine("*** Found {0} Entities.",Entities.Tags.Count);
if (Entities.Tags.Count > 0)
{
LoadEnts(ref c, (int)x, (int)z, Entities);
}
// Blocks
c.Blocks = DecompressBlocks(level.Get<NbtByteArray>("Blocks").Value);
c.BlockLight = DecompressLightmap(level.Get<NbtByteArray>("BlockLight").Value);
c.SkyLight = DecompressLightmap(level.Get<NbtByteArray>("SkyLight").Value);
c.Data = DecompressDatamap(level.Get<NbtByteArray>("Data").Value);
c.Loading = false;
Vector2i ci = GetChunkHandle(x, z);
if (mChunks.ContainsKey(ci))
return mChunks[ci];
mChunks.Add(ci, c);
//TODO: Make Pig spawner converter.
for (int Z = 0; Z < ChunkScale.X; Z++)
{
for (int Y = 0; Y < ChunkScale.Y; Y++)
{
for (int X = 0; X < ChunkScale.X; X++)
{
byte b = c.Blocks[X, Y, Z];
if (b == 52)
{
MobSpawner ms = new MobSpawner();
ms.Pos=new Vector3i(X, Y, Z);
ms.EntityId = "Pig";
ms.UUID = Guid.NewGuid();
mTileEntities.Add(ms.UUID, ms);
c.TileEntities.Add(ms.UUID, ms);
//c++;
}
}
}
}
// Update overview called in here (if needed).
if (!Cache.LoadChunkMetadata(ref c))
{
Cache.SaveChunkMetadata(c);
}
//File.WriteAllText(Path.ChangeExtension(c.Filename,".dump.txt"), mChunk.RootTag.ToString());
//if (c>0) if(_DEBUG) Console.WriteLine("*** {0} spawners found.", c);
//if(_DEBUG) Console.WriteLine("Loaded {0} bytes from chunk {1}.", CurrentChunks.Length, c.Filename);
return c;
#if !DEBUG || CATCH_CHUNK_ERRORS
}
catch (Exception e)
{
string err = string.Format(" *** ERROR: Chunk {0},{1} ({2}) failed to load:\n\n{3}", x, z, c.Filename, e);
if (mAutoRepair)
{
File.Delete(c.Filename);
return null;
}
if(_DEBUG) Console.WriteLine(err);
if (CorruptChunk != null)
CorruptChunk(x,z,err, c.Filename);
return null;
}
#endif
}