xoreos  0.0.5
savedgame.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/error.h"
26 #include "src/common/scopedptr.h"
27 #include "src/common/filepath.h"
28 #include "src/common/readfile.h"
29 
31 
33 
34 namespace Engines {
35 
36 namespace KotOR {
37 
38 SavedGame *SavedGame::load(const Common::UString &dir, bool loadSav) {
39  Common::UString nfoPath(Common::FilePath::normalize(dir + "/savenfo.res"));
40  Common::ReadFile *nfoFile = new Common::ReadFile(nfoPath);
41  Aurora::GFF3File nfoGff(nfoFile);
42 
43  SavedGame *save = new SavedGame();
44  SavedGame::fillFromNFO(nfoGff, save);
45 
46  if (loadSav) {
47  Common::UString savPath(Common::FilePath::normalize(dir + "/SAVEGAME.sav"));
48  Common::ReadFile *savFile = new Common::ReadFile(savPath);
49  Aurora::ERFFile savErf(savFile);
50  SavedGame::fillFromSAV(savErf, save->_moduleName, save);
51  }
52 
53  return save;
54 }
55 
57  const Aurora::GFF3Struct &root = gff.getTopLevel();
58  save->_name = root.getString("SAVEGAMENAME");
59  save->_moduleName = root.getString("LASTMODULE");
60  save->_timePlayed = root.getUint("TIMEPLAYED");
61 }
62 
64  const Common::UString &moduleName, SavedGame *save) {
65  int moduleSavIndex = -1;
66 
67  const Aurora::Archive::ResourceList &resources = erf.getResources();
68  for (Aurora::Archive::ResourceList::const_iterator it = resources.begin();
69  it != resources.end(); ++it) {
70  const Aurora::Archive::Resource &res = *it;
71  if (res.name.stricmp(moduleName) == 0 && res.type == Aurora::kFileTypeSAV) {
72  moduleSavIndex = res.index;
73  break;
74  }
75  }
76 
77  if (moduleSavIndex >= 0) {
78  Aurora::ERFFile moduleSav(erf.getResource(moduleSavIndex));
79  SavedGame::fillFromModuleSAV(moduleSav, save);
80  } else
81  warning("SAV file not found: %s", moduleName.c_str());
82 }
83 
85  int ifoIndex = -1;
86 
87  const Aurora::Archive::ResourceList &resources = erf.getResources();
88  for (Aurora::Archive::ResourceList::const_iterator it = resources.begin();
89  it != resources.end(); ++it) {
90  const Aurora::Archive::Resource &res = *it;
91  if (res.name == "Module" && res.type == Aurora::kFileTypeIFO) {
92  ifoIndex = res.index;
93  break;
94  }
95  }
96 
97  if (ifoIndex >= 0) {
98  Aurora::GFF3File moduleIfo(erf.getResource(ifoIndex));
99  SavedGame::fillFromModuleIFO(moduleIfo, save);
100  } else
101  warning("Module IFO file not found");
102 }
103 
105  const Aurora::GFF3List &playerList = gff.getTopLevel().getList("Mod_PlayerList");
106  if (!playerList.empty()) {
107  const Aurora::GFF3Struct *playerGff = playerList[0];
108  save->_pcGender = playerGff->getUint("Gender");
109  save->_pcPosition[0] = playerGff->getDouble("XPosition");
110  save->_pcPosition[1] = playerGff->getDouble("YPosition");
111  save->_pcPosition[2] = playerGff->getDouble("ZPosition");
112  save->_pcLoaded = true;
113  } else
114  warning("Player list not found in module IFO");
115 }
116 
117 SavedGame::SavedGame() : _timePlayed(0),
118  _pcGender(kGenderMale),
119  _pcLoaded(false),
120  _pc(0) {
121 }
122 
124  return _name;
125 }
126 
128  return _moduleName;
129 }
130 
132  return _timePlayed;
133 }
134 
136  if (_pc)
137  return _pc;
138 
140 
141  switch (_pcGender) {
142  case kGenderFemale:
144  break;
145  default:
147  break;
148  }
149 
150  _pc = info->getCharacter();
151 
152  if (_pcLoaded)
154 
155  return _pc;
156 }
157 
158 bool SavedGame::isPCLoaded() const {
159  return _pcLoaded;
160 }
161 
162 } // End of namespace KotOR
163 
164 } // End of namespace Engines
A class holding an UTF-8 string.
Definition: ustring.h:48
static void fillFromSAV(const Aurora::ERFFile &erf, const Common::UString &moduleName, SavedGame *save)
Definition: savedgame.cpp:63
uint32 getTimePlayed() const
Definition: savedgame.cpp:131
A simple streaming file reading class.
Definition: readfile.h:40
const GFF3Struct & getTopLevel() const
Returns the top-level struct.
Definition: gff3file.cpp:91
Common::UString name
The resource&#39;s name.
Definition: archive.h:49
uint64 getUint(const Common::UString &field, uint64 def=0) const
Definition: gff3file.cpp:436
Common::UString _name
Definition: savedgame.h:64
A simple scoped smart pointer template.
Basic exceptions to throw.
static void fillFromModuleSAV(const Aurora::ERFFile &erf, SavedGame *save)
Definition: savedgame.cpp:84
Common::UString _moduleName
Definition: savedgame.h:65
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
Game save, ERF.
Definition: types.h:123
Common::SeekableReadStream * getResource(uint32 index, bool tryNoCopy=false) const
Return a stream of the resource&#39;s contents.
Definition: erffile.cpp:712
static CharacterGenerationInfo * createRandomMaleSoldier()
Definition: chargeninfo.cpp:38
const ResourceList & getResources() const
Return the list of resources.
Definition: erffile.cpp:697
double getDouble(const Common::UString &field, double def=0.0) const
Definition: gff3file.cpp:514
std::list< Resource > ResourceList
Definition: archive.h:57
void info(const char *s,...)
Definition: util.cpp:69
A GFF (generic file format) V3.2/V3.3 file, found in all Aurora games except Sonic Chronicles: The Da...
Definition: gff3file.h:85
const Common::UString & getName() const
Definition: savedgame.cpp:123
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
A resource within the archive.
Definition: archive.h:48
void warning(const char *s,...)
Definition: util.cpp:33
Module information, GFF.
Definition: types.h:83
std::vector< const GFF3Struct * > GFF3List
Definition: types.h:449
Implementing the stream reading interfaces for files.
const GFF3List & getList(const Common::UString &field) const
Definition: gff3file.cpp:741
FileType type
The resource&#39;s type.
Definition: archive.h:51
A struct within a GFF3.
Definition: gff3file.h:164
Class to hold resource data of an ERF archive file.
Definition: erffile.h:105
uint32_t uint32
Definition: types.h:204
static SavedGame * load(const Common::UString &dir, bool loadSav=false)
Load saved game from a specified directory.
Definition: savedgame.cpp:38
static UString normalize(const UString &p, bool resolveSymLinks=true)
Normalize a path.
Definition: filepath.cpp:154
void setPosition(float x, float y, float z)
Set the creature&#39;s position.
Definition: creature.cpp:176
const Common::UString & getModuleName() const
Definition: savedgame.cpp:127
static void fillFromNFO(const Aurora::GFF3File &gff, SavedGame *save)
Definition: savedgame.cpp:56
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
The class for storing character information for generation.
static CharacterGenerationInfo * createRandomFemaleSoldier()
Definition: chargeninfo.cpp:83
Utility class for manipulating file paths.
Creation and loading of KotOR saved games.
uint32 index
The resource&#39;s local index within the archive.
Definition: archive.h:52
int stricmp(const UString &str) const
Definition: ustring.cpp:192
static void fillFromModuleIFO(const Aurora::GFF3File &gff, SavedGame *save)
Definition: savedgame.cpp:104