Start of multiple save support.

This commit is contained in:
Steve 2018-03-18 09:42:34 +00:00
parent c39974e2a7
commit 32949a192c
14 changed files with 45 additions and 42 deletions

View File

@ -91,6 +91,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MAX_SND_CHANNELS 64 #define MAX_SND_CHANNELS 64
#define MAX_SAVE_SLOTS 5
enum enum
{ {
ET_NONE, ET_NONE,

View File

@ -270,7 +270,7 @@ void loadGame(void)
Tuple *t; Tuple *t;
Trophy *trophy; Trophy *trophy;
sprintf(filename, "%s/game.json", app.saveDir); sprintf(filename, "%s/%d/game.json", app.saveDir, game.saveSlot);
if (fileExists(filename)) if (fileExists(filename))
{ {
@ -335,7 +335,7 @@ void saveGame(void)
Trophy *trophy; Trophy *trophy;
int i; int i;
sprintf(filename, "%s/game.json", app.saveDir); sprintf(filename, "%s/%d/game.json", app.saveDir, game.saveSlot);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving game to '%s' ...", filename); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving game to '%s' ...", filename);
@ -388,6 +388,7 @@ void saveGame(void)
if (!writeFile(filename, out)) if (!writeFile(filename, out))
{ {
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to save game"); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to save game");
exit(1);
} }
cJSON_Delete(root); cJSON_Delete(root);
@ -404,7 +405,7 @@ void restoreGameState(void)
char *text, filename[MAX_FILENAME_LENGTH]; char *text, filename[MAX_FILENAME_LENGTH];
int i; int i;
sprintf(filename, "%s/game.json", app.saveDir); sprintf(filename, "%s/%d/game.json", app.saveDir, game.saveSlot);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Restoring game from '%s' ...", filename); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Restoring game from '%s' ...", filename);

View File

@ -24,25 +24,29 @@ void createSaveFolder(void)
{ {
char *userHome; char *userHome;
char dir[MAX_FILENAME_LENGTH]; char dir[MAX_FILENAME_LENGTH];
int i;
userHome = getenv("HOME"); userHome = getenv("HOME");
if (!userHome) if (!userHome)
{ {
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Unable to determine user save folder. Will save to current dir."); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Unable to determine user save folder.");
return; exit(1);
} }
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "User home = %s", userHome); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "User home = %s", userHome);
sprintf(dir, "%s/.local/share/blobwarsAttrition", userHome); for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{
sprintf(dir, "%s/.local/share/blobwarsAttrition/%d", userHome, i);
if (mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0 && errno != EEXIST) if (mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0 && errno != EEXIST)
{ {
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Failed to create save dir '%s'. Will save to current dir.", dir); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to create save dir '%s'.", dir);
return; exit(1);
}
} }
STRNCPY(app.saveDir, dir, MAX_FILENAME_LENGTH); sprintf(app.saveDir, "%s/.local/share/blobwarsAttrition", userHome);
} }
void createScreenshotFolder(void) void createScreenshotFolder(void)

View File

@ -24,25 +24,29 @@ void createSaveFolder(void)
{ {
char *userHome; char *userHome;
char dir[MAX_FILENAME_LENGTH]; char dir[MAX_FILENAME_LENGTH];
int i;
userHome = getenv("USERPROFILE"); userHome = getenv("USERPROFILE");
if (!userHome) if (!userHome)
{ {
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Unable to determine user save folder. Will save to current dir."); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Unable to determine user save folder.");
return; exit(1);
} }
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "User home = %s", userHome); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "User home = %s", userHome);
sprintf(dir, "%s\\blobwarsAttrition", userHome); for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{
sprintf(dir, "%s\\blobwarsAttrition\\%d", userHome, i);
if (mkdir(dir) != 0 && errno != EEXIST) if (mkdir(dir) != 0 && errno != EEXIST)
{ {
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Failed to create save dir '%s'. Will save to current dir.", dir); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to create save dir '%s'.", dir);
return; exit(1);
}
} }
STRNCPY(app.saveDir, dir, MAX_FILENAME_LENGTH); sprintf(app.saveDir, "%s\\blobwarsAttrition", userHome);
} }
void createScreenshotFolder(void) void createScreenshotFolder(void)

View File

