Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
savegame.cpp File Reference
#include "cs_python.h"
#include "cmd/unit_generic.h"
#include "hashtable.h"
#include <float.h>
#include "vsfilesystem.h"
#include <vector>
#include <string>
#include "configxml.h"
#include "vs_globals.h"
#include "savegame.h"
#include "load_mission.h"
#include <algorithm>
#include "cmd/script/mission.h"
#include "gfx/cockpit_generic.h"
#include "networking/const.h"
#include "cmd/fg_util.h"
#include "options.h"

Go to the source code of this file.

Classes

class  MissionStringDat
 
class  MissionFloatDat
 

Functions

std::string GetHelperPlayerSaveGame (int num)
 
std::string GetWritePlayerSaveGame (int num)
 
std::string GetReadPlayerSaveGame (int num)
 
void SaveFileCopy (const char *src, const char *dst)
 
vector< string > parsePipedString (string s)
 
string createPipedString (vector< string >s)
 
void CopySavedShips (std::string filename, int player_num, const std::vector< std::string > &starships, bool load)
 
void WriteSaveGame (Cockpit *cp, bool auto_save)
 
int hopto (char *buf, char endln, char endln2, int readlen)
 
template<class MContainerType >
void RemoveEmpty (MContainerType &t)
 
std::string scanInString (char *&buf)
 
string AnyStringScanInString (char *&buf)
 
void AnyStringSkipInString (char *&buf)
 
string AnyStringWriteString (string input)
 
static void PushBackUInt (unsigned int i, vector< char > &ret)
 
static void PushBackChars (const char *c, vector< char > &ret)
 
static void PushBackString (const string &input, vector< char > &ret)
 
static char * tmprealloc (char *var, int &oldlength, int newlength)
 
const string & GetCurrentSaveGame ()
 
string SetCurrentSaveGame (string newname)
 
const string & GetSaveDir ()
 

Variables

vs_options game_options
 
std::string CurrentSaveGameName = ""
 
bool STATIC_VARS_DESTROYED
 
static float savedcredits = 0
 

Function Documentation

string AnyStringScanInString ( char *&  buf)

Definition at line 501 of file savegame.cpp.

References size.

Referenced by SaveGame::ReadSavedPackets().

502 {
503  unsigned int size = 0;
504  bool found = false;
505  while ( (*buf) && ( (*buf) != ' ' || (!found) ) ) {
506  if ( (*buf) >= '0' && (*buf) <= '9' ) {
507  size *= 10;
508  size += (*buf)-'0';
509  found = true;
510  }
511  buf++;
512  }
513  if (*buf)
514  buf++;
515  string ret;
516  ret.resize( size );
517  unsigned int i = 0;
518  while (i < size && *buf)
519  ret[i++] = *(buf++);
520  return ret;
521 }
void AnyStringSkipInString ( char *&  buf)

Definition at line 523 of file savegame.cpp.

References size.

524 {
525  unsigned int size = 0;
526  bool found = false;
527  while ( (*buf) && ( (*buf) != ' ' || (!found) ) ) {
528  if ( (*buf) >= '0' && (*buf) <= '9' ) {
529  size *= 10;
530  size += (*buf)-'0';
531  found = true;
532  }
533  buf++;
534  }
535  if (*buf)
536  buf++;
537  unsigned int i = 0;
538  while (i < size && *buf)
539  ++i, ++buf;
540 }
string AnyStringWriteString ( string  input)

Definition at line 542 of file savegame.cpp.

References XMLSupport::tostring().

Referenced by SaveGame::WriteDynamicUniverse().

543 {
544  return XMLSupport::tostring( (int) input.length() )+" "+input;
545 }
void CopySavedShips ( std::string  filename,
int  player_num,
const std::vector< std::string > &  starships,
bool  load 
)

Definition at line 276 of file savegame.cpp.

References VSFileSystem::CreateDirectoryHome(), e, f, GetWritePlayerSaveGame(), VSFileSystem::Ok, VSFileSystem::VSFile::OpenCreateWrite(), VSFileSystem::VSFile::OpenReadOnly(), VSFileSystem::VSFile::ReadFull(), VSFileSystem::savedunitpath, VSFileSystem::UnitFile, VSFileSystem::UnitSaveFile, and VSFileSystem::VSFile::Write().

Referenced by bootstrap_main_loop(), NetClient::startGame(), Cockpit::Update(), and WriteSaveGame().

