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 #include "common/savefile.h"
24
25 #include "graphics/thumbnail.h"
26
27 #include "illusions/illusions.h"
28 #include "illusions/gamestate.h"
29
30 namespace Illusions {
31
32 #define ILLUSIONS_SAVEGAME_VERSION 0
33
readSaveHeader(Common::SeekableReadStream * in,SaveHeader & header,bool skipThumbnail)34 IllusionsEngine::kReadSaveHeaderError IllusionsEngine::readSaveHeader(Common::SeekableReadStream *in, SaveHeader &header, bool skipThumbnail) {
35
36 header.version = in->readUint32LE();
37 if (header.version > ILLUSIONS_SAVEGAME_VERSION)
38 return kRSHEInvalidVersion;
39
40 byte descriptionLen = in->readByte();
41 header.description = "";
42 while (descriptionLen--) {
43 header.description += (char)in->readByte();
44 }
45
46 if (!Graphics::loadThumbnail(*in, header.thumbnail, skipThumbnail)) {
47 return kRSHEIoError;
48 }
49
50 // Not used yet, reserved for future usage
51 header.gameID = in->readByte();
52 header.flags = in->readUint32LE();
53
54 header.saveDate = in->readUint32LE();
55 header.saveTime = in->readUint32LE();
56 header.playTime = in->readUint32LE();
57
58 return ((in->eos() || in->err()) ? kRSHEIoError : kRSHENoError);
59 }
60
savegame(const char * filename,const char * description)61 bool IllusionsEngine::savegame(const char *filename, const char *description) {
62
63 Common::OutSaveFile *out;
64 if (!(out = g_system->getSavefileManager()->openForSaving(filename))) {
65 warning("Can't create file '%s', game not saved", filename);
66 return false;
67 }
68
69 TimeDate curTime;
70 g_system->getTimeAndDate(curTime);
71
72 // Header start
73 out->writeUint32LE(ILLUSIONS_SAVEGAME_VERSION);
74
75 byte descriptionLen = strlen(description);
76 out->writeByte(descriptionLen);
77 out->write(description, descriptionLen);
78
79 // TODO Probably pre-generate the thumbnail before the internal menu system is
80 // called to have a thumbnail without the menu system itself on it.
81 // Use the automatic thumbnail generation only when the ScummVM save dialog is used.
82 Graphics::saveThumbnail(*out);
83
84 // Not used yet, reserved for future usage
85 out->writeByte(0);
86 out->writeUint32LE(0);
87 uint32 saveDate = ((curTime.tm_mday & 0xFF) << 24) | (((curTime.tm_mon + 1) & 0xFF) << 16) | ((curTime.tm_year + 1900) & 0xFFFF);
88 uint32 saveTime = ((curTime.tm_hour & 0xFF) << 16) | (((curTime.tm_min) & 0xFF) << 8) | ((curTime.tm_sec) & 0xFF);
89 uint32 playTime = g_engine->getTotalPlayTime() / 1000;
90 out->writeUint32LE(saveDate);
91 out->writeUint32LE(saveTime);
92 out->writeUint32LE(playTime);
93 // Header end
94
95 _gameState->write(out);
96
97 out->finalize();
98 delete out;
99 return true;
100 }
101
loadgame(const char * filename)102 bool IllusionsEngine::loadgame(const char *filename) {
103 Common::InSaveFile *in;
104 if (!(in = g_system->getSavefileManager()->openForLoading(filename))) {
105 warning("Can't open file '%s', game not loaded", filename);
106 return false;
107 }
108
109 SaveHeader header;
110
111 kReadSaveHeaderError errorCode = readSaveHeader(in, header);
112
113 if (errorCode != kRSHENoError) {
114 warning("Error loading savegame '%s'", filename);
115 delete in;
116 return false;
117 }
118
119 g_engine->setTotalPlayTime(header.playTime * 1000);
120
121 _gameState->read(in);
122
123 delete in;
124 return true;
125 }
126
loadGameState(int slot)127 Common::Error IllusionsEngine::loadGameState(int slot) {
128 _resumeFromSavegameRequested = false;
129 const char *fileName = getSavegameFilename(slot);
130 if (!loadgame(fileName))
131 return Common::kReadingFailed;
132 _resumeFromSavegameRequested = true;
133 _savegameSlotNum = slot;
134 return Common::kNoError;
135 }
136
saveGameState(int slot,const Common::String & description,bool isAutosave)137 Common::Error IllusionsEngine::saveGameState(int slot, const Common::String &description, bool isAutosave) {
138 const char *fileName = getSavegameFilename(slot);
139 if (!savegame(fileName, description.c_str()))
140 return Common::kWritingFailed;
141 return Common::kNoError;
142 }
143
removeGameState(int slot)144 Common::Error IllusionsEngine::removeGameState(int slot) {
145 Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
146 Common::String filename = Illusions::IllusionsEngine::getSavegameFilename(_targetName, slot);
147 saveFileMan->removeSavefile(filename.c_str());
148 return Common::kNoError;
149 }
150
getSavegameFilename(int num)151 const char *IllusionsEngine::getSavegameFilename(int num) {
152 static Common::String filename;
153 filename = getSavegameFilename(_targetName, num);
154 return filename.c_str();
155 }
156
getSavegameFilename(const Common::String & target,int num)157 Common::String IllusionsEngine::getSavegameFilename(const Common::String &target, int num) {
158 assert(num >= 0 && num <= 999);
159 return Common::String::format("%s.%03d", target.c_str(), num);
160 }
161
162 } // End of namespace Illusions
163