1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/* SavedGame.cpp
Copyright (c) 2014 by Michael Zahniser
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "SavedGame.h"
#include "DataFile.h"
#include "DataNode.h"
#include "Date.h"
#include "text/Format.h"
#include "GameData.h"
#include "Planet.h"
#include "image/SpriteSet.h"
#include "System.h"
using namespace std;
SavedGame::SavedGame(const filesystem::path &path)
{
Load(path);
}
void SavedGame::Load(const filesystem::path &path)
{
Clear();
DataFile file(path);
if(file.begin() != file.end())
this->path = path;
int flagshipIterator = -1;
int flagshipTarget = 0;
for(const DataNode &node : file)
{
const string &key = node.Token(0);
bool hasValue = node.Size() >= 2;
if(key == "pilot" && node.Size() >= 3)
name = node.Token(1) + " " + node.Token(2);
else if(key == "date" && node.Size() >= 4)
date = Date(node.Value(1), node.Value(2), node.Value(3)).ToString();
else if(key == "system" && hasValue)
{
system = node.Token(1);
const System *savedSystem = GameData::Systems().Find(system);
if(savedSystem && savedSystem->IsValid())
system = savedSystem->DisplayName();
}
else if(key == "planet" && hasValue)
{
planet = node.Token(1);
const Planet *savedPlanet = GameData::Planets().Find(planet);
if(savedPlanet && savedPlanet->IsValid())
planet = savedPlanet->DisplayName();
}
else if(key == "playtime" && hasValue)
playTime = Format::PlayTime(node.Value(1));
else if(key == "flagship index" && hasValue)
flagshipTarget = node.Value(1);
else if(key == "account")
{
for(const DataNode &child : node)
if(child.Token(0) == "credits" && child.Size() >= 2)
{
credits = Format::Credits(child.Value(1));
break;
}
}
else if(key == "ship" && ++flagshipIterator == flagshipTarget)
{
for(const DataNode &child : node)
{
const string &childKey = child.Token(0);
bool childHasValue = child.Size() >= 2;
if(childKey == "name" && childHasValue)
shipName = child.Token(1);
else if(childKey == "sprite" && childHasValue)
shipSprite = SpriteSet::Get(child.Token(1));
}
}
}
}
const filesystem::path &SavedGame::Path() const
{
return path;
}
bool SavedGame::IsLoaded() const
{
return !path.empty();
}
void SavedGame::Clear()
{
path.clear();
name.clear();
credits.clear();
date.clear();
system.clear();
planet.clear();
playTime = "0s";
shipSprite = nullptr;
shipName.clear();
}
const string &SavedGame::Name() const
{
return name;
}
const string &SavedGame::Credits() const
{
return credits;
}
const string &SavedGame::GetDate() const
{
return date;
}
const string &SavedGame::GetSystem() const
{
return system;
}
const string &SavedGame::GetPlanet() const
{
return planet;
}
const string &SavedGame::GetPlayTime() const
{
return playTime;
}
const Sprite *SavedGame::ShipSprite() const
{
return shipSprite;
}
const string &SavedGame::ShipName() const
{
return shipName;
}
|