277 {
278  for (unsigned int i = 0; i < starships.size(); i += 2) {
279  if (i == 2) i = 1;
280  VSFile src, dst;
281  string srcnam = filename;
282  string dstnam = GetWritePlayerSaveGame( player_num );
283  string tmp;
284  if (load) {
285  tmp = srcnam;
286  srcnam = dstnam;
287  dstnam = tmp;
288  }
289  VSError e = src.OpenReadOnly( srcnam+"/"+starships[i]+".csv", UnitSaveFile );
290  if (e <= Ok) {
292  VSError f = dst.OpenCreateWrite( dstnam+"/"+starships[i]+".csv", UnitFile );
293  if (f <= Ok) {
294  string srcdata = src.ReadFull();
295  dst.Write( srcdata );
296  } else {
297  printf( "Error: Cannot Copy Unit %s from save file %s to %s\n",
298  starships[i].c_str(),
299  srcnam.c_str(),
300  dstnam.c_str() );
301  }
302  } else {
303  printf( "Error: Cannot Open Unit %s from save file %s.\n",
304  starships[i].c_str(),
305  srcnam.c_str() );
306  }
307  }
308 }
string createPipedString ( vector< string >  s)

Definition at line 266 of file savegame.cpp.

Referenced by SaveGame::WritePlayerData().

267 {
268  string ret;
269  for (unsigned int i = 0; i < s.size()-1; i++)
270  ret += s[i]+"|";
271  if ( s.size() )
272  ret += s.back();
273  return ret;
274 }
const string& GetCurrentSaveGame ( )

Definition at line 998 of file savegame.cpp.

References CurrentSaveGameName.

Referenced by UniverseUtil::getCurrentSaveGame().

999 {
1000  return CurrentSaveGameName;
1001 }
std::string GetHelperPlayerSaveGame ( int  num)

Definition at line 32 of file savegame.cpp.

References VSFileSystem::VSFile::Close(), CurrentSaveGameName, f, fprintf, VSFileSystem::VSFile::GetFullPath(), i, Network, vs_options::new_game_save_name, VSFileSystem::Ok, VSFileSystem::VSFile::OpenCreateWrite(), VSFileSystem::VSFile::OpenReadOnly(), VSFileSystem::VSFile::Read(), vs_options::remember_savegame, VSFileSystem::VSFile::Size(), XMLSupport::tostring(), VSFileSystem::UnknownFile, and VSFileSystem::VSFile::Write().

Referenced by GetReadPlayerSaveGame(), and GetWritePlayerSaveGame().

33 {
34  if (Network == NULL) {
35  if (CurrentSaveGameName.length() > 0) {
37  VSFile f;
38  VSError err = f.OpenCreateWrite( "save.4.x.txt", UnknownFile );
39  if (err <= Ok) {
41  f.Close();
42  }
43  }
44  if (num != 0)
46  return CurrentSaveGameName;
47  }
48  cout<<"Hi helper play "<<num<<endl;
49  static string *res = NULL;
50  if (res == NULL) {
51  res = new std::string;
52  VSFile f;
53  //TRY TO OPEN THE save.4.x.txt FILE WHICH SHOULD CONTAIN THE NAME OF THE SAVE TO USE
54  VSError err = f.OpenReadOnly( "save.4.x.txt", UnknownFile ); // DELETE .4.x no longer used
55  if (err > Ok) {
56  //IF save.4.x.txt DOES NOT EXIST WE CREATE ONE WITH "default" AS SAVENAME
57  err = f.OpenCreateWrite( "save.4.x.txt", UnknownFile );
58  if (err <= Ok) {
60  f.Write( "\n", 1 );
61  f.Close();
62  } else {
63  fprintf( stderr, "!!! ERROR : Creating default save.4.x.txt file : %s\n", f.GetFullPath().c_str() );
64  exit( 1 );
65  }
66  err = f.OpenReadOnly( "save.4.x.txt", UnknownFile );
67  if (err > Ok) {
68  fprintf( stderr, "!!! ERROR : Opening the default save we just created\n" );
69  exit( 1 );
70  }
71  }
72  if (err <= Ok) {
73  long length = f.Size();
74  if (length > 0) {
75  char *temp = (char*) malloc( length+1 );
76  temp[length] = '\0';
77  f.Read( temp, length );
78  bool end = true;
79  for (int i = length-1; i >= 0; i--) {
80  if (temp[i] == '\r' || temp[i] == '\n')
81  temp[i] = (end ? '\0' : '_');
82  else if (temp[i] == '\0' || temp[i] == ' ' || temp[i] == '\t')
83  temp[i] = (end ? '\0' : '_');
84  else
85  end = false;
86  }
87  *res = (temp);
88  free( temp );
89  }
90  f.Close();
91  }
92  if ( game_options.remember_savegame && !res->empty() ) {
93  //Set filetype to Unknown so that it is searched in homedir/
94  if (*res->begin() == '~') {
95  err = f.OpenCreateWrite( "save.4.x.txt", VSFileSystem::UnknownFile );
96  if (err <= Ok) {
97  for (unsigned int i = 1; i < res->length(); i++) {
98  char cc = *(res->begin()+i);
99  f.Write( &cc, sizeof (char) );
100  }
101  char cc = 0;
102  f.Write( &cc, sizeof (char) );
103  f.Close();
104  }
105  }
106  }
107  }
108  if ( num == 0 || res->empty() ) {
109  cout<<"Here";
110  return *res;
111  }
112  return (*res)+XMLSupport::tostring( num );
113  } else {
114  //Return "" so that the filename argument to ParseSavegame will be used
115  return "";
116  }
117 }
std::string GetReadPlayerSaveGame ( int  num)

