FS2_Open
Open source remastering of the Freespace 2 engine
pilotfile_convert.cpp
Go to the documentation of this file.
1 
4 
5 
7 {
8  fver = 0;
9 
10  m_size_offset = 0;
11 
12  cfp = NULL;
13  plr = NULL;
14  csg = NULL;
15 }
16 
18 {
19  if (cfp) {
20  cfclose(cfp);
21  }
22 
23  if (plr) {
24  delete plr;
25  }
26 
27  if (csg) {
28  delete csg;
29  }
30 }
31 
32 void pilotfile_convert::startSection(Section::id section_id)
33 {
34  Assert( cfp );
35 
36  const int zero = 0;
37 
38  cfwrite_ushort( (ushort)section_id, cfp );
39 
40  // to be updated when endSection() is called
41  cfwrite_int(zero, cfp);
42 
43  // starting offset, for size of section
44  m_size_offset = cftell(cfp);
45 }
46 
47 void pilotfile_convert::endSection()
48 {
49  Assert( cfp );
50  Assert( m_size_offset > 0 );
51 
52  size_t cur = cftell(cfp);
53 
54  Assert( cur >= m_size_offset );
55 
56  size_t section_size = cur - m_size_offset;
57 
58  if (section_size) {
59  // go back to section size in file and write proper value
60  cfseek(cfp, cur - section_size - sizeof(int), CF_SEEK_SET);
61  cfwrite_int((int)section_size, cfp);
62 
63  // go back to previous location for next section
64  cfseek(cfp, cur, CF_SEEK_SET);
65  }
66 }
67 
68 
70 {
71  size_t idx, j, i;
72  size_t count, inf_count;
73  int max_convert, num_converted = 0;
74  SCP_vector<SCP_string> existing;
75  SCP_vector<SCP_string> old_files;
76 
77  // get list of pilots which already exist (new or converted already)
78  cf_get_file_list(existing, CF_TYPE_PLAYERS, "*.plr");
79 
80  // get list of old pilots which may need converting, starting with inferno pilots
81  Get_file_list_child = "inferno";
82  cf_get_file_list(old_files, CF_TYPE_SINGLE_PLAYERS, "*.pl2");
83  inf_count = old_files.size();
84  cf_get_file_list(old_files, CF_TYPE_SINGLE_PLAYERS, "*.pl2");
85 
86  if ( old_files.empty() ) {
87  return;
88  }
89 
90  // rip out all files which already exist
91  i = 0;
92  count = old_files.size();
93  for (idx = 0; idx < existing.size(); idx++) {
94  const char *fname = existing[idx].c_str();
95 
96  for (j = 0; j < count; j++) {
97  if ( !stricmp(fname, old_files[j].c_str()) ) {
98  // NOTE: we just clear the name here to avoid the fragmentation
99  // from resizing the vector
100  old_files[j] = "";
101  ++i;
102  }
103  }
104  }
105 
106  // don't convert enough pilots to exceed the pilot limit
107  max_convert = MAX_PILOTS - existing.size();
108 
109  // if everything is already converted then bail
110  // also bail if MAX_PILOTS (or more!) already exist
111  if (i == count || max_convert <= 0) {
112  return;
113  }
114 
115  mprintf(("PILOT: Beginning pilot file conversion...\n"));
116 
117  // now proceed to convert all of the old files
118  count = old_files.size();
119  for (idx = 0; idx < count; idx++) {
120  if ( old_files[idx].empty() ) {
121  continue;
122  }
123 
125 
126  bool inferno = (idx < inf_count);
127 
128  if ( pcon->plr_convert(old_files[idx].c_str(), inferno) ) {
129  SCP_vector<SCP_string> savefiles;
130 
131  SCP_string wildcard(old_files[idx]);
132  wildcard.append("*.cs2");
133 
134  if (inferno) {
135  Get_file_list_child = "inferno";
136  }
137 
138  cf_get_file_list(savefiles, CF_TYPE_SINGLE_PLAYERS, wildcard.c_str());
139 
140  for (j = 0; j < savefiles.size(); j++) {
141  pcon->csg_convert(savefiles[j].c_str(), inferno);
142  }
143 
144  ++num_converted;
145  }
146 
147  delete pcon;
148 
149  if (num_converted >= max_convert) {
150  break;
151  }
152  }
153 
154  mprintf(("PILOT: Pilot file conversion complete!\n"));
155 }
#define MAX_PILOTS
Definition: managepilot.h:19
int cfwrite_ushort(ushort s, CFILE *file)
Definition: cfile.cpp:1328
int i
Definition: multi_pxo.cpp:466
#define CF_TYPE_SINGLE_PLAYERS
Definition: cfile.h:70
bool csg_convert(const char *fname, bool inferno)
#define CF_TYPE_PLAYERS
Definition: cfile.h:67
Assert(pm!=NULL)
#define mprintf(args)
Definition: pstypes.h:238
#define CF_SEEK_SET
Definition: cfile.h:24
std::basic_string< char, std::char_traits< char >, std::allocator< char > > SCP_string
Definition: vmallocator.h:21
void convert_pilot_files()
bool plr_convert(const char *fname, bool inferno)
GLenum GLuint id
Definition: Glext.h:5156
int idx
Definition: multiui.cpp:761
int cfwrite_int(int i, CFILE *file)
Definition: cfile.cpp:1310
int cftell(CFILE *fp)
unsigned short ushort
Definition: pstypes.h:63
const char * Get_file_list_child
GLint GLsizei count
Definition: Gl.h:1491
int cfclose(CFILE *cfile)
Definition: cfile.cpp:895
int cf_get_file_list(SCP_vector< SCP_string > &list, int pathtype, const char *filter, int sort=CF_SORT_NONE, SCP_vector< file_list_info > *info=NULL)
#define stricmp(s1, s2)
Definition: config.h:271
int cfseek(CFILE *fp, int offset, int where)