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/glulx/detection.h"
24 #include "glk/glulx/detection_tables.h"
25 #include "glk/blorb.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 Glulx {
33 
getSupportedGames(PlainGameList & games)34 void GlulxMetaEngine::getSupportedGames(PlainGameList &games) {
35 	for (const PlainGameDescriptor *pd = GLULXE_GAME_LIST; pd->gameId; ++pd) {
36 		games.push_back(*pd);
37 	}
38 }
39 
findGame(const char * gameId)40 GameDescriptor GlulxMetaEngine::findGame(const char *gameId) {
41 	for (const PlainGameDescriptor *pd = GLULXE_GAME_LIST; pd->gameId; ++pd) {
42 		if (!strcmp(gameId, pd->gameId)) {
43 			GameDescriptor gd = *pd;
44 			gd._supportLevel = kTestingGame;
45 			return gd;
46 		}
47 	}
48 
49 	return GameDescriptor::empty();
50 }
51 
detectGames(const Common::FSList & fslist,DetectedGames & gameList)52 bool GlulxMetaEngine::detectGames(const Common::FSList &fslist, DetectedGames &gameList) {
53 	const char *const EXTENSIONS[] = { ".ulx", nullptr };
54 
55 	// Loop through the files of the folder
56 	for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
57 		// Check for a recognised filename
58 		if (file->isDirectory())
59 			continue;
60 		Common::String filename = file->getName();
61 		bool hasExt = Blorb::hasBlorbExt(filename), isBlorb = false;
62 		for (const char *const *ext = &EXTENSIONS[0]; *ext && !hasExt; ++ext)
63 			hasExt = filename.hasSuffixIgnoreCase(*ext);
64 		if (!hasExt)
65 			continue;
66 
67 		// Open up the file and calculate the md5
68 		Common::File gameFile;
69 		if (!gameFile.open(*file))
70 			continue;
71 		Common::String md5 = Common::computeStreamMD5AsString(gameFile, 5000);
72 		size_t filesize = gameFile.size();
73 		gameFile.seek(0);
74 		isBlorb = Blorb::isBlorb(gameFile, ID_GLUL);
75 		gameFile.close();
76 
77 		if (!isBlorb && Blorb::hasBlorbExt(filename))
78 			continue;
79 
80 		// Check for known games
81 		const GlkDetectionEntry *p = GLULXE_GAMES;
82 		while (p->_gameId && (md5 != p->_md5 || filesize != p->_filesize))
83 			++p;
84 
85 		if (!p->_gameId) {
86 			const PlainGameDescriptor &desc = GLULXE_GAME_LIST[0];
87 			gameList.push_back(GlkDetectedGame(desc.gameId, desc.description, filename, md5, filesize));
88 		} else {
89 			PlainGameDescriptor gameDesc = findGame(p->_gameId);
90 			DetectedGame gd = DetectedGame("glk", p->_gameId, gameDesc.description, p->_language, Common::kPlatformUnknown, p->_extra);
91 
92 			gd.addExtraEntry("filename", filename);
93 			gameList.push_back(gd);
94 		}
95 	}
96 
97 	return !gameList.empty();
98 }
99 
detectClashes(Common::StringMap & map)100 void GlulxMetaEngine::detectClashes(Common::StringMap &map) {
101 	for (const PlainGameDescriptor *pd = GLULXE_GAME_LIST; pd->gameId; ++pd) {
102 		if (map.contains(pd->gameId))
103 			error("Duplicate game Id found - %s", pd->gameId);
104 		map[pd->gameId] = "";
105 	}
106 }
107 
108 } // End of namespace Glulx
109 } // End of namespace Glk
110