Section pilot::BinaryFileHandler::nextSection() { Assertion(!_in_array, "nextSection() may not be called in an array!"); if (_section_start_pos != INVALID_SIZE && _section_end_pos != INVALID_SIZE) { cf_set_max_read_len(_cfp, 0); // There was a previous section auto current = (size_t)cftell(_cfp); if (current != _section_end_pos) { mprintf(("PLR => WARNING: Advancing to the next section. " SIZE_T_ARG " bytes were skipped!\n", _section_end_pos - current)); cfseek(_cfp, (int)_section_end_pos, CF_SEEK_SET); } _section_start_pos = INVALID_SIZE; _section_end_pos = INVALID_SIZE; } auto section_id = cfread_ushort(_cfp); auto size = cfread_uint(_cfp); if (size == 0) { return Section::Invalid; } _section_start_pos = (size_t)cftell(_cfp); _section_end_pos = _section_start_pos + size; cf_set_max_read_len(_cfp, size); return static_cast<Section>(section_id); }
void pilotfile::plr_read_multiplayer() { // netgame options p->m_server_options.squad_set = cfread_ubyte(cfp); p->m_server_options.endgame_set = cfread_ubyte(cfp); p->m_server_options.flags = cfread_int(cfp); p->m_server_options.respawn = cfread_uint(cfp); p->m_server_options.max_observers = cfread_ubyte(cfp); p->m_server_options.skill_level = cfread_ubyte(cfp); p->m_server_options.voice_qos = cfread_ubyte(cfp); p->m_server_options.voice_token_wait = cfread_int(cfp); p->m_server_options.voice_record_time = cfread_int(cfp); p->m_server_options.mission_time_limit = (fix)cfread_int(cfp); p->m_server_options.kill_limit = cfread_int(cfp); // local options p->m_local_options.flags = cfread_int(cfp); p->m_local_options.obj_update_level = cfread_int(cfp); // netgame protocol Multi_options_g.protocol = cfread_int(cfp); if (Multi_options_g.protocol == NET_VMT) { Multi_options_g.protocol = NET_TCP; } Assert( (Multi_options_g.protocol == NET_IPX) || (Multi_options_g.protocol == NET_TCP) ); }
void pilotfile::csg_read_cutscenes() { size_t list_size = cfread_uint(cfp); for(size_t i = 0; i < list_size; i++) { char tempFilename[MAX_FILENAME_LEN]; cfread_string_len(tempFilename, MAX_FILENAME_LEN, cfp); cutscene_mark_viewable(tempFilename); } }
void pilotfile::csg_read_stats() { int idx, list_size = 0; int count; if ( !m_have_info ) { throw "Stats before Info!"; } // scoring stats p->stats.score = cfread_int(cfp); p->stats.rank = cfread_int(cfp); p->stats.assists = cfread_int(cfp); p->stats.kill_count = cfread_int(cfp); p->stats.kill_count_ok = cfread_int(cfp); p->stats.bonehead_kills = cfread_int(cfp); p->stats.p_shots_fired = cfread_uint(cfp); p->stats.p_shots_hit = cfread_uint(cfp); p->stats.p_bonehead_hits = cfread_uint(cfp); p->stats.s_shots_fired = cfread_uint(cfp); p->stats.s_shots_hit = cfread_uint(cfp); p->stats.s_bonehead_hits = cfread_uint(cfp); p->stats.flight_time = cfread_uint(cfp); p->stats.missions_flown = cfread_uint(cfp); p->stats.last_flown = (_fs_time_t)cfread_int(cfp); p->stats.last_backup = (_fs_time_t)cfread_int(cfp); // ship kills (scoring) list_size = (int)ship_list.size(); for (idx = 0; idx < list_size; idx++) { count = cfread_int(cfp); if (ship_list[idx].index >= 0) { p->stats.kills[ship_list[idx].index] = count; } } // medals earned (scoring) list_size = (int)medals_list.size(); for (idx = 0; idx < list_size; idx++) { count = cfread_int(cfp); if (medals_list[idx].index >= 0) { p->stats.medal_counts[medals_list[idx].index] = count; } } }
void pilotfile_convert::plr_import_multiplayer() { plr->multi_squad_set = cfread_ubyte(cfp); plr->multi_endgame_set = cfread_ubyte(cfp); plr->multi_flags = cfread_int(cfp); plr->multi_respawn = cfread_uint(cfp); plr->multi_max_observers = cfread_ubyte(cfp); plr->multi_skill_level = cfread_ubyte(cfp); plr->multi_voice_qos = cfread_ubyte(cfp); plr->multi_voice_token_wait = cfread_int(cfp); plr->multi_voice_record_time = cfread_int(cfp); plr->multi_time_limit = cfread_int(cfp); plr->multi_kill_limit = cfread_int(cfp); plr->multi_local_flags = cfread_int(cfp); plr->multi_local_update_level = cfread_int(cfp); }
void pilotfile_convert::csg_import_stats() { int list_size = 0; int idx; csg->stats.score = cfread_int(cfp); csg->stats.rank = cfread_int(cfp); csg->stats.assists = cfread_int(cfp); csg->stats.medals_earned = csg->medals_list; list_size = (int)csg->stats.medals_earned.size(); for (idx = 0; idx < list_size; idx++) { csg->stats.medals_earned[idx].val = cfread_int(cfp); } csg->stats.ship_kills = csg->ship_list; list_size = cfread_int(cfp); // NOTE: could be less, but never greater than if ( list_size > (int)csg->stats.ship_kills.size() ) { throw std::runtime_error("Data check failure (kills size)!"); } for (idx = 0; idx < list_size; idx++) { csg->stats.ship_kills[idx].val = (int)cfread_ushort(cfp); } csg->stats.kill_count = cfread_int(cfp); csg->stats.kill_count_ok = cfread_int(cfp); csg->stats.p_shots_fired = cfread_uint(cfp); csg->stats.s_shots_fired = cfread_uint(cfp); csg->stats.p_shots_hit = cfread_uint(cfp); csg->stats.s_shots_hit = cfread_uint(cfp); csg->stats.p_bonehead_hits = cfread_uint(cfp); csg->stats.s_bonehead_hits = cfread_uint(cfp); csg->stats.bonehead_kills = cfread_uint(cfp); }
void pilotfile_convert::plr_import_stats() { int idx; char name[35]; if (fver >= 242) { return; } // read everything, but we don't need any of it ... cfread_int(cfp); // score cfread_int(cfp); // rank cfread_int(cfp); // assists // medals for (idx = 0; idx < 18; idx++) { cfread_int(cfp); } // kills per ship int count = cfread_int(cfp); for (idx = 0; idx < count; idx++) { cfread_ushort(cfp); cfread_string_len(name, sizeof(name), cfp); } cfread_int(cfp); // kill_count cfread_int(cfp); // kill_count_ok cfread_uint(cfp); // p_shots_fired cfread_uint(cfp); // s_shots_fired cfread_uint(cfp); // p_shots_hit cfread_uint(cfp); // s_shots_hit cfread_uint(cfp); // p_bonehead_hits cfread_uint(cfp); // s_bonehead_hits cfread_uint(cfp); // bonehead_kills }
bool pilotfile::verify(const char *fname, int *rank) { player t_plr; // set player ptr first thing p = &t_plr; filename = fname; if ( filename.size() == 4 ) { mprintf(("PLR => Invalid filename '%s'!\n", filename.c_str())); return false; } cfp = cfopen((char*)filename.c_str(), "rb", CFILE_NORMAL, CF_TYPE_PLAYERS); if ( !cfp ) { mprintf(("PLR => Unable to open '%s'!\n", filename.c_str())); return false; } unsigned int plr_id = cfread_uint(cfp); if (plr_id != PLR_FILE_ID) { mprintf(("PLR => Invalid header id for '%s'!\n", filename.c_str())); plr_close(); return false; } // version, should be able to just ignore it ubyte plr_ver = cfread_ubyte(cfp); mprintf(("PLR => Verifying '%s' with version %d...\n", filename.c_str(), (int)plr_ver)); // the point of all this: read in the PLR contents while ( !m_have_flags && !cfeof(cfp) ) { ushort section_id = cfread_ushort(cfp); uint section_size = cfread_uint(cfp); size_t start_pos = cftell(cfp); // safety, to help protect against long reads cf_set_max_read_len(cfp, section_size); try { switch (section_id) { case Section::Flags: mprintf(("PLR => Parsing: Flags...\n")); m_have_flags = true; plr_read_flags(); break; default: break; } } catch (cfile::max_read_length &msg) { // read to max section size, move to next section, discarding // extra/unknown data mprintf(("PLR => (0x%04x) %s\n", section_id, msg.what())); } catch (const char *err) { mprintf(("PLR => ERROR: %s\n", err)); plr_close(); return false; } // reset safety catch cf_set_max_read_len(cfp, 0); // skip to next section (if not already there) size_t offset_pos = (start_pos + section_size) - cftell(cfp); if (offset_pos) { mprintf(("PLR => Warning: (0x%04x) Short read, information may have been lost!\n", section_id)); cfseek(cfp, offset_pos, CF_SEEK_CUR); } } if (rank) { *rank = p->stats.rank; } mprintf(("PLR => Verifying complete!\n")); // cleanup and return plr_close(); return true; }
bool pilotfile::load_player(const char *callsign, player *_p) { // if we're a standalone server in multiplayer, just fill in some bogus values // since we don't have a pilot file if ( (Game_mode & GM_MULTIPLAYER) && (Game_mode & GM_STANDALONE_SERVER) ) { Player->insignia_texture = -1; strcpy_s(Player->callsign, NOX("Standalone")); strcpy_s(Player->short_callsign, NOX("Standalone")); return true; } // set player ptr first thing p = _p; if ( !p ) { Assert( (Player_num >= 0) && (Player_num < MAX_PLAYERS) ); p = &Players[Player_num]; } filename = callsign; filename += ".plr"; if ( filename.size() == 4 ) { mprintf(("PLR => Invalid filename '%s'!\n", filename.c_str())); return false; } cfp = cfopen((char*)filename.c_str(), "rb", CFILE_NORMAL, CF_TYPE_PLAYERS); if ( !cfp ) { mprintf(("PLR => Unable to open '%s' for reading!\n", filename.c_str())); return false; } unsigned int plr_id = cfread_uint(cfp); if (plr_id != PLR_FILE_ID) { mprintf(("PLR => Invalid header id for '%s'!\n", filename.c_str())); plr_close(); return false; } // version, should be able to just ignore it version = cfread_ubyte(cfp); mprintf(("PLR => Loading '%s' with version %d...\n", filename.c_str(), version)); plr_reset_data(); // the point of all this: read in the PLR contents while ( !cfeof(cfp) ) { ushort section_id = cfread_ushort(cfp); uint section_size = cfread_uint(cfp); size_t start_pos = cftell(cfp); // safety, to help protect against long reads cf_set_max_read_len(cfp, section_size); try { switch (section_id) { case Section::Flags: mprintf(("PLR => Parsing: Flags...\n")); m_have_flags = true; plr_read_flags(); break; case Section::Info: mprintf(("PLR => Parsing: Info...\n")); m_have_info = true; plr_read_info(); break; case Section::Variables: mprintf(("PLR => Parsing: Variables...\n")); plr_read_variables(); break; case Section::HUD: mprintf(("PLR => Parsing: HUD...\n")); plr_read_hud(); break; case Section::Scoring: mprintf(("PLR => Parsing: Scoring...\n")); plr_read_stats(); break; case Section::ScoringMulti: mprintf(("PLR => Parsing: ScoringMulti...\n")); plr_read_stats_multi(); break; case Section::Multiplayer: mprintf(("PLR => Parsing: Multiplayer...\n")); plr_read_multiplayer(); break; case Section::Controls: mprintf(("PLR => Parsing: Controls...\n")); plr_read_controls(); break; case Section::Settings: mprintf(("PLR => Parsing: Settings...\n")); plr_read_settings(); break; default: mprintf(("PLR => Skipping unknown section 0x%04x!\n", section_id)); break; } } catch (cfile::max_read_length &msg) { // read to max section size, move to next section, discarding // extra/unknown data mprintf(("PLR => (0x%04x) %s\n", section_id, msg.what())); } catch (const char *err) { mprintf(("PLR => ERROR: %s\n", err)); plr_close(); return false; } // reset safety catch cf_set_max_read_len(cfp, 0); // skip to next section (if not already there) size_t offset_pos = (start_pos + section_size) - cftell(cfp); if (offset_pos) { cfseek(cfp, offset_pos, CF_SEEK_CUR); } } // restore the callsign into the Player structure strcpy_s(p->callsign, callsign); // restore the truncated callsign into Player structure pilot_set_short_callsign(p, SHORT_CALLSIGN_PIXEL_W); player_set_squad_bitmap(p, p->m_squad_filename, true); hud_squadmsg_save_keys(); // set last pilot os_config_write_string(NULL, "LastPlayer", (char*)callsign); mprintf(("PLR => Loading complete!\n")); // cleanup and return plr_close(); return true; }
void pilotfile::plr_read_stats_multi() { int idx, j, list_size = 0; index_list_t ilist; char t_string[NAME_LENGTH+1]; // global, all-time stats (used only until campaign stats are loaded) multi_stats.score = cfread_int(cfp); multi_stats.rank = cfread_int(cfp); multi_stats.assists = cfread_int(cfp); multi_stats.kill_count = cfread_int(cfp); multi_stats.kill_count_ok = cfread_int(cfp); multi_stats.bonehead_kills = cfread_int(cfp); multi_stats.p_shots_fired = cfread_uint(cfp); multi_stats.p_shots_hit = cfread_uint(cfp); multi_stats.p_bonehead_hits = cfread_uint(cfp); multi_stats.s_shots_fired = cfread_uint(cfp); multi_stats.s_shots_hit = cfread_uint(cfp); multi_stats.s_bonehead_hits = cfread_uint(cfp); multi_stats.flight_time = cfread_uint(cfp); multi_stats.missions_flown = cfread_uint(cfp); multi_stats.last_flown = (_fs_time_t)cfread_int(cfp); multi_stats.last_backup = (_fs_time_t)cfread_int(cfp); // ship kills (contains ships across all mods, not just current) list_size = cfread_int(cfp); multi_stats.ship_kills.reserve(list_size); for (idx = 0; idx < list_size; idx++) { cfread_string_len(t_string, NAME_LENGTH, cfp); ilist.name = t_string; ilist.index = ship_info_lookup(t_string); ilist.val = cfread_int(cfp); multi_stats.ship_kills.push_back(ilist); } // medals earned (contains medals across all mods, not just current) list_size = cfread_int(cfp); multi_stats.medals_earned.reserve(list_size); for (idx = 0; idx < list_size; idx++) { cfread_string_len(t_string, NAME_LENGTH,cfp); ilist.name = t_string; ilist.index = medals_info_lookup(t_string); ilist.val = cfread_int(cfp); multi_stats.medals_earned.push_back(ilist); } // if in multiplayer mode then set these stats as the player stats if (Game_mode & GM_MULTIPLAYER) { p->stats.score = multi_stats.score; p->stats.rank = multi_stats.rank; p->stats.assists = multi_stats.assists; p->stats.kill_count = multi_stats.kill_count; p->stats.kill_count_ok = multi_stats.kill_count_ok; p->stats.bonehead_kills = multi_stats.bonehead_kills; p->stats.p_shots_fired = multi_stats.p_shots_fired; p->stats.p_shots_hit = multi_stats.p_shots_hit; p->stats.p_bonehead_hits = multi_stats.p_bonehead_hits; p->stats.s_shots_fired = multi_stats.s_shots_fired; p->stats.s_shots_hit = multi_stats.s_shots_hit; p->stats.s_bonehead_hits = multi_stats.s_bonehead_hits; p->stats.flight_time = multi_stats.flight_time; p->stats.missions_flown = multi_stats.missions_flown; p->stats.last_flown = multi_stats.last_flown; p->stats.last_backup = multi_stats.last_backup; // ship kills (have to find ones that match content) list_size = (int)multi_stats.ship_kills.size(); for (idx = 0; idx < list_size; idx++) { j = multi_stats.ship_kills[idx].index; if (j >= 0) { p->stats.kills[j] = multi_stats.ship_kills[idx].val; } } // medals earned (have to fine ones that match content) list_size = (int)multi_stats.medals_earned.size(); for (idx = 0; idx < list_size; idx++) { j = multi_stats.medals_earned[idx].index; if (j >= 0) { p->stats.medals[j] = multi_stats.medals_earned[idx].val; } } } }
std::uint32_t pilot::BinaryFileHandler::readUInt(const char*) { return cfread_uint(_cfp); }
void pilotfile::csg_read_missions() { int i, j, idx, list_size; cmission *missionp; if ( !m_have_info ) { throw "Missions before Info!"; } for (i = 0; i < Campaign.num_missions_completed; i++) { idx = cfread_int(cfp); missionp = &Campaign.missions[idx]; missionp->completed = 1; // flags missionp->flags = cfread_int(cfp); // goals missionp->num_goals = cfread_int(cfp); if (missionp->num_goals > 0) { missionp->goals = (mgoal *) vm_malloc( missionp->num_goals * sizeof(mgoal) ); Verify( missionp->goals != NULL ); memset( missionp->goals, 0, missionp->num_goals * sizeof(mgoal) ); for (j = 0; j < missionp->num_goals; j++) { cfread_string_len(missionp->goals[j].name, NAME_LENGTH, cfp); missionp->goals[j].status = cfread_char(cfp); } } // events missionp->num_events = cfread_int(cfp); if (missionp->num_events > 0) { missionp->events = (mevent *) vm_malloc( missionp->num_events * sizeof(mevent) ); Verify( missionp->events != NULL ); memset( missionp->events, 0, missionp->num_events * sizeof(mevent) ); for (j = 0; j < missionp->num_events; j++) { cfread_string_len(missionp->events[j].name, NAME_LENGTH, cfp); missionp->events[j].status = cfread_char(cfp); } } // variables missionp->num_variables = cfread_int(cfp); if (missionp->num_variables > 0) { missionp->variables = (sexp_variable *) vm_malloc( missionp->num_variables * sizeof(sexp_variable) ); Verify( missionp->variables != NULL ); memset( missionp->variables, 0, missionp->num_variables * sizeof(sexp_variable) ); for (j = 0; j < missionp->num_variables; j++) { missionp->variables[j].type = cfread_int(cfp); cfread_string_len(missionp->variables[j].text, TOKEN_LENGTH, cfp); cfread_string_len(missionp->variables[j].variable_name, TOKEN_LENGTH, cfp); } } // scoring stats missionp->stats.score = cfread_int(cfp); missionp->stats.rank = cfread_int(cfp); missionp->stats.assists = cfread_int(cfp); missionp->stats.kill_count = cfread_int(cfp); missionp->stats.kill_count_ok = cfread_int(cfp); missionp->stats.bonehead_kills = cfread_int(cfp); missionp->stats.p_shots_fired = cfread_uint(cfp); missionp->stats.p_shots_hit = cfread_uint(cfp); missionp->stats.p_bonehead_hits = cfread_uint(cfp); missionp->stats.s_shots_fired = cfread_uint(cfp); missionp->stats.s_shots_hit = cfread_uint(cfp); missionp->stats.s_bonehead_hits = cfread_uint(cfp); // ship kills (scoring) list_size = (int)ship_list.size(); for (j = 0; j < list_size; j++) { idx = cfread_int(cfp); if (ship_list[j].index >= 0) { missionp->stats.kills[ship_list[j].index] = idx; } } // medals (scoring) list_size = (int)medals_list.size(); for (j = 0; j < list_size; j++) { idx = cfread_int(cfp); if (medals_list[j].index >= 0) { missionp->stats.medal_counts[medals_list[j].index] = idx; } } } }
/* * get_csg_rank: this function is called from plr.cpp & is * tightly linked with pilotfile::verify() */ bool pilotfile::get_csg_rank(int *rank) { player t_csg; // set player ptr first thing p = &t_csg; // filename has already been set cfp = cfopen((char*)filename.c_str(), "rb", CFILE_NORMAL, CF_TYPE_PLAYERS); if ( !cfp ) { mprintf(("CSG => Unable to open '%s'!\n", filename.c_str())); return false; } unsigned int csg_id = cfread_uint(cfp); if (csg_id != CSG_FILE_ID) { mprintf(("CSG => Invalid header id for '%s'!\n", filename.c_str())); csg_close(); return false; } // version, now used csg_ver = cfread_ubyte(cfp); mprintf(("CSG => Get Rank from '%s' with version %d...\n", filename.c_str(), (int)csg_ver)); // the point of all this: read in the CSG contents while ( !m_have_flags && !cfeof(cfp) ) { ushort section_id = cfread_ushort(cfp); uint section_size = cfread_uint(cfp); size_t start_pos = cftell(cfp); size_t offset_pos; // safety, to help protect against long reads cf_set_max_read_len(cfp, section_size); try { switch (section_id) { case Section::Flags: mprintf(("CSG => Parsing: Flags...\n")); m_have_flags = true; csg_read_flags(); break; default: break; } } catch (cfile::max_read_length &msg) { // read to max section size, move to next section, discarding // extra/unknown data mprintf(("CSG => (0x%04x) %s\n", section_id, msg.what())); } catch (const char *err) { mprintf(("CSG => ERROR: %s\n", err)); csg_close(); return false; } // reset safety catch cf_set_max_read_len(cfp, 0); // skip to next section (if not already there) offset_pos = (start_pos + section_size) - cftell(cfp); if (offset_pos) { mprintf(("CSG => Warning: (0x%04x) Short read, information may have been lost!\n", section_id)); cfseek(cfp, (int)offset_pos, CF_SEEK_CUR); } } // this is what we came for... *rank = p->stats.rank; mprintf(("CSG => Get Rank complete!\n")); // cleanup & return csg_close(); return true; }
bool pilotfile::load_savefile(const char *campaign) { char base[_MAX_FNAME] = { '\0' }; std::ostringstream buf; if (Game_mode & GM_MULTIPLAYER) { return false; } if ( (campaign == NULL) || !strlen(campaign) ) { return false; } // set player ptr first thing Assert( (Player_num >= 0) && (Player_num < MAX_PLAYERS) ); p = &Players[Player_num]; // build up filename for the savefile... _splitpath((char*)campaign, NULL, NULL, base, NULL); buf << p->callsign << "." << base << ".csg"; filename = buf.str().c_str(); // if campaign file doesn't exist, abort so we don't load irrelevant data buf.str(std::string()); buf << base << FS_CAMPAIGN_FILE_EXT; if ( !cf_exists_full((char*)buf.str().c_str(), CF_TYPE_MISSIONS) ) { mprintf(("CSG => Unable to find campaign file '%s'!\n", buf.str().c_str())); return false; } // we need to reset this early, in case open fails and we need to create m_data_invalid = false; // open it, hopefully... cfp = cfopen((char*)filename.c_str(), "rb", CFILE_NORMAL, CF_TYPE_PLAYERS); if ( !cfp ) { mprintf(("CSG => Unable to open '%s' for reading!\n", filename.c_str())); return false; } unsigned int csg_id = cfread_uint(cfp); if (csg_id != CSG_FILE_ID) { mprintf(("CSG => Invalid header id for '%s'!\n", filename.c_str())); csg_close(); return false; } // version, now used csg_ver = cfread_ubyte(cfp); mprintf(("CSG => Loading '%s' with version %d...\n", filename.c_str(), (int)csg_ver)); csg_reset_data(); // the point of all this: read in the CSG contents while ( !cfeof(cfp) ) { ushort section_id = cfread_ushort(cfp); uint section_size = cfread_uint(cfp); size_t start_pos = cftell(cfp); // safety, to help protect against long reads cf_set_max_read_len(cfp, section_size); try { switch (section_id) { case Section::Flags: mprintf(("CSG => Parsing: Flags...\n")); m_have_flags = true; csg_read_flags(); break; case Section::Info: mprintf(("CSG => Parsing: Info...\n")); m_have_info = true; csg_read_info(); break; case Section::Variables: mprintf(("CSG => Parsing: Variables...\n")); csg_read_variables(); break; case Section::HUD: mprintf(("CSG => Parsing: HUD...\n")); csg_read_hud(); break; case Section::RedAlert: mprintf(("CSG => Parsing: RedAlert...\n")); csg_read_redalert(); break; case Section::Scoring: mprintf(("CSG => Parsing: Scoring...\n")); csg_read_stats(); break; case Section::Loadout: mprintf(("CSG => Parsing: Loadout...\n")); csg_read_loadout(); break; case Section::Techroom: mprintf(("CSG => Parsing: Techroom...\n")); csg_read_techroom(); break; case Section::Missions: mprintf(("CSG => Parsing: Missions...\n")); csg_read_missions(); break; case Section::Settings: mprintf(("CSG => Parsing: Settings...\n")); csg_read_settings(); break; case Section::Controls: mprintf(("CSG => Parsing: Controls...\n")); csg_read_controls(); break; case Section::Cutscenes: mprintf(("CSG => Parsing: Cutscenes...\n")); csg_read_cutscenes(); break; case Section::LastMissions: mprintf(("CSG => Parsing: Last Missions...\n")); csg_read_lastmissions(); break; default: mprintf(("CSG => Skipping unknown section 0x%04x!\n", section_id)); break; } } catch (cfile::max_read_length &msg) { // read to max section size, move to next section, discarding // extra/unknown data mprintf(("CSG => Warning: (0x%04x) %s\n", section_id, msg.what())); } catch (const char *err) { mprintf(("CSG => ERROR: %s\n", err)); csg_close(); return false; } // reset safety catch cf_set_max_read_len(cfp, 0); // skip to next section (if not already there) size_t offset_pos = (start_pos + section_size) - cftell(cfp); if (offset_pos) { mprintf(("CSG => Warning: (0x%04x) Short read, information may have been lost!\n", section_id)); cfseek(cfp, (int)offset_pos, CF_SEEK_CUR); } } // if the campaign (for whatever reason) doesn't have a squad image, use the multi one if (p->s_squad_filename[0] == '\0') { strcpy_s(p->s_squad_filename, p->m_squad_filename); } player_set_squad_bitmap(p, p->s_squad_filename, false); mprintf(("CSG => Loading complete!\n")); // cleanup and return csg_close(); return true; }
void pilotfile_convert::csg_import(bool inferno) { Assert( cfp != NULL ); char name[35]; unsigned int csg_id = cfread_uint(cfp); if (csg_id != 0xbeefcafe) { throw std::runtime_error("Invalid file signature!"); } fver = cfread_uint(cfp); if (fver != 15) { throw std::runtime_error("Unsupported file version!"); } // campaign type (single/multi) csg->sig = cfread_int(cfp); // trash cfread_string_len(name, sizeof(name), cfp); csg->prev_mission = cfread_int(cfp); csg->next_mission = cfread_int(cfp); csg->loop_reentry = cfread_int(cfp); csg->loop_enabled = cfread_int(cfp); csg_import_ships_weapons(); csg_import_missions(inferno); csg->main_hall = cfread_ubyte(cfp); csg_import_red_alert(); csg_import_techroom(); csg_import_loadout(); csg_import_stats(); csg->cutscenes = cfread_int(cfp); // final data checks if ( csg->ship_list.size() != csg->ships_allowed.size() ) { throw std::runtime_error("Data check failure (ship size)!"); } else if ( csg->ship_list.size() != csg->ships_techroom.size() ) { throw std::runtime_error("Data check failure (ship size)!"); } else if ( csg->weapon_list.size() != csg->weapons_allowed.size() ) { throw std::runtime_error("Data check failure (weapon size)!"); } else if ( csg->weapon_list.size() != csg->weapons_techroom.size() ) { throw std::runtime_error("Data check failure (weapon size)!"); } else if ( csg->intel_list.size() != csg->intel_techroom.size() ) { throw std::runtime_error("Data check failure (intel size)!"); } // and... we're done! }
bool pilotfile::verify(const char *fname, int *rank, char *valid_language) { player t_plr; // set player ptr first thing p = &t_plr; filename = fname; if ( filename.size() == 4 ) { mprintf(("PLR => Invalid filename '%s'!\n", filename.c_str())); return false; } cfp = cfopen((char*)filename.c_str(), "rb", CFILE_NORMAL, CF_TYPE_PLAYERS); if ( !cfp ) { mprintf(("PLR => Unable to open '%s'!\n", filename.c_str())); return false; } unsigned int plr_id = cfread_uint(cfp); if (plr_id != PLR_FILE_ID) { mprintf(("PLR => Invalid header id for '%s'!\n", filename.c_str())); plr_close(); return false; } // version, now used version = cfread_ubyte(cfp); mprintf(("PLR => Verifying '%s' with version %d...\n", filename.c_str(), (int)version)); // the point of all this: read in the PLR contents while ( !(m_have_flags && m_have_info) && !cfeof(cfp) ) { ushort section_id = cfread_ushort(cfp); uint section_size = cfread_uint(cfp); size_t start_pos = cftell(cfp); // safety, to help protect against long reads cf_set_max_read_len(cfp, section_size); try { switch (section_id) { case Section::Flags: mprintf(("PLR => Parsing: Flags...\n")); m_have_flags = true; plr_read_flags(); break; // now reading the Info section to get the campaign // and be able to lookup the campaign rank case Section::Info: mprintf(("PLR => Parsing: Info...\n")); m_have_info = true; plr_read_info(); break; default: break; } } catch (cfile::max_read_length &msg) { // read to max section size, move to next section, discarding // extra/unknown data mprintf(("PLR => (0x%04x) %s\n", section_id, msg.what())); } catch (const char *err) { mprintf(("PLR => ERROR: %s\n", err)); plr_close(); return false; } // reset safety catch cf_set_max_read_len(cfp, 0); // skip to next section (if not already there) size_t offset_pos = (start_pos + section_size) - cftell(cfp); if (offset_pos) { mprintf(("PLR => Warning: (0x%04x) Short read, information may have been lost!\n", section_id)); cfseek(cfp, offset_pos, CF_SEEK_CUR); } } if (valid_language) { strncpy(valid_language, p->language, sizeof(p->language)); } // need to cleanup early to ensure everything is OK for use in the CSG next // also means we can't use *p from now on, use t_plr instead for a few vars plr_close(); if (rank) { // maybe get the rank from the CSG if ( !(Game_mode & GM_MULTIPLAYER) ) { // build the csg filename // since filename/fname was validated above, perform less safety checks here filename = fname; filename = filename.replace(filename.find_last_of('.')+1,filename.npos, t_plr.current_campaign); filename.append(".csg"); if (!this->get_csg_rank(rank)) { // if we failed to get the csg rank, default to multi rank *rank = t_plr.stats.rank; } } else { // if the CSG isn't valid, or for multi, use this rank *rank = t_plr.stats.rank; } } mprintf(("PLR => Verifying complete!\n")); return true; }
void pilotfile_convert::plr_import() { char name[35]; int idx; unsigned int plr_id = cfread_uint(cfp); if (plr_id != 0x46505346) { throw "Invalid file signature!"; } fver = cfread_uint(cfp); if ( (fver != 142) && (fver != 242) ) { throw "Unsupported file version!"; } // multi flag plr->is_multi = (int)cfread_ubyte(cfp); // rank plr->rank = cfread_int(cfp); // mainhall, don't need it if (fver < 242) { cfread_ubyte(cfp); } plr->tips = cfread_int(cfp); if ( (plr->tips < 0) || (plr->tips > 1) ) { throw "Data check failure!"; } cfread_string_len(plr->image_filename, sizeof(plr->image_filename), cfp); cfread_string_len(plr->squad_name, sizeof(plr->squad_name), cfp); cfread_string_len(plr->squad_filename, sizeof(plr->squad_filename), cfp); cfread_string_len(plr->current_campaign, sizeof(plr->current_campaign), cfp); cfread_string_len(plr->last_ship_flown, sizeof(plr->last_ship_flown), cfp); // controls plr_import_controls(); // hud config plr_import_hud(); // cutscenes, don't need it if (fver < 242) { cfread_int(cfp); } // volume stuff plr->sound_volume = cfread_float(cfp); plr->music_volume = cfread_float(cfp); plr->voice_volume = cfread_float(cfp); // detail settings plr_import_detail(); // recent missions, don't need it int num_missions = cfread_int(cfp); for (idx = 0; idx < num_missions; idx++) { cfread_string_len(name, sizeof(name), cfp); } // stats, will skip if fver < 242 plr_import_stats(); plr->skill_level = cfread_int(cfp); if ( (plr->skill_level < 0) || (plr->skill_level > 4) ) { throw "Data check failure!"; } // extra joystick stuff for (idx = 0; idx < 5; idx++) { plr->joy_axis_map_to[idx] = cfread_int(cfp); plr->joy_invert_axis[idx] = cfread_int(cfp); } // flags plr->save_flags = cfread_int(cfp); // loadout, will skip if fver < 242 plr_import_loadout(); // multiplayer plr_import_multiplayer(); // two briefing related values plr->readyroom_listing_mode = cfread_int(cfp); Briefing_voice_enabled = cfread_int(cfp); plr->net_protocol = cfread_int(cfp); // protocol must be set to something if (plr->net_protocol == NET_NONE) { plr->net_protocol = NET_TCP; } else if ( (plr->net_protocol < 0) || (plr->net_protocol > NET_VMT) ) { throw "Data check failure!"; } // red alert status, will skip if fver < 242 (and should be empty if multi) plr_import_red_alert(); // briefing auto-advance plr->auto_advance = cfread_int(cfp); if ( (plr->auto_advance < 0) || (plr->auto_advance > 1) ) { throw "Data check failure!"; } // some input options plr->Use_mouse_to_fly = cfread_int(cfp); plr->Mouse_sensitivity = cfread_int(cfp); plr->Joy_sensitivity = cfread_int(cfp); plr->Dead_zone_size = cfread_int(cfp); // variables plr_import_variables(); // and... we're done! :) }