Skip to content

Commit 4df04ce

Browse files
committed
feature(load): sort savegames by timestamp
1 parent c670dd1 commit 4df04ce

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

src/fheroes2/dialog/dialog_selectfile.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,14 @@ namespace
244244
}
245245
}
246246

247-
std::sort( mapInfos.begin(), mapInfos.end(), Maps::FileInfo::sortByFileName );
247+
const SaveFileSortType sortType = Settings::Get().GetSaveFileSortType();
248+
if ( sortType == SaveFileSortType::FILENAME ) {
249+
std::sort( mapInfos.begin(), mapInfos.end(), Maps::FileInfo::sortByFileName );
250+
}
251+
else {
252+
assert( sortType == SaveFileSortType::LATEST );
253+
std::sort( mapInfos.begin(), mapInfos.end(), Maps::FileInfo::sortByTimestampDescending );
254+
}
248255

249256
return mapInfos;
250257
}

src/fheroes2/maps/maps_fileinfo.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,12 @@ bool Maps::FileInfo::sortByMapName( const FileInfo & lhs, const FileInfo & rhs )
563563
return CaseInsensitiveCompare( lhs.name, rhs.name );
564564
}
565565

566+
bool Maps::FileInfo::sortByTimestampDescending( const FileInfo & lhs, const FileInfo & rhs )
567+
{
568+
// we want the latest timestamp first
569+
return lhs.timestamp > rhs.timestamp;
570+
}
571+
566572
int Maps::FileInfo::KingdomRace( int color ) const
567573
{
568574
switch ( color ) {

src/fheroes2/maps/maps_fileinfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ namespace Maps
145145
static bool sortByFileName( const FileInfo & lhs, const FileInfo & rhs );
146146

147147
static bool sortByMapName( const FileInfo & lhs, const FileInfo & rhs );
148+
static bool sortByTimestampDescending( const FileInfo & lhs, const FileInfo & rhs );
148149

149150
// Only Resurrection Maps contain supported language.
150151
std::optional<fheroes2::SupportedLanguage> getSupportedLanguage() const

src/fheroes2/system/settings.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ std::string Settings::GetVersion()
101101
Settings::Settings()
102102
: _resolutionInfo( fheroes2::Display::DEFAULT_WIDTH, fheroes2::Display::DEFAULT_HEIGHT )
103103
, _gameDifficulty( Difficulty::NORMAL )
104+
, _saveFileSortType( SaveFileSortType::FILENAME )
104105
, sound_volume( 6 )
105106
, music_volume( 6 )
106107
, _musicType( MUSIC_EXTERNAL )
@@ -355,6 +356,15 @@ bool Settings::Read( const std::string & filePath )
355356
setEditorPassability( config.StrParams( "editor passability" ) == "on" );
356357
}
357358

359+
if ( config.Exists( "save load order" ) ) {
360+
if ( config.StrParams( "save load order" ) == "date" ) {
361+
_saveFileSortType = SaveFileSortType::LATEST;
362+
}
363+
else {
364+
_saveFileSortType = SaveFileSortType::FILENAME;
365+
}
366+
}
367+
358368
return true;
359369
}
360370

@@ -519,6 +529,9 @@ std::string Settings::String() const
519529
os << std::endl << "# display object passability in the Editor: on/off" << std::endl;
520530
os << "editor passability = " << ( _editorOptions.Modes( EDITOR_PASSABILITY ) ? "on" : "off" ) << std::endl;
521531

532+
os << std::endl << "# sort game saves when loading: name/date" << std::endl;
533+
os << "save load order = " << ( _saveFileSortType == SaveFileSortType::LATEST ? "date" : "name" ) << std::endl;
534+
522535
return os.str();
523536
}
524537

@@ -589,6 +602,11 @@ void Settings::SetProgramPath( const char * path )
589602
_programPath = path;
590603
}
591604

605+
SaveFileSortType Settings::GetSaveFileSortType() const
606+
{
607+
return _saveFileSortType;
608+
}
609+
592610
const std::vector<std::string> & Settings::GetRootDirs()
593611
{
594612
static const std::vector<std::string> rootDirs = []() {

src/fheroes2/system/settings.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ enum class InterfaceType : uint8_t
7171
DYNAMIC = 2
7272
};
7373

74+
enum class SaveFileSortType : uint8_t
75+
{
76+
FILENAME = 0,
77+
LATEST = 1,
78+
};
79+
7480
class Settings
7581
{
7682
public:
@@ -354,6 +360,8 @@ class Settings
354360

355361
void SetProgramPath( const char * path );
356362

363+
SaveFileSortType GetSaveFileSortType() const;
364+
357365
static std::string GetVersion();
358366

359367
static const std::vector<std::string> & GetRootDirs();
@@ -387,6 +395,8 @@ class Settings
387395

388396
Maps::FileInfo _currentMapInfo;
389397

398+
SaveFileSortType _saveFileSortType;
399+
390400
int sound_volume;
391401
int music_volume;
392402
MusicSource _musicType;

0 commit comments

Comments
 (0)