Files
fs2open.github.com/code/pilotfile/pilotfile_convert.cpp
T
Zacam 593be42e66 *drum roll* Pilot Code! Originated by taylor. Additional direct work by: The E, niffiwan, CommanderDJ, Pyro3D, Echelon9 (PVS-Studio), Karajorma and Eli2. (If I missed you, don't kill me.)
MASSIVE thanks to Mjn.Mixael and Blue Planet for tireless constant testing.
(It's still not perfect, but it probably wouldn't be where it is at without them)

Spans a lot from initial commit to AP Branch in r6835.

git-svn-id: svn://svn.icculus.org/fs2open/trunk@9561 387891d4-d844-0410-90c0-e4c51a9137d3
2013-03-18 20:26:06 +10:00

146 lines
2.9 KiB
C++

#include "globalincs/pstypes.h"
#include "pilotfile/pilotfile_convert.h"
#include "cfile/cfile.h"
pilotfile_convert::pilotfile_convert()
{
fver = 0;
m_size_offset = 0;
cfp = NULL;
plr = NULL;
csg = NULL;
}
pilotfile_convert::~pilotfile_convert()
{
if (cfp) {
cfclose(cfp);
}
if (plr) {
delete plr;
}
if (csg) {
delete csg;
}
}
void pilotfile_convert::startSection(Section::id section_id)
{
Assert( cfp );
const int zero = 0;
cfwrite_ushort( (ushort)section_id, cfp );
// to be updated when endSection() is called
cfwrite_int(zero, cfp);
// starting offset, for size of section
m_size_offset = cftell(cfp);
}
void pilotfile_convert::endSection()
{
Assert( cfp );
Assert( m_size_offset > 0 );
size_t cur = cftell(cfp);
Assert( cur >= m_size_offset );
size_t section_size = cur - m_size_offset;
if (section_size) {
// go back to section size in file and write proper value
cfseek(cfp, cur - section_size - sizeof(int), CF_SEEK_SET);
cfwrite_int((int)section_size, cfp);
// go back to previous location for next section
cfseek(cfp, cur, CF_SEEK_SET);
}
}
void convert_pilot_files()
{
size_t idx, j, i;
size_t count, inf_count;
SCP_vector<SCP_string> existing;
SCP_vector<SCP_string> old_files;
// get list of pilots which already exist (new or converted already)
cf_get_file_list(existing, CF_TYPE_PLAYERS, "*.plr");
// get list of old pilots which may need converting, starting with inferno pilots
Get_file_list_child = "inferno";
cf_get_file_list(old_files, CF_TYPE_SINGLE_PLAYERS, "*.pl2");
inf_count = old_files.size();
cf_get_file_list(old_files, CF_TYPE_SINGLE_PLAYERS, "*.pl2");
if ( old_files.empty() ) {
return;
}
// rip out all files which already exist
i = 0;
count = old_files.size();
for (idx = 0; idx < existing.size(); idx++) {
const char *fname = existing[idx].c_str();
for (j = 0; j < count; j++) {
if ( !stricmp(fname, old_files[j].c_str()) ) {
// NOTE: we just clear the name here to avoid the fragmentation
// from resizing the vector
old_files[j] = "";
++i;
}
}
}
// if everything is already converted then bail
if (i == count) {
return;
}
mprintf(("PILOT: Beginning pilot file conversion...\n"));
// now proceed to convert all of the old files
count = old_files.size();
for (idx = 0; idx < count; idx++) {
if ( old_files[idx].empty() ) {
continue;
}
pilotfile_convert *pcon = new pilotfile_convert;
bool inferno = (idx < inf_count);
if ( pcon->plr_convert(old_files[idx].c_str(), inferno) ) {
SCP_vector<SCP_string> savefiles;
SCP_string wildcard(old_files[idx]);
wildcard.append("*.cs2");
if (inferno) {
Get_file_list_child = "inferno";
}
cf_get_file_list(savefiles, CF_TYPE_SINGLE_PLAYERS, const_cast<char*>(wildcard.c_str()));
for (j = 0; j < savefiles.size(); j++) {
pcon->csg_convert(savefiles[j].c_str(), inferno);
}
}
delete pcon;
}
mprintf(("PILOT: Pilot file conversion complete!\n"));
}