Definition at line 128 of file savegame.cpp.

References GetHelperPlayerSaveGame().

Referenced by SaveGame::ParseSaveGame().

129 {
130  string ret = GetHelperPlayerSaveGame( num );
131  if ( !ret.empty() )
132  if (*ret.begin() == '~')
133  return "";
134  return ret;
135 }
const string& GetSaveDir ( )

Definition at line 1010 of file savegame.cpp.

References VSFileSystem::homedir.

Referenced by UniverseUtil::getSaveDir().

1011 {
1012  static string rv = VSFileSystem::homedir+"/save/";
1013  return rv;
1014 }
std::string GetWritePlayerSaveGame ( int  num)

Definition at line 119 of file savegame.cpp.

References GetHelperPlayerSaveGame().

Referenced by CopySavedShips(), SaveGame::WriteSaveGame(), and WriteSaveGame().

120 {
121  string ret = GetHelperPlayerSaveGame( num );
122  if ( !ret.empty() )
123  if (*ret.begin() == '~')
124  return ret.substr( 1, ret.length() );
125  return ret;
126 }
int hopto ( char *  buf,
char  endln,
char  endln2,
int  readlen 
)

Definition at line 335 of file savegame.cpp.

Referenced by SaveGame::ParseSaveGame(), and SaveGame::ReadSavedPackets().

336 {
337  if (endln == ' ' || endln2 == ' ')
338  while (buf[readlen] && buf[readlen] == ' ')
339  readlen++;
340  for (; buf[readlen] != 0 && buf[readlen] != endln && buf[readlen] != endln2; readlen++)
341  ;
342  if ( (buf[readlen] && buf[readlen] == endln) || buf[readlen] == endln2 )
343  readlen++;
344  return readlen;
345 }
vector< string > parsePipedString ( string  s)

Definition at line 250 of file savegame.cpp.

Referenced by SaveGame::ParseSaveGame().

251 {
252  string::size_type loc;
253  vector< string > ret;
254  while ( ( loc = s.find( "|" ) ) != string::npos ) {
255  ret.push_back( s.substr( 0, loc ) );
256  cout<<"Found ship named : "<<s.substr( 0, loc )<<endl;
257  s = s.substr( loc+1 );
258  }
259  if ( s.length() ) {
260  ret.push_back( s );
261  cout<<"Found ship named : "<<s<<endl;
262  }
263  return ret;
264 }
static void PushBackChars ( const char *  c,
vector< char > &  ret 
)
inlinestatic

Definition at line 608 of file savegame.cpp.

Referenced by PushBackString().

609 {
610  int ini = ret.size();
611  ret.resize( ret.size()+strlen( c ) );
612  while (*c)
613  ret[ini++] = *(c++);
614 }
static void PushBackString ( const string &  input,
vector< char > &  ret 
)
inlinestatic

Definition at line 616 of file savegame.cpp.

References PushBackChars(), and PushBackUInt().

617 {
618  PushBackUInt( input.length(), ret );
619  PushBackChars( " ", ret );
620  PushBackChars( input.c_str(), ret );
621 }
static void PushBackUInt ( unsigned int  i,
vector< char > &  ret 
)
inlinestatic

Definition at line 591 of file savegame.cpp.

References q.

Referenced by PushBackString().

592 {
593  char tmp[32];
594  if (!i) {
595  ret.push_back( '0' );
596  } else {
597  unsigned int p = 0, q = ret.size();
598  while (i) {
599  tmp[p++] = (i%10+'0');
600  i /= 10;
601  }
602  ret.resize( q+p );
603  while (p)
604  ret[q++] = tmp[--p];
605  }
606 }
template<class MContainerType >
void RemoveEmpty ( MContainerType &  t)

Definition at line 417 of file savegame.cpp.

418 {
419  typename MContainerType::iterator i;
420  for (i = t.begin(); i != t.end();)
421  if ( i->second.empty() )
422  t.erase( i++ );
423 
424  else
425  ++i;
426 }
void SaveFileCopy ( const char *  src,
const char *  dst 
)

