File: detection.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (309 lines) | stat: -rw-r--r-- 10,816 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "base/plugins.h"
#include "common/md5.h"
#include "common/memstream.h"
#include "common/str-array.h"
#include "common/file.h"
#include "common/config-manager.h"

#include "glk/detection.h"
#include "glk/game_description.h"
#include "glk/glk.h"

#include "glk/adrift/detection.h"
#include "glk/advsys/detection.h"
#include "glk/agt/detection.h"
#include "glk/alan2/detection.h"
#include "glk/alan3/detection.h"
#include "glk/archetype/detection.h"
#include "glk/comprehend/detection.h"
#include "glk/glulx/detection.h"
#include "glk/hugo/detection.h"
#include "glk/jacl/detection.h"
#include "glk/level9/detection.h"
#include "glk/magnetic/detection.h"
#include "glk/quest/detection.h"
#include "glk/scott/detection.h"
#include "glk/zcode/detection.h"

#ifndef RELEASE_BUILD
#include "glk/tads/detection.h"
#endif

#include "base/plugins.h"
#include "common/md5.h"
#include "common/memstream.h"
#include "common/savefile.h"
#include "common/str-array.h"
#include "common/system.h"
#include "graphics/surface.h"
#include "common/config-manager.h"
#include "common/file.h"

static const DebugChannelDef debugFlagList[] = {
	{Glk::kDebugCore, "core", "Core engine debug level"},
	{Glk::kDebugScripts, "scripts", "Game scripts"},
	{Glk::kDebugGraphics, "graphics", "Graphics handling"},
	{Glk::kDebugSound, "sound", "Sound and Music handling"},
	{Glk::kDebugSpeech, "speech", "Text to Speech handling"},
	DEBUG_CHANNEL_END
};

namespace Glk {

Common::String GlkDetectedGame::getGlkGUIOptions() {
#if defined (USE_TTS)
	return GUIO2(GUIO_NOMUSIC, GUIO_NOSUBTITLES);
#else
	return GUIO3(GUIO_NOSPEECH, GUIO_NOMUSIC, GUIO_NOSUBTITLES);
#endif
}

GlkDetectedGame::GlkDetectedGame(const char *id, const char *desc, const Common::String &filename,
		GameSupportLevel supportLevel) :
		DetectedGame("glk", id, desc, Common::EN_ANY, Common::kPlatformUnknown) {
	setGUIOptions(getGlkGUIOptions());
	gameSupportLevel = supportLevel;
	addExtraEntry("filename", filename);
}

GlkDetectedGame::GlkDetectedGame(const char *id, const char *desc, const Common::String &filename,
		Common::Language lang, Common::Platform p, GameSupportLevel supportLevel) : DetectedGame("glk", id, desc, lang, p) {
	setGUIOptions(getGlkGUIOptions());
	gameSupportLevel = supportLevel;
	addExtraEntry("filename", filename);
}

GlkDetectedGame::GlkDetectedGame(const char *id, const char *desc, const char *xtra,
		const Common::String &filename, Common::Language lang,
		GameSupportLevel supportLevel) :
		DetectedGame("glk", id, desc, lang, Common::kPlatformUnknown, xtra) {
	setGUIOptions(getGlkGUIOptions());
	gameSupportLevel = supportLevel;
	addExtraEntry("filename", filename);
}

GlkDetectedGame::GlkDetectedGame(const char *id, const char *desc, const Common::String &filename,
		const Common::String &md5, size_t filesize, GameSupportLevel supportLevel) :
		DetectedGame("glk", id, desc, Common::UNK_LANG, Common::kPlatformUnknown) {
	setGUIOptions(getGlkGUIOptions());
	gameSupportLevel = supportLevel;
	addExtraEntry("filename", filename);

	canBeAdded = true;
	hasUnknownFiles = true;

	FileProperties fp;
	fp.md5 = md5;
	fp.size = filesize;
	matchedFiles[Common::Path(filename)] = fp;
}

} // End of namespace Glk

