Skip to content

Commit

Permalink
[TD] Updates Save/Load code to correctly use user data folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
OmniBlade committed Oct 17, 2022
1 parent c582edd commit 837c554
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
2 changes: 1 addition & 1 deletion common/cdfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ int CDFileClass::Open(char const* filename, int rights)
std::string write_path = Paths.User_Path();
write_path += PathsClass::SEP;
write_path += filename;
BufferIOFileClass::Set_Name(filename);
BufferIOFileClass::Set_Name(write_path.c_str());
return (BufferIOFileClass::Open(rights));
}

Expand Down
80 changes: 57 additions & 23 deletions tiberiandawn/loaddlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "loaddlg.h"
#include "common/file.h"
#include "common/framelimit.h"
#include "common/paths.h"

/***********************************************************************************************
* LoadOptionsClass::LoadOptionsClass -- class constructor *
Expand Down Expand Up @@ -481,6 +482,22 @@ int LoadOptionsClass::Process(void)
*/
if (game_idx != 0) {
strcpy(game_descr, listbtn.Get_Item(game_idx));

/*
** Strip any leading parenthesis off of the description.
*/
if (game_descr[0] == '(') {
char* ptr = strchr(game_descr, ')');
if (ptr != NULL) {
char* dst = game_descr;
const char* src = ptr + 1; // Skip ')'

// Don't use strcpy, as the memory regions overlap.
while ((*dst++ = *src++) != '\0')
; // Copy string
strtrim(game_descr);
}
}
} else {
game_descr[0] = 0;
}
Expand Down Expand Up @@ -568,10 +585,13 @@ void LoadOptionsClass::Fill_List(ListClass* list)
{
FileEntryClass* fdata; // for adding entries to 'Files'
char descr[DESCRIP_MAX];
char scan_path[_MAX_PATH];
char sep[2] = {0};
unsigned scenario; // scenario #
HousesType house; // house
Find_File_Data* ff = nullptr;
int id;
bool found;

/*
** Make sure the list is empty
Expand All @@ -591,38 +611,52 @@ void LoadOptionsClass::Fill_List(ListClass* list)
/*
** Find all savegame files
*/
bool rc = Find_First("savegame.*", 0, &ff);
sep[0] = PathsClass::SEP;
snprintf(scan_path, sizeof(scan_path), "%s%s%s", Paths.User_Path(), sep, "SAVEGAME.*");

while (rc) {
/*
** Extract the game ID from the filename
*/
id = Num_From_Ext(ff->GetName());
found = Find_First(scan_path, 0, &ff);
while (found) {
if (stricmp(ff->GetName(), NET_SAVE_FILE_NAME) != 0) {
/*
** Extract the game ID from the filename
*/
id = Num_From_Ext(ff->GetName());

/*
** get the game's info; if success, add it to the list
*/
bool ok = Get_Savefile_Info(id, descr, &scenario, &house);
/*
** get the game's info; if success, add it to the list
*/
bool ok = Get_Savefile_Info(id, descr, &scenario, &house);

fdata = new FileEntryClass;
fdata = new FileEntryClass;

fdata->Descr[0] = '\0';
if (!ok)
strcpy(fdata->Descr, Text_String(TXT_OLD_GAME));
strncat(fdata->Descr, descr, (sizeof(fdata->Descr) - strlen(fdata->Descr)) - 1);
fdata->Valid = ok;
fdata->Scenario = scenario;
fdata->House = house;
fdata->Num = id;
fdata->DateTime = ff->GetTime();
Files.Add(fdata);
fdata->Descr[0] = '\0';
if (!ok) {
strcpy(fdata->Descr, Text_String(TXT_OLD_GAME));
} else {
if (house == HOUSE_BAD) {
sprintf(fdata->Descr, "(%s) ", Text_String(TXT_N_O_D));
} else {
sprintf(fdata->Descr, "(%s) ", Text_String(TXT_G_D_I));
}
}
strncat(fdata->Descr, descr, (sizeof(fdata->Descr) - strlen(fdata->Descr)) - 1);
fdata->Valid = ok;
fdata->Scenario = scenario;
fdata->House = house;
fdata->Num = id;
fdata->DateTime = ff->GetTime();
Files.Add(fdata);
}

/*
** Find the next file
*/
rc = Find_Next(ff);
found = Find_Next(ff);
}

if (ff) {
Find_Close(ff);
}
Find_Close(ff);

/*
** If saving a game, determine a unique file ID for the empty slot
Expand Down
6 changes: 3 additions & 3 deletions tiberiandawn/saveload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ bool Save_Game(int id, char* descr)
*/
bool Save_Game(const char* file_name, const char* descr)
{
RawFileClass file;
CDFileClass file;
int i;
unsigned int version;
unsigned scenario;
Expand Down Expand Up @@ -323,7 +323,7 @@ bool Load_Game(int id)
*/
bool Load_Game(const char* file_name)
{
RawFileClass file;
CDFileClass file;
int i;
unsigned int version;
unsigned scenario;
Expand Down Expand Up @@ -986,7 +986,7 @@ void Decode_All_Pointers(void)
*=========================================================================*/
bool Get_Savefile_Info(int id, char* buf, unsigned* scenp, HousesType* housep)
{
RawFileClass file;
CDFileClass file;
char name[_MAX_FNAME + _MAX_EXT];
unsigned int version;
char descr_buf[DESCRIP_MAX];
Expand Down

0 comments on commit 837c554

Please sign in to comment.