Esempio n. 1
0
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;
}
Esempio n. 2
0
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;
}
Esempio n. 3
0
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;
}