From 6d00e4a3f36288297d13376000142faf83371b90 Mon Sep 17 00:00:00 2001 From: Shiny Metagross <30511800+ShinyMetagross@users.noreply.github.com> Date: Thu, 30 Jun 2022 22:23:55 -0700 Subject: [PATCH] Finished serializing - Implemented an FString TArray which goes into save files that saves a model file and path, and when the game is loaded, spits back out the model to be Loaded when loading a save file --- src/common/models/model.cpp | 1 + src/common/models/model.h | 1 + src/g_game.cpp | 9 +++++++++ src/p_saveg.cpp | 4 +++- src/playsim/p_actionfunctions.cpp | 18 ++++++++++++++++-- 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/common/models/model.cpp b/src/common/models/model.cpp index e8826d5b8e..1829ccebbd 100644 --- a/src/common/models/model.cpp +++ b/src/common/models/model.cpp @@ -42,6 +42,7 @@ #include "modelrenderer.h" +TArray savedModelFiles; TDeletingArray Models; TArray SpriteModelFrames; diff --git a/src/common/models/model.h b/src/common/models/model.h index 7e005a2d88..df4b0e6a62 100644 --- a/src/common/models/model.h +++ b/src/common/models/model.h @@ -12,6 +12,7 @@ struct FSpriteModelFrame; FTextureID LoadSkin(const char* path, const char* fn); void FlushModels(); +extern TArray savedModelFiles; extern TDeletingArray Models; extern TArray SpriteModelFrames; diff --git a/src/g_game.cpp b/src/g_game.cpp index a48e66df24..1e61d0f6da 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -78,6 +78,7 @@ #include "s_music.h" #include "p_setup.h" #include "d_event.h" +#include "model.h" #include "v_video.h" #include "g_hub.h" @@ -2119,6 +2120,14 @@ void G_DoLoadGame () BackupSaveName = savename; + //Push any added models from A_ChangeModel + for (int i = 0; i < savedModelFiles.Size(); i++) + { + FString modelFilePath = savedModelFiles[i].Left(savedModelFiles[i].LastIndexOf("/")+1); + FString modelFileName = savedModelFiles[i].Right(savedModelFiles[i].Len() - savedModelFiles[i].Left(savedModelFiles[i].LastIndexOf("/") + 1).Len()); + FindModel(modelFilePath, modelFileName); + } + // At this point, the GC threshold is likely a lot higher than the // amount of memory in use, so bring it down now by starting a // collection. diff --git a/src/p_saveg.cpp b/src/p_saveg.cpp index 49d479b5a2..25b244ffd3 100644 --- a/src/p_saveg.cpp +++ b/src/p_saveg.cpp @@ -61,6 +61,7 @@ #include "version.h" #include "fragglescript/t_script.h" #include "s_music.h" +#include "model.h" EXTERN_CVAR(Bool, save_formatted) @@ -996,7 +997,8 @@ void FLevelLocals::Serialize(FSerializer &arc, bool hubload) ("scrolls", Scrolls) ("automap", automap) ("interpolator", interpolator) - ("frozenstate", frozenstate); + ("frozenstate", frozenstate) + ("savedModelFiles", savedModelFiles); // Hub transitions must keep the current total time diff --git a/src/playsim/p_actionfunctions.cpp b/src/playsim/p_actionfunctions.cpp index ecd761fc43..626dce04bd 100644 --- a/src/playsim/p_actionfunctions.cpp +++ b/src/playsim/p_actionfunctions.cpp @@ -5067,13 +5067,27 @@ DEFINE_ACTION_FUNCTION(AActor, A_ChangeModel) int maxModels = mobj->modelData->modelIDs.Size(); int maxSkins = mobj->modelData->skinIDs.Size(); + int queryModel = model != NAME_None ? FindModel(modelpath.GetChars(), model.GetChars()) : -1; + mobj->modelData->modelDef = modeldef; - if (maxModels > index) mobj->modelData->modelIDs.Pop(mobj->modelData->modelIDs[index]); + if(maxModels > index) mobj->modelData->modelIDs.Pop(mobj->modelData->modelIDs[index]); if(maxSkins > index) mobj->modelData->skinIDs.Pop(mobj->modelData->skinIDs[index]); - mobj->modelData->modelIDs.Insert(index, model != NAME_None ? FindModel(modelpath.GetChars(), model.GetChars()) : -1); + mobj->modelData->modelIDs.Insert(index, queryModel); mobj->modelData->skinIDs.Insert(index, skin != NAME_None ? LoadSkin(skinpath.GetChars(), skin.GetChars()) : FNullTextureID()); + //[SM] - We need to serialize file paths and model names so that they are pushed on loading save files. + if (model != NAME_None) + { + FString fullName; + fullName.Format("%s%s", modelpath, model.GetChars()); + bool allowPush = true; + for (unsigned i = 0; i < savedModelFiles.Size(); i++) + if (!savedModelFiles[i].CompareNoCase(fullName)) allowPush = false; + + if(allowPush) savedModelFiles.Push(fullName); + } + //[SM] - if an indice of modelIDs or skinIDs comes up blank and it's the last one, just delete it. For using very large amounts of indices, common sense says to just not run this repeatedly. int i = 0;