diff --git a/src/defs.h b/src/defs.h index da2fa2d..68cd0ef 100644 --- a/src/defs.h +++ b/src/defs.h @@ -91,6 +91,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define MAX_SND_CHANNELS 64 +#define MAX_SAVE_SLOTS 5 + enum { ET_NONE, diff --git a/src/game/game.c b/src/game/game.c index 0df4b9c..e90c63e 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -270,7 +270,7 @@ void loadGame(void) Tuple *t; Trophy *trophy; - sprintf(filename, "%s/game.json", app.saveDir); + sprintf(filename, "%s/%d/game.json", app.saveDir, game.saveSlot); if (fileExists(filename)) { @@ -335,7 +335,7 @@ void saveGame(void) Trophy *trophy; 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); @@ -388,6 +388,7 @@ void saveGame(void) if (!writeFile(filename, out)) { SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to save game"); + exit(1); } cJSON_Delete(root); @@ -404,7 +405,7 @@ void restoreGameState(void) char *text, filename[MAX_FILENAME_LENGTH]; 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); diff --git a/src/plat/unix/unixInit.c b/src/plat/unix/unixInit.c index a3ddb10..2a0c5ce 100644 --- a/src/plat/unix/unixInit.c +++ b/src/plat/unix/unixInit.c @@ -24,25 +24,29 @@ void createSaveFolder(void) { char *userHome; char dir[MAX_FILENAME_LENGTH]; + int i; userHome = getenv("HOME"); if (!userHome) { - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Unable to determine user save folder. Will save to current dir."); - return; + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Unable to determine user save folder."); + exit(1); } SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "User home = %s", userHome); - sprintf(dir, "%s/.local/share/blobwarsAttrition", userHome); - if (mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0 && errno != EEXIST) + for (i = 0 ; i < MAX_SAVE_SLOTS ; i++) { - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Failed to create save dir '%s'. Will save to current dir.", dir); - return; + sprintf(dir, "%s/.local/share/blobwarsAttrition/%d", userHome, i); + if (mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0 && errno != EEXIST) + { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to create save dir '%s'.", dir); + exit(1); + } } - STRNCPY(app.saveDir, dir, MAX_FILENAME_LENGTH); + sprintf(app.saveDir, "%s/.local/share/blobwarsAttrition", userHome); } void createScreenshotFolder(void) diff --git a/src/plat/win32/win32Init.c b/src/plat/win32/win32Init.c index 3ded1ab..3729a13 100644 --- a/src/plat/win32/win32Init.c +++ b/src/plat/win32/win32Init.c @@ -24,25 +24,29 @@ void createSaveFolder(void) { char *userHome; char dir[MAX_FILENAME_LENGTH]; + int i; userHome = getenv("USERPROFILE"); if (!userHome) { - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Unable to determine user save folder. Will save to current dir."); - return; + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Unable to determine user save folder."); + exit(1); } SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "User home = %s", userHome); - sprintf(dir, "%s\\blobwarsAttrition", userHome); - if (mkdir(dir) != 0 && errno != EEXIST) + for (i = 0 ; i < MAX_SAVE_SLOTS ; i++) { - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Failed to create save dir '%s'. Will save to current dir.", dir); - return; + sprintf(dir, "%s\\blobwarsAttrition\\%d", userHome, i); + if (mkdir(dir) != 0 && errno != EEXIST) + { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to create save dir '%s'.", dir); + exit(1); + } } - STRNCPY(app.saveDir, dir, MAX_FILENAME_LENGTH); + sprintf(app.saveDir, "%s\\blobwarsAttrition", userHome); } void createScreenshotFolder(void) diff --git a/src/structs.h b/src/structs.h index 5f34d30..c2bbbb3 100644 --- a/src/structs.h +++ b/src/structs.h @@ -369,6 +369,7 @@ typedef struct { unsigned int stats[STAT_MAX]; char worldId[MAX_NAME_LENGTH]; int isComplete; + int saveSlot; Tuple keys[MAX_KEY_TYPES]; Tuple missionStatusHead, *missionStatusTail; Trophy trophyHead, *trophyTail; diff --git a/src/system/draw.c b/src/system/draw.c index 94611f7..85c0d92 100644 --- a/src/system/draw.c +++ b/src/system/draw.c @@ -266,7 +266,7 @@ void saveScreenshot(char *name) if (name != NULL) { - sprintf(filename, "%s/%s.png", app.saveDir, name); + sprintf(filename, "%s/%d/%s.png", app.saveDir, game.saveSlot, name); } else { diff --git a/src/system/draw.h b/src/system/draw.h index f7db280..2e53195 100644 --- a/src/system/draw.h +++ b/src/system/draw.h @@ -26,4 +26,5 @@ extern void drawText(int x, int y, int size, int align, SDL_Color c, const char extern App app; extern Colors colors; extern Dev dev; +extern Game game; diff --git a/src/system/init.c b/src/system/init.c index ca81d5a..18d38bd 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -39,7 +39,7 @@ void init18N(int argc, char *argv[]) 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; cJSON *root, *controlsJSON, *node; - char *text; + char *text, filename[MAX_FILENAME_LENGTH]; initDefaultConfig(); + + sprintf(filename, "%s/%s", app.saveDir, CONFIG_FILENAME); - if (fileExists(getSaveFilePath(CONFIG_FILENAME))) + if (fileExists(filename)) { - text = readFile(getSaveFilePath(CONFIG_FILENAME)); + text = readFile(filename); root = cJSON_Parse(text); @@ -281,10 +283,10 @@ static void loadConfig(void) void saveConfig(void) { int i; - char *out, *configFilename; + char *out, filename[MAX_FILENAME_LENGTH]; 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 ..."); @@ -321,9 +323,10 @@ void saveConfig(void) 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"); + exit(1); } cJSON_Delete(root); diff --git a/src/system/init.h b/src/system/init.h index ced614a..811016a 100644 --- a/src/system/init.h +++ b/src/system/init.h @@ -35,7 +35,6 @@ extern void destroyTextures(void); extern void expireTexts(int all); extern int fileExists(const char *filename); extern char *getLookupName(const char *prefix, long num); -extern char *getSaveFilePath(const char *filename); extern void initAtlas(void); extern void initBackground(void); extern void initEntityFactory(void); diff --git a/src/system/io.c b/src/system/io.c index c75b1e4..a634f14 100644 --- a/src/system/io.c +++ b/src/system/io.c @@ -131,18 +131,6 @@ int writeFile(const char *filename, const char *data) 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) { DIR *d; diff --git a/src/system/io.h b/src/system/io.h index c8f0e84..1ee0e21 100644 --- a/src/system/io.h +++ b/src/system/io.h @@ -24,5 +24,3 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "dirent.h" #include -extern App app; - diff --git a/src/world/worldLoader.c b/src/world/worldLoader.c index 86802c6..3a0ab68 100644 --- a/src/world/worldLoader.c +++ b/src/world/worldLoader.c @@ -38,7 +38,7 @@ void loadWorld(char *id) world.objectiveTail = &world.objectiveHead; 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)) { diff --git a/src/world/worldSaver.c b/src/world/worldSaver.c index d9c9afb..d9fb827 100644 --- a/src/world/worldSaver.c +++ b/src/world/worldSaver.c @@ -32,7 +32,7 @@ void saveWorld(void) cJSON *root; 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); @@ -61,6 +61,7 @@ void saveWorld(void) if (!writeFile(filename, out)) { SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to save world"); + exit(1); } cJSON_Delete(root); diff --git a/src/world/worldSaver.h b/src/world/worldSaver.h index 7f2d881..5f039f3 100644 --- a/src/world/worldSaver.h +++ b/src/world/worldSaver.h @@ -25,5 +25,6 @@ extern int writeFile(const char *filename, const char *data); extern App app; extern Entity *self; +extern Game game; extern World world;