From f70cc705cb3a4172ed53740dc8855e13f53b9545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Thu, 10 Nov 2022 12:02:27 +0100 Subject: [PATCH] Remove GameSave::Collapse and GameSave::originalData ... and everything built around them. A GameSave would hold at least one but sometimes two representations of a save: one serialized, and one "friendly", accessible for modification. Thus, a GameSave would have three states: - "Collapsed": only the serialized representation was present; this was the initial state of GameSaves loaded from files; - "Expanded With Data": both the serialized and the friendly representations were present; this was the state of GameSaves loaded from files after a call to Expand; - "Expanded Without Data": only the friendly representation was present; this was the initial state of GameSaves being prepared for being saved to files. A GameSave would be able to go from Collapsed to Expanded With Data with a call to Expand, and back with a call to Collapse. Of course, this latter transition would discard any changes made to the friendly representation, for example with Translate. A GameSave would however be unable to go from Expanded Without Data to any other state; a call to Collapse in this state would have been a no-op. There were two instances of Collapse being called, one in the GameSave constructor taking the serialized representation, immediately after a call to Expand, and another in SaveRenderer, which would Collapse a save "back down" if it had originally been Collapsed. Now, consider that there reasons for constructing a GameSave from the serialized representation are as follows: - loading an online save at startup from the command line; - loading a local save at startup from the command line; - loading a local save when it is dropped into the window; - loading a local save for placement of the most recently used stamp; - loading a local save for stamp placement via Lua; - loading an online save for preview generation while browsing; - loading a local save in the stamp browser for thumbnail generation; - loading a local save in the local save browser for thumbnail generation. In some cases, the friendly representation is needed for thumbnail generation by ThumbnailRendererTask. ThumbnailRendererTask operates on its own copy of the GameSave, because it runs SaveRenderer on a thread different from the main one and cannot be sure of the lifetime of the original GameSave. It destroys this copy when it is done rendering, so the call SaveRenderer makes to Collapse is pointless. In all other cases, the friendly representation is needed immediately. In some of these, SaveRenderer is used from the main thread, but since the friendly representation of the GameSave will be needed for pasting anyway, the call SaveRenderer makes to Collapse is pointless again. So, Collapse goes away. This also means that it is pointless for GameSaves to hold on to the serialized representation, since in all cases in which they have access to it, the friendly representation is needed immediately, and with Collapse gone, they will never need it again. --- src/client/GameSave.cpp | 82 ++++++++------------------------- src/client/GameSave.h | 16 ++----- src/gui/game/GameController.cpp | 4 -- src/simulation/SaveRenderer.cpp | 3 -- src/simulation/Simulation.cpp | 11 ----- 5 files changed, 24 insertions(+), 92 deletions(-) diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index 2b67134da..ba5eddfe7 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -34,34 +34,23 @@ GameSave::GameSave(const GameSave & save): signs(save.signs), stkm(save.stkm), palette(save.palette), - pmapbits(save.pmapbits), - expanded(save.expanded), - hasOriginalData(save.hasOriginalData), - originalData(save.originalData) + pmapbits(save.pmapbits) { InitData(); hasPressure = save.hasPressure; hasAmbientHeat = save.hasAmbientHeat; - if (save.expanded) - { - setSize(save.blockWidth, save.blockHeight); + setSize(save.blockWidth, save.blockHeight); - std::copy(save.particles, save.particles+NPART, particles); - for (int j = 0; j < blockHeight; j++) - { - std::copy(save.blockMap[j], save.blockMap[j]+blockWidth, blockMap[j]); - std::copy(save.fanVelX[j], save.fanVelX[j]+blockWidth, fanVelX[j]); - std::copy(save.fanVelY[j], save.fanVelY[j]+blockWidth, fanVelY[j]); - std::copy(save.pressure[j], save.pressure[j]+blockWidth, pressure[j]); - std::copy(save.velocityX[j], save.velocityX[j]+blockWidth, velocityX[j]); - std::copy(save.velocityY[j], save.velocityY[j]+blockWidth, velocityY[j]); - std::copy(save.ambientHeat[j], save.ambientHeat[j]+blockWidth, ambientHeat[j]); - } - } - else + std::copy(save.particles, save.particles+NPART, particles); + for (int j = 0; j < blockHeight; j++) { - blockWidth = save.blockWidth; - blockHeight = save.blockHeight; + std::copy(save.blockMap[j], save.blockMap[j]+blockWidth, blockMap[j]); + std::copy(save.fanVelX[j], save.fanVelX[j]+blockWidth, fanVelX[j]); + std::copy(save.fanVelY[j], save.fanVelY[j]+blockWidth, fanVelY[j]); + std::copy(save.pressure[j], save.pressure[j]+blockWidth, pressure[j]); + std::copy(save.velocityX[j], save.velocityX[j]+blockWidth, velocityX[j]); + std::copy(save.velocityY[j], save.velocityY[j]+blockWidth, velocityY[j]); + std::copy(save.ambientHeat[j], save.ambientHeat[j]+blockWidth, ambientHeat[j]); } particlesCount = save.particlesCount; authors = save.authors; @@ -71,24 +60,19 @@ GameSave::GameSave(int width, int height) { InitData(); InitVars(); - hasOriginalData = false; - expanded = true; setSize(width, height); } -GameSave::GameSave(std::vector data) +GameSave::GameSave(const std::vector &data) { blockWidth = 0; blockHeight = 0; InitData(); InitVars(); - expanded = false; - hasOriginalData = true; - originalData = data; try { - Expand(); + Expand(data); } catch(ParseException & e) { @@ -96,7 +80,6 @@ GameSave::GameSave(std::vector data) dealloc(); //Free any allocated memory throw; } - Collapse(); } // Called on every new GameSave, including the copy constructor @@ -136,32 +119,13 @@ void GameSave::InitVars() pmapbits = 8; // default to 8 bits for older saves } -bool GameSave::Collapsed() +void GameSave::Expand(const std::vector &data) { - return !expanded; + InitVars(); + read(&data[0], data.size()); } -void GameSave::Expand() -{ - if(hasOriginalData && !expanded) - { - InitVars(); - expanded = true; - read(&originalData[0], originalData.size()); - } -} - -void GameSave::Collapse() -{ - if(expanded && hasOriginalData) - { - expanded = false; - dealloc(); - signs.clear(); - } -} - -void GameSave::read(char * data, int dataSize) +void GameSave::read(const char * data, int dataSize) { if(dataSize > 15) { @@ -242,8 +206,6 @@ char * GameSave::Serialise(unsigned int & dataSize) vector2d GameSave::Translate(vector2d translate) { - if (Collapsed()) - Expand(); float nx, ny; vector2d pos; vector2d translateReal = translate; @@ -313,9 +275,6 @@ vector2d GameSave::Translate(vector2d translate) void GameSave::Transform(matrix2d transform, vector2d translate) { - if (Collapsed()) - Expand(); - int width = blockWidth*CELL, height = blockHeight*CELL, newWidth, newHeight; vector2d tmp, ctl, cbr; vector2d cornerso[4]; @@ -347,9 +306,6 @@ void GameSave::Transform(matrix2d transform, vector2d translate) // translateReal is the original amount we tried to translate, used to calculate wall shifting void GameSave::Transform(matrix2d transform, vector2d translate, vector2d translateReal, int newWidth, int newHeight) { - if (Collapsed()) - Expand(); - if (newWidth>XRES) newWidth = XRES; if (newHeight>YRES) newHeight = YRES; @@ -558,7 +514,7 @@ void GameSave::CheckBsonFieldFloat(bson_iterator iter, const char *field, float } } -void GameSave::readOPS(char * data, int dataLength) +void GameSave::readOPS(const char * data, int dataLength) { unsigned char *inputData = (unsigned char*)data, *bsonData = NULL, *partsData = NULL, *partsPosData = NULL, *fanData = NULL, *wallData = NULL, *soapLinkData = NULL; unsigned char *pressData = NULL, *vxData = NULL, *vyData = NULL, *ambientData = NULL; @@ -1390,7 +1346,7 @@ void GameSave::readOPS(char * data, int dataLength) } } -void GameSave::readPSv(char * saveDataChar, int dataLength) +void GameSave::readPSv(const char * saveDataChar, int dataLength) { unsigned char * saveData = (unsigned char *)saveDataChar; int q,j,k,x,y,p=0, ver, pty, ty, legacy_beta=0; diff --git a/src/client/GameSave.h b/src/client/GameSave.h index e69e4adf2..4b81c0027 100644 --- a/src/client/GameSave.h +++ b/src/client/GameSave.h @@ -117,7 +117,7 @@ public: GameSave(); GameSave(const GameSave & save); GameSave(int width, int height); - GameSave(std::vector data); + GameSave(const std::vector &data); ~GameSave(); void setSize(int width, int height); char * Serialise(unsigned int & dataSize); @@ -126,9 +126,7 @@ public: void Transform(matrix2d transform, vector2d translate); void Transform(matrix2d transform, vector2d translate, vector2d translateReal, int newWidth, int newHeight); - void Expand(); - void Collapse(); - bool Collapsed(); + void Expand(const std::vector &data); static bool TypeInCtype(int type, int ctype); static bool TypeInTmp(int type); @@ -139,13 +137,9 @@ public: GameSave& operator << (sign &v); private: - bool expanded; - bool hasOriginalData; // number of pixels translated. When translating CELL pixels, shift all CELL grids vector2d translated; - std::vector originalData; - void InitData(); void InitVars(); void CheckBsonFieldUser(bson_iterator iter, const char *field, unsigned char **data, unsigned int *fieldLen); @@ -155,9 +149,9 @@ private: template T ** Allocate2DArray(int blockWidth, int blockHeight, T defaultVal); template void Deallocate2DArray(T ***array, int blockHeight); void dealloc(); - void read(char * data, int dataSize); - void readOPS(char * data, int dataLength); - void readPSv(char * data, int dataLength); + void read(const char * data, int dataSize); + void readOPS(const char * data, int dataLength); + void readPSv(const char * data, int dataLength); char * serialiseOPS(unsigned int & dataSize); void ConvertJsonToBson(bson *b, Json::Value j, int depth = 0); void ConvertBsonToJson(bson_iterator *b, Json::Value *j, int depth = 0); diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index 9a10ceab2..51ac12ac3 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -432,16 +432,12 @@ bool GameController::LoadClipboard() if (!clip) return false; gameModel->SetPlaceSave(clip); - if (gameModel->GetPlaceSave() && gameModel->GetPlaceSave()->Collapsed()) - gameModel->GetPlaceSave()->Expand(); return true; } void GameController::LoadStamp(GameSave *stamp) { gameModel->SetPlaceSave(stamp); - if(gameModel->GetPlaceSave() && gameModel->GetPlaceSave()->Collapsed()) - gameModel->GetPlaceSave()->Expand(); } void GameController::TranslateSave(ui::Point point) diff --git a/src/simulation/SaveRenderer.cpp b/src/simulation/SaveRenderer.cpp index 8fe52153f..48db5cde7 100644 --- a/src/simulation/SaveRenderer.cpp +++ b/src/simulation/SaveRenderer.cpp @@ -37,7 +37,6 @@ VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire, VideoBuffer * tempThumb = NULL; width = save->blockWidth; height = save->blockHeight; - bool doCollapse = save->Collapsed(); g->Clear(); sim->clear_sim(); @@ -79,8 +78,6 @@ VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire, tempThumb = new VideoBuffer(pData, width*CELL, height*CELL); free(pData); } - if(doCollapse) - save->Collapse(); return tempThumb; } diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 690924106..0c22c4d55 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -53,17 +53,6 @@ int Simulation::Load(const GameSave * originalSave, bool includePressure, int fu if (!originalSave) return 1; auto save = std::unique_ptr(new GameSave(*originalSave)); - try - { - save->Expand(); - } - catch (const ParseException &e) - { -#ifdef LUACONSOLE - luacon_ci->SetLastError(ByteString(e.what()).FromUtf8()); -#endif - return 1; - } //Align to blockMap int blockX = (fullX + CELL/2)/CELL;