PlainGameList GlkMetaEngineDetection::getSupportedGames() const {
	PlainGameList list;
	Glk::Adrift::AdriftMetaEngine::getSupportedGames(list);
	Glk::AdvSys::AdvSysMetaEngine::getSupportedGames(list);
	Glk::AGT::AGTMetaEngine::getSupportedGames(list);
	Glk::Alan2::Alan2MetaEngine::getSupportedGames(list);
	Glk::Alan3::Alan3MetaEngine::getSupportedGames(list);
	Glk::Archetype::ArchetypeMetaEngine::getSupportedGames(list);
	Glk::Comprehend::ComprehendMetaEngine::getSupportedGames(list);
	Glk::Glulx::GlulxMetaEngine::getSupportedGames(list);
	Glk::Hugo::HugoMetaEngine::getSupportedGames(list);
	Glk::JACL::JACLMetaEngine::getSupportedGames(list);
	Glk::Level9::Level9MetaEngine::getSupportedGames(list);
	Glk::Magnetic::MagneticMetaEngine::getSupportedGames(list);
	Glk::Quest::QuestMetaEngine::getSupportedGames(list);
	Glk::Scott::ScottMetaEngine::getSupportedGames(list);
	Glk::ZCode::ZCodeMetaEngine::getSupportedGames(list);
#ifndef RELEASE_BUILD
	Glk::TADS::TADSMetaEngine::getSupportedGames(list);
#endif

	return list;
}

