[Add] Saves are now backed up.
This commit is contained in:
		
							parent
							
								
									d7aa607380
								
							
						
					
					
						commit
						be72a73740
					
				| @ -180,14 +180,14 @@ static void commodity_exchange_open(void) { | ||||
|  */ | ||||
| static void commodity_update(unsigned int wid, char* str) { | ||||
|   (void)str; | ||||
|   char buf[128]; | ||||
|   char buf[PATH_MAX]; | ||||
|   char* comname; | ||||
|   Commodity* com; | ||||
| 
 | ||||
|   comname = toolkit_getList(wid, "lstGoods"); | ||||
|   com = commodity_get(comname); | ||||
| 
 | ||||
|   snprintf(buf, 128, | ||||
|   snprintf(buf, PATH_MAX, | ||||
|       "%d tons\n" | ||||
|       "%d Scred\n" | ||||
|       "\n" | ||||
| @ -1473,7 +1473,10 @@ void takeoff(void) { | ||||
|   hyperspace_target = h; | ||||
| 
 | ||||
|   /* 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. */ | ||||
|   hooks_run("takeoff"); /* Must be run after cleanup sine we don't want the
 | ||||
|                            missions to think we are landed. */ | ||||
|  | ||||
| @ -643,7 +643,12 @@ void gl_freeTexture(glTexture* texture) { | ||||
|  *    @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) { | ||||
|   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))); | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  | ||||
							
								
								
									
										50
									
								
								src/save.c
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								src/save.c
									
									
									
									
									
								
							| @ -5,6 +5,8 @@ | ||||
|  */ | ||||
| 
 | ||||
| #include <stdio.h> /* remove() */ | ||||
| #include <errno.h> | ||||
| 
 | ||||
| #include "lephisto.h" | ||||
| #include "log.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 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) { | ||||
|   /* The data itself. */ | ||||
|   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; | ||||
| } | ||||
| 
 | ||||
| /* Save the current game. */ | ||||
| /**
 | ||||
|  * @brief Save the current game. | ||||
|  *    @return 0 on success. | ||||
|  */ | ||||
| int save_all(void) { | ||||
|   char file[PATH_MAX]; | ||||
|   char file[PATH_MAX], old[PATH_MAX]; | ||||
|   xmlDocPtr doc; | ||||
|   xmlTextWriterPtr writer; | ||||
| 
 | ||||
| @ -98,9 +107,7 @@ int save_all(void) { | ||||
|   /* Save the data. */ | ||||
|   if(save_data(writer) < 0) { | ||||
|     ERR("Trying to save game data"); | ||||
|     xmlFreeTextWriter(writer); | ||||
|     xmlFreeDoc(doc); | ||||
|     return -1; | ||||
|     goto err_writer; | ||||
|   } | ||||
| 
 | ||||
|   /* Finish the element. */ | ||||
| @ -110,23 +117,42 @@ int save_all(void) { | ||||
|   /* Write to file. */ | ||||
|   if(lfile_dirMakeExist("%ssaves", lfile_basePath()) < 0) { | ||||
|     WARN("Aborting save..."); | ||||
|     xmlFreeTextWriter(writer); | ||||
|     xmlFreeDoc(doc); | ||||
|     return -1; | ||||
|     goto err_writer; | ||||
|   } | ||||
|   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!!!
 | ||||
|    * HAHAHAHAHA!!!!! -- NOT! | ||||
|    * Luckily we have a copy just in case.. | ||||
|    */ | ||||
|   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); | ||||
| 
 | ||||
|   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) { | ||||
|   unsigned int wid; | ||||
|   char** files; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Allanis
						Allanis