[Add] Saves are now backed up.

This commit is contained in:
Allanis 2014-05-27 15:28:23 +01:00
parent d7aa607380
commit be72a73740
3 changed files with 50 additions and 16 deletions

View File

@ -180,14 +180,14 @@ static void commodity_exchange_open(void) {
*/ */
static void commodity_update(unsigned int wid, char* str) { static void commodity_update(unsigned int wid, char* str) {
(void)str; (void)str;
char buf[128]; char buf[PATH_MAX];
char* comname; char* comname;
Commodity* com; Commodity* com;
comname = toolkit_getList(wid, "lstGoods"); comname = toolkit_getList(wid, "lstGoods");
com = commodity_get(comname); com = commodity_get(comname);
snprintf(buf, 128, snprintf(buf, PATH_MAX,
"%d tons\n" "%d tons\n"
"%d Scred\n" "%d Scred\n"
"\n" "\n"
@ -1473,7 +1473,10 @@ void takeoff(void) {
hyperspace_target = h; hyperspace_target = h;
/* Cleanup. */ /* Cleanup. */
save_all(); /* Must be before cleaning up planet. Duh! */ if(save_all() < 0) { /* Must be before cleaning up planet. Duh! */
dialogue_alert("Failed to save game! You should exit and check the log to \
see what happened and then file a bug report!");
}
land_cleanup(); /* Cleanup stuff. */ land_cleanup(); /* Cleanup stuff. */
hooks_run("takeoff"); /* Must be run after cleanup sine we don't want the hooks_run("takeoff"); /* Must be run after cleanup sine we don't want the
missions to think we are landed. */ missions to think we are landed. */

View File

@ -643,7 +643,12 @@ void gl_freeTexture(glTexture* texture) {
* @return 1 if the pixel is transparent or 0 if it isn't. * @return 1 if the pixel is transparent or 0 if it isn't.
*/ */
int gl_isTrans(const glTexture* t, const int x, const int y) { int gl_isTrans(const glTexture* t, const int x, const int y) {
return !(t->trans[(y*(int)(t->w)+x)/8] & (1<<((y*(int)(t->w)+x)%8))); int i;
/* Get the position in the sheet. */
i = y*(int)(t->w) + x;
/* Now we have to pull out the individual bit. */
return !(t->trans[i/8] & (1 << (i%8)));
} }
/** /**

View File

@ -5,6 +5,8 @@
*/ */
#include <stdio.h> /* remove() */ #include <stdio.h> /* remove() */
#include <errno.h>
#include "lephisto.h" #include "lephisto.h"
#include "log.h" #include "log.h"
#include "lxml.h" #include "lxml.h"
@ -55,7 +57,11 @@ static void load_menu_load(unsigned int wdw, char* str);
static void load_menu_delete(unsigned wdw, char* str); static void load_menu_delete(unsigned wdw, char* str);
static int load_game(char* file); static int load_game(char* file);
/* Save all the game data. */ /**
* @brief Save all the players game data.
* @param writer XML writer to use.
* @return 0 on success.
*/
static int save_data(xmlTextWriterPtr writer) { static int save_data(xmlTextWriterPtr writer) {
/* The data itself. */ /* The data itself. */
if(diff_save(writer) < 0) return -1; /* Must save first or can get cleared. */ if(diff_save(writer) < 0) return -1; /* Must save first or can get cleared. */
@ -68,9 +74,12 @@ static int save_data(xmlTextWriterPtr writer) {
return 0; return 0;
} }
/* Save the current game. */ /**
* @brief Save the current game.
* @return 0 on success.
*/
int save_all(void) { int save_all(void) {
char file[PATH_MAX]; char file[PATH_MAX], old[PATH_MAX];
xmlDocPtr doc; xmlDocPtr doc;
xmlTextWriterPtr writer; xmlTextWriterPtr writer;
@ -98,9 +107,7 @@ int save_all(void) {
/* Save the data. */ /* Save the data. */
if(save_data(writer) < 0) { if(save_data(writer) < 0) {
ERR("Trying to save game data"); ERR("Trying to save game data");
xmlFreeTextWriter(writer); goto err_writer;
xmlFreeDoc(doc);
return -1;
} }
/* Finish the element. */ /* Finish the element. */
@ -110,23 +117,42 @@ int save_all(void) {
/* Write to file. */ /* Write to file. */
if(lfile_dirMakeExist("%ssaves", lfile_basePath()) < 0) { if(lfile_dirMakeExist("%ssaves", lfile_basePath()) < 0) {
WARN("Aborting save..."); WARN("Aborting save...");
xmlFreeTextWriter(writer); goto err_writer;
xmlFreeDoc(doc);
return -1;
} }
snprintf(file, PATH_MAX, "%ssaves/%s.ls", lfile_basePath(), player_name); snprintf(file, PATH_MAX, "%ssaves/%s.ls", lfile_basePath(), player_name);
/* Back up old savegame. */
snprintf(old, PATH_MAX, "%s.backup", file);
if(lfile_fileExists(file)) {
if(rename(file, old) < 0) {
WARN("Unable to back up old save: %s. Not saving!", strerror(errno));
goto err_writer;
}
}
/* Critical section, if crashes, the players save game gets screwed!!! /* Critical section, if crashes, the players save game gets screwed!!!
* HAHAHAHAHA!!!!! -- NOT! * Luckily we have a copy just in case..
*/ */
xmlFreeTextWriter(writer); xmlFreeTextWriter(writer);
xmlSaveFileEnc(file, doc, "UTF-8"); if(xmlSaveFileEnc(file, doc, "UTF-8") < 0) {
WARN("Failed to write savegame! You'll most likey have to restore it by \
copying your backup savegame over your current savegame.");
goto err;
}
xmlFreeDoc(doc); xmlFreeDoc(doc);
return 0; return 0;
err_writer:
xmlFreeTextWriter(writer);
err:
xmlFreeDoc(doc);
return -1;
} }
/* Open the load game menu. */ /**
* @brief Open the load game menu.
*/
void load_game_menu(void) { void load_game_menu(void) {
unsigned int wid; unsigned int wid;
char** files; char** files;