#define FIND_GAME(SUBENGINE) \
	Glk::GameDescriptor gd##SUBENGINE = Glk::SUBENGINE::SUBENGINE##MetaEngine::findGame(gameId); \
	if (gd##SUBENGINE._description) return gd##SUBENGINE

const DebugChannelDef *GlkMetaEngineDetection::getDebugChannels() const {
	return debugFlagList;
}

PlainGameDescriptor GlkMetaEngineDetection::findGame(const char *gameId) const {
	FIND_GAME(Adrift);
	FIND_GAME(AdvSys);
	FIND_GAME(Alan2);
	FIND_GAME(AGT);
	FIND_GAME(Alan3);
	FIND_GAME(Archetype);
	FIND_GAME(Comprehend);
	FIND_GAME(Glulx);
	FIND_GAME(Hugo);
	FIND_GAME(JACL);
	FIND_GAME(Level9);
	FIND_GAME(Magnetic);
	FIND_GAME(Quest);
	FIND_GAME(Scott);
	FIND_GAME(ZCode);
#ifndef RELEASE_BUILD
	FIND_GAME(TADS);
#endif

	return PlainGameDescriptor::empty();
}

#undef FIND_GAME

Common::String GlkMetaEngineDetection::findFileByGameId(const Common::String &gameId) {
	// Get the list of files in the folder and return detection against them
	Common::FSNode folder = Common::FSNode(ConfMan.getPath("path"));
	Common::FSList fslist;
	folder.getChildren(fslist, Common::FSNode::kListFilesOnly);

	// Iterate over the files
	for (Common::FSList::iterator i = fslist.begin(); i != fslist.end(); ++i) {
		// Run a detection on each file in the folder individually
		Common::FSList singleList;
		singleList.push_back(*i);
		DetectedGames games = detectGames(singleList);

		// If a detection was found with the correct game Id, we have a winner
		if (!games.empty() && games.front().gameId == gameId)
			return (*i).getName();
	}

	// No match found
	return Common::String();
}

Common::Error GlkMetaEngineDetection::identifyGame(DetectedGame &game, const void **descriptor) {
	*descriptor = nullptr;

	// Populate the game description
	Glk::GlkGameDescription *gameDesc = new Glk::GlkGameDescription;

	gameDesc->_gameId = ConfMan.get("gameid");
	gameDesc->_filename = ConfMan.get("filename");

	gameDesc->_language = Common::UNK_LANG;
	gameDesc->_platform = Common::kPlatformUnknown;
	if (ConfMan.hasKey("language"))
		gameDesc->_language = Common::parseLanguage(ConfMan.get("language"));
	if (ConfMan.hasKey("platform"))
		gameDesc->_platform = Common::parsePlatform(ConfMan.get("platform"));

	// If the game description has no filename, the engine has been launched directly from
	// the command line. Do a scan for supported games for that Id in the game folder
	if (gameDesc->_filename.empty()) {
		gameDesc->_filename = findFileByGameId(gameDesc->_gameId);
		if (gameDesc->_filename.empty()) {
			delete gameDesc;
			return Common::kNoGameDataFoundError;
		}
	}

	// Get the MD5
	Common::File f;
	if (!f.open(Common::FSNode(ConfMan.getPath("path")).getChild(gameDesc->_filename))) {
		delete gameDesc;
		return Common::kNoGameDataFoundError;
	}

	Common::String fileName = f.getName();
	if (fileName.hasSuffixIgnoreCase(".D64"))
		gameDesc->_md5 = Common::computeStreamMD5AsString(f);
	else
		gameDesc->_md5 = Common::computeStreamMD5AsString(f, 5000);
	f.close();

	*descriptor = gameDesc;

	PlainGameDescriptor pdesc(findGame(ConfMan.get("gameid").c_str()));

	if (!pdesc.gameId || !*pdesc.gameId)
		return Common::kUnknownError;

	game = DetectedGame(getName(), pdesc);
	return Common::kNoError;
}

DetectedGames GlkMetaEngineDetection::detectGames(const Common::FSList &fslist, uint32 /*skipADFlags*/, bool /*skipIncomplete*/) {
#ifndef RELEASE_BUILD
	// This is as good a place as any to detect multiple sub-engines using the same Ids
	detectClashes();
#endif

	DetectedGames detectedGames;
	Glk::Adrift::AdriftMetaEngine::detectGames(fslist, detectedGames);
	Glk::AdvSys::AdvSysMetaEngine::detectGames(fslist, detectedGames);
	Glk::AGT::AGTMetaEngine::detectGames(fslist, detectedGames);
	Glk::Alan2::Alan2MetaEngine::detectGames(fslist, detectedGames);
	Glk::Alan3::Alan3MetaEngine::detectGames(fslist, detectedGames);
	Glk::Archetype::ArchetypeMetaEngine::detectGames(fslist, detectedGames);
	Glk::Comprehend::ComprehendMetaEngine::detectGames(fslist, detectedGames);
	Glk::Glulx::GlulxMetaEngine::detectGames(fslist, detectedGames);
	Glk::Hugo::HugoMetaEngine::detectGames(fslist, detectedGames);
	Glk::JACL::JACLMetaEngine::detectGames(fslist, detectedGames);
	Glk::Level9::Level9MetaEngine::detectGames(fslist, detectedGames);
	Glk::Magnetic::MagneticMetaEngine::detectGames(fslist, detectedGames);
	Glk::Quest::QuestMetaEngine::detectGames(fslist, detectedGames);
	Glk::Scott::ScottMetaEngine::detectGames(fslist, detectedGames);
	Glk::ZCode::ZCodeMetaEngine::detectGames(fslist, detectedGames);
#ifndef RELEASE_BUILD
	Glk::TADS::TADSMetaEngine::detectGames(fslist, detectedGames);
#endif

	return detectedGames;
}

void GlkMetaEngineDetection::detectClashes() const {
	Common::StringMap map;
	Glk::Adrift::AdriftMetaEngine::detectClashes(map);
	Glk::AdvSys::AdvSysMetaEngine::detectClashes(map);
	Glk::AGT::AGTMetaEngine::detectClashes(map);
	Glk::Alan2::Alan2MetaEngine::detectClashes(map);
	Glk::Alan3::Alan3MetaEngine::detectClashes(map);
	Glk::Archetype::ArchetypeMetaEngine::detectClashes(map);
	Glk::Comprehend::ComprehendMetaEngine::detectClashes(map);
	Glk::Glulx::GlulxMetaEngine::detectClashes(map);
	Glk::Hugo::HugoMetaEngine::detectClashes(map);
	Glk::JACL::JACLMetaEngine::detectClashes(map);
	Glk::Level9::Level9MetaEngine::detectClashes(map);
	Glk::Magnetic::MagneticMetaEngine::detectClashes(map);
	Glk::Quest::QuestMetaEngine::detectClashes(map);
	Glk::Scott::ScottMetaEngine::detectClashes(map);
	Glk::ZCode::ZCodeMetaEngine::detectClashes(map);
#ifndef RELEASE_BUILD
	Glk::TADS::TADSMetaEngine::detectClashes(map);
#endif
}

uint GlkMetaEngineDetection::getMD5Bytes() const {
	return 5000;
}

REGISTER_PLUGIN_STATIC(GLK_DETECTION, PLUGIN_TYPE_ENGINE_DETECTION, GlkMetaEngineDetection);