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
savenet_util.cpp
Go to the documentation of this file.
1 #include <string>
2 #include "savenet_util.h"
3 #include "gfx/cockpit_generic.h"
4 #include "cmd/unit_generic.h"
5 #include "networking/client.h"
6 #include "faction_generic.h"
7 
8 using std::string;
9 
10 void SaveNetUtil::GetSaveStrings( ClientPtr clt, string &savestr, string &xmlstr, bool detail )
11 {
12  if (!clt) return;
13  Unit *un = clt->game_unit.GetUnit();
14  if (!un) return;
15  /*
16  * Cockpit * cp = _Universe->isPlayerStarship( un);
17  * const QVector POS( un->curr_physical_state.position);
18  * if (cp) {
19  * // Only get the player data, the dynamic universe part is separated
20  * savestr = cp->savegame->WritePlayerData ( POS, cp->unitfilename, cp->savegame->GetStarSystem().c_str(), cp->credits, FactionUtil::GetFactionName( cp->GetParent()->faction));
21  * }
22  * xmlstr = un->WriteUnitString();
23  */
24  int cpnum = _Universe->whichPlayerStarship( un );
25  if (cpnum < 0) return;
26  GetSaveStrings( cpnum, savestr, xmlstr, detail );
27 }
28 
29 void SaveNetUtil::GetSaveStrings( int numplayer, string &savestr, string &xmlstr, bool savevars )
30 {
31  Cockpit *cp;
32  Unit *un;
33  cp = _Universe->AccessCockpit( numplayer );
34  if (cp) {
35  un = cp->GetParent();
36  if (un) {
37  xmlstr = un->WriteUnitString();
38  const QVector POS( un->LocalPosition() );
39  string fname( cp->activeStarSystem->getFileName() );
40  vector< string > packedInfo;
41 
42  cp->PackUnitInfo(packedInfo);
43 
44  savestr = cp->savegame->WriteSaveGame( fname.c_str(), un->LocalPosition(), cp->credits, packedInfo, numplayer,
46  //Only get the player data, the dynamic universe part is separated
47  //savestr = cp->savegame->WritePlayerData ( POS, cp->unitfilename, fname.c_str(), cp->credits, FactionUtil::GetFactionName( cp->GetParent()->faction));
48  }
49  }
50 }
51 
52 void SaveNetUtil::GetSaveBuffer( string savestr, string xmlstr, char *buffer )
53 {
54  unsigned int total_size = savestr.length()+xmlstr.length()+2*sizeof (unsigned int);
55  unsigned int nxmllen, nsavelen;
56  //char * buffer = new char[total_size];
57  memset( buffer, 0, total_size );
58  nsavelen = htonl( savestr.length() );
59  nxmllen = htonl( xmlstr.length() );
60  //Put the save length in the buffer
61  memcpy( buffer, &nsavelen, sizeof (unsigned int) );
62  //Put the save string in the buffer
63  memcpy( buffer+sizeof (unsigned int), savestr.c_str(), savestr.length() );
64  //Put the xml length in the buffer
65  memcpy( buffer+sizeof (unsigned int)+savestr.length(), &nxmllen, sizeof (unsigned int) );
66  //Put the xml string in the buffer
67  memcpy( buffer+2*sizeof (unsigned int)+savestr.length(), xmlstr.c_str(), xmlstr.length() );
68 
69  //return buffer;
70 }
71 
72 /*
73  * void SaveNetUtil::SaveFiles( string savestr, string xmlstr, string path, string name)
74  * {
75  * string savefile;
76  * FILE * fp;
77  * savefile = path+cp->savegame->GetCallsign()+".xml";
78  * fp = VSFileSystem::OpenFile( savefile.c_str(), "wb");
79  * if( !fp)
80  * {
81  * cout<<"Error opening save file "<<savefile<<endl;
82  * VSExit(1);
83  * }
84  * VSFileSystem::Write( xmlstr.c_str(), sizeof( char), xmlstr.length(), fp);
85  * VSFileSystem::Close( fp);
86  * // Write the save file
87  * savefile = path+cp->savegame->GetCallsign()+".save";
88  * fp = VSFileSystem::OpenFile( savefile.c_str(), "wb");
89  * if( !fp)
90  * {
91  * cout<<"Error opening save file "<<savefile<<endl;
92  * VSExit(1);
93  * }
94  * VSFileSystem::Write( savestr.c_str(), sizeof( char), savestr.length(), fp);
95  * VSFileSystem::Close( fp);
96  * }
97  */
98