|
20 | 20 | #include <fstream> |
21 | 21 | #include <sstream> |
22 | 22 | #include <iomanip> |
23 | | -#include "../dirent.h" |
24 | 23 | #include "yaml.h" |
25 | 24 | #include "../Ruleset/Ruleset.h" |
26 | 25 | #include "../Engine/RNG.h" |
27 | 26 | #include "../Engine/Language.h" |
28 | 27 | #include "../Interface/TextList.h" |
29 | 28 | #include "../Engine/Exception.h" |
30 | 29 | #include "../Engine/Options.h" |
| 30 | +#include "../Engine/CrossPlatform.h" |
31 | 31 | #include "SavedBattleGame.h" |
32 | 32 | #include "GameTime.h" |
33 | 33 | #include "Country.h" |
@@ -129,44 +129,45 @@ SavedGame::~SavedGame() |
129 | 129 | */ |
130 | 130 | void SavedGame::getList(TextList *list, Language *lang) |
131 | 131 | { |
132 | | - DIR *dp = opendir(Options::getUserFolder().c_str()); |
133 | | - if (dp == 0) |
134 | | - { |
135 | | - throw Exception("Failed to open saves directory"); |
136 | | - } |
| 132 | + std::vector<std::string> saves = CrossPlatform::getFolderContents(Options::getUserFolder(), "sav"); |
137 | 133 |
|
138 | | - struct dirent *dirp; |
139 | | - while ((dirp = readdir(dp)) != 0) |
| 134 | + for (std::vector<std::string>::iterator i = saves.begin(); i != saves.end(); ++i) |
140 | 135 | { |
141 | | - std::string file = dirp->d_name; |
142 | | - // Check if it's a valid save |
143 | | - if (file.size() < 4 || file.rfind(".sav") != file.size() - 4) |
| 136 | + std::string file = (*i); |
| 137 | + std::string fullname = Options::getUserFolder() + file; |
| 138 | + std::ifstream fin(fullname.c_str()); |
| 139 | + try |
144 | 140 | { |
| 141 | + if (!fin) |
| 142 | + { |
| 143 | + throw Exception("Failed to load savegame"); |
| 144 | + } |
| 145 | + YAML::Parser parser(fin); |
| 146 | + YAML::Node doc; |
| 147 | + |
| 148 | + parser.GetNextDocument(doc); |
| 149 | + GameTime time = GameTime(6, 1, 1, 1999, 12, 0, 0); |
| 150 | + time.load(doc["time"]); |
| 151 | + std::stringstream saveTime; |
| 152 | + std::wstringstream saveDay, saveMonth, saveYear; |
| 153 | + saveTime << time.getHour() << ":" << std::setfill('0') << std::setw(2) << time.getMinute(); |
| 154 | + saveDay << time.getDay() << lang->getString(time.getDayString()); |
| 155 | + saveMonth << lang->getString(time.getMonthString()); |
| 156 | + saveYear << time.getYear(); |
| 157 | + list->addRow(5, Language::utf8ToWstr(file.substr(0, file.length()-4)).c_str(), Language::utf8ToWstr(saveTime.str()).c_str(), saveDay.str().c_str(), saveMonth.str().c_str(), saveYear.str().c_str()); |
| 158 | + fin.close(); |
| 159 | + } |
| 160 | + catch (Exception &e) |
| 161 | + { |
| 162 | + std::cerr << e.what() << std::endl; |
145 | 163 | continue; |
146 | 164 | } |
147 | | - std::string fullname = Options::getUserFolder() + file; |
148 | | - std::ifstream fin(fullname.c_str()); |
149 | | - if (!fin) |
| 165 | + catch (YAML::Exception &e) |
150 | 166 | { |
151 | | - closedir(dp); |
152 | | - throw Exception("Failed to load savegame"); |
| 167 | + std::cerr << e.what() << std::endl; |
| 168 | + continue; |
153 | 169 | } |
154 | | - YAML::Parser parser(fin); |
155 | | - YAML::Node doc; |
156 | | - |
157 | | - parser.GetNextDocument(doc); |
158 | | - GameTime time = GameTime(6, 1, 1, 1999, 12, 0, 0); |
159 | | - time.load(doc["time"]); |
160 | | - std::stringstream saveTime; |
161 | | - std::wstringstream saveDay, saveMonth, saveYear; |
162 | | - saveTime << time.getHour() << ":" << std::setfill('0') << std::setw(2) << time.getMinute(); |
163 | | - saveDay << time.getDay() << lang->getString(time.getDayString()); |
164 | | - saveMonth << lang->getString(time.getMonthString()); |
165 | | - saveYear << time.getYear(); |
166 | | - list->addRow(5, Language::utf8ToWstr(file.substr(0, file.length()-4)).c_str(), Language::utf8ToWstr(saveTime.str()).c_str(), saveDay.str().c_str(), saveMonth.str().c_str(), saveYear.str().c_str()); |
167 | | - fin.close(); |
168 | 170 | } |
169 | | - closedir(dp); |
170 | 171 | } |
171 | 172 |
|
172 | 173 | /** |
|
0 commit comments