File: metaengine.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (691 lines) | stat: -rw-r--r-- 23,018 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/* 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 "engines/advancedDetector.h"
#include "base/plugins.h"
#include "common/config-manager.h"
#include "common/file.h"
#include "common/ptr.h"
#include "common/savefile.h"
#include "common/system.h"
#include "common/translation.h"
#include "graphics/thumbnail.h"
#include "graphics/surface.h"

#include "sci/sci.h"
#include "sci/detection_internal.h"
#include "sci/dialogs.h"
#include "sci/engine/features.h"
#include "sci/engine/guest_additions.h"
#include "sci/engine/kernel.h"
#include "sci/engine/savegame.h"
#include "sci/engine/script.h"
#include "sci/engine/seg_manager.h"
#include "sci/engine/state.h"

namespace Sci {

struct DemoIdEntry {
	const char *demoId;
	const char *scummVMId;
};

static const DemoIdEntry s_demoIdTable[] = {
	{ "icedemo",   "iceman" },
	{ "eco2",      "ecoquest2" },
	{ "hoyledemo", "hoyle1" },
	{ "demo000",   "kq1sci" },
	{ "mm1",       "laurabow" },
	{ "rh",        "longbow" },
	{ "ll1",       "lsl1sci" },
	{ "ll5",       "lsl5" },
	{ "hq",        "qfg1" },	// QFG1 SCI0/EGA
	{ "hq2demo",   "qfg2" },
	{ "demo",      "rama" },
	{ "sq1demo",   "sq1sci" },
	{ nullptr,      nullptr }
};

static bool isSierraDemo(const Common::String &sierraId, uint32 resourceCount) {
	// If the game has less than the expected scripts, it's a demo
	uint32 demoThreshold = 100;
	// ...but there are some exceptions
	if (sierraId == "brain" || sierraId == "lsl1" ||
		sierraId == "mg" || sierraId == "pq" ||
		sierraId == "jones" ||
		sierraId == "cardgames" || sierraId == "solitare" ||
		sierraId == "catdate" ||
		sierraId == "hoyle4")
		demoThreshold = 40;
	if (sierraId == "hoyle3")
		demoThreshold = 45; // cnick-kq has 42 scripts. The actual hoyle 3 demo has 27.
	if (sierraId == "fp" || sierraId == "gk" || sierraId == "pq4" || sierraId == "swat")
		demoThreshold = 150;

	if (sierraId == "torin" && resourceCount) // Torin's Passage demo
		return true;

	// TODO: distinguish the full version of Phantasmagoria from the demo
	// TODO: distinguish the full version of RAMA from the demo
	// TODO: distinguish the full version of SQ6 from the demo

	return resourceCount < demoThreshold;
}

/**
 * Converts the builtin Sierra game object name to the ID we use in ScummVM
 * @param[in] sierraId		The internal `Game` object's name
 * @param[in] sciVersion    The detected SCI version
 * @param[in] resMan		The resource manager
 * @param[in] isDemo        Returns a flag if it's a demo
 * @return					The equivalent ScummVM game id
 */
