Skip to content

Commit

Permalink
SavegameManager::GetLastSavegame
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoLuis0 committed Nov 25, 2023
1 parent a3e0ef4 commit 2a2d8b3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/common/menu/savegamemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,23 @@ DEFINE_ACTION_FUNCTION(FSavegameManager, GetSavegame)
//
//=============================================================================

int ZS_GetLastSavegame(FSavegameManagerBase * self)
{
return self->GetLastSavegame();
}

DEFINE_ACTION_FUNCTION_NATIVE(FSavegameManager, GetLastSavegame, ZS_GetLastSavegame)
{
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManagerBase);
ACTION_RETURN_INT(self->GetLastSavegame());
}

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

void FSavegameManagerBase::InsertNewSaveNode()
{
NewSaveNode.SaveTitle = GStrings("NEWSAVE");
Expand Down
1 change: 1 addition & 0 deletions src/common/menu/savegamemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct FSavegameManagerBase
void ClearSaveGames();

virtual void ReadSaveStrings() = 0;
virtual int GetLastSavegame() = 0;
void UnloadSaveData();

int RemoveSaveSlot(int index);
Expand Down
4 changes: 4 additions & 0 deletions src/menu/doommenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ void OnMenuOpen(bool makeSound);

class FSavegameManager : public FSavegameManagerBase
{
time_t lastSaveTime = 0;
FSaveGameNode * lastSave = nullptr;

void PerformSaveGame(const char *fn, const char *sgdesc) override;
void PerformLoadGame(const char *fn, bool) override;
FString ExtractSaveComment(FSerializer &arc) override;
FString BuildSaveName(const char* prefix, int slot) override;
void ReadSaveStrings() override;
int GetLastSavegame() override;
};

extern FSavegameManager savegameManager;
Expand Down
31 changes: 31 additions & 0 deletions src/menu/loadsavemenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
**
*/

#include <sstream>
#include <iomanip>

#include "doommenu.h"
#include "version.h"
#include "g_game.h"
Expand All @@ -58,6 +61,16 @@
//
//=============================================================================

int FSavegameManager::GetLastSavegame()
{
ReadSaveStrings();
for(int i = 0; i < SaveGames.Size(); i++)
{
if(SaveGames[i] == lastSave) return i;
}
return -1;
}

void FSavegameManager::ReadSaveStrings()
{
if (SaveGames.Size() == 0)
Expand Down Expand Up @@ -91,7 +104,19 @@ void FSavegameManager::ReadSaveStrings()
FString engine = arc.GetString("Engine");
FString iwad = arc.GetString("Game WAD");
FString title = arc.GetString("Title");
FString time_str = arc.GetString("Creation Time");
time_t time = 0;

if(time_str.CompareNoCase("Unknown\n") != 0)
{
tm t = {};
//strptime(time_str.GetChars(), "%F %T", &time); //microsoft WHY

std::istringstream is(time_str.GetChars());
is >> std::get_time(&t, "%Y-%m-%d %H:%M:%S");

time = mktime(&t);
}

if (engine.Compare(GAMESIG) != 0 || savever > SAVEVER)
{
Expand Down Expand Up @@ -120,6 +145,12 @@ void FSavegameManager::ReadSaveStrings()
node->bOldVersion = oldVer;
node->bMissingWads = missing;
node->SaveTitle = title;

if(time > lastSaveTime && !missing)
{
lastSave = node;
}

InsertSaveNode(node);
}
}
Expand Down
1 change: 1 addition & 0 deletions wadsrc/static/zscript/engine/ui/menu/loadsavemenu.zs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct SavegameManager native ui
native void SetFileInfo(int Selected);
native int SavegameCount();
native SaveGameNode GetSavegame(int i);
native int GetLastSavegame();
native void InsertNewSaveNode();
native bool RemoveNewSaveNode();

Expand Down

0 comments on commit 2a2d8b3

Please sign in to comment.