bool Player::loadData() { char buff[256]; std::string filename = get_config_dir_name(); filename+="/"; filename+=name; filename+=".sav"; std::ifstream sfile(filename.c_str()); if(!sfile) return false; int version; int numcourses; std::string notused; sfile >> buff >> buff >> version; sfile >> notused; //character sfile >> notused; //difficulty sfile >> notused; //num difficulties sfile >> numcourses; for (int i=0; i<numcourses ; i++) { double time; int _herring; int _score; sfile.get(); sfile.getline(buff,256); sfile >> time >> _herring >> _score; PlayerCourseData& data = courses[buff]; data.time=time; data.herring=_herring; data.score=_score; } int numevents; sfile >> numevents; for(int i=0; i<numevents; i++) { sfile.get(); sfile.getline(buff,256); events[buff].loadData(sfile); } return true; }
int get_save_game_file_name( char *buff, char *player, int len ) { if (get_config_dir_name( buff, len ) != 0) { return 1; } if ( strlen( buff ) + strlen( player ) + 6 > len ) { return 1; } strcat( buff, DIR_SEPARATOR ); strcat( buff, player); strcat( buff, ".sav"); return 0; }
int get_config_file_name( char *buff, int len ) { if (get_config_dir_name( buff, len ) != 0) { return 1; } if ( strlen( buff ) + strlen( CONFIG_FILE ) +2 > len ) { return 1; } #if defined( WIN32 ) strcat( buff, "\\" ); #else strcat( buff, "/" ); #endif /* defined( WIN32 ) */ strcat( buff, CONFIG_FILE); return 0; }
int get_high_score_file_name( char *buff, int len ) { if (get_config_dir_name( buff, len ) != 0) { return 1; } if ( strlen( buff ) + strlen( HIGH_SCORE_FILE ) +2 > len ) { return 1; } #if defined( WIN32 ) strcat( buff, "\\" ); #else strcat( buff, "/" ); #endif /* defined( WIN32 ) */ strcat( buff, HIGH_SCORE_FILE); return 0; }
void read_config_file() { char config_file[BUFF_LEN]; char config_dir[BUFF_LEN]; clear_config_cache(); if ( get_config_file_name( config_file, sizeof( config_file ) ) != 0 ) { return; } if ( get_config_dir_name( config_dir, sizeof( config_dir ) ) != 0 ) { return; } if ( dir_exists( config_dir ) ) { if ( file_exists( config_file ) ) { /* File exists -- let's try to evaluate it. */ if ( Tcl_EvalFile( g_game.tcl_interp, config_file ) != TCL_OK ) { handle_error( 1, "error evalating %s: %s", config_file, Tcl_GetStringResult( g_game.tcl_interp ) ); } } return; } /* File does not exist -- look for old version */ if ( get_old_config_file_name( config_file, sizeof( config_file ) ) != 0 ) { return; } if ( !file_exists( config_file ) ) { return; } /* Old file exists -- let's try to evaluate it. */ if ( Tcl_EvalFile( g_game.tcl_interp, config_file ) != TCL_OK ) { handle_error( 1, "error evalating deprecated %s: %s", config_file, Tcl_GetStringResult( g_game.tcl_interp ) ); } else { /* Remove old file and save info in new file location */ remove(config_file); write_config_file(); } }
bool Player::saveData() { std::string filename=get_config_dir_name(); filename+="/"; filename+=name; filename+=".sav"; std::ofstream sfile(filename.c_str()); sfile << "PPRacer SAVE " << 1 << std::endl; sfile << "tux" << std::endl; sfile << "easy" << std::endl; sfile << 1 << std::endl; sfile << courses.size() << std::endl; { std::map<std::string,PlayerCourseData>::iterator it; for(it=courses.begin(); it!=courses.end(); it++) { sfile << (*it).first << std::endl; sfile << (*it).second.time << std::endl; sfile << (*it).second.herring << std::endl; sfile << (*it).second.score << std::endl; } } sfile << events.size() << std::endl; if(events.size()>0) { std::map<std::string,PlayerEventData>::iterator it; for (it=events.begin(); it!=events.end(); it++) { sfile << (*it).first << std::endl; (*it).second.saveData(sfile); } } return true; }
void write_config_file() { FILE *config_stream; char config_file[BUFF_LEN]; char config_dir[BUFF_LEN]; struct param *parm; int i; #ifdef __APPLE__ // Don't save the config file on iphone, no use for it. return; #endif if ( get_config_file_name( config_file, sizeof( config_file ) ) != 0 ) { return; } if ( get_config_dir_name( config_dir, sizeof( config_dir ) ) != 0 ) { return; } if ( !dir_exists( config_dir ) ) { #if defined(WIN32) && !defined(__CYGWIN__) if (mkdir( config_dir ) != 0) { return; } #else if (mkdir( config_dir, 0775) != 0) { return; } #endif } config_stream = fopen( config_file, "w" ); if ( config_stream == NULL ) { print_warning( CRITICAL_WARNING, "couldn't open %s for writing: %s", config_file, strerror(errno) ); return; } fprintf( config_stream, "# Tux Racer " VERSION " configuration file\n" "#\n" ); for (i=0; i<sizeof(Params)/sizeof(struct param); i++) { parm = (struct param*)&Params + i; //FIXME : dans la version simulateur, on ne veut as qu'il enregistre le data_dir car il change à chaque fois et sinon c'est la merde if (!strcmp(parm->name,"data_dir")) continue; // Don't save the resolution if (!strcmp(parm->name,"x_resolution")) continue; if (!strcmp(parm->name,"y_resolution")) continue; if ( parm->comment != NULL ) { fprintf( config_stream, "\n# %s\n#\n%s\n#\n", parm->name, parm->comment ); } switch ( parm->type ) { case PARAM_STRING: fetch_param_string( parm ); fprintf( config_stream, "set %s \"%s\"\n", parm->name, parm->val.string_val ); break; case PARAM_CHAR: fetch_param_char( parm ); fprintf( config_stream, "set %s %c\n", parm->name, parm->val.char_val ); break; case PARAM_INT: fetch_param_int( parm ); fprintf( config_stream, "set %s %d\n", parm->name, parm->val.int_val ); break; case PARAM_BOOL: fetch_param_bool( parm ); fprintf( config_stream, "set %s %s\n", parm->name, parm->val.bool_val ? "true" : "false" ); break; default: code_not_reached(); } } if ( fclose( config_stream ) != 0 ) { perror( "fclose" ); } }
void init_saved_games( void ) { char dir_name[BUFF_LEN]; FILE* save_stream; save_info_t this_save; char player_name[BUFF_LEN]; int sav_index; char file_name[BUFF_LEN]; int i; char magic[4]; list_t dir_file_list = NULL; list_elem_t cur_dir_file = NULL; char *cur_dir_filename = NULL; progress_save_table = create_hash_table(); for (i=0; i<DIFFICULTY_NUM_LEVELS; i++) { results_save_table[i] = create_hash_table(); } if (get_config_dir_name( dir_name, sizeof(dir_name) ) != 0) { return; } dir_file_list = get_dir_file_list(dir_name); if ( dir_file_list == NULL ) { /* Config dir doesn't exist. Don't print warning since this is a normal condition the first time the program is run. */ return; } for ( cur_dir_file = get_list_head( dir_file_list ); cur_dir_file != NULL; cur_dir_file = get_next_list_elem( dir_file_list, cur_dir_file ) ) { cur_dir_filename = (char*) get_list_elem_data( cur_dir_file ); if (get_sav_index(cur_dir_filename, &sav_index)) { strncpy(player_name, cur_dir_filename, sav_index); player_name[sav_index] = '\0'; sprintf( file_name, "%s" DIR_SEPARATOR "%s", dir_name, cur_dir_filename ); save_stream = fopen( file_name, "r" ); if ( fread( magic, sizeof(magic), 1, save_stream ) != 1 || strncmp( magic, SAVE_MAGIC_V1, sizeof(magic) ) != 0 ) { print_warning( IMPORTANT_WARNING, "`%s' is not a valid saved game file", file_name ); fclose( save_stream); continue; } if (save_stream != NULL) { while (fread( &this_save, sizeof(this_save), 1, save_stream)) { switch ( this_save.data_type ) { case EVENT_INFO: set_last_completed_cup( player_name, this_save.data.event.event, this_save.data.event.difficulty, this_save.data.event.cup ); print_debug( DEBUG_SAVE, "Read completed from `%s': " "name: %s, event: %s, difficulty: %d, cup: %s", cur_dir_filename, player_name, this_save.data.event.event, this_save.data.event.difficulty, this_save.data.event.cup ); break; case RACE_RESULTS: set_saved_race_results( player_name, this_save.data.results.event, this_save.data.results.cup, this_save.data.results.race, this_save.data.results.difficulty, this_save.data.results.time, this_save.data.results.herring, this_save.data.results.score ); print_debug( DEBUG_SAVE, "Read results from `%s': " "name: %s, event: %s, cup: %s, " "race: %s, difficulty: %d, time: %g, " "herring: %d, score: %d", cur_dir_filename, player_name, this_save.data.results.event, this_save.data.results.cup, this_save.data.results.race, this_save.data.results.difficulty, this_save.data.results.time, this_save.data.results.herring, this_save.data.results.score ); break; default: print_warning( IMPORTANT_WARNING, "Unrecognized data type in save file." ); } } if ( fclose( save_stream ) != 0 ) { perror( "fclose" ); } } else { print_warning( IMPORTANT_WARNING, "Couldn't read file `%s': %s", file_name, strerror( errno ) ); } } } free_dir_file_list( dir_file_list ); }