Fixed layers saving/loading

Global scenery save/load testing was also improved to detect these cases better
This commit is contained in:
Michaël Lemaire 2015-08-13 19:49:16 +02:00
parent 46e9e73793
commit 6062c755b5
6 changed files with 50 additions and 4 deletions

View file

@ -65,6 +65,11 @@ void NoiseGenerator::load(PackStream* stream)
stream->read(&height_offset);
stream->read(&level_count);
if (level_count > MAX_LEVEL_COUNT)
{
level_count = MAX_LEVEL_COUNT;
}
for (x = 0; x < level_count; x++)
{
NoiseLevel* level = levels + x;

View file

@ -29,6 +29,12 @@ void NoiseState::load(PackStream *stream)
int levels;
stream->read(&levels);
level_offsets.clear();
if (levels > 1000)
{
levels = 1000;
}
for (int i = 0; i < levels; i++)
{
NoiseOffset level_offset;

View file

@ -1,5 +1,6 @@
#include "Layers.h"
#include "PackStream.h"
#include "Logs.h"
Layers::Layers(DefinitionNode* parent, const std::string &name, LayerConstructor layer_constructor):
@ -15,6 +16,32 @@ Layers::~Layers()
delete null_layer;
}
void Layers::save(PackStream *stream) const
{
int layer_count = (int)layers.size();
stream->write(&layer_count);
DefinitionNode::save(stream);
}
void Layers::load(PackStream *stream)
{
int layer_count;
stream->read(&layer_count);
if (layer_count > max_layer_count)
{
layer_count = max_layer_count;
}
clear();
for (int i = 0; i < layer_count; i++)
{
addLayer();
}
DefinitionNode::load(stream);
}
void Layers::copy(DefinitionNode* destination_) const
{
DefinitionNode::copy(destination_);

View file

@ -19,6 +19,8 @@ public:
Layers(DefinitionNode* parent, const std::string &name, LayerConstructor layer_constructor);
virtual ~Layers();
virtual void save(PackStream *stream) const override;
virtual void load(PackStream *stream) override;
virtual void copy(DefinitionNode* destination) const override;
Layers* newCopy() const;
@ -48,6 +50,7 @@ public:
int max_layer_count;
std::vector<DefinitionNode*> layers;
DefinitionNode* null_layer;
};
}

View file

@ -92,6 +92,10 @@ Scenery::FileOperationResult Scenery::loadGlobal(const std::string &filepath)
load(&stream);
// Reset read headers, to test them again
app_header = -1;
version_header = -1;
stream.read(&version_header);
if ((int)version_header != DATA_VERSION)
{

View file

@ -4,12 +4,13 @@
TEST(Scenery, saveGlobal)
{
Scenery scenery;
scenery.autoPreset();
Scenery scenery1;
scenery1.autoPreset();
Scenery::FileOperationResult result = scenery.saveGlobal("/tmp/test_paysages_scenery");
Scenery::FileOperationResult result = scenery1.saveGlobal("/tmp/test_paysages_scenery");
EXPECT_EQ((int)Scenery::FILE_OPERATION_OK, result);
result = scenery.loadGlobal("/tmp/test_paysages_scenery");
Scenery scenery2;
result = scenery2.loadGlobal("/tmp/test_paysages_scenery");
EXPECT_EQ((int)Scenery::FILE_OPERATION_OK, result);
}