From 4f09e8df6ecf20bd50573b7bf40c46f5ade21124 Mon Sep 17 00:00:00 2001 From: Tycho Date: Mon, 20 Jan 2014 09:59:12 -0800 Subject: Moved Schematic file methods to seperate class --- src/WorldStorage/SchematicFileSerilizer.cpp | 155 ++++++++++++++++++++++++++++ src/WorldStorage/SchematicFileSerilizer.h | 20 ++++ 2 files changed, 175 insertions(+) create mode 100644 src/WorldStorage/SchematicFileSerilizer.cpp create mode 100644 src/WorldStorage/SchematicFileSerilizer.h (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/SchematicFileSerilizer.cpp b/src/WorldStorage/SchematicFileSerilizer.cpp new file mode 100644 index 000000000..bce0119ec --- /dev/null +++ b/src/WorldStorage/SchematicFileSerilizer.cpp @@ -0,0 +1,155 @@ + +bool cSchematicFileSerializer::LoadFromSchematicFile(const AString & a_FileName) +{ + // Un-GZip the contents: + AString Contents; + cGZipFile File; + if (!File.Open(a_FileName, cGZipFile::fmRead)) + { + LOG("Cannot open the schematic file \"%s\".", a_FileName.c_str()); + return false; + } + int NumBytesRead = File.ReadRestOfFile(Contents); + if (NumBytesRead < 0) + { + LOG("Cannot read GZipped data in the schematic file \"%s\", error %d", a_FileName.c_str(), NumBytesRead); + return false; + } + File.Close(); + + // Parse the NBT: + cParsedNBT NBT(Contents.data(), Contents.size()); + if (!NBT.IsValid()) + { + LOG("Cannot parse the NBT in the schematic file \"%s\".", a_FileName.c_str()); + return false; + } + + return LoadFromSchematicNBT(NBT); +} + +bool cSchematicFileSerializer::SaveToSchematicFile(const AString & a_FileName) +{ + cFastNBTWriter Writer("Schematic"); + Writer.AddShort("Width", m_SizeX); + Writer.AddShort("Height", m_SizeY); + Writer.AddShort("Length", m_SizeZ); + Writer.AddString("Materials", "Alpha"); + if (HasBlockTypes()) + { + Writer.AddByteArray("Blocks", (const char *)m_BlockTypes, GetBlockCount()); + } + else + { + AString Dummy(GetBlockCount(), 0); + Writer.AddByteArray("Blocks", Dummy.data(), Dummy.size()); + } + if (HasBlockMetas()) + { + Writer.AddByteArray("Data", (const char *)m_BlockMetas, GetBlockCount()); + } + else + { + AString Dummy(GetBlockCount(), 0); + Writer.AddByteArray("Data", Dummy.data(), Dummy.size()); + } + // TODO: Save entities and block entities + Writer.BeginList("Entities", TAG_Compound); + Writer.EndList(); + Writer.BeginList("TileEntities", TAG_Compound); + Writer.EndList(); + Writer.Finish(); + + // Save to file + cGZipFile File; + if (!File.Open(a_FileName, cGZipFile::fmWrite)) + { + LOG("Cannot open file \"%s\" for writing.", a_FileName.c_str()); + return false; + } + if (!File.Write(Writer.GetResult())) + { + LOG("Cannot write data to file \"%s\".", a_FileName.c_str()); + return false; + } + return true; +} + +bool cBlockArea::LoadFromSchematicNBT(cParsedNBT & a_NBT) +{ + int TMaterials = a_NBT.FindChildByName(a_NBT.GetRoot(), "Materials"); + if ((TMaterials > 0) && (a_NBT.GetType(TMaterials) == TAG_String)) + { + AString Materials = a_NBT.GetString(TMaterials); + if (Materials.compare("Alpha") != 0) + { + LOG("Materials tag is present and \"%s\" instead of \"Alpha\". Possibly a wrong-format schematic file.", Materials.c_str()); + return false; + } + } + int TSizeX = a_NBT.FindChildByName(a_NBT.GetRoot(), "Width"); + int TSizeY = a_NBT.FindChildByName(a_NBT.GetRoot(), "Height"); + int TSizeZ = a_NBT.FindChildByName(a_NBT.GetRoot(), "Length"); + if ( + (TSizeX < 0) || (TSizeY < 0) || (TSizeZ < 0) || + (a_NBT.GetType(TSizeX) != TAG_Short) || + (a_NBT.GetType(TSizeY) != TAG_Short) || + (a_NBT.GetType(TSizeZ) != TAG_Short) + ) + { + LOG("Dimensions are missing from the schematic file (%d, %d, %d), (%d, %d, %d)", + TSizeX, TSizeY, TSizeZ, + a_NBT.GetType(TSizeX), a_NBT.GetType(TSizeY), a_NBT.GetType(TSizeZ) + ); + return false; + } + + int SizeX = a_NBT.GetShort(TSizeX); + int SizeY = a_NBT.GetShort(TSizeY); + int SizeZ = a_NBT.GetShort(TSizeZ); + if ((SizeX < 1) || (SizeY < 1) || (SizeZ < 1)) + { + LOG("Dimensions are invalid in the schematic file: %d, %d, %d", SizeX, SizeY, SizeZ); + return false; + } + + int TBlockTypes = a_NBT.FindChildByName(a_NBT.GetRoot(), "Blocks"); + int TBlockMetas = a_NBT.FindChildByName(a_NBT.GetRoot(), "Data"); + if ((TBlockTypes < 0) || (a_NBT.GetType(TBlockTypes) != TAG_ByteArray)) + { + LOG("BlockTypes are invalid in the schematic file: %d", TBlockTypes); + return false; + } + bool AreMetasPresent = (TBlockMetas > 0) && (a_NBT.GetType(TBlockMetas) == TAG_ByteArray); + + Clear(); + SetSize(SizeX, SizeY, SizeZ, AreMetasPresent ? (baTypes | baMetas) : baTypes); + + // Copy the block types and metas: + int NumBytes = m_SizeX * m_SizeY * m_SizeZ; + if (a_NBT.GetDataLength(TBlockTypes) < NumBytes) + { + LOG("BlockTypes truncated in the schematic file (exp %d, got %d bytes). Loading partial.", + NumBytes, a_NBT.GetDataLength(TBlockTypes) + ); + NumBytes = a_NBT.GetDataLength(TBlockTypes); + } + memcpy(m_BlockTypes, a_NBT.GetData(TBlockTypes), NumBytes); + + if (AreMetasPresent) + { + int NumBytes = m_SizeX * m_SizeY * m_SizeZ; + if (a_NBT.GetDataLength(TBlockMetas) < NumBytes) + { + LOG("BlockMetas truncated in the schematic file (exp %d, got %d bytes). Loading partial.", + NumBytes, a_NBT.GetDataLength(TBlockMetas) + ); + NumBytes = a_NBT.GetDataLength(TBlockMetas); + } + memcpy(m_BlockMetas, a_NBT.GetData(TBlockMetas), NumBytes); + } + + return true; +} + + diff --git a/src/WorldStorage/SchematicFileSerilizer.h b/src/WorldStorage/SchematicFileSerilizer.h new file mode 100644 index 000000000..b8a7162c6 --- /dev/null +++ b/src/WorldStorage/SchematicFileSerilizer.h @@ -0,0 +1,20 @@ + +#include "../BlockArea.h" + +// fwd: FastNBT.h +class cParsedNBT; + +class cSchematicFileSerializer +{ +public: + + /// Loads an area from a .schematic file. Returns true if successful + static bool LoadFromSchematicFile(const AString & a_FileName); + + /// Saves the area into a .schematic file. Returns true if successful + static bool SaveToSchematicFile(const AString & a_FileName); + +private: + /// Loads the area from a schematic file uncompressed and parsed into a NBT tree. Returns true if successful. + static bool LoadFromSchematicNBT(cBlockArea& a_BlockArea, cParsedNBT & a_NBT); +}; -- cgit v1.2.3 From ca3389231e111d8ca0bf4ca60ae4f96896da48b0 Mon Sep 17 00:00:00 2001 From: Tycho Date: Mon, 20 Jan 2014 10:15:19 -0800 Subject: Actually implemented interfaces --- src/WorldStorage/SchematicFileSerilizer.cpp | 45 +++++++++++++++++------------ src/WorldStorage/SchematicFileSerilizer.h | 4 +-- 2 files changed, 28 insertions(+), 21 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/SchematicFileSerilizer.cpp b/src/WorldStorage/SchematicFileSerilizer.cpp index bce0119ec..df68f3436 100644 --- a/src/WorldStorage/SchematicFileSerilizer.cpp +++ b/src/WorldStorage/SchematicFileSerilizer.cpp @@ -1,5 +1,12 @@ -bool cSchematicFileSerializer::LoadFromSchematicFile(const AString & a_FileName) +#include "Globals.h" + +#include "OSSupport/GZipFile.h" +#include "FastNBT.h" + +#include "SchematicFileSerilizer.h" + +bool cSchematicFileSerializer::LoadFromSchematicFile(cBlockArea& a_BlockArea, const AString & a_FileName) { // Un-GZip the contents: AString Contents; @@ -25,32 +32,32 @@ bool cSchematicFileSerializer::LoadFromSchematicFile(const AString & a_FileName) return false; } - return LoadFromSchematicNBT(NBT); + return LoadFromSchematicNBT(a_BlockArea, NBT); } -bool cSchematicFileSerializer::SaveToSchematicFile(const AString & a_FileName) +bool cSchematicFileSerializer::SaveToSchematicFile(cBlockArea& a_BlockArea, const AString & a_FileName) { cFastNBTWriter Writer("Schematic"); - Writer.AddShort("Width", m_SizeX); - Writer.AddShort("Height", m_SizeY); - Writer.AddShort("Length", m_SizeZ); + Writer.AddShort("Width", a_BlockArea.m_SizeX); + Writer.AddShort("Height", a_BlockArea.m_SizeY); + Writer.AddShort("Length", a_BlockArea.m_SizeZ); Writer.AddString("Materials", "Alpha"); - if (HasBlockTypes()) + if (a_BlockArea.HasBlockTypes()) { - Writer.AddByteArray("Blocks", (const char *)m_BlockTypes, GetBlockCount()); + Writer.AddByteArray("Blocks", (const char *)a_BlockArea.m_BlockTypes, a_BlockArea.GetBlockCount()); } else { - AString Dummy(GetBlockCount(), 0); + AString Dummy(a_BlockArea.GetBlockCount(), 0); Writer.AddByteArray("Blocks", Dummy.data(), Dummy.size()); } - if (HasBlockMetas()) + if (a_BlockArea.HasBlockMetas()) { - Writer.AddByteArray("Data", (const char *)m_BlockMetas, GetBlockCount()); + Writer.AddByteArray("Data", (const char *)a_BlockArea.m_BlockMetas, a_BlockArea.GetBlockCount()); } else { - AString Dummy(GetBlockCount(), 0); + AString Dummy(a_BlockArea.GetBlockCount(), 0); Writer.AddByteArray("Data", Dummy.data(), Dummy.size()); } // TODO: Save entities and block entities @@ -75,7 +82,7 @@ bool cSchematicFileSerializer::SaveToSchematicFile(const AString & a_FileName) return true; } -bool cBlockArea::LoadFromSchematicNBT(cParsedNBT & a_NBT) +bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea& a_BlockArea, cParsedNBT & a_NBT) { int TMaterials = a_NBT.FindChildByName(a_NBT.GetRoot(), "Materials"); if ((TMaterials > 0) && (a_NBT.GetType(TMaterials) == TAG_String)) @@ -122,11 +129,11 @@ bool cBlockArea::LoadFromSchematicNBT(cParsedNBT & a_NBT) } bool AreMetasPresent = (TBlockMetas > 0) && (a_NBT.GetType(TBlockMetas) == TAG_ByteArray); - Clear(); - SetSize(SizeX, SizeY, SizeZ, AreMetasPresent ? (baTypes | baMetas) : baTypes); + a_BlockArea.Clear(); + a_BlockArea.SetSize(SizeX, SizeY, SizeZ, AreMetasPresent ? (cBlockArea::baTypes | cBlockArea::baMetas) : cBlockArea::baTypes); // Copy the block types and metas: - int NumBytes = m_SizeX * m_SizeY * m_SizeZ; + int NumBytes = a_BlockArea.m_SizeX * a_BlockArea.m_SizeY * a_BlockArea.m_SizeZ; if (a_NBT.GetDataLength(TBlockTypes) < NumBytes) { LOG("BlockTypes truncated in the schematic file (exp %d, got %d bytes). Loading partial.", @@ -134,11 +141,11 @@ bool cBlockArea::LoadFromSchematicNBT(cParsedNBT & a_NBT) ); NumBytes = a_NBT.GetDataLength(TBlockTypes); } - memcpy(m_BlockTypes, a_NBT.GetData(TBlockTypes), NumBytes); + memcpy(a_BlockArea.m_BlockTypes, a_NBT.GetData(TBlockTypes), NumBytes); if (AreMetasPresent) { - int NumBytes = m_SizeX * m_SizeY * m_SizeZ; + int NumBytes = a_BlockArea.m_SizeX * a_BlockArea.m_SizeY * a_BlockArea.m_SizeZ; if (a_NBT.GetDataLength(TBlockMetas) < NumBytes) { LOG("BlockMetas truncated in the schematic file (exp %d, got %d bytes). Loading partial.", @@ -146,7 +153,7 @@ bool cBlockArea::LoadFromSchematicNBT(cParsedNBT & a_NBT) ); NumBytes = a_NBT.GetDataLength(TBlockMetas); } - memcpy(m_BlockMetas, a_NBT.GetData(TBlockMetas), NumBytes); + memcpy(a_BlockArea.m_BlockMetas, a_NBT.GetData(TBlockMetas), NumBytes); } return true; diff --git a/src/WorldStorage/SchematicFileSerilizer.h b/src/WorldStorage/SchematicFileSerilizer.h index b8a7162c6..e4dcf3eb9 100644 --- a/src/WorldStorage/SchematicFileSerilizer.h +++ b/src/WorldStorage/SchematicFileSerilizer.h @@ -9,10 +9,10 @@ class cSchematicFileSerializer public: /// Loads an area from a .schematic file. Returns true if successful - static bool LoadFromSchematicFile(const AString & a_FileName); + static bool LoadFromSchematicFile(cBlockArea& a_BlockArea, const AString & a_FileName); /// Saves the area into a .schematic file. Returns true if successful - static bool SaveToSchematicFile(const AString & a_FileName); + static bool SaveToSchematicFile(cBlockArea& a_BlockArea, const AString & a_FileName); private: /// Loads the area from a schematic file uncompressed and parsed into a NBT tree. Returns true if successful. -- cgit v1.2.3 From 1c320fa18c65ddb546ec5ff396f5554db306bd8b Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 22 Jan 2014 10:13:41 -0800 Subject: formatting changes --- src/WorldStorage/SchematicFileSerilizer.cpp | 16 +++++++++++++--- src/WorldStorage/SchematicFileSerilizer.h | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/SchematicFileSerilizer.cpp b/src/WorldStorage/SchematicFileSerilizer.cpp index df68f3436..4e2ecb752 100644 --- a/src/WorldStorage/SchematicFileSerilizer.cpp +++ b/src/WorldStorage/SchematicFileSerilizer.cpp @@ -6,7 +6,7 @@ #include "SchematicFileSerilizer.h" -bool cSchematicFileSerializer::LoadFromSchematicFile(cBlockArea& a_BlockArea, const AString & a_FileName) +bool cSchematicFileSerializer::LoadFromSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName) { // Un-GZip the contents: AString Contents; @@ -35,7 +35,12 @@ bool cSchematicFileSerializer::LoadFromSchematicFile(cBlockArea& a_BlockArea, co return LoadFromSchematicNBT(a_BlockArea, NBT); } -bool cSchematicFileSerializer::SaveToSchematicFile(cBlockArea& a_BlockArea, const AString & a_FileName) + + + + + +bool cSchematicFileSerializer::SaveToSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName) { cFastNBTWriter Writer("Schematic"); Writer.AddShort("Width", a_BlockArea.m_SizeX); @@ -82,7 +87,12 @@ bool cSchematicFileSerializer::SaveToSchematicFile(cBlockArea& a_BlockArea, cons return true; } -bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea& a_BlockArea, cParsedNBT & a_NBT) + + + + + +bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cParsedNBT & a_NBT) { int TMaterials = a_NBT.FindChildByName(a_NBT.GetRoot(), "Materials"); if ((TMaterials > 0) && (a_NBT.GetType(TMaterials) == TAG_String)) diff --git a/src/WorldStorage/SchematicFileSerilizer.h b/src/WorldStorage/SchematicFileSerilizer.h index e4dcf3eb9..cb30e55d8 100644 --- a/src/WorldStorage/SchematicFileSerilizer.h +++ b/src/WorldStorage/SchematicFileSerilizer.h @@ -1,20 +1,30 @@ +#pragma once + #include "../BlockArea.h" + + + + // fwd: FastNBT.h class cParsedNBT; + + + + class cSchematicFileSerializer { public: /// Loads an area from a .schematic file. Returns true if successful - static bool LoadFromSchematicFile(cBlockArea& a_BlockArea, const AString & a_FileName); + static bool LoadFromSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName); /// Saves the area into a .schematic file. Returns true if successful - static bool SaveToSchematicFile(cBlockArea& a_BlockArea, const AString & a_FileName); + static bool SaveToSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName); private: /// Loads the area from a schematic file uncompressed and parsed into a NBT tree. Returns true if successful. - static bool LoadFromSchematicNBT(cBlockArea& a_BlockArea, cParsedNBT & a_NBT); + static bool LoadFromSchematicNBT(cBlockArea & a_BlockArea, cParsedNBT & a_NBT); }; -- cgit v1.2.3 From 571200019d7f0a948145b6cf8c2594d3e75cb375 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 22 Jan 2014 10:35:36 -0800 Subject: Added manual bindings for moved functions --- src/WorldStorage/SchematicFileSerilizer.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/SchematicFileSerilizer.h b/src/WorldStorage/SchematicFileSerilizer.h index cb30e55d8..31b36695c 100644 --- a/src/WorldStorage/SchematicFileSerilizer.h +++ b/src/WorldStorage/SchematicFileSerilizer.h @@ -13,7 +13,7 @@ class cParsedNBT; - +// tolua_begin class cSchematicFileSerializer { public: @@ -24,7 +24,9 @@ public: /// Saves the area into a .schematic file. Returns true if successful static bool SaveToSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName); + // tolua_end + private: /// Loads the area from a schematic file uncompressed and parsed into a NBT tree. Returns true if successful. static bool LoadFromSchematicNBT(cBlockArea & a_BlockArea, cParsedNBT & a_NBT); -}; +}; // tolua_export -- cgit v1.2.3 From 5ef0a00a6c11c183070b97d2121009de27830713 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 22 Jan 2014 10:39:09 -0800 Subject: Fixed spelling error --- src/WorldStorage/SchematicFileSerializer.cpp | 172 +++++++++++++++++++++++++++ src/WorldStorage/SchematicFileSerializer.h | 32 +++++ src/WorldStorage/SchematicFileSerilizer.cpp | 172 --------------------------- src/WorldStorage/SchematicFileSerilizer.h | 32 ----- 4 files changed, 204 insertions(+), 204 deletions(-) create mode 100644 src/WorldStorage/SchematicFileSerializer.cpp create mode 100644 src/WorldStorage/SchematicFileSerializer.h delete mode 100644 src/WorldStorage/SchematicFileSerilizer.cpp delete mode 100644 src/WorldStorage/SchematicFileSerilizer.h (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/SchematicFileSerializer.cpp b/src/WorldStorage/SchematicFileSerializer.cpp new file mode 100644 index 000000000..45fd967bd --- /dev/null +++ b/src/WorldStorage/SchematicFileSerializer.cpp @@ -0,0 +1,172 @@ + +#include "Globals.h" + +#include "OSSupport/GZipFile.h" +#include "FastNBT.h" + +#include "SchematicFileSerializer.h" + +bool cSchematicFileSerializer::LoadFromSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName) +{ + // Un-GZip the contents: + AString Contents; + cGZipFile File; + if (!File.Open(a_FileName, cGZipFile::fmRead)) + { + LOG("Cannot open the schematic file \"%s\".", a_FileName.c_str()); + return false; + } + int NumBytesRead = File.ReadRestOfFile(Contents); + if (NumBytesRead < 0) + { + LOG("Cannot read GZipped data in the schematic file \"%s\", error %d", a_FileName.c_str(), NumBytesRead); + return false; + } + File.Close(); + + // Parse the NBT: + cParsedNBT NBT(Contents.data(), Contents.size()); + if (!NBT.IsValid()) + { + LOG("Cannot parse the NBT in the schematic file \"%s\".", a_FileName.c_str()); + return false; + } + + return LoadFromSchematicNBT(a_BlockArea, NBT); +} + + + + + + +bool cSchematicFileSerializer::SaveToSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName) +{ + cFastNBTWriter Writer("Schematic"); + Writer.AddShort("Width", a_BlockArea.m_SizeX); + Writer.AddShort("Height", a_BlockArea.m_SizeY); + Writer.AddShort("Length", a_BlockArea.m_SizeZ); + Writer.AddString("Materials", "Alpha"); + if (a_BlockArea.HasBlockTypes()) + { + Writer.AddByteArray("Blocks", (const char *)a_BlockArea.m_BlockTypes, a_BlockArea.GetBlockCount()); + } + else + { + AString Dummy(a_BlockArea.GetBlockCount(), 0); + Writer.AddByteArray("Blocks", Dummy.data(), Dummy.size()); + } + if (a_BlockArea.HasBlockMetas()) + { + Writer.AddByteArray("Data", (const char *)a_BlockArea.m_BlockMetas, a_BlockArea.GetBlockCount()); + } + else + { + AString Dummy(a_BlockArea.GetBlockCount(), 0); + Writer.AddByteArray("Data", Dummy.data(), Dummy.size()); + } + // TODO: Save entities and block entities + Writer.BeginList("Entities", TAG_Compound); + Writer.EndList(); + Writer.BeginList("TileEntities", TAG_Compound); + Writer.EndList(); + Writer.Finish(); + + // Save to file + cGZipFile File; + if (!File.Open(a_FileName, cGZipFile::fmWrite)) + { + LOG("Cannot open file \"%s\" for writing.", a_FileName.c_str()); + return false; + } + if (!File.Write(Writer.GetResult())) + { + LOG("Cannot write data to file \"%s\".", a_FileName.c_str()); + return false; + } + return true; +} + + + + + + +bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cParsedNBT & a_NBT) +{ + int TMaterials = a_NBT.FindChildByName(a_NBT.GetRoot(), "Materials"); + if ((TMaterials > 0) && (a_NBT.GetType(TMaterials) == TAG_String)) + { + AString Materials = a_NBT.GetString(TMaterials); + if (Materials.compare("Alpha") != 0) + { + LOG("Materials tag is present and \"%s\" instead of \"Alpha\". Possibly a wrong-format schematic file.", Materials.c_str()); + return false; + } + } + int TSizeX = a_NBT.FindChildByName(a_NBT.GetRoot(), "Width"); + int TSizeY = a_NBT.FindChildByName(a_NBT.GetRoot(), "Height"); + int TSizeZ = a_NBT.FindChildByName(a_NBT.GetRoot(), "Length"); + if ( + (TSizeX < 0) || (TSizeY < 0) || (TSizeZ < 0) || + (a_NBT.GetType(TSizeX) != TAG_Short) || + (a_NBT.GetType(TSizeY) != TAG_Short) || + (a_NBT.GetType(TSizeZ) != TAG_Short) + ) + { + LOG("Dimensions are missing from the schematic file (%d, %d, %d), (%d, %d, %d)", + TSizeX, TSizeY, TSizeZ, + a_NBT.GetType(TSizeX), a_NBT.GetType(TSizeY), a_NBT.GetType(TSizeZ) + ); + return false; + } + + int SizeX = a_NBT.GetShort(TSizeX); + int SizeY = a_NBT.GetShort(TSizeY); + int SizeZ = a_NBT.GetShort(TSizeZ); + if ((SizeX < 1) || (SizeY < 1) || (SizeZ < 1)) + { + LOG("Dimensions are invalid in the schematic file: %d, %d, %d", SizeX, SizeY, SizeZ); + return false; + } + + int TBlockTypes = a_NBT.FindChildByName(a_NBT.GetRoot(), "Blocks"); + int TBlockMetas = a_NBT.FindChildByName(a_NBT.GetRoot(), "Data"); + if ((TBlockTypes < 0) || (a_NBT.GetType(TBlockTypes) != TAG_ByteArray)) + { + LOG("BlockTypes are invalid in the schematic file: %d", TBlockTypes); + return false; + } + bool AreMetasPresent = (TBlockMetas > 0) && (a_NBT.GetType(TBlockMetas) == TAG_ByteArray); + + a_BlockArea.Clear(); + a_BlockArea.SetSize(SizeX, SizeY, SizeZ, AreMetasPresent ? (cBlockArea::baTypes | cBlockArea::baMetas) : cBlockArea::baTypes); + + // Copy the block types and metas: + int NumBytes = a_BlockArea.m_SizeX * a_BlockArea.m_SizeY * a_BlockArea.m_SizeZ; + if (a_NBT.GetDataLength(TBlockTypes) < NumBytes) + { + LOG("BlockTypes truncated in the schematic file (exp %d, got %d bytes). Loading partial.", + NumBytes, a_NBT.GetDataLength(TBlockTypes) + ); + NumBytes = a_NBT.GetDataLength(TBlockTypes); + } + memcpy(a_BlockArea.m_BlockTypes, a_NBT.GetData(TBlockTypes), NumBytes); + + if (AreMetasPresent) + { + int NumBytes = a_BlockArea.m_SizeX * a_BlockArea.m_SizeY * a_BlockArea.m_SizeZ; + if (a_NBT.GetDataLength(TBlockMetas) < NumBytes) + { + LOG("BlockMetas truncated in the schematic file (exp %d, got %d bytes). Loading partial.", + NumBytes, a_NBT.GetDataLength(TBlockMetas) + ); + NumBytes = a_NBT.GetDataLength(TBlockMetas); + } + memcpy(a_BlockArea.m_BlockMetas, a_NBT.GetData(TBlockMetas), NumBytes); + } + + return true; +} + + diff --git a/src/WorldStorage/SchematicFileSerializer.h b/src/WorldStorage/SchematicFileSerializer.h new file mode 100644 index 000000000..31b36695c --- /dev/null +++ b/src/WorldStorage/SchematicFileSerializer.h @@ -0,0 +1,32 @@ + +#pragma once + +#include "../BlockArea.h" + + + + + +// fwd: FastNBT.h +class cParsedNBT; + + + + +// tolua_begin +class cSchematicFileSerializer +{ +public: + + /// Loads an area from a .schematic file. Returns true if successful + static bool LoadFromSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName); + + /// Saves the area into a .schematic file. Returns true if successful + static bool SaveToSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName); + + // tolua_end + +private: + /// Loads the area from a schematic file uncompressed and parsed into a NBT tree. Returns true if successful. + static bool LoadFromSchematicNBT(cBlockArea & a_BlockArea, cParsedNBT & a_NBT); +}; // tolua_export diff --git a/src/WorldStorage/SchematicFileSerilizer.cpp b/src/WorldStorage/SchematicFileSerilizer.cpp deleted file mode 100644 index 4e2ecb752..000000000 --- a/src/WorldStorage/SchematicFileSerilizer.cpp +++ /dev/null @@ -1,172 +0,0 @@ - -#include "Globals.h" - -#include "OSSupport/GZipFile.h" -#include "FastNBT.h" - -#include "SchematicFileSerilizer.h" - -bool cSchematicFileSerializer::LoadFromSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName) -{ - // Un-GZip the contents: - AString Contents; - cGZipFile File; - if (!File.Open(a_FileName, cGZipFile::fmRead)) - { - LOG("Cannot open the schematic file \"%s\".", a_FileName.c_str()); - return false; - } - int NumBytesRead = File.ReadRestOfFile(Contents); - if (NumBytesRead < 0) - { - LOG("Cannot read GZipped data in the schematic file \"%s\", error %d", a_FileName.c_str(), NumBytesRead); - return false; - } - File.Close(); - - // Parse the NBT: - cParsedNBT NBT(Contents.data(), Contents.size()); - if (!NBT.IsValid()) - { - LOG("Cannot parse the NBT in the schematic file \"%s\".", a_FileName.c_str()); - return false; - } - - return LoadFromSchematicNBT(a_BlockArea, NBT); -} - - - - - - -bool cSchematicFileSerializer::SaveToSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName) -{ - cFastNBTWriter Writer("Schematic"); - Writer.AddShort("Width", a_BlockArea.m_SizeX); - Writer.AddShort("Height", a_BlockArea.m_SizeY); - Writer.AddShort("Length", a_BlockArea.m_SizeZ); - Writer.AddString("Materials", "Alpha"); - if (a_BlockArea.HasBlockTypes()) - { - Writer.AddByteArray("Blocks", (const char *)a_BlockArea.m_BlockTypes, a_BlockArea.GetBlockCount()); - } - else - { - AString Dummy(a_BlockArea.GetBlockCount(), 0); - Writer.AddByteArray("Blocks", Dummy.data(), Dummy.size()); - } - if (a_BlockArea.HasBlockMetas()) - { - Writer.AddByteArray("Data", (const char *)a_BlockArea.m_BlockMetas, a_BlockArea.GetBlockCount()); - } - else - { - AString Dummy(a_BlockArea.GetBlockCount(), 0); - Writer.AddByteArray("Data", Dummy.data(), Dummy.size()); - } - // TODO: Save entities and block entities - Writer.BeginList("Entities", TAG_Compound); - Writer.EndList(); - Writer.BeginList("TileEntities", TAG_Compound); - Writer.EndList(); - Writer.Finish(); - - // Save to file - cGZipFile File; - if (!File.Open(a_FileName, cGZipFile::fmWrite)) - { - LOG("Cannot open file \"%s\" for writing.", a_FileName.c_str()); - return false; - } - if (!File.Write(Writer.GetResult())) - { - LOG("Cannot write data to file \"%s\".", a_FileName.c_str()); - return false; - } - return true; -} - - - - - - -bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cParsedNBT & a_NBT) -{ - int TMaterials = a_NBT.FindChildByName(a_NBT.GetRoot(), "Materials"); - if ((TMaterials > 0) && (a_NBT.GetType(TMaterials) == TAG_String)) - { - AString Materials = a_NBT.GetString(TMaterials); - if (Materials.compare("Alpha") != 0) - { - LOG("Materials tag is present and \"%s\" instead of \"Alpha\". Possibly a wrong-format schematic file.", Materials.c_str()); - return false; - } - } - int TSizeX = a_NBT.FindChildByName(a_NBT.GetRoot(), "Width"); - int TSizeY = a_NBT.FindChildByName(a_NBT.GetRoot(), "Height"); - int TSizeZ = a_NBT.FindChildByName(a_NBT.GetRoot(), "Length"); - if ( - (TSizeX < 0) || (TSizeY < 0) || (TSizeZ < 0) || - (a_NBT.GetType(TSizeX) != TAG_Short) || - (a_NBT.GetType(TSizeY) != TAG_Short) || - (a_NBT.GetType(TSizeZ) != TAG_Short) - ) - { - LOG("Dimensions are missing from the schematic file (%d, %d, %d), (%d, %d, %d)", - TSizeX, TSizeY, TSizeZ, - a_NBT.GetType(TSizeX), a_NBT.GetType(TSizeY), a_NBT.GetType(TSizeZ) - ); - return false; - } - - int SizeX = a_NBT.GetShort(TSizeX); - int SizeY = a_NBT.GetShort(TSizeY); - int SizeZ = a_NBT.GetShort(TSizeZ); - if ((SizeX < 1) || (SizeY < 1) || (SizeZ < 1)) - { - LOG("Dimensions are invalid in the schematic file: %d, %d, %d", SizeX, SizeY, SizeZ); - return false; - } - - int TBlockTypes = a_NBT.FindChildByName(a_NBT.GetRoot(), "Blocks"); - int TBlockMetas = a_NBT.FindChildByName(a_NBT.GetRoot(), "Data"); - if ((TBlockTypes < 0) || (a_NBT.GetType(TBlockTypes) != TAG_ByteArray)) - { - LOG("BlockTypes are invalid in the schematic file: %d", TBlockTypes); - return false; - } - bool AreMetasPresent = (TBlockMetas > 0) && (a_NBT.GetType(TBlockMetas) == TAG_ByteArray); - - a_BlockArea.Clear(); - a_BlockArea.SetSize(SizeX, SizeY, SizeZ, AreMetasPresent ? (cBlockArea::baTypes | cBlockArea::baMetas) : cBlockArea::baTypes); - - // Copy the block types and metas: - int NumBytes = a_BlockArea.m_SizeX * a_BlockArea.m_SizeY * a_BlockArea.m_SizeZ; - if (a_NBT.GetDataLength(TBlockTypes) < NumBytes) - { - LOG("BlockTypes truncated in the schematic file (exp %d, got %d bytes). Loading partial.", - NumBytes, a_NBT.GetDataLength(TBlockTypes) - ); - NumBytes = a_NBT.GetDataLength(TBlockTypes); - } - memcpy(a_BlockArea.m_BlockTypes, a_NBT.GetData(TBlockTypes), NumBytes); - - if (AreMetasPresent) - { - int NumBytes = a_BlockArea.m_SizeX * a_BlockArea.m_SizeY * a_BlockArea.m_SizeZ; - if (a_NBT.GetDataLength(TBlockMetas) < NumBytes) - { - LOG("BlockMetas truncated in the schematic file (exp %d, got %d bytes). Loading partial.", - NumBytes, a_NBT.GetDataLength(TBlockMetas) - ); - NumBytes = a_NBT.GetDataLength(TBlockMetas); - } - memcpy(a_BlockArea.m_BlockMetas, a_NBT.GetData(TBlockMetas), NumBytes); - } - - return true; -} - - diff --git a/src/WorldStorage/SchematicFileSerilizer.h b/src/WorldStorage/SchematicFileSerilizer.h deleted file mode 100644 index 31b36695c..000000000 --- a/src/WorldStorage/SchematicFileSerilizer.h +++ /dev/null @@ -1,32 +0,0 @@ - -#pragma once - -#include "../BlockArea.h" - - - - - -// fwd: FastNBT.h -class cParsedNBT; - - - - -// tolua_begin -class cSchematicFileSerializer -{ -public: - - /// Loads an area from a .schematic file. Returns true if successful - static bool LoadFromSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName); - - /// Saves the area into a .schematic file. Returns true if successful - static bool SaveToSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName); - - // tolua_end - -private: - /// Loads the area from a schematic file uncompressed and parsed into a NBT tree. Returns true if successful. - static bool LoadFromSchematicNBT(cBlockArea & a_BlockArea, cParsedNBT & a_NBT); -}; // tolua_export -- cgit v1.2.3 From 2806b48afa4e3d087c8a123d81f9eabe3ec797c5 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 25 Jan 2014 06:06:30 -0800 Subject: Fixed exports --- src/WorldStorage/SchematicFileSerializer.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/WorldStorage') diff --git a/src/WorldStorage/SchematicFileSerializer.h b/src/WorldStorage/SchematicFileSerializer.h index 31b36695c..9be2e5b57 100644 --- a/src/WorldStorage/SchematicFileSerializer.h +++ b/src/WorldStorage/SchematicFileSerializer.h @@ -13,7 +13,6 @@ class cParsedNBT; -// tolua_begin class cSchematicFileSerializer { public: @@ -24,9 +23,7 @@ public: /// Saves the area into a .schematic file. Returns true if successful static bool SaveToSchematicFile(cBlockArea & a_BlockArea, const AString & a_FileName); - // tolua_end - private: /// Loads the area from a schematic file uncompressed and parsed into a NBT tree. Returns true if successful. static bool LoadFromSchematicNBT(cBlockArea & a_BlockArea, cParsedNBT & a_NBT); -}; // tolua_export +}; -- cgit v1.2.3