Definition at line 138 of file savegame.cpp.

References VSFileSystem::VSFile::Close(), f, fprintf, VSFileSystem::Ok, VSFileSystem::VSFile::OpenCreateWrite(), VSFileSystem::VSFile::OpenReadOnly(), VSFileSystem::VSFile::ReadFull(), VSFileSystem::SaveFile, and VSFileSystem::VSFile::Write().

Referenced by SaveGame::WriteSaveGame().

139 {
140  if (dst[0] != '\0' && src[0] != '\0') {
141  VSFile f;
142  VSError err = f.OpenReadOnly( src, SaveFile );
143  if (err <= Ok) {
144  string savecontent = f.ReadFull();
145  f.Close();
146  err = f.OpenCreateWrite( dst, SaveFile );
147  if (err <= Ok) {
148  f.Write( savecontent );
149  f.Close();
150  } else {
151  fprintf( stderr, "WARNING : couldn't open savegame to copy to : %s as SaveFile", dst );
152  }
153  } else {
154  fprintf( stderr, "WARNING : couldn't find the savegame to copy : %s as SaveFile", src );
155  }
156  }
157 }
std::string scanInString ( char *&  buf)

Definition at line 448 of file savegame.cpp.

References start.

449 {
450  std::string str;
451  while ( *buf && isspace( *buf ) )
452  buf++;
453  char *start = buf;
454  while ( *buf && ( !isspace( *buf ) ) )
455  buf++;
456  str.resize(buf - start);
457  for (size_t i=0; start < buf; ++i)
458  str[i] = *(start++);
459  return str;
460 }
string SetCurrentSaveGame ( string  newname)

Definition at line 1003 of file savegame.cpp.

References CurrentSaveGameName.

Referenced by UniverseUtil::setCurrentSaveGame().

1004 {
1005  string oldname = CurrentSaveGameName;
1006  CurrentSaveGameName = newname;
1007  return oldname;
1008 }
static char* tmprealloc ( char *  var,
int oldlength,
int  newlength 
)
static

Definition at line 730 of file savegame.cpp.

Referenced by SaveGame::WriteDynamicUniverse(), and SaveGame::WritePlayerData().

731 {
732  if (oldlength < newlength) {
733  oldlength = newlength;
734  var = (char*) realloc( var, newlength );
735  }
736  memset( var, 0, newlength );
737  return var;
738 }
void WriteSaveGame ( Cockpit cp,
bool  auto_save 
)

Definition at line 310 of file savegame.cpp.

References _Universe, Universe::AccessCockpit(), Cockpit::activeStarSystem, CopySavedShips(), Cockpit::credits, StarSystem::getFileName(), Unit::GetHull(), Cockpit::GetSaveParent(), Cockpit::GetUnitModifications(), GetWritePlayerSaveGame(), Unit::LocalPosition(), Universe::numPlayers(), Cockpit::PackUnitInfo(), Cockpit::savegame, SaveGame::SetPlayerLocation(), SaveGame::SetSavedCredits(), SaveGame::SetStarSystem(), SaveGame::WriteSaveGame(), and Unit::WriteUnit().

Referenced by BaseComputer::actionConfirmedSaveGame(), bootstrap_main_loop(), buyShip(), and UniverseUtil::saveGame().

311 {
312  int player_num = 0;
313  for (unsigned int kk = 0; kk < _Universe->numPlayers(); ++kk)
314  if (_Universe->AccessCockpit( kk ) == cp)
315  player_num = kk;
316  Unit *un = cp->GetSaveParent();
317  if (!un)
318  return;
319  if (un->GetHull() > 0) {
320  vector< string > packedInfo;
321  cp->PackUnitInfo(packedInfo);
322 
323  cp->savegame->WriteSaveGame( cp->activeStarSystem->getFileName().c_str(),
324  un->LocalPosition(), cp->credits, packedInfo, auto_save ? -1 : player_num );
325  un->WriteUnit( cp->GetUnitModifications().c_str() );
326  if (GetWritePlayerSaveGame( player_num ).length() && !auto_save) {
330  CopySavedShips( cp->GetUnitModifications(), player_num, packedInfo, false );
331  }
332  }
333 }

Variable Documentation

std::string CurrentSaveGameName = ""
vs_options game_options

Draws cockpit parts Draws gauges, info strings, radar, ...

Definition at line 83 of file main.cpp.

float savedcredits = 0
static

Definition at line 855 of file savegame.cpp.

Referenced by SaveGame::GetSavedCredits(), and SaveGame::SetSavedCredits().

bool STATIC_VARS_DESTROYED

Definition at line 21 of file vs_globals.cpp.

Referenced by cleanup(), and SaveGame::WriteDynamicUniverse().