1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program 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 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program 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 this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23
24 #include "kyra/gui/gui.h"
25 #include "kyra/kyra_v1.h"
26 #include "kyra/engine/util.h"
27
28 #include "common/savefile.h"
29 #include "common/system.h"
30
31
32 namespace Kyra {
33
GUI(KyraEngine_v1 * kyra)34 GUI::GUI(KyraEngine_v1 *kyra) : _vm(kyra), _screen(kyra->screen()) {
35 _saveSlotsListUpdateNeeded = true;
36 _savegameListSize = 0;
37 _savegameList = 0;
38 }
39
~GUI()40 GUI::~GUI() {
41 if (_savegameList) {
42 for (int i = 0; i < _savegameListSize; i++)
43 delete[] _savegameList[i];
44 delete[] _savegameList;
45 _savegameList = 0;
46 }
47 }
48
updateSaveFileList(Common::String targetName,bool excludeQuickSaves)49 void GUI::updateSaveFileList(Common::String targetName, bool excludeQuickSaves) {
50 Common::String pattern = targetName + ".###";
51 Common::StringArray saveFileList = _vm->_saveFileMan->listSavefiles(pattern);
52 _saveSlots.clear();
53
54 for (Common::StringArray::const_iterator i = saveFileList.begin(); i != saveFileList.end(); ++i) {
55 // The last 3 digits of the filename correspond to the save slot.
56 const int slotNum = atoi(i->c_str() + i->size() - 3);
57 if (excludeQuickSaves && slotNum >= 990)
58 continue;
59 _saveSlots.push_back(slotNum);
60 }
61
62 if (_saveSlots.begin() == _saveSlots.end())
63 return;
64
65 sortSaveSlots();
66 }
67
sortSaveSlots()68 void GUI::sortSaveSlots() {
69 Common::sort(_saveSlots.begin(), _saveSlots.end(), Common::Less<int>());
70 if (_saveSlots.size() > 2)
71 Common::sort(_saveSlots.begin() + 1, _saveSlots.end(), Common::Greater<int>());
72 }
73
getNextSavegameSlot()74 int GUI::getNextSavegameSlot() {
75 Common::InSaveFile *in;
76
77 int start = _vm->game() == GI_LOL ? 0 : 1;
78
79 for (int i = start; i < 990; i++) {
80 if ((in = _vm->_saveFileMan->openForLoading(_vm->getSavegameFilename(i))))
81 delete in;
82 else
83 return i;
84 }
85 warning("Didn't save: Ran out of saveGame filenames");
86 return 0;
87 }
88
updateSaveSlotsList(Common::String targetName,bool force)89 void GUI::updateSaveSlotsList(Common::String targetName, bool force) {
90 if (!_saveSlotsListUpdateNeeded && !force)
91 return;
92
93 _saveSlotsListUpdateNeeded = false;
94
95 if (_savegameList) {
96 for (int i = 0; i < _savegameListSize; i++)
97 delete[] _savegameList[i];
98 delete[] _savegameList;
99 }
100
101 updateSaveFileList(targetName, true);
102 int numSaves = _savegameListSize = _saveSlots.size();
103 bool allowEmptySlots = (_vm->game() == GI_EOB1 || _vm->game() == GI_EOB2);
104
105 if (_savegameListSize) {
106 if (allowEmptySlots)
107 _savegameListSize = 990;
108
109 KyraEngine_v1::SaveHeader header;
110 Common::InSaveFile *in;
111
112 _savegameList = new char*[_savegameListSize];
113 memset(_savegameList, 0, _savegameListSize * sizeof(char *));
114
115 for (int i = 0; i < numSaves; i++) {
116 in = _vm->openSaveForReading(_vm->getSavegameFilename(targetName, _saveSlots[i]).c_str(), header, targetName == _vm->_targetName);
117 char **listEntry = &_savegameList[allowEmptySlots ? _saveSlots[i] : i];
118 if (in) {
119 *listEntry = new char[header.description.size() + 1];
120 Common::strlcpy(*listEntry, header.description.c_str(), header.description.size() + 1);
121 Util::convertISOToDOS(*listEntry);
122 delete in;
123 } else {
124 *listEntry = 0;
125 error("GUI::updateSavegameList(): Unexpected missing save file for slot: %d.", _saveSlots[i]);
126 }
127 }
128
129 } else {
130 _savegameList = 0;
131 }
132 }
133
134 } // End of namespace Kyra
135