@ -369,6 +369,7 @@ typedef struct {
unsigned int stats[STAT_MAX]; unsigned int stats[STAT_MAX];
char worldId[MAX_NAME_LENGTH]; char worldId[MAX_NAME_LENGTH];
int isComplete; int isComplete;
int saveSlot;
Tuple keys[MAX_KEY_TYPES]; Tuple keys[MAX_KEY_TYPES];
Tuple missionStatusHead, *missionStatusTail; Tuple missionStatusHead, *missionStatusTail;
Trophy trophyHead, *trophyTail; Trophy trophyHead, *trophyTail;

View File

@ -266,7 +266,7 @@ void saveScreenshot(char *name)
if (name != NULL) if (name != NULL)
{ {
sprintf(filename, "%s/%s.png", app.saveDir, name); sprintf(filename, "%s/%d/%s.png", app.saveDir, game.saveSlot, name);
} }
else else
{ {

View File

@ -26,4 +26,5 @@ extern void drawText(int x, int y, int size, int align, SDL_Color c, const char
extern App app; extern App app;
extern Colors colors; extern Colors colors;
extern Dev dev; extern Dev dev;
extern Game game;

View File

@ -39,7 +39,7 @@ void init18N(int argc, char *argv[])
if (languageId >= argc) if (languageId >= argc)
{ {
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "You must specify a language to use with -language. Using default."); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "You must specify a language to use with -language. Using default.");
} }
} }
} }
@ -227,13 +227,15 @@ static void loadConfig(void)
{ {
int i; int i;
cJSON *root, *controlsJSON, *node; cJSON *root, *controlsJSON, *node;
char *text; char *text, filename[MAX_FILENAME_LENGTH];
initDefaultConfig(); initDefaultConfig();
if (fileExists(getSaveFilePath(CONFIG_FILENAME))) sprintf(filename, "%s/%s", app.saveDir, CONFIG_FILENAME);
if (fileExists(filename))
{ {
text = readFile(getSaveFilePath(CONFIG_FILENAME)); text = readFile(filename);
root = cJSON_Parse(text); root = cJSON_Parse(text);
@ -281,10 +283,10 @@ static void loadConfig(void)
void saveConfig(void) void saveConfig(void)
{ {
int i; int i;
char *out, *configFilename; char *out, filename[MAX_FILENAME_LENGTH];
cJSON *root, *controlsJSON, *keysJSON, *joypadJSON; cJSON *root, *controlsJSON, *keysJSON, *joypadJSON;
configFilename = getSaveFilePath(CONFIG_FILENAME); sprintf(filename, "%s/%s", app.saveDir, CONFIG_FILENAME);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving config ..."); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving config ...");
@ -321,9 +323,10 @@ void saveConfig(void)
out = cJSON_Print(root); out = cJSON_Print(root);
if (!writeFile(configFilename, out)) if (!writeFile(filename, out))
{ {
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to save config"); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to save config");
exit(1);
} }
cJSON_Delete(root); cJSON_Delete(root);

View File

@ -35,7 +35,6 @@ extern void destroyTextures(void);
extern void expireTexts(int all); extern void expireTexts(int all);
extern int fileExists(const char *filename); extern int fileExists(const char *filename);
extern char *getLookupName(const char *prefix, long num); extern char *getLookupName(const char *prefix, long num);
extern char *getSaveFilePath(const char *filename);
extern void initAtlas(void); extern void initAtlas(void);
extern void initBackground(void); extern void initBackground(void);
extern void initEntityFactory(void); extern void initEntityFactory(void);

View File

@ -131,18 +131,6 @@ int writeFile(const char *filename, const char *data)
return 0; return 0;
} }
char *getSaveFilePath(const char *filename)
{
static char path[MAX_FILENAME_LENGTH];
memset(path, '\0', MAX_FILENAME_LENGTH);
sprintf(path, "%s/%s", app.saveDir, filename);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "getSaveFilePath = '%s'", path);
return path;
}
char **getFileList(const char *dir, int *count) char **getFileList(const char *dir, int *count)
{ {
DIR *d; DIR *d;

View File

@ -24,5 +24,3 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "dirent.h" #include "dirent.h"
#include <zlib.h> #include <zlib.h>
extern App app;

View File

@ -38,7 +38,7 @@ void loadWorld(char *id)
world.objectiveTail = &world.objectiveHead; world.objectiveTail = &world.objectiveHead;
world.particleTail = &world.particleHead; world.particleTail = &world.particleHead;
sprintf(filename, "%s/%s.json", app.saveDir, id); sprintf(filename, "%s/%d/%s.json", app.saveDir, game.saveSlot, id);
if (!game.isComplete && fileExists(filename)) if (!game.isComplete && fileExists(filename))
{ {

View File

@ -32,7 +32,7 @@ void saveWorld(void)
cJSON *root; cJSON *root;
char filename[MAX_FILENAME_LENGTH], *out; char filename[MAX_FILENAME_LENGTH], *out;
sprintf(filename, "%s/%s.json", app.saveDir, world.id); sprintf(filename, "%s/%d/%s.json", app.saveDir, game.saveSlot, world.id);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving world to '%s' ...", filename); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving world to '%s' ...", filename);
@ -61,6 +61,7 @@ void saveWorld(void)
if (!writeFile(filename, out)) if (!writeFile(filename, out))
{ {
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to save world"); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to save world");
exit(1);
} }
cJSON_Delete(root); cJSON_Delete(root);

View File

@ -25,5 +25,6 @@ extern int writeFile(const char *filename, const char *data);
extern App app; extern App app;
extern Entity *self; extern Entity *self;
extern Game game;
extern World world; extern World world;