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 "base/plugins.h"
24
25 #include "engines/advancedDetector.h"
26 #include "common/file.h"
27 #include "common/md5.h"
28 #include "common/savefile.h"
29
30 #include "tinsel/bmv.h"
31 #include "tinsel/cursor.h"
32 #include "tinsel/tinsel.h"
33 #include "tinsel/savescn.h" // needed by TinselMetaEngine::listSaves
34
35 namespace Tinsel {
36
37 struct TinselGameDescription {
38 ADGameDescription desc;
39
40 int gameID;
41 int gameType;
42 uint32 features;
43 uint16 version;
44 };
45
getGameID() const46 uint32 TinselEngine::getGameID() const {
47 return _gameDescription->gameID;
48 }
49
getFeatures() const50 uint32 TinselEngine::getFeatures() const {
51 return _gameDescription->features;
52 }
53
getLanguage() const54 Common::Language TinselEngine::getLanguage() const {
55 return _gameDescription->desc.language;
56 }
57
getPlatform() const58 Common::Platform TinselEngine::getPlatform() const {
59 return _gameDescription->desc.platform;
60 }
61
getVersion() const62 uint16 TinselEngine::getVersion() const {
63 return _gameDescription->version;
64 }
65
getIsADGFDemo() const66 bool TinselEngine::getIsADGFDemo() const {
67 return (bool)(_gameDescription->desc.flags & ADGF_DEMO);
68 }
69
isV1CD() const70 bool TinselEngine::isV1CD() const {
71 return (bool)(_gameDescription->desc.flags & ADGF_CD);
72 }
73
74 } // End of namespace Tinsel
75
76 static const PlainGameDescriptor tinselGames[] = {
77 {"tinsel", "Tinsel engine game"},
78 {"dw", "Discworld"},
79 {"dw2", "Discworld 2: Missing Presumed ...!?"},
80 {0, 0}
81 };
82
83 #include "tinsel/detection_tables.h"
84
85 class TinselMetaEngine : public AdvancedMetaEngine {
86 public:
TinselMetaEngine()87 TinselMetaEngine() : AdvancedMetaEngine(Tinsel::gameDescriptions, sizeof(Tinsel::TinselGameDescription), tinselGames) {
88 _singleId = "tinsel";
89 }
90
getName() const91 virtual const char *getName() const {
92 return "Tinsel";
93 }
94
getOriginalCopyright() const95 virtual const char *getOriginalCopyright() const {
96 return "Tinsel (C) Psygnosis";
97 }
98
99 virtual bool createInstance(OSystem *syst, Engine **engine, const ADGameDescription *desc) const;
100 ADDetectedGame fallbackDetect(const FileMap &allFiles, const Common::FSList &fslist) const override;
101
102 virtual bool hasFeature(MetaEngineFeature f) const;
103 virtual SaveStateList listSaves(const char *target) const;
104 virtual int getMaximumSaveSlot() const;
105 virtual SaveStateDescriptor querySaveMetaInfos(const char *target, int slot) const;
106 virtual void removeSaveState(const char *target, int slot) const;
107 };
108
hasFeature(MetaEngineFeature f) const109 bool TinselMetaEngine::hasFeature(MetaEngineFeature f) const {
110 return
111 (f == kSupportsListSaves) ||
112 (f == kSupportsLoadingDuringStartup) ||
113 (f == kSupportsDeleteSave) ||
114 (f == kSimpleSavesNames) ||
115 (f == kSavesSupportMetaInfo) ||
116 (f == kSavesSupportPlayTime) ||
117 (f == kSavesSupportCreationDate);
118 }
119
hasFeature(EngineFeature f) const120 bool Tinsel::TinselEngine::hasFeature(EngineFeature f) const {
121 return
122 #if 0
123 // FIXME: It is possible to return to the launcher from tinsel.
124 // But then any attempt to re-enter the engine will lead to
125 // a crash or at least seriously broken behavior.
126 //
127 // This is because the Tinsel engine makes use of tons of
128 // global variables (static and non-static) which are never
129 // explicitly re-initialized when the engine is started
130 // for a second time.
131 (f == kSupportsRTL) ||
132 #endif
133 (f == kSupportsLoadingDuringRuntime);
134 }
135
querySaveMetaInfos(const char * target,int slot) const136 SaveStateDescriptor TinselMetaEngine::querySaveMetaInfos(const char *target, int slot) const {
137 Common::String fileName;
138 fileName = Common::String::format("%s.%03u", target, slot);
139
140 Common::InSaveFile *file = g_system->getSavefileManager()->openForLoading(fileName);
141
142 if (!file) {
143 return SaveStateDescriptor();
144 }
145
146 file->readUint32LE(); // skip id
147 file->readUint32LE(); // skip size
148 uint32 ver = file->readUint32LE();
149 char saveDesc[Tinsel::SG_DESC_LEN];
150 file->read(saveDesc, sizeof(saveDesc));
151
152 saveDesc[Tinsel::SG_DESC_LEN - 1] = 0;
153 SaveStateDescriptor desc(slot, saveDesc);
154
155 int8 tm_year = file->readUint16LE();
156 int8 tm_mon = file->readSByte();
157 int8 tm_mday = file->readSByte();
158 int8 tm_hour = file->readSByte();
159 int8 tm_min = file->readSByte();
160 file->readSByte(); // skip secs
161
162 desc.setSaveDate(1900 + tm_year, 1 + tm_mon, tm_mday);
163 desc.setSaveTime(tm_hour, tm_min);
164
165 if (ver >= 3) {
166 uint32 playTime = file->readUint32LE(); // playTime in seconds
167 desc.setPlayTime(playTime);
168 }
169
170 delete file;
171 return desc;
172 }
173
174 namespace Tinsel {
175 extern int getList(Common::SaveFileManager *saveFileMan, const Common::String &target);
176 }
177
listSaves(const char * target) const178 SaveStateList TinselMetaEngine::listSaves(const char *target) const {
179 Common::String pattern = target;
180 pattern = pattern + ".###";
181 Common::StringArray files = g_system->getSavefileManager()->listSavefiles(pattern);
182
183 SaveStateList saveList;
184 int slotNum = 0;
185 for (Common::StringArray::const_iterator file = files.begin(); file != files.end(); ++file) {
186 // Obtain the last 3 digits of the filename, since they correspond to the save slot
187 slotNum = atoi(file->c_str() + file->size() - 3);
188
189 const Common::String &fname = *file;
190 Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(fname);
191 if (in) {
192 in->readUint32LE(); // skip id
193 in->readUint32LE(); // skip size
194 in->readUint32LE(); // skip version
195 char saveDesc[Tinsel::SG_DESC_LEN];
196 in->read(saveDesc, sizeof(saveDesc));
197
198 saveDesc[Tinsel::SG_DESC_LEN - 1] = 0;
199
200 saveList.push_back(SaveStateDescriptor(slotNum, saveDesc));
201 delete in;
202 }
203 }
204
205 // Sort saves based on slot number.
206 Common::sort(saveList.begin(), saveList.end(), SaveStateDescriptorSlotComparator());
207 return saveList;
208 }
209
createInstance(OSystem * syst,Engine ** engine,const ADGameDescription * desc) const210 bool TinselMetaEngine::createInstance(OSystem *syst, Engine **engine, const ADGameDescription *desc) const {
211 const Tinsel::TinselGameDescription *gd = (const Tinsel::TinselGameDescription *)desc;
212 if (gd) {
213 *engine = new Tinsel::TinselEngine(syst, gd);
214 }
215 return gd != 0;
216 }
217
218 struct SizeMD5 {
219 int size;
220 Common::String md5;
221 };
222 typedef Common::HashMap<Common::String, SizeMD5, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> SizeMD5Map;
223 typedef Common::HashMap<Common::String, Common::FSNode, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> FileMap;
224 typedef Common::Array<const ADGameDescription *> ADGameDescList;
225
226 /**
227 * Fallback detection scans the list of Discworld 2 targets to see if it can detect an installation
228 * where the files haven't been renamed (i.e. don't have the '1' just before the extension)
229 */
fallbackDetect(const FileMap & allFilesXXX,const Common::FSList & fslist) const230 ADDetectedGame TinselMetaEngine::fallbackDetect(const FileMap &allFilesXXX, const Common::FSList &fslist) const {
231 Common::String extra;
232 FileMap allFiles;
233 SizeMD5Map filesSizeMD5;
234
235 const ADGameFileDescription *fileDesc;
236 const Tinsel::TinselGameDescription *g;
237
238 if (fslist.empty())
239 return ADDetectedGame();
240
241 // TODO: The following code is essentially a slightly modified copy of the
242 // complete code of function detectGame() in engines/advancedDetector.cpp.
243 // That quite some hefty and undesirable code duplication. Its only purpose
244 // seems to be to treat filenames of the form "foo1.ext" as "foo.ext".
245 // It would be nice to avoid this code duplication.
246
247 // First we compose a hashmap of all files in fslist.
248 for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
249 if (file->isDirectory()) {
250 if (!scumm_stricmp(file->getName().c_str(), "dw2")) {
251 // Probably Discworld 2 subfolder on CD, so add it's contents as well
252 Common::FSList files;
253 if (file->getChildren(files, Common::FSNode::kListAll)) {
254 Common::FSList::const_iterator file2;
255 for (file2 = files.begin(); file2 != files.end(); ++file2) {
256 if (file2->isDirectory())
257 continue;
258
259 Common::String fname = file2->getName();
260 allFiles[fname] = *file2;
261 }
262 }
263 }
264 continue;
265 }
266
267 Common::String tstr = file->getName();
268
269 allFiles[tstr] = *file; // Record the presence of this file
270 }
271
272 // Check which files are included in some dw2 ADGameDescription *and* present
273 // in fslist without a '1' suffix character. Compute MD5s and file sizes for these files.
274 for (g = &Tinsel::gameDescriptions[0]; g->desc.gameId != 0; ++g) {
275 if (strcmp(g->desc.gameId, "dw2") != 0)
276 continue;
277
278 for (fileDesc = g->desc.filesDescriptions; fileDesc->fileName; fileDesc++) {
279 // Get the next filename, stripping off any '1' suffix character
280 char tempFilename[50];
281 Common::strlcpy(tempFilename, fileDesc->fileName, 50);
282 char *pOne = strchr(tempFilename, '1');
283 if (pOne) {
284 do {
285 *pOne = *(pOne + 1);
286 pOne++;
287 } while (*pOne);
288 }
289
290 Common::String fname(tempFilename);
291 if (allFiles.contains(fname) && !filesSizeMD5.contains(fname)) {
292 SizeMD5 tmp;
293 Common::File testFile;
294
295 if (testFile.open(allFiles[fname])) {
296 tmp.size = (int32)testFile.size();
297 tmp.md5 = computeStreamMD5AsString(testFile, _md5Bytes);
298 } else {
299 tmp.size = -1;
300 }
301
302 filesSizeMD5[fname] = tmp;
303 }
304 }
305 }
306
307 ADDetectedGame matched;
308 int maxFilesMatched = 0;
309
310 // MD5 based matching
311 for (g = &Tinsel::gameDescriptions[0]; g->desc.gameId != 0; ++g) {
312 if (strcmp(g->desc.gameId, "dw2") != 0)
313 continue;
314
315 bool fileMissing = false;
316
317 // Try to match all files for this game
318 for (fileDesc = g->desc.filesDescriptions; fileDesc->fileName; fileDesc++) {
319 // Get the next filename, stripping off any '1' suffix character
320 char tempFilename[50];
321 Common::strlcpy(tempFilename, fileDesc->fileName, 50);
322 char *pOne = strchr(tempFilename, '1');
323 if (pOne) {
324 do {
325 *pOne = *(pOne + 1);
326 pOne++;
327 } while (*pOne);
328 }
329
330 Common::String tstr(tempFilename);
331
332 if (!filesSizeMD5.contains(tstr)) {
333 fileMissing = true;
334 break;
335 }
336
337 if (fileDesc->md5 != NULL && fileDesc->md5 != filesSizeMD5[tstr].md5) {
338 fileMissing = true;
339 break;
340 }
341
342 if (fileDesc->fileSize != -1 && fileDesc->fileSize != filesSizeMD5[tstr].size) {
343 fileMissing = true;
344 break;
345 }
346 }
347
348 if (!fileMissing) {
349 // Count the number of matching files. Then, only keep those
350 // entries which match a maximal amount of files.
351 int curFilesMatched = 0;
352 for (fileDesc = g->desc.filesDescriptions; fileDesc->fileName; fileDesc++)
353 curFilesMatched++;
354
355 if (curFilesMatched >= maxFilesMatched) {
356 maxFilesMatched = curFilesMatched;
357
358 matched = ADDetectedGame(&g->desc);
359 }
360 }
361 }
362
363 return matched;
364 }
365
getMaximumSaveSlot() const366 int TinselMetaEngine::getMaximumSaveSlot() const { return 99; }
367
removeSaveState(const char * target,int slot) const368 void TinselMetaEngine::removeSaveState(const char *target, int slot) const {
369 Tinsel::setNeedLoad();
370 // Same issue here as with loadGameState(): we need the physical savegame
371 // slot. Refer to bug #3387551.
372 int listSlot = -1;
373 const int numStates = Tinsel::getList(g_system->getSavefileManager(), target);
374 for (int i = 0; i < numStates; ++i) {
375 const char *fileName = Tinsel::ListEntry(i, Tinsel::LE_NAME);
376 const int saveSlot = atoi(fileName + strlen(fileName) - 3);
377
378 if (saveSlot == slot) {
379 listSlot = i;
380 break;
381 }
382 }
383
384 g_system->getSavefileManager()->removeSavefile(Tinsel::ListEntry(listSlot, Tinsel::LE_NAME));
385 Tinsel::setNeedLoad();
386 Tinsel::getList(g_system->getSavefileManager(), target);
387 }
388
389 #if PLUGIN_ENABLED_DYNAMIC(TINSEL)
390 REGISTER_PLUGIN_DYNAMIC(TINSEL, PLUGIN_TYPE_ENGINE, TinselMetaEngine);
391 #else
392 REGISTER_PLUGIN_STATIC(TINSEL, PLUGIN_TYPE_ENGINE, TinselMetaEngine);
393 #endif
394
395 namespace Tinsel {
396
loadGameState(int slot)397 Common::Error TinselEngine::loadGameState(int slot) {
398 // FIXME: Hopefully this is only used when loading games via
399 // the launcher, since we do a hacky savegame slot to savelist
400 // entry mapping here.
401 //
402 // You might wonder why is needed and here is the answer:
403 // The save/load dialog of the GMM operates with the physical
404 // savegame slots, while Tinsel internally uses entry numbers in
405 // a savelist (which is sorted latest to first). Now to allow
406 // proper loading of (especially Discworld2) saves we need to
407 // get a savelist entry number instead of the physical slot.
408 //
409 // There are different possible solutions:
410 //
411 // One way to fix this would be to pass the filename instead of
412 // the savelist entry number to RestoreGame, though it could make
413 // problems how DW2 handles CD switches. Normally DW2 would pass
414 // '-2' as slot when it changes CDs.
415 //
416 // Another way would be to convert all of Tinsel to use physical
417 // slot numbers instead of savelist entry numbers for loading.
418 // This would also allow '-2' as slot for CD changes without
419 // any major hackery.
420
421 int listSlot = -1;
422 const int numStates = Tinsel::getList();
423 for (int i = 0; i < numStates; ++i) {
424 const char *fileName = Tinsel::ListEntry(i, Tinsel::LE_NAME);
425 const int saveSlot = atoi(fileName + strlen(fileName) - 3);
426
427 if (saveSlot == slot) {
428 listSlot = i;
429 break;
430 }
431 }
432
433 if (listSlot == -1)
434 return Common::kUnknownError; // TODO: proper error code
435
436 RestoreGame(listSlot);
437 return Common::kNoError; // TODO: return success/failure
438 }
439
440 #if 0
441 Common::Error TinselEngine::saveGameState(int slot, const Common::String &desc) {
442 Common::String saveName = _vm->getSavegameFilename((int16)(slot + 1));
443 char saveDesc[SG_DESC_LEN];
444 Common::strlcpy(saveDesc, desc, SG_DESC_LEN);
445 SaveGame((char *)saveName.c_str(), saveDesc);
446 ProcessSRQueue(); // This shouldn't be needed, but for some reason it is...
447 return Common::kNoError; // TODO: return success/failure
448 }
449 #endif
450
canLoadGameStateCurrently()451 bool TinselEngine::canLoadGameStateCurrently() { return !_bmv->MoviePlaying(); }
452
453 #if 0
454 bool TinselEngine::canSaveGameStateCurrently() { return isCursorShown(); }
455 #endif
456
457 } // End of namespace Tinsel
458