Skip to content

Commit

Permalink
Fix A_ChangeModel serialization
Browse files Browse the repository at this point in the history
I overlooked this part on my first A_ChangeModel fix
  • Loading branch information
RicardoLuis0 committed Mar 17, 2024
1 parent 631eb58 commit ce479e0
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 76 deletions.
15 changes: 15 additions & 0 deletions src/common/engine/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ bool FSerializer::BeginObject(const char *name)
//
//==========================================================================

bool FSerializer::HasKey(const char* name)
{
if (isReading())
{
return r->FindKey(name) != nullptr;
}
return false;
}

//==========================================================================
//
//
//
//==========================================================================

bool FSerializer::HasObject(const char* name)
{
if (isReading())
Expand Down
12 changes: 12 additions & 0 deletions src/common/engine/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class FSerializer
void ReadObjects(bool hubtravel);
bool BeginObject(const char *name);
void EndObject();
bool HasKey(const char* name);
bool HasObject(const char* name);
bool BeginArray(const char *name);
void EndArray();
Expand Down Expand Up @@ -245,6 +246,7 @@ FSerializer &Serialize(FSerializer &arc, const char *key, FSoundID &sid, FSoundI
FSerializer &Serialize(FSerializer &arc, const char *key, FString &sid, FString *def);
FSerializer &Serialize(FSerializer &arc, const char *key, NumericValue &sid, NumericValue *def);
FSerializer &Serialize(FSerializer &arc, const char *key, struct ModelOverride &mo, struct ModelOverride *def);
FSerializer &Serialize(FSerializer &arc, const char *key, struct AnimModelOverride &mo, struct AnimModelOverride *def);
FSerializer &Serialize(FSerializer &arc, const char *key, struct AnimOverride &ao, struct AnimOverride *def);
FSerializer& Serialize(FSerializer& arc, const char* key, FTranslationID& value, FTranslationID* defval);

Expand All @@ -259,6 +261,16 @@ FSerializer &Serialize(FSerializer &arc, const char *key, T *&value, T **)
return arc;
}

template<class A, class B>
FSerializer &Serialize(FSerializer &arc, const char *key, std::pair<A, B> &value, std::pair<A, B> *def)
{
arc.BeginObject(key);
Serialize(arc, "first", value.first, def ? &def->first : nullptr);
Serialize(arc, "second", value.second, def ? &def->second : nullptr);
arc.EndObject();
return arc;
}

template<class T, class TT>
FSerializer &Serialize(FSerializer &arc, const char *key, TArray<T, TT> &value, TArray<T, TT> *def)
{
Expand Down
5 changes: 2 additions & 3 deletions src/common/models/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
#include "texturemanager.h"
#include "modelrenderer.h"


TArray<FString> savedModelFiles;
TDeletingArray<FModel*> Models;
TArray<FSpriteModelFrame> SpriteModelFrames;
TMap<void*, FSpriteModelFrame> BaseSpriteModelFrames;
Expand Down Expand Up @@ -160,7 +158,7 @@ unsigned FindModel(const char * path, const char * modelfile, bool silent)

for(unsigned i = 0; i< Models.Size(); i++)
{
if (!Models[i]->mFileName.CompareNoCase(fullname)) return i;
if (Models[i]->mFileName.CompareNoCase(fullname) == 0) return i;
}

auto len = fileSystem.FileLength(lump);
Expand Down Expand Up @@ -236,6 +234,7 @@ unsigned FindModel(const char * path, const char * modelfile, bool silent)
}
// The vertex buffer cannot be initialized here because this gets called before OpenGL is initialized
model->mFileName = fullname;
model->mFilePath = {path, modelfile};
return Models.Push(model);
}

5 changes: 4 additions & 1 deletion src/common/models/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct FSpriteModelFrame;

FTextureID LoadSkin(const char* path, const char* fn);
void FlushModels();
extern TArray<FString> savedModelFiles;

extern TDeletingArray<FModel*> Models;
extern TArray<FSpriteModelFrame> SpriteModelFrames;
extern TMap<void*, FSpriteModelFrame> BaseSpriteModelFrames;
Expand Down Expand Up @@ -76,6 +76,7 @@ enum EFrameError
class FModel
{
public:

FModel();
virtual ~FModel();

Expand All @@ -100,7 +101,9 @@ class FModel
void DestroyVertexBuffer();

bool hasSurfaces = false;

FString mFileName;
std::pair<FString, FString> mFilePath;

FSpriteModelFrame *baseFrame;
private:
Expand Down
8 changes: 0 additions & 8 deletions src/g_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2148,14 +2148,6 @@ void G_DoLoadGame ()

BackupSaveName = savename;

//Push any added models from A_ChangeModel
for (auto& smf : savedModelFiles)
{
FString modelFilePath = smf.Left(smf.LastIndexOf("/")+1);
FString modelFileName = smf.Right(smf.Len() - smf.Left(smf.LastIndexOf("/") + 1).Len());
FindModel(modelFilePath.GetChars(), modelFileName.GetChars());
}

// 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.
Expand Down
3 changes: 1 addition & 2 deletions src/p_saveg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,7 @@ void FLevelLocals::Serialize(FSerializer &arc, bool hubload)
("scrolls", Scrolls)
("automap", automap)
("interpolator", interpolator)
("frozenstate", frozenstate)
("savedModelFiles", savedModelFiles);
("frozenstate", frozenstate);


// Hub transitions must keep the current total time
Expand Down
26 changes: 18 additions & 8 deletions src/playsim/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,16 @@ struct ModelOverride
TArray<FTextureID> surfaceSkinIDs;
};

