@ -76,12 +76,12 @@ std::string GetSavePath(uint32_t saveNum, std::string_view savePrefix = {})
) ;
}
std : : string GetStashSavePath ( std : : string_view savePrefix = { } )
{
return StrCat ( paths : : PrefPath ( ) , savePrefix ,
gbIsSpawn ? " stash_spawn " : " stash " ,
# ifdef UNPACKED_SAVES
gbIsHellfire ? " _hsv " DIRECTORY_SEPARATOR_STR : " _sv " DIRECTORY_SEPARATOR_STR
std : : string GetStashSavePath ( std : : string_view savePrefix = { } )
{
return StrCat ( paths : : PrefPath ( ) , savePrefix ,
gbIsSpawn ? " stash_spawn " : " stash " ,
# ifdef UNPACKED_SAVES
gbIsHellfire ? " _hsv " DIRECTORY_SEPARATOR_STR : " _sv " DIRECTORY_SEPARATOR_STR
# else
gbIsHellfire ? " .hsv " : " .sv "
# endif
@ -165,44 +165,44 @@ SaveWriter GetSaveWriter(uint32_t saveNum)
return SaveWriter ( GetSavePath ( saveNum ) ) ;
}
SaveWriter GetStashWriter ( )
{
return SaveWriter ( GetStashSavePath ( ) ) ;
}
# if !(defined(UNPACKED_SAVES) && defined(DVL_NO_FILESYSTEM))
void CopySaveLocation ( const std : : string & sourceLocation , const std : : string & targetLocation )
{
# if defined(UNPACKED_SAVES)
if ( ! targetLocation . empty ( ) ) {
CreateDir ( targetLocation . c_str ( ) ) ;
}
for ( const std : : filesystem : : directory_entry & entry : std : : filesystem : : directory_iterator ( sourceLocation ) ) {
const std : : filesystem : : path targetFilePath = std : : filesystem : : path ( targetLocation ) / entry . path ( ) . filename ( ) ;
CopyFileOverwrite ( entry . path ( ) . string ( ) . c_str ( ) , targetFilePath . string ( ) . c_str ( ) ) ;
}
# else
CopyFileOverwrite ( sourceLocation . c_str ( ) , targetLocation . c_str ( ) ) ;
# endif
}
void RestoreSaveLocation ( const std : : string & targetLocation , const std : : string & backupLocation )
{
# if defined(UNPACKED_SAVES)
if ( DirectoryExists ( targetLocation . c_str ( ) ) ) {
for ( const std : : filesystem : : directory_entry & entry : std : : filesystem : : directory_iterator ( targetLocation ) )
RemoveFile ( entry . path ( ) . string ( ) . c_str ( ) ) ;
}
CreateDir ( targetLocation . c_str ( ) ) ;
for ( const std : : filesystem : : directory_entry & entry : std : : filesystem : : directory_iterator ( backupLocation ) ) {
const std : : filesystem : : path restoredFilePath = std : : filesystem : : path ( targetLocation ) / entry . path ( ) . filename ( ) ;
CopyFileOverwrite ( entry . path ( ) . string ( ) . c_str ( ) , restoredFilePath . string ( ) . c_str ( ) ) ;
}
# else
CopyFileOverwrite ( backupLocation . c_str ( ) , targetLocation . c_str ( ) ) ;
# endif
}
# endif
SaveWriter GetStashWriter ( )
{
return SaveWriter ( GetStashSavePath ( ) ) ;
}
# if !(defined(UNPACKED_SAVES) && defined(DVL_NO_FILESYSTEM))
void CopySaveLocation ( const std : : string & sourceLocation , const std : : string & targetLocation )
{
# if defined(UNPACKED_SAVES)
if ( ! targetLocation . empty ( ) ) {
CreateDir ( targetLocation . c_str ( ) ) ;
}
for ( const std : : filesystem : : directory_entry & entry : std : : filesystem : : directory_iterator ( sourceLocation ) ) {
const std : : filesystem : : path targetFilePath = std : : filesystem : : path ( targetLocation ) / entry . path ( ) . filename ( ) ;
CopyFileOverwrite ( entry . path ( ) . string ( ) . c_str ( ) , targetFilePath . string ( ) . c_str ( ) ) ;
}
# else
CopyFileOverwrite ( sourceLocation . c_str ( ) , targetLocation . c_str ( ) ) ;
# endif
}
void RestoreSaveLocation ( const std : : string & targetLocation , const std : : string & backupLocation )
{
# if defined(UNPACKED_SAVES)
if ( DirectoryExists ( targetLocation . c_str ( ) ) ) {
for ( const std : : filesystem : : directory_entry & entry : std : : filesystem : : directory_iterator ( targetLocation ) )
RemoveFile ( entry . path ( ) . string ( ) . c_str ( ) ) ;
}
CreateDir ( targetLocation . c_str ( ) ) ;
for ( const std : : filesystem : : directory_entry & entry : std : : filesystem : : directory_iterator ( backupLocation ) ) {
const std : : filesystem : : path restoredFilePath = std : : filesystem : : path ( targetLocation ) / entry . path ( ) . filename ( ) ;
CopyFileOverwrite ( entry . path ( ) . string ( ) . c_str ( ) , restoredFilePath . string ( ) . c_str ( ) ) ;
}
# else
CopyFileOverwrite ( backupLocation . c_str ( ) , targetLocation . c_str ( ) ) ;
# endif
}
# endif
void Game2UiPlayer ( const Player & player , _uiheroinfo * heroinfo , bool bHasSaveFile )
{
@ -637,73 +637,73 @@ const char *pfile_get_password()
return gbIsMultiplayer ? PASSWORD_MULTI : PASSWORD_SINGLE ;
}
void pfile_write_hero ( bool writeGameData )
{
SaveWriter saveWriter = GetSaveWriter ( gSaveNumber ) ;
pfile_write_hero ( saveWriter , writeGameData ) ;
}
# if !(defined(UNPACKED_SAVES) && defined(DVL_NO_FILESYSTEM))
bool pfile_write_game_with_backup ( )
{
const std : : string backupPrefix = " backup_ " ;
const std : : string backupLocation = GetSavePath ( gSaveNumber , backupPrefix ) ;
const std : : string saveLocation = GetSavePath ( gSaveNumber ) ;
if ( FileExists ( saveLocation ) | | DirectoryExists ( saveLocation . c_str ( ) ) )
CopySaveLocation ( saveLocation , backupLocation ) ;
pfile_write_hero ( /*writeGameData=*/ true ) ;
auto archive = OpenSaveArchive ( gSaveNumber ) ;
const bool saveIsValid = archive & & ArchiveContainsGame ( * archive ) ;
if ( saveIsValid | | ! ( FileExists ( backupLocation ) | | DirectoryExists ( backupLocation . c_str ( ) ) ) )
return saveIsValid ;
RestoreSaveLocation ( saveLocation , backupLocation ) ;
return false ;
}
bool pfile_write_stash_with_backup ( )
{
if ( ! Stash . dirty )
return true ;
const std : : string backupPrefix = " backup_ " ;
const std : : string backupLocation = GetStashSavePath ( backupPrefix ) ;
const std : : string stashLocation = GetStashSavePath ( ) ;
if ( FileExists ( stashLocation ) | | DirectoryExists ( stashLocation . c_str ( ) ) )
CopySaveLocation ( stashLocation , backupLocation ) ;
SaveWriter stashWriter = GetStashWriter ( ) ;
SaveStash ( stashWriter ) ;
auto archive = OpenStashArchive ( ) ;
const char * stashFileName = gbIsMultiplayer ? " mpstashitems " : " spstashitems " ;
const bool stashIsValid = archive & & ReadArchive ( * archive , stashFileName ) ! = nullptr ;
if ( stashIsValid | | ! ( FileExists ( backupLocation ) | | DirectoryExists ( backupLocation . c_str ( ) ) ) ) {
if ( stashIsValid )
Stash . dirty = false ;
return stashIsValid ;
}
RestoreSaveLocation ( stashLocation , backupLocation ) ;
return false ;
}
# endif
# ifndef DISABLE_DEMOMODE
# if !(defined(UNPACKED_SAVES) && defined(DVL_NO_FILESYSTEM))
void pfile_write_hero_demo ( int demo )
{
const std : : string saveLocation = GetSavePath ( gSaveNumber , StrCat ( " demo_ " , demo , " _reference_ " ) ) ;
CopySaveLocation ( GetSavePath ( gSaveNumber ) , saveLocation ) ;
auto saveWriter = SaveWriter ( saveLocation . c_str ( ) ) ;
pfile_write_hero ( saveWriter , true ) ;
}
void pfile_write_hero ( bool writeGameData )
{
SaveWriter saveWriter = GetSaveWriter ( gSaveNumber ) ;
pfile_write_hero ( saveWriter , writeGameData ) ;
}
# if !(defined(UNPACKED_SAVES) && defined(DVL_NO_FILESYSTEM))
bool pfile_write_game_with_backup ( )
{
const std : : string backupPrefix = " backup_ " ;
const std : : string backupLocation = GetSavePath ( gSaveNumber , backupPrefix ) ;
const std : : string saveLocation = GetSavePath ( gSaveNumber ) ;
if ( FileExists ( saveLocation ) | | DirectoryExists ( saveLocation . c_str ( ) ) )
CopySaveLocation ( saveLocation , backupLocation ) ;
pfile_write_hero ( /*writeGameData=*/ true ) ;
auto archive = OpenSaveArchive ( gSaveNumber ) ;
const bool saveIsValid = archive & & ArchiveContainsGame ( * archive ) ;
if ( saveIsValid | | ! ( FileExists ( backupLocation ) | | DirectoryExists ( backupLocation . c_str ( ) ) ) )
return saveIsValid ;
RestoreSaveLocation ( saveLocation , backupLocation ) ;
return false ;
}
bool pfile_write_stash_with_backup ( )
{
if ( ! Stash . dirty )
return true ;
const std : : string backupPrefix = " backup_ " ;
const std : : string backupLocation = GetStashSavePath ( backupPrefix ) ;
const std : : string stashLocation = GetStashSavePath ( ) ;
if ( FileExists ( stashLocation ) | | DirectoryExists ( stashLocation . c_str ( ) ) )
CopySaveLocation ( stashLocation , backupLocation ) ;
SaveWriter stashWriter = GetStashWriter ( ) ;
SaveStash ( stashWriter ) ;
auto archive = OpenStashArchive ( ) ;
const char * stashFileName = gbIsMultiplayer ? " mpstashitems " : " spstashitems " ;
const bool stashIsValid = archive & & ReadArchive ( * archive , stashFileName ) ! = nullptr ;
if ( stashIsValid | | ! ( FileExists ( backupLocation ) | | DirectoryExists ( backupLocation . c_str ( ) ) ) ) {
if ( stashIsValid )
Stash . dirty = false ;
return stashIsValid ;
}
RestoreSaveLocation ( stashLocation , backupLocation ) ;
return false ;
}
# endif
# ifndef DISABLE_DEMOMODE
# if !(defined(UNPACKED_SAVES) && defined(DVL_NO_FILESYSTEM))
void pfile_write_hero_demo ( int demo )
{
const std : : string saveLocation = GetSavePath ( gSaveNumber , StrCat ( " demo_ " , demo , " _reference_ " ) ) ;
CopySaveLocation ( GetSavePath ( gSaveNumber ) , saveLocation ) ;
auto saveWriter = SaveWriter ( saveLocation . c_str ( ) ) ;
pfile_write_hero ( saveWriter , true ) ;
}
HeroCompareResult pfile_compare_hero_demo ( int demo , bool logDetails )
{
@ -712,33 +712,33 @@ HeroCompareResult pfile_compare_hero_demo(int demo, bool logDetails)
if ( ! FileExists ( referenceSavePath . c_str ( ) ) )
return { HeroCompareResult : : ReferenceNotFound , { } } ;
const std : : string actualSavePath = GetSavePath ( gSaveNumber , StrCat ( " demo_ " , demo , " _actual_ " ) ) ;
{
CopySaveLocation ( GetSavePath ( gSaveNumber ) , actualSavePath ) ;
SaveWriter saveWriter ( actualSavePath . c_str ( ) ) ;
pfile_write_hero ( saveWriter , true ) ;
}
return CompareSaves ( actualSavePath , referenceSavePath , logDetails ) ;
}
# else
// Demo save comparison is unavailable on UNPACKED_SAVES targets without filesystem support.
void pfile_write_hero_demo ( int demo )
{
( void ) demo ;
}
HeroCompareResult pfile_compare_hero_demo ( int demo , bool logDetails )
{
( void ) demo ;
( void ) logDetails ;
return { HeroCompareResult : : ReferenceNotFound , { } } ;
}
# endif
# endif
void sfile_write_stash ( )
{
const std : : string actualSavePath = GetSavePath ( gSaveNumber , StrCat ( " demo_ " , demo , " _actual_ " ) ) ;
{
CopySaveLocation ( GetSavePath ( gSaveNumber ) , actualSavePath ) ;
SaveWriter saveWriter ( actualSavePath . c_str ( ) ) ;
pfile_write_hero ( saveWriter , true ) ;
}
return CompareSaves ( actualSavePath , referenceSavePath , logDetails ) ;
}
# else
// Demo save comparison is unavailable on UNPACKED_SAVES targets without filesystem support.
void pfile_write_hero_demo ( int demo )
{
( void ) demo ;
}
HeroCompareResult pfile_compare_hero_demo ( int demo , bool logDetails )
{
( void ) demo ;
( void ) logDetails ;
return { HeroCompareResult : : ReferenceNotFound , { } } ;
}
# endif
# endif
void sfile_write_stash ( )
{
if ( ! Stash . dirty )
return ;