static Common::String convertSierraGameId(Common::String sierraId, SciVersion sciVersion, ResourceManager &resMan, bool *isDemo) {
	// Convert the id to lower case, so that we match all upper/lower case variants.
	sierraId.toLowercase();

	Common::List<ResourceId> resources = resMan.listResources(kResourceTypeScript, -1);
	const uint32 scriptResourceCount = resources.size();
	*isDemo = isSierraDemo(sierraId, scriptResourceCount);

	if (*isDemo) {
		// Crazy Nick's Picks
		if (sierraId == "lsl1" && scriptResourceCount == 34)
			return "cnick-lsl";
		if (sierraId == "sq4" && scriptResourceCount == 34)
			return "cnick-sq";
		if (sierraId == "hoyle3" && scriptResourceCount == 42)
			return "cnick-kq";
		if (sierraId == "rh budget" && scriptResourceCount == 39)
			return "cnick-longbow";
		// TODO: cnick-laurabow (the name of the game object contains junk)

		// Handle Astrochicken 1 (SQ3) and 2 (SQ4)
		if (sierraId == "sq3" && scriptResourceCount == 20)
			return "astrochicken";
		if (sierraId == "sq4")
			return "msastrochicken";
	}

	for (const GameIdStrToEnum *cur = gameIdStrToEnum; cur->gameidStr; ++cur) {
		if (sierraId == cur->sierraIdStr) {
			// Distinguish same IDs via the SCI version
			if (cur->version != SCI_VERSION_NONE && cur->version != sciVersion)
				continue;

			return cur->gameidStr;
		}
	}

	// Check demo game IDs
	for (const DemoIdEntry *cur = s_demoIdTable; cur->demoId; ++cur) {
		if (sierraId == cur->demoId)
			return cur->scummVMId;
	}

	if (sierraId == "brain") {
		// Distinguish between Castle and Island of Dr. Brain from the SCI version.
		// Island is always SCI1.1, whereas Castle can be either SCI1 mid (Amiga) or
		// late (PC).
		return sciVersion < SCI_VERSION_1_1 ? "castlebrain" : "islandbrain";
	}

	if (sierraId == "glory") {
		// QFG1EGA, QFG1VGA, QFG3 and QFG4 all used the "glory" game ID

		// QFG4 floppy is SCI2, CD SCI2.1
		if (sciVersion >= SCI_VERSION_2)
			return "qfg4";

		// QFG1 EGA is SCI0
		if (sciVersion == SCI_VERSION_0_LATE)
			return "qfg1";

		// QFG1 VGA doesn't have view 1
		if (!resMan.testResource(ResourceId(kResourceTypeView, 1)))
			return "qfg1vga";

		// QFG4 demo has less than 50 scripts
		if (scriptResourceCount < 50)
			return "qfg4demo";

		// Otherwise it's QFG3
		return "qfg3";
	}

	// We don't recognize the game object name; assume it's a fan game.
	return "sci-fanmade";
}

} // End of namespace Sci

namespace Sci {

class SciMetaEngine : public AdvancedMetaEngine<ADGameDescription> {
public:
	const char *getName() const override {
		return "sci";
	}

	Common::Error createInstance(OSystem *syst, Engine **engine, const ADGameDescription *desc) const override;
	bool hasFeature(MetaEngineFeature f) const override;

	SaveStateList listSaves(const char *target) const override;
	int getMaximumSaveSlot() const override;
	bool removeSaveState(const char *target, int slot) const override;
	SaveStateDescriptor querySaveMetaInfos(const char *target, int slot) const override;
	// Disable autosave (see mirrored method in sci.h for detailed explanation)
	int getAutosaveSlot() const override { return -1; }

	// A fallback detection method. This is not ideal as all detection lives in MetaEngine, but
	// here fb detection has many engine dependencies.
	ADDetectedGame fallbackDetectExtern(uint md5Bytes, const FileMap &allFiles, const Common::FSList &fslist, ADDetectedGameExtraInfo **extra) const override;

	void registerDefaultSettings(const Common::String &target) const override;
	GUI::OptionsContainerWidget *buildEngineOptionsWidget(GUI::GuiObject *boss, const Common::String &name, const Common::String &target) const override;
};

Common::Error SciMetaEngine::createInstance(OSystem *syst, Engine **engine, const ADGameDescription *desc) const {
	const GameIdStrToEnum *g = gameIdStrToEnum;
	for (; g->gameidStr; ++g) {
		if (0 == strcmp(desc->gameId, g->gameidStr)) {
#ifndef ENABLE_SCI32
			if (g->isSci32) {
				return Common::Error(Common::kUnsupportedGameidError, _s("SCI32 support not compiled in"));
			}
#endif
			*engine = new SciEngine(syst, desc, g->gameidEnum);

			// If the GUI options were updated, we catch this here and update them in the users config file transparently.
			Common::updateGameGUIOptions(customizeGuiOptions(ConfMan.getPath("path"), desc->guiOptions, desc->platform, g->gameidStr, g->version), getGameGUIOptionsDescriptionLanguage(desc->language));

			return Common::kNoError;
		}
	}

	return Common::kUnsupportedGameidError;
}

bool SciMetaEngine::hasFeature(MetaEngineFeature f) const {
	return
		(f == kSupportsListSaves) ||
		(f == kSupportsLoadingDuringStartup) ||
		(f == kSupportsDeleteSave) ||
		(f == kSavesSupportMetaInfo) ||
		(f == kSavesSupportThumbnail) ||
		(f == kSavesSupportCreationDate) ||
		(f == kSimpleSavesNames) ||
		(f == kSavesSupportPlayTime);
}

bool SciEngine::hasFeature(EngineFeature f) const {
	return
		(f == kSupportsReturnToLauncher) ||
		(f == kSupportsLoadingDuringRuntime) ||
		(f == kSupportsSavingDuringRuntime);
}

SaveStateList SciMetaEngine::listSaves(const char *target) const {
	Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
	Common::StringArray filenames;
	Common::String pattern = target;
	pattern += ".###";

	filenames = saveFileMan->listSavefiles(pattern);

	SaveStateList saveList;
	bool hasAutosave = false;
	for (Common::StringArray::const_iterator file = filenames.begin(); file != filenames.end(); ++file) {
		// Obtain the last 3 digits of the filename, since they correspond to the save slot
		int slotNr = atoi(file->c_str() + file->size() - 3);

		if (slotNr >= 0 && slotNr <= 99) {
			Common::InSaveFile *in = saveFileMan->openForLoading(*file);
			if (in) {
				SavegameMetadata meta;
				if (!get_savegame_metadata(in, meta)) {
					// invalid
					delete in;
					continue;
				}
				SaveStateDescriptor descriptor(this, slotNr, meta.name);

				if (slotNr == 0) {
					// ScummVM auto-save slot (note however, that autosave support is currently revoked)
					hasAutosave = true;
				}

				saveList.push_back(descriptor);
				delete in;
			}
		}
	}

	if (!hasAutosave) {
		SaveStateDescriptor descriptor(this, 0, _("(Autosave)"));
		descriptor.setLocked(true);
		saveList.push_back(descriptor);
	}

	// Sort saves based on slot number.
	Common::sort(saveList.begin(), saveList.end(), SaveStateDescriptorSlotComparator());
	return saveList;
}

SaveStateDescriptor SciMetaEngine::querySaveMetaInfos(const char *target, int slotNr) const {
	const Common::String fileName = Common::String::format("%s.%03d", target, slotNr);
	Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(fileName);
	SaveStateDescriptor descriptor(this, slotNr, "");

	if (in) {
		SavegameMetadata meta;

		if (!get_savegame_metadata(in, meta)) {
			// invalid
			delete in;

			descriptor.setDescription("*Invalid*");
			return descriptor;
		}

		descriptor.setDescription(meta.name);

		Graphics::Surface *thumbnail;
		if (!Graphics::loadThumbnail(*in, thumbnail)) {
			// invalid
			delete in;

			descriptor.setDescription("*Invalid*");
			return descriptor;
		}
		descriptor.setThumbnail(thumbnail);

		const int day = (meta.saveDate >> 24) & 0xFF;
		const int month = (meta.saveDate >> 16) & 0xFF;
		const int year = meta.saveDate & 0xFFFF;

		descriptor.setSaveDate(year, month, day);

		const int hour = (meta.saveTime >> 16) & 0xFF;
		const int minutes = (meta.saveTime >> 8) & 0xFF;

		descriptor.setSaveTime(hour, minutes);

		if (meta.version >= 34) {
			descriptor.setPlayTime(meta.playTime * 1000 / 60);
		} else {
			descriptor.setPlayTime(meta.playTime * 1000);
		}

		delete in;

		return descriptor;
	}
	// Return empty descriptor
	return descriptor;
}

int SciMetaEngine::getMaximumSaveSlot() const { return 99; }

bool SciMetaEngine::removeSaveState(const char *target, int slot) const {
	Common::String fileName = Common::String::format("%s.%03d", target, slot);
	return g_system->getSavefileManager()->removeSavefile(fileName);
}

Common::Error SciEngine::loadGameState(int slot) {
	_gamestate->_delayedRestoreGameId = slot;
	return Common::kNoError;
}

Common::Error SciEngine::saveGameState(int slot, const Common::String &desc, bool isAutosave) {
	const char *version = "";
	_soundCmd->pauseAll(false); // unpause music (we can't have it paused during save)
	const bool res = gamestate_save(_gamestate, slot, desc, version);
	_soundCmd->pauseAll(true); // pause music
	return res ? Common::kNoError : Common::kWritingFailed;
}

bool SciEngine::canLoadGameStateCurrently(Common::U32String *msg) {
#ifdef ENABLE_SCI32
	const Common::String &guiOptions = ConfMan.get("guioptions");
	if (getSciVersion() >= SCI_VERSION_2) {
		if (ConfMan.getBool("originalsaveload") ||
			Common::checkGameGUIOption(GUIO_NOLAUNCHLOAD, guiOptions)) {

			return false;
		}
	}
#endif

	return !_gamestate->executionStackBase;
}

bool SciEngine::canSaveGameStateCurrently(Common::U32String *msg) {
	return
		_features->canSaveFromGMM() &&
		!_gamestate->executionStackBase &&
		_guestAdditions->userHasControl();
}

} // End of namespace Sci

/**
 * External fallback detection-related code.
 */
namespace Sci {

Common::Language charToScummVMLanguage(const char c) {
	switch (c) {
	case 'F':
		return Common::FR_FRA;
	case 'S':
		return Common::ES_ESP;
	case 'I':
		return Common::IT_ITA;
	case 'G':
		return Common::DE_DEU;
	case 'J':
	case 'j':
		return Common::JA_JPN;
	case 'P':
		return Common::PT_BRA;
	default:
		return Common::UNK_LANG;
	}
}

Common::Language sciToScummVMLanguage(const int sciLanguage) {
	switch (sciLanguage) {
	case K_LANG_ENGLISH:    return Common::EN_ANY;
	case K_LANG_FRENCH:     return Common::FR_FRA;
	case K_LANG_SPANISH:    return Common::ES_ESP;
	case K_LANG_ITALIAN:    return Common::IT_ITA;
	case K_LANG_GERMAN:     return Common::DE_DEU;
	case K_LANG_JAPANESE:   return Common::JA_JPN;
	case K_LANG_PORTUGUESE: return Common::PT_BRA;
	default:                return Common::UNK_LANG;
	}
}

static char s_fallbackGameIdBuf[256];

/**
 * The fallback game descriptor used by the SCI engine's fallbackDetector.
 * Contents of this struct are overwritten by the fallbackDetector.
 */
static ADGameDescription s_fallbackDesc = {
	"",
	"",
	AD_ENTRY1(nullptr, nullptr), // This should always be AD_ENTRY1(0, 0) in the fallback descriptor
	Common::UNK_LANG,
	Common::kPlatformDOS,
	ADGF_NO_FLAGS,
	GUIO3(GAMEOPTION_PREFER_DIGITAL_SFX, GAMEOPTION_ORIGINAL_SAVELOAD, GAMEOPTION_MIDI_MODE)
};

Common::Platform getSciFallbackDetectionPlatform(const AdvancedMetaEngineBase::FileMap &allFiles) {
	// Data1 contains both map and volume for SCI1.1+ Mac games
	if (allFiles.contains("Data1"))
		return Common::kPlatformMacintosh;

	// Determine the game platform
	// The existence of any of these files indicates an Amiga game
	if (allFiles.contains("9.pat") || allFiles.contains("spal") ||
		allFiles.contains("patch.005") || allFiles.contains("bank.001"))
		return Common::kPlatformAmiga;

	// The existence of 7.pat or patch.200 indicates a Mac game
	if (allFiles.contains("7.pat") || allFiles.contains("patch.200"))
		return Common::kPlatformMacintosh;

	// The data files for Atari ST versions are the same as their DOS counterparts

	return Common::kPlatformDOS;
}

bool necessarySciResourceFilesFound(const AdvancedMetaEngineBase::FileMap &allFiles) {
	bool foundResMap = false;
	bool foundRes000 = false;

	if (
		allFiles.contains("resource.map") ||
		allFiles.contains("resmap.000") ||
		allFiles.contains("resmap.001") ||
		// Data1 contains both map and volume for SCI1.1+ Mac games
		allFiles.contains("Data1")
	)
		foundResMap = true;

	if (
		allFiles.contains("resource.000") ||
		allFiles.contains("resource.001") ||
		allFiles.contains("ressci.000") ||
		allFiles.contains("ressci.001") ||
		// Data1 contains both map and volume for SCI1.1+ Mac games
		allFiles.contains("Data1")
	)
		foundRes000 = true;

	return foundResMap && foundRes000;
}

bool isSciCDVersion(const AdvancedMetaEngineBase::FileMap &allFiles) {
	// Determine if we got a CD version and set the CD flag accordingly, by checking for
	// resource.aud for SCI1.1 CD games, or audio001.002 for SCI1 CD games. We assume that
	// the file should be over 10MB, as it contains all the game speech and is usually
	// around 450MB+. The size check is for some floppy game versions like KQ6 floppy, which
	// also have a small resource.aud file
	if (allFiles.contains("resource.aud") || allFiles.contains("resaud.001") || allFiles.contains("audio001.002")) {
		Common::FSNode file = allFiles.contains("resource.aud") ? allFiles["resource.aud"] : (allFiles.contains("resaud.001") ? allFiles["resaud.001"] : allFiles["audio001.002"]);
		Common::SeekableReadStream *tmpStream = file.createReadStream();
		const int32 size = tmpStream->size();
		delete tmpStream;

		return size > 10 * 1024 * 1024;
	}

	return false;
}

void constructFallbackDetectionEntry(const Common::String &gameId, Common::Platform platform, SciVersion sciVersion, Common::Language language, bool hasEgaViews, bool isCD, bool isDemo) {
	Common::strlcpy(s_fallbackGameIdBuf, gameId.c_str(), sizeof(s_fallbackGameIdBuf));

	s_fallbackDesc.extra = "";
	s_fallbackDesc.language = language;
	s_fallbackDesc.flags = !isCD ? ADGF_NO_FLAGS : ADGF_CD;
	s_fallbackDesc.platform = platform;
	s_fallbackDesc.gameId = s_fallbackGameIdBuf;

	if (isDemo)
		s_fallbackDesc.flags |= ADGF_DEMO;

	if (isCD) {
		s_fallbackDesc.guiOptions = GUIO3(GAMEOPTION_PREFER_DIGITAL_SFX, GAMEOPTION_ORIGINAL_SAVELOAD, GAMEOPTION_MIDI_MODE);
	} else {
		s_fallbackDesc.guiOptions = GUIO4(GUIO_NOSPEECH, GAMEOPTION_PREFER_DIGITAL_SFX, GAMEOPTION_ORIGINAL_SAVELOAD, GAMEOPTION_MIDI_MODE);
	}

	// Is this an EGA version that might have a VGA pendant? Then we want
	// to mark it as such in the "extra" field.
	const bool markAsEGA = (
		hasEgaViews &&
		s_fallbackDesc.platform != Common::kPlatformAmiga &&
		sciVersion > SCI_VERSION_1_EGA_ONLY
	);

	// Fill in "extra" field
	if (gameId.hasSuffix("sci")) {
		s_fallbackDesc.extra = "SCI";

		// Differentiate EGA versions from the VGA ones, where needed
		if (markAsEGA)
			s_fallbackDesc.extra = "SCI/EGA";

		// Mark as demo.
		// Note: This overwrites the 'EGA' info, if it was previously set.
		if (isDemo)
			s_fallbackDesc.extra = "SCI/Demo";
	} else {
		if (markAsEGA)
			s_fallbackDesc.extra = "EGA";

		// Set "CD" and "Demo" as appropriate.
		// Note: This overwrites the 'EGA' info, if it was previously set.
		if (isDemo && isCD)
			s_fallbackDesc.extra = "CD Demo";
		else if (isDemo)
			s_fallbackDesc.extra = "Demo";
		else if (isCD)
			s_fallbackDesc.extra = "CD";
	}
}

ADDetectedGame SciMetaEngine::fallbackDetectExtern(uint md5Bytes, const FileMap &allFiles, const Common::FSList &fslist, ADDetectedGameExtraInfo **extra) const {
	// If necessary SCI files aren't found, it can't be SCI
	if (!necessarySciResourceFilesFound(allFiles))
		return ADDetectedGame();

	const bool isCD = isSciCDVersion(allFiles);
	Common::Platform platform = getSciFallbackDetectionPlatform(allFiles);

	ResourceManager resMan(true);
	resMan.addAppropriateSourcesForDetection(fslist);
	resMan.init();
	// TODO: Add error handling.
	const SciVersion sciVersion = getSciVersionForDetection();

#ifndef ENABLE_SCI32
	// Is SCI32 compiled in? If not, and this is a SCI32 game,
	// stop here
	if (sciVersion >= SCI_VERSION_2)
		return ADDetectedGame();
#endif

	const ViewType gameViews = resMan.getViewType();

	// Have we identified the game views? If not, stop here
	// Can't be SCI (or unsupported SCI views). Pinball Creep by Sierra also uses resource.map/resource.000 files
	// but doesn't share SCI format at all
	if (gameViews == kViewUnknown)
		return ADDetectedGame();

	// Set the platform to Amiga if the game is using Amiga views
	if (gameViews == kViewAmiga)
		platform = Common::kPlatformAmiga;

	// Determine the game id
	const Common::String sierraGameId = resMan.findSierraGameId();

	// If we don't have a game id, the game is not SCI
	if (sierraGameId.empty())
		return ADDetectedGame();

	bool isDemo = false;
	Common::String gameId = convertSierraGameId(sierraGameId, sciVersion, resMan, &isDemo);
	Common::Language language = Common::EN_ANY;

	// Try to determine the game language
	// Load up text 0 and start looking for "#" characters
	// Non-English versions contain strings like XXXX#YZZZZ
	// Where XXXX is the English string, #Y a separator indicating the language
	// (e.g. #G for German) and ZZZZ is the translated text
	// NOTE: This doesn't work for games which use message instead of text resources
	// (like, for example, Eco Quest 1 and all SCI1.1 games and newer, e.g. Freddy Pharkas).
	// As far as we know, these games store the messages of each language in separate
	// resources, and it's not possible to detect that easily
	// Also look for "%J" which is used in japanese games
	Resource *text = resMan.findResource(ResourceId(kResourceTypeText, 0), false);
	if (text) {
		uint seeker = 0;
		while (seeker < text->size()) {
			if (text->getUint8At(seeker) == '#')  {
				if (seeker + 1 < text->size())
					language = charToScummVMLanguage(text->getUint8At(seeker + 1));
				break;
			}
			if (text->getUint8At(seeker) == '%') {
				if ((seeker + 1 < text->size()) && (text->getUint8At(seeker + 1) == 'J')) {
					language = charToScummVMLanguage(text->getUint8At(seeker + 1));
					break;
				}
			}
			seeker++;
		}
	}

	// Try to determine the game language from config file (SCI1.1 and later)
	const char *configNames[] = { "resource.cfg", "resource.win" };
	for (int i = 0; i < ARRAYSIZE(configNames) && language == Common::EN_ANY; i++) {
		Common::File file;
		if (allFiles.contains(configNames[i]) && file.open(allFiles[configNames[i]])) {
			while (!file.eos()) {
				Common::String line = file.readLine();
				uint32 separatorPos = line.find('=');
				if (separatorPos == Common::String::npos) {
					continue;
				}
				Common::String key = line.substr(0, separatorPos);
				key.trim();
				if (key.equalsIgnoreCase("language")) {
					Common::String val = line.substr(separatorPos + 1);
					val.trim();
					Common::Language parsedLanguage = sciToScummVMLanguage(atoi(val.c_str()));
					if (parsedLanguage != Common::UNK_LANG) {
						language = parsedLanguage;
					}
					break;
				}
			}
		}
	}

	constructFallbackDetectionEntry(gameId, platform, sciVersion, language, gameViews == kViewEga, isCD, isDemo);

	return ADDetectedGame(&s_fallbackDesc);
}

void SciMetaEngine::registerDefaultSettings(const Common::String &target) const {
	for (const ADExtraGuiOptionsMap *entry = optionsList; entry->guioFlag; ++entry)
		ConfMan.registerDefault(entry->option.configOption, entry->option.defaultState);

	for (const PopUpOptionsMap *entry = popUpOptionsList; entry->guioFlag; ++entry)
		ConfMan.registerDefault(entry->configOption, entry->defaultState);

	// enable_high_resolution_graphics is normally enabled by default,
	// except for KQ6 where it overrides the DOS platform with Windows.
	// If it were enabled by default for KQ6, then the DOS platform
	// would produce the Windows experience by default instead of DOS.
	if (ConfMan.get("gameid", target) == "kq6" && ConfMan.get("platform", target) == "pc") {
		ConfMan.registerDefault("enable_high_resolution_graphics", false);
	}
}

GUI::OptionsContainerWidget *SciMetaEngine::buildEngineOptionsWidget(GUI::GuiObject *boss, const Common::String &name, const Common::String &target) const {
	return new OptionsWidget(boss, name, target);
}

} // End of namespace Sci

#if PLUGIN_ENABLED_DYNAMIC(SCI)
	REGISTER_PLUGIN_DYNAMIC(SCI, PLUGIN_TYPE_ENGINE, Sci::SciMetaEngine);
#else
	REGISTER_PLUGIN_STATIC(SCI, PLUGIN_TYPE_ENGINE, Sci::SciMetaEngine);
#endif