struct AnimModelOverride
{
int id;

AnimModelOverride() = default;

AnimModelOverride(int i) : id(i) {}
operator int() { return id; }
};

enum EModelDataFlags
{
MODELDATA_HADMODEL = 1 << 0,
Expand All @@ -729,14 +739,14 @@ class DActorModelData : public DObject
{
DECLARE_CLASS(DActorModelData, DObject);
public:
PClass * modelDef;
TArray<ModelOverride> models;
TArray<FTextureID> skinIDs;
TArray<int> animationIDs;
TArray<int> modelFrameGenerators;
int flags;
int overrideFlagsSet;
int overrideFlagsClear;
PClass * modelDef;
TArray<ModelOverride> models;
TArray<FTextureID> skinIDs;
TArray<AnimModelOverride> animationIDs;
TArray<int> modelFrameGenerators;
int flags;
int overrideFlagsSet;
int overrideFlagsClear;

AnimOverride curAnim;
AnimOverride prevAnim; // used for interpolation when switching anims
Expand Down
51 changes: 0 additions & 51 deletions src/playsim/p_actionfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5438,57 +5438,6 @@ void ChangeModelNative(
}
}

//[SM] - We need to serialize file paths and model names so that they are pushed on loading save files. Likewise, let's not include models that were already parsed when initialized.
if (queryModel >= 0)
{
FString fullName;
fullName.Format("%s%s", modelpath.GetChars(), model.GetChars());
bool found = false;

for (auto &m : savedModelFiles)
{
if(m.CompareNoCase(fullName) == 0)
{
found = true;
break;
}
}
if(!found) for (auto &m : Models)
{
if (m->mFileName.CompareNoCase(fullName) == 0)
{
found = true;
break;
}
}
if(!found) savedModelFiles.Push(fullName);
}
//Same for animations
if (queryAnimation >= 0)
{
FString fullName;
fullName.Format("%s%s", animationpath.GetChars(), animation.GetChars());
bool found = false;

for (auto &m : savedModelFiles)
{
if(m.CompareNoCase(fullName) == 0)
{
found = true;
break;
}
}
if(!found) for (auto &m : Models)
{
if (m->mFileName.CompareNoCase(fullName) == 0)
{
found = true;
break;
}
}
if(!found) savedModelFiles.Push(fullName);
}

CleanupModelData(mobj);

return;
Expand Down
44 changes: 42 additions & 2 deletions src/playsim/p_mobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
#include "fragglescript/t_fs.h"
#include "shadowinlines.h"
#include "d_net.h"
#include "model.h"

// MACROS ------------------------------------------------------------------

Expand Down Expand Up @@ -1387,18 +1388,57 @@ bool AActor::Massacre ()
//
//----------------------------------------------------------------------------

void SerializeModelID(FSerializer &arc, const char *key, int &id)
{ // TODO: make it a proper serializable type (FModelID) instead of an int
if(arc.isWriting())
{
if(id >= 0)
{
arc(key, Models[id]->mFilePath);
}
}
else
{
if(arc.HasKey(key))
{
std::pair<FString, FString> modelFile;
arc(key, modelFile);

id = FindModel(modelFile.first.GetChars(), modelFile.second.GetChars(), true);
}
else
{
id = -1;
}
}
}

FSerializer &Serialize(FSerializer &arc, const char *key, ModelOverride &mo, ModelOverride *def)
{
arc.BeginObject(key);
arc("modelID", mo.modelID);
SerializeModelID(arc, "model", mo.modelID);
arc("surfaceSkinIDs", mo.surfaceSkinIDs);
arc.EndObject();
return arc;
}

FSerializer &Serialize(FSerializer &arc, const char *key, AnimModelOverride &amo, AnimModelOverride *def)
{
int ok = arc.BeginObject(key);
if(arc.isReading() && !ok)
{
amo.id = -1;
}
else if(ok)
{
SerializeModelID(arc, "model", amo.id);
arc.EndObject();
}
return arc;
}

FSerializer &Serialize(FSerializer &arc, const char *key, struct AnimOverride &ao, struct AnimOverride *def)
{
//TODO
arc.BeginObject(key);
arc("firstFrame", ao.firstFrame);
arc("lastFrame", ao.lastFrame);
Expand Down
2 changes: 1 addition & 1 deletion src/r_data/models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ void RenderFrameModels(FModelRenderer *renderer, FLevelLocals *Level, const FSpr
skinid = smf->skinIDs[i];
}

if (modelid >= 0)
if (modelid >= 0 && modelid < Models.size())
{
FModel * mdl = Models[modelid];
auto tex = skinid.isValid() ? TexMan.GetGameTexture(skinid, true) : nullptr;
Expand Down

0 comments on commit ce479e0

Please sign in to comment.