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 "glk/advsys/detection.h"
24 #include "glk/advsys/detection_tables.h"
25 #include "glk/advsys/game.h"
26 #include "common/debug.h"
27 #include "common/file.h"
28 #include "common/md5.h"
29 #include "engines/game.h"
30 
31 namespace Glk {
32 namespace AdvSys {
33 
getSupportedGames(PlainGameList & games)34 void AdvSysMetaEngine::getSupportedGames(PlainGameList &games) {
35 	for (const PlainGameDescriptor *pd = ADVSYS_GAME_LIST; pd->gameId; ++pd)
36 		games.push_back(*pd);
37 }
38 
findGame(const char * gameId)39 GameDescriptor AdvSysMetaEngine::findGame(const char *gameId) {
40 	for (const PlainGameDescriptor *pd = ADVSYS_GAME_LIST; pd->gameId; ++pd) {
41 		if (!strcmp(gameId, pd->gameId))
42 			return *pd;
43 	}
44 
45 	return GameDescriptor::empty();
46 }
47 
detectGames(const Common::FSList & fslist,DetectedGames & gameList)48 bool AdvSysMetaEngine::detectGames(const Common::FSList &fslist, DetectedGames &gameList) {
49 	const char *const EXTENSIONS[] = { ".dat", nullptr };
50 
51 	// Loop through the files of the folder
52 	for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
53 		// Check for a recognised filename
54 		if (file->isDirectory())
55 			continue;
56 
57 		Common::String filename = file->getName();
58 		bool hasExt = false;
59 		for (const char *const *ext = &EXTENSIONS[0]; *ext && !hasExt; ++ext)
60 			hasExt = filename.hasSuffixIgnoreCase(*ext);
61 		if (!hasExt)
62 			continue;
63 
64 		Common::File gameFile;
65 		if (!gameFile.open(*file))
66 			continue;
67 
68 		Header hdr(&gameFile);
69 		if (!hdr._valid)
70 			continue;
71 
72 		gameFile.seek(0);
73 		Common::String md5 = Common::computeStreamMD5AsString(gameFile, 5000);
74 		uint32 filesize = gameFile.size();
75 
76 		// Scan through the AdvSys game list for a match
77 		const GlkDetectionEntry *p = ADVSYS_GAMES;
78 		while (p->_md5 && p->_filesize != filesize && md5 != p->_md5)
79 			++p;
80 
81 		if (!p->_gameId) {
82 			const PlainGameDescriptor &desc = ADVSYS_GAME_LIST[0];
83 			gameList.push_back(GlkDetectedGame(desc.gameId, desc.description, filename, md5, filesize));
84 		} else {
85 			// Found a match
86 			PlainGameDescriptor gameDesc = findGame(p->_gameId);
87 			gameList.push_back(GlkDetectedGame(p->_gameId, gameDesc.description, filename));
88 		}
89 	}
90 
91 	return !gameList.empty();
92 }
93 
detectClashes(Common::StringMap & map)94 void AdvSysMetaEngine::detectClashes(Common::StringMap &map) {
95 	for (const PlainGameDescriptor *pd = ADVSYS_GAME_LIST; pd->gameId; ++pd) {
96 		if (map.contains(pd->gameId))
97 			error("Duplicate game Id found - %s", pd->gameId);
98 		map[pd->gameId] = "";
99 	}
100 }
101 
102 } // End of namespace AdvSys
103 } // End of namespace Glk
104