1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
/*
*
* Copyright (c) 2002, 2003 Johannes Prix
* Copyright (c) 2004-2010 Arthur Huillet
*
* This file is part of Freedroid
*
* Freedroid is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Freedroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Freedroid; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*/
/**
* All functions that have to do with loading and saving of games.
*/
#define _saveloadgame_c 1
#include "system.h"
#include "defs.h"
#include "struct.h"
#include "global.h"
#include "proto.h"
#include <sys/stat.h>
#include "widgets/widgets.h"
#include "savestruct.h"
#include "savegame/savegame.h"
#include "scandir.h"
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#ifndef HAVE_NL_LANGINFO
#if __WIN32__
#define nl_langinfo(X) "%a %b %#d %H:%M:%S %Y"
#else
#define nl_langinfo(X) "%a %b %e %H:%M:%S %Y"
#endif
#endif
#define SAVEDGAME_EXT ".sav.gz"
#define SAVE_GAME_THUMBNAIL_EXT ".thumbnail.png"
/**
* Filter function for scandir calls.
* This function keeps files with the SAVEDGAME_EXT extension.
*/
static int filename_filter_func(const struct dirent *file)
{
char *pos = strstr(file->d_name, SAVEDGAME_EXT);
if (pos != NULL) { // SAVEDGAME_EXT found
if (strlen(pos) == strlen(SAVEDGAME_EXT)) {
// d_name *ENDS* with SAVEDGAME_EXT
if (strstr(file->d_name, ".bkp"SAVEDGAME_EXT) + 4 == pos) {
//then we have .bkp+SAVEDGAME_EXT = filter it out
return 0;
}
return 1;
}
}
return 0;
}
/**
* Find all currently saved games.
* @param namelist the found files -- must bee freed just like scandir(3)
* @return the number of found save games
*/
int find_saved_games(struct dirent ***namelist)
{
int n = scandir(data_dirs[CONFIG_DIR].path, namelist, filename_filter_func, alphasort);
if (n == -1)
{
error_message(__FUNCTION__, "Error occurred while reading save game directory.",
NO_REPORT);
return 0;
}
// For each element in list, remove the suffix SAVEDGAME_EXT
int i;
for (i = 0; i < n; i++) {
*strstr((*namelist)[i]->d_name, SAVEDGAME_EXT) = 0;
if (!strlen((*namelist)[i]->d_name))
strcpy((*namelist)[i]->d_name, "INVALID");
}
return n;
}
void load_and_show_thumbnail(char *core_filename)
{
static char last_filename[PATH_MAX] = "";
static struct image thumbnail = EMPTY_IMAGE;
if (strcmp(last_filename, core_filename)) {
struct image empty_thumbnail = EMPTY_IMAGE;
thumbnail = empty_thumbnail;
if (!strlen(data_dirs[CONFIG_DIR].path))
return;
char filename[PATH_MAX];
char filepath[PATH_MAX];
sprintf(filename, "%s%s", core_filename, SAVE_GAME_THUMBNAIL_EXT);
if (!find_file(filepath, CONFIG_DIR, filename, NULL, NO_REPORT))
return;
/* Load the image */
thumbnail.surface = load_surface_bitmap(filepath);
if (!thumbnail.surface)
return;
thumbnail.w = thumbnail.surface->w;
thumbnail.h = thumbnail.surface->h;
if (use_open_gl)
make_texture_out_of_surface(&thumbnail);
strcpy(last_filename, core_filename);
}
SDL_Rect target_rectangle = { .x = 10, .y = (GameConfig.screen_height - thumbnail.h - 10) };
display_image_on_screen(&thumbnail, target_rectangle.x, target_rectangle.y, IMAGE_NO_TRANSFO);
}
/**
*
*
*/
void load_and_show_stats(char *core_filename)
{
static char last_filename[PATH_MAX] = "";
static char info_string_date[5000] = "";
static char info_string_size[5000] = "";
if (strcmp(last_filename, core_filename)) {
if (!strlen(data_dirs[CONFIG_DIR].path))
return;
char filename[PATH_MAX];
char filepath[PATH_MAX];
struct stat file_info_buffer;
long int file_size;
// First we get the information of the .sav file
sprintf(filename, "%s%s", core_filename, SAVEDGAME_EXT);
if (!find_file(filepath, CONFIG_DIR, filename, NULL, NO_REPORT)) {
error_once_message(ONCE_PER_RUN, __FUNCTION__,
"FreedroidRPG was unable to access your saved game file (%s).\n"
"This is either a bug in FreedroidRPG or an indication, that the directory\n"
"or file permissions of ~/.freedroid_rpg are somehow not right.",
PLEASE_INFORM, filename);
return;
}
if (stat(filepath, &file_info_buffer)) {
error_once_message(ONCE_PER_RUN, __FUNCTION__,
"FreedroidRPG was unable to get the stats of your saved game file (%s).\n"
"This is either a bug in FreedroidRPG or an indication, that the directory\n"
"or file permissions of ~/.freedroid_rpg are somehow not right.",
PLEASE_INFORM, filename);
return;
};
file_size = file_info_buffer.st_size;
strftime(info_string_date, sizeof(info_string_date), nl_langinfo(D_T_FMT), localtime(&(file_info_buffer.st_mtime)));
// Now we compute the overall disk space of all files in question.
// The saved .shp must exist. On not, it's a sever error!
sprintf(filename, "%s%s", core_filename, ".shp");
if (!find_file(filepath, CONFIG_DIR, filename, NULL, NO_REPORT)) {
error_once_message(ONCE_PER_RUN, __FUNCTION__,
"FreedroidRPG was unable to access your saved game file (%s).\n"
"This is either a bug in FreedroidRPG or an indication, that the directory\n"
"or file permissions of ~/.freedroid_rpg are somehow not right.",
PLEASE_INFORM, filename);
return;
}
if (!stat(filepath, &file_info_buffer)) {
file_size += file_info_buffer.st_size;
}
// A thumbnail may not yet exist. We won't make much fuss if it doesn't.
sprintf(filename, "%s%s", core_filename, SAVE_GAME_THUMBNAIL_EXT);
if (find_file(filepath, CONFIG_DIR, filename, NULL, SILENT)) {
if (!stat(filename, &(file_info_buffer))) {
file_size += file_info_buffer.st_size;
}
}
sprintf(info_string_size, _("File Size: %2.3f MB"), ((float)file_size) / (1024.0 * 1024.0));
strcpy(last_filename, core_filename);
}
put_string(get_current_font(), UNIVERSAL_COORD_W(240), GameConfig.screen_height - 3 * get_font_height(get_current_font()), _("Last Modified:"));
put_string(get_current_font(), UNIVERSAL_COORD_W(240), GameConfig.screen_height - 2 * get_font_height(get_current_font()), info_string_date);
put_string(get_current_font(), UNIVERSAL_COORD_W(240), GameConfig.screen_height - 1 * get_font_height(get_current_font()), info_string_size);
}
/**
* This function stores a thumbnail of the currently running game, so that
* these thumbnails can be browsed when choosing which game to load.
*/
void save_thumbnail(void)
{
char filepath[PATH_MAX];
if (!strlen(data_dirs[CONFIG_DIR].path))
return;
find_file(filepath, CONFIG_DIR, Me.character_name, SAVE_GAME_THUMBNAIL_EXT, SILENT);
AssembleCombatPicture(SHOW_ITEMS | NO_CURSOR);
save_screenshot(filepath, 210);
}
/**
* This function saves the current game of FreedroidRPG to a file.
*/
int save_game(void)
{
char filepath[PATH_MAX];
char backup_filepath[PATH_MAX];
int ret;
FILE *savegame_file;
if (Me.energy <= 0) {
alert_window(_("You are dead. Savegame not modified."));
return (ERR);
}
if (!strlen(data_dirs[CONFIG_DIR].path))
return (OK);
/* Start with a 1MB string */
savestruct_autostr = alloc_autostr(1048576);
Activate_Conservative_Frame_Computation();
find_file(filepath, CONFIG_DIR, Me.character_name, ".shp", SILENT);
if (find_file(backup_filepath, CONFIG_DIR, Me.character_name, ".bkp.shp", SILENT))
unlink(backup_filepath);
ret = rename(filepath, backup_filepath);
if (ret && errno != ENOENT) {
error_message(__FUNCTION__, "Unable to create the shipfile backup. Errno: %d", PLEASE_INFORM, errno);
}
put_string_centered(Menu_Font, 10, _("Saving"));
our_SDL_flip_wrapper();
if (SaveShip(filepath, FALSE, 1) != OK) {
error_message(__FUNCTION__,
"The SAVING OF THE SHIP DATA FOR THE SAVED GAME FAILED!\n"
"This is either a bug in FreedroidRPG or an indication, that the directory\n"
"or file permissions of ~/.freedroid_rpg are somehow not right.",
PLEASE_INFORM | IS_FATAL);
}
find_file(filepath, CONFIG_DIR, Me.character_name, SAVEDGAME_EXT, SILENT);
if (find_file(backup_filepath, CONFIG_DIR, Me.character_name, ".bkp"SAVEDGAME_EXT, SILENT))
unlink(backup_filepath);
ret = rename(filepath, backup_filepath);
if (ret && errno != ENOENT) {
error_message(__FUNCTION__, "Unable to create the savegame backup. Errno: %d", PLEASE_INFORM, errno);
}
if ((savegame_file = fopen(filepath, "wb")) == NULL) {
error_message(__FUNCTION__, "Error opening save game file for writing...\n\nTerminating...", IS_FATAL);
}
save_game_data(savestruct_autostr);
deflate_to_stream((unsigned char *)savestruct_autostr->value, savestruct_autostr->length+1, savegame_file);
fclose(savegame_file);
save_thumbnail();
append_new_game_message(_("Game saved."));
free_autostr(savestruct_autostr);
savestruct_autostr = NULL;
return OK;
}
/**
* This function delete a saved game and its backup (if there's one)
*/
int delete_game(void)
{
char file_path[PATH_MAX];
if (find_file(file_path, CONFIG_DIR, Me.character_name, ".shp", SILENT))
remove(file_path);
if (find_file(file_path, CONFIG_DIR, Me.character_name, SAVEDGAME_EXT, SILENT))
remove(file_path);
if (find_file(file_path, CONFIG_DIR, Me.character_name, SAVE_GAME_THUMBNAIL_EXT, SILENT))
remove(file_path);
// We do not check if the backup files exists before to remove it, but since
// do not check the returned value of remove(), this is not an issue
if (find_file(file_path, CONFIG_DIR, Me.character_name, ".bkp.shp", SILENT))
remove(file_path);
if (find_file(file_path, CONFIG_DIR, Me.character_name, ".bkp"SAVEDGAME_EXT, SILENT))
remove(file_path);
return (OK);
}
static int load_saved_game(int use_backup)
{
char ship_filepath[PATH_MAX];
char sav_filepath[PATH_MAX];
if (!strlen(data_dirs[CONFIG_DIR].path)) {
return OK;
}
put_string_centered(Menu_Font, 10, _("Loading"));
StoreMenuBackground(1);
our_SDL_flip_wrapper();
RestoreMenuBackground(1);
Activate_Conservative_Frame_Computation();
clean_error_msg_store();
// Check the existence of the two files that we have to load
if (!find_file(ship_filepath, CONFIG_DIR, Me.character_name, (use_backup) ? ".bkp.shp" : ".shp", SILENT) ||
!find_file(sav_filepath, CONFIG_DIR, Me.character_name, (use_backup) ? ".bkp"SAVEDGAME_EXT : SAVEDGAME_EXT, SILENT)) {
alert_window(_("No saved game found."));
return ERR;
}
/*
* Load maps (still using the legacy format)
*/
LoadShip(ship_filepath, 1);
/*
* Load data
*/
int loaded_size = 0;
char *game_data = NULL;
// Read savegame into a buffer
FILE *data_file = fopen(sav_filepath, "rb");
if (inflate_stream(data_file, (unsigned char **)&game_data, &loaded_size)) {
fclose(data_file);
alert_window(_("Unable to decompress saved game - this is probably a very old, incompatible game. Sorry."));
return ERR;
}
fclose(data_file);
// Try to apply savegame converters
int rtc = convert_old_savegame(&game_data, &loaded_size);
if (rtc == FILTER_APPLIED) {
// A fix/conversion was needed. Save the new data.
data_file = fopen(sav_filepath, "wb");
deflate_to_stream((unsigned char *)game_data, loaded_size, data_file);
fclose(data_file);
} else if (rtc == FILTER_ABORT) {
// Conversion was aborted.
// However game_data was possibly changed by a filter function.
// We reload the savegame and use it as is.
free(game_data);
data_file = fopen(sav_filepath, "rb");
int data_size = 0;
inflate_stream(data_file, (unsigned char **)&game_data, &data_size);
fclose(data_file);
}
// Load the savegame (lua format)
clear_out_arrays_for_fresh_game();
reset_lua_game_scripting();
if (setjmp(saveload_jmpbuf)) {
// We longjump here if load_game_data() detects an error
if (game_data != NULL)
free((char *)game_data);
game_data = NULL;
return (ERR);
}
load_game_data(game_data);
free(game_data);
game_data = NULL;
/*
* Post-loading: Some transient states have to be adapted
**/
reset_visible_levels();
get_visible_levels();
animation_timeline_reset();
switch_background_music(curShip.AllLevels[Me.pos.z]->Background_Song_Name);
// Reset animation of dead bots
enemy *erot;
BROWSE_DEAD_BOTS(erot) {
// It is sufficient to set ->animation_type, since ->animation_phase will be set to
// last_death_animation_image[erot->type] inside animate_enemy();
//
// Also, we might not know the correct value of last_death_animation_image[] yet,
// since it's initialized by grab_enemy_images_from_archive(erot->type), and that
// function may not have been called for the current bot type.
if (!level_is_visible(erot->pos.z))
erot->animation_type = DEAD_ANIMATION;
}
// Count and initialize the number of droids used in this ship.
// Otherwise we might ignore some robots.
CountNumberOfDroidsOnShip();
// Maybe someone just lost in the game and has then pressed the load
// button. Then a new game is loaded and the game-over status has
// to be restored as well of course.
GameOver = FALSE;
// Now we know that right after loading an old saved game, the Tux might have
// to 'change clothes' i.e. a lot of tux images need to be updated which can
// take a little time. Therefore we print some message so the user will not
// panic and push the reset button :)
put_string(FPS_Display_Font, 75, 150, _("Updating Tux images (this may take a little while...)"));
our_SDL_flip_wrapper();
/*
* We're now ready to start playing !
**/
widget_text_init(message_log, _("--- Message Log ---"));
append_new_game_message(_("Game loaded."));
return OK;
}
/**
* This function loads an old saved game of FreedroidRPG from a file.
*/
int load_game(void)
{
return load_saved_game(0);
}
/**
* This loads the backup for the current player name
*/
int load_backup_game(void)
{
return load_saved_game(1);
}
#undef _saveloadgame_c
|