This commit is contained in:
tycho 2015-12-27 15:39:30 +00:00
parent 0185716a91
commit be1284e40a
3 changed files with 54 additions and 41 deletions

13
src/Option.h Normal file
View File

@ -0,0 +1,13 @@
template<class T>
class Option
{
public:
Option(T a_value);
static Option<T> None();
template<class U, class F>
U To(F a_mapper, U a_default) const;
bool HasValue();
T GetValue();
};

View File

@ -139,7 +139,7 @@ bool cMapSerializer::LoadMapFromNBT(const cParsedNBT & a_NBT)
CurrLine = a_NBT.FindChildByName(Data.GetValue(), "dimension");
if ((CurrLine.HasValue()) && (a_NBT.GetType(CurrLine.GetValue()) == TAG_Byte))
{
eDimension Dimension = static_cast<eDimension>(a_NBT.GetByte(CurrLine));
eDimension Dimension = static_cast<eDimension>(a_NBT.GetByte(CurrLine.GetValue()));
if (Dimension != m_Map->m_World->GetDimension())
{
@ -148,10 +148,10 @@ bool cMapSerializer::LoadMapFromNBT(const cParsedNBT & a_NBT)
}
}
CurrLine = a_NBT.FindChildByName(Data, "width");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_Short))
CurrLine = a_NBT.FindChildByName(Data.GetValue(), "width");
if ((CurrLine.HasValue()) && (a_NBT.GetType(CurrLine.GetValue()) == TAG_Short))
{
unsigned int Width = static_cast<unsigned int>(a_NBT.GetShort(CurrLine));
unsigned int Width = static_cast<unsigned int>(a_NBT.GetShort(CurrLine.GetValue()));
if (Width != 128)
{
return false;
@ -159,10 +159,10 @@ bool cMapSerializer::LoadMapFromNBT(const cParsedNBT & a_NBT)
m_Map->m_Width = Width;
}
CurrLine = a_NBT.FindChildByName(Data, "height");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_Short))
CurrLine = a_NBT.FindChildByName(Data.GetValue(), "height");
if ((CurrLine.HasValue()) && (a_NBT.GetType(CurrLine.GetValue()) == TAG_Short))
{
unsigned int Height = static_cast<unsigned int>(a_NBT.GetShort(CurrLine));
unsigned int Height = static_cast<unsigned int>(a_NBT.GetShort(CurrLine.GetValue()));
if (Height >= 256)
{
return false;
@ -170,27 +170,27 @@ bool cMapSerializer::LoadMapFromNBT(const cParsedNBT & a_NBT)
m_Map->m_Height = Height;
}
CurrLine = a_NBT.FindChildByName(Data, "xCenter");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_Int))
CurrLine = a_NBT.FindChildByName(Data.GetValue(), "xCenter");
if ((CurrLine.HasValue()) && (a_NBT.GetType(CurrLine.GetValue()) == TAG_Int))
{
int CenterX = a_NBT.GetInt(CurrLine);
int CenterX = a_NBT.GetInt(CurrLine.GetValue());
m_Map->m_CenterX = CenterX;
}
CurrLine = a_NBT.FindChildByName(Data, "zCenter");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_Int))
CurrLine = a_NBT.FindChildByName(Data.GetValue(), "zCenter");
if ((CurrLine.HasValue()) && (a_NBT.GetType(CurrLine.GetValue()) == TAG_Int))
{
int CenterZ = a_NBT.GetInt(CurrLine);
int CenterZ = a_NBT.GetInt(CurrLine.GetValue());
m_Map->m_CenterZ = CenterZ;
}
unsigned int NumPixels = m_Map->GetNumPixels();
m_Map->m_Data.resize(NumPixels);
CurrLine = a_NBT.FindChildByName(Data, "colors");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_ByteArray))
CurrLine = a_NBT.FindChildByName(Data.GetValue(), "colors");
if ((CurrLine.HasValue()) && (a_NBT.GetType(CurrLine.GetValue()) == TAG_ByteArray))
{
memcpy(m_Map->m_Data.data(), a_NBT.GetData(CurrLine), NumPixels);
memcpy(m_Map->m_Data.data(), a_NBT.GetData(CurrLine.GetValue()), NumPixels);
}
return true;
@ -225,17 +225,17 @@ bool cIDCountSerializer::Load(void)
// NOTE: idcounts.dat is not compressed (raw format)
// Parse the NBT data:
cParsedNBT NBT(Data.data(), Data.size());
cParsedNBT NBT(std::basic_string<Byte>(reinterpret_cast<const Byte *>(Data.data()), Data.size()));
if (!NBT.IsValid())
{
// NBT Parsing failed
return false;
}
int CurrLine = NBT.FindChildByName(0, "map");
if (CurrLine >= 0)
auto CurrLine = NBT.FindChildByName(0, "map");
if (CurrLine.HasValue())
{
m_MapCount = static_cast<unsigned int>(NBT.GetShort(CurrLine) + 1);
m_MapCount = static_cast<unsigned int>(NBT.GetShort(CurrLine.GetValue()) + 1);
}
else
{
@ -261,7 +261,7 @@ bool cIDCountSerializer::Save(void)
Writer.Finish();
#ifdef _DEBUG
cParsedNBT TestParse(Writer.GetResult().data(), Writer.GetResult().size());
cParsedNBT TestParse(Writer.GetResult());
ASSERT(TestParse.IsValid());
#endif // _DEBUG

View File

@ -72,7 +72,7 @@ bool cSchematicFileSerializer::LoadFromSchematicFile(cBlockArea & a_BlockArea, c
File.Close();
// Parse the NBT:
cParsedNBT NBT(Contents.data(), Contents.size());
cParsedNBT NBT(std::basic_string<Byte>(reinterpret_cast<const Byte *>(Contents.data()), Contents.size()));
if (!NBT.IsValid())
{
LOG("Cannot parse the NBT in the schematic file \"%s\".", a_FileName.c_str());
@ -90,7 +90,7 @@ bool cSchematicFileSerializer::LoadFromSchematicFile(cBlockArea & a_BlockArea, c
bool cSchematicFileSerializer::LoadFromSchematicString(cBlockArea & a_BlockArea, const AString & a_SchematicData)
{
// Uncompress the data:
AString UngzippedData;
std::basic_string<Byte> UngzippedData;
if (UncompressStringGZIP(a_SchematicData.data(), a_SchematicData.size(), UngzippedData) != Z_OK)
{
LOG("%s: Cannot unGZip the schematic data.", __FUNCTION__);
@ -98,7 +98,7 @@ bool cSchematicFileSerializer::LoadFromSchematicString(cBlockArea & a_BlockArea,
}
// Parse the NBT:
cParsedNBT NBT(UngzippedData.data(), UngzippedData.size());
cParsedNBT NBT(UngzippedData);
if (!NBT.IsValid())
{
LOG("%s: Cannot parse the NBT in the schematic data.", __FUNCTION__);
@ -168,38 +168,38 @@ bool cSchematicFileSerializer::SaveToSchematicString(const cBlockArea & a_BlockA
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))
auto TMaterials = a_NBT.FindChildByName(a_NBT.GetRoot(), "Materials");
if ((TMaterials.HasValue()) && (a_NBT.GetType(TMaterials.GetValue()) == TAG_String))
{
AString Materials = a_NBT.GetString(TMaterials);
AString Materials = a_NBT.GetString(TMaterials.GetValue());
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");
auto TSizeX = a_NBT.FindChildByName(a_NBT.GetRoot(), "Width");
auto TSizeY = a_NBT.FindChildByName(a_NBT.GetRoot(), "Height");
auto 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)
(!TSizeX.HasValue()) || (!TSizeY.HasValue()) || (!TSizeZ.HasValue()) ||
(a_NBT.GetType(TSizeX.GetValue()) != TAG_Short) ||
(a_NBT.GetType(TSizeY.GetValue()) != TAG_Short) ||
(a_NBT.GetType(TSizeZ.GetValue()) != TAG_Short)
)
{
LOG("Dimensions are missing from the schematic file (%d, %d, %d), (%d, %d, %d)",
TSizeX, TSizeY, TSizeZ,
(TSizeX >= 0) ? a_NBT.GetType(TSizeX) : -1,
(TSizeY >= 0) ? a_NBT.GetType(TSizeY) : -1,
(TSizeZ >= 0) ? a_NBT.GetType(TSizeZ) : -1
TSizeX.GetValue(), TSizeY.GetValue(), TSizeZ.GetValue(),
(TSizeX.HasValue()) ? a_NBT.GetType(TSizeX.GetValue()) : -1,
(TSizeY.HasValue()) ? a_NBT.GetType(TSizeY.GetValue()) : -1,
(TSizeZ.HasValue()) ? a_NBT.GetType(TSizeZ.GetValue()) : -1
);
return false;
}
int SizeX = a_NBT.GetShort(TSizeX);
int SizeY = a_NBT.GetShort(TSizeY);
int SizeZ = a_NBT.GetShort(TSizeZ);
int SizeX = a_NBT.GetShort(TSizeX.GetValue());
int SizeY = a_NBT.GetShort(TSizeY.GetValue());
int SizeZ = a_NBT.GetShort(TSizeZ.GetValue());
if ((SizeX < 1) || (SizeX > 65535) || (SizeY < 1) || (SizeY > 256) || (SizeZ < 1) || (SizeZ > 65535))
{
LOG("Dimensions are invalid in the schematic file: %d, %d, %d", SizeX, SizeY, SizeZ);