From 6062c755b5213aeaa75f6c0e8ffe57d00fa23708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Thu, 13 Aug 2015 19:49:16 +0200 Subject: [PATCH] Fixed layers saving/loading Global scenery save/load testing was also improved to detect these cases better --- src/basics/NoiseGenerator.cpp | 5 +++++ src/basics/NoiseState.cpp | 6 ++++++ src/definition/Layers.cpp | 27 +++++++++++++++++++++++++++ src/definition/Layers.h | 3 +++ src/definition/Scenery.cpp | 4 ++++ src/tests/Scenery_Test.cpp | 9 +++++---- 6 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/basics/NoiseGenerator.cpp b/src/basics/NoiseGenerator.cpp index 907095b..c8fb741 100644 --- a/src/basics/NoiseGenerator.cpp +++ b/src/basics/NoiseGenerator.cpp @@ -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; diff --git a/src/basics/NoiseState.cpp b/src/basics/NoiseState.cpp index 64ee605..14019fe 100644 --- a/src/basics/NoiseState.cpp +++ b/src/basics/NoiseState.cpp @@ -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; diff --git a/src/definition/Layers.cpp b/src/definition/Layers.cpp index a509335..fe46bfe 100644 --- a/src/definition/Layers.cpp +++ b/src/definition/Layers.cpp @@ -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_); diff --git a/src/definition/Layers.h b/src/definition/Layers.h index 87ba85d..7e514d0 100644 --- a/src/definition/Layers.h +++ b/src/definition/Layers.h @@ -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 layers; DefinitionNode* null_layer; + }; } diff --git a/src/definition/Scenery.cpp b/src/definition/Scenery.cpp index 910d484..08a9236 100644 --- a/src/definition/Scenery.cpp +++ b/src/definition/Scenery.cpp @@ -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) { diff --git a/src/tests/Scenery_Test.cpp b/src/tests/Scenery_Test.cpp index 2a251e1..5dbc439 100644 --- a/src/tests/Scenery_Test.cpp +++ b/src/tests/Scenery_Test.cpp @@ -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); }