File: metaengine.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 (964 lines) | stat: -rw-r--r-- 30,855 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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
/* 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/obsolete.h"

#include "common/savefile.h"
#include "common/config-manager.h"
#include "common/translation.h"
#include "common/md5.h"

#include "gui/dialog.h"
#include "gui/message.h"

#include "audio/mididrv.h"

#include "backends/keymapper/action.h"
#include "backends/keymapper/keymap.h"
#include "backends/keymapper/standard-actions.h"

#include "scumm/he/intern_he.h"
#include "scumm/scumm_v0.h"
#include "scumm/scumm_v8.h"
#include "scumm/dialogs.h"
#include "scumm/resource.h"

// Files related for detection.
#include "scumm/metaengine.h"
#include "scumm/detection.h"
#include "scumm/detection_tables.h"
#include "scumm/file.h"
#include "scumm/file_nes.h"

namespace Scumm {

Common::Path ScummEngine::generateFilename(const int room) const {
	const int diskNumber = (room > 0) ? _res->_types[rtRoom][room]._roomno : 0;
	Common::String result;

	if (_game.version == 4) {
		if (room == 0 || room >= 900) {
			result = Common::String::format("%03d.lfl", room);
		} else {
			result = Common::String::format("disk%02d.lec", diskNumber);
		}
	} else {
		switch (_filenamePattern.genMethod) {
		case kGenDiskNum:
		case kGenDiskNumSteam:
			result = Common::String::format(_filenamePattern.pattern, diskNumber);
			break;

		case kGenRoomNum:
		case kGenRoomNumSteam:
			result = Common::String::format(_filenamePattern.pattern, room);
			break;

		case kGenUnchanged:
			result = _filenamePattern.pattern;
			break;

		default:
			error("generateFilename: Unsupported genMethod");
		}
	}

	return Common::Path(result, Common::Path::kNoSeparator);
}

Common::Path ScummEngine_v60he::generateFilename(const int room) const {
	Common::String result;
	char id;

	switch (_filenamePattern.genMethod) {
	case kGenHEMac:
	case kGenHEMacNoParens:
	case kGenHEPC:
		if (room < 0) {
			id = '0' - room;
		} else {
			const int diskNumber = (room > 0) ? _res->_types[rtRoom][room]._roomno : 0;
			id = diskNumber + '0';
		}

		if (_filenamePattern.genMethod == kGenHEPC) {
			result = Common::String::format("%s.he%c", _filenamePattern.pattern, id);
		} else {
			if (id == '3') { // Special case for cursors.
				// For mac they're stored in game binary.
				result = _filenamePattern.pattern;
			} else {
				if (_filenamePattern.genMethod == kGenHEMac)
					result = Common::String::format("%s (%c)", _filenamePattern.pattern, id);
				else
					result = Common::String::format("%s %c", _filenamePattern.pattern, id);
			}
		}

		break;

	default:
		// Fallback to parent method.
		return ScummEngine::generateFilename(room);
	}

	return Common::Path(result, Common::Path::kNoSeparator);
}

Common::Path ScummEngine_v70he::generateFilename(const int room) const {
	Common::String result;
	char id = 0;

	Common::String bPattern = _filenamePattern.pattern;

	// Special cases for Blue's games, which share common (b) files.
	if (_game.id == GID_BIRTHDAYYELLOW || _game.id == GID_BIRTHDAYRED)
		bPattern = "Blue'sBirthday";
	else if (_game.id == GID_TREASUREHUNT)
		bPattern = "Blue'sTreasureHunt";

	switch (_filenamePattern.genMethod) {
	case kGenHEMac:
	case kGenHEMacNoParens:
	case kGenHEPC:
	case kGenHEIOS:
		if (_game.heversion >= 98 && room >= 0) {
			int disk = 0;
			if (_heV7DiskOffsets)
				disk = _heV7DiskOffsets[room];

			switch (disk) {
			case 2:
				id = 'b';
				result = bPattern + ".(b)";
				break;
			case 1:
				id = 'a';
				// Some of the newer HE games for iOS use the ".hea" suffix instead.
				if (_filenamePattern.genMethod == kGenHEIOS)
					result = Common::String::format("%s.hea", _filenamePattern.pattern);
				else
					result = Common::String::format("%s.(a)", _filenamePattern.pattern);
				break;
			default:
				id = '0';
				result = Common::String::format("%s.he0", _filenamePattern.pattern);
			}
		} else if (room < 0) {
			id = '0' - room;
		} else {
			id = (room == 0) ? '0' : '1';
		}

		if (_filenamePattern.genMethod == kGenHEPC || _filenamePattern.genMethod == kGenHEIOS) {
			if (id == '3' && _game.id == GID_MOONBASE) {
				result = Common::String::format("%s.u32", _filenamePattern.pattern);
				break;
			}

			// For HE >= 98, we already called snprintf above.
			if (_game.heversion < 98 || room < 0)
				result = Common::String::format("%s.he%c", _filenamePattern.pattern, id);
		} else {
			if (id == '3') { // Special case for cursors.
				// For mac they're stored in game binary.
				result = _filenamePattern.pattern;
			} else {
				Common::String pattern = id == 'b' ? Common::move(bPattern) : _filenamePattern.pattern;
				if (_filenamePattern.genMethod == kGenHEMac)
					result = Common::String::format("%s (%c)", pattern.c_str(), id);
				else
					result = Common::String::format("%s %c", pattern.c_str(), id);
			}
		}

		break;

	default:
		// Fallback to parent method.
		return ScummEngine_v60he::generateFilename(room);
	}

	return Common::Path(result, Common::Path::kNoSeparator);
}

bool ScummEngine::isMacM68kIMuse() const {
	return _game.platform == Common::kPlatformMacintosh && (_game.id == GID_MONKEY2 || _game.id == GID_INDY4) && !(_game.features & GF_MAC_CONTAINER);
}

} // End of namespace Scumm

#pragma mark -
#pragma mark --- Detection code ---
#pragma mark -


// Various methods to help in core detection.
#include "scumm/detection_internal.h"


#pragma mark -
#pragma mark --- Plugin code ---
#pragma mark -


using namespace Scumm;

const char *ScummMetaEngine::getName() const {
	return "scumm";
}

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

bool ScummEngine::hasFeature(EngineFeature f) const {
	return
		(f == kSupportsReturnToLauncher) ||
		(f == kSupportsLoadingDuringRuntime) ||
		(f == kSupportsSavingDuringRuntime) ||
		(f == kSupportsSubtitleOptions) ||
		(f == kSupportsHelp) ||
		(
			f == kSupportsChangingOptionsDuringRuntime &&
			(Common::String(_game.guioptions).contains(GAMEOPTION_AUDIO_OVERRIDE) ||
			 Common::String(_game.guioptions).contains(GAMEOPTION_NETWORK))
		) ||
		(f == kSupportsQuitDialogOverride && (gameSupportsQuitDialogOverride() || !ChainedGamesMan.empty()));
}

bool ScummEngine::gameSupportsQuitDialogOverride() const {
	bool supportsOverride = isUsingOriginalGUI();

	supportsOverride &= !(_game.platform == Common::kPlatformNES);
	supportsOverride &= !(_game.platform == Common::kPlatformSegaCD);

	return supportsOverride;
}


/**
 * Create a ScummEngine instance, based on the given detector data.
 *
 * This is heavily based on our MD5 detection scheme.
 */
Common::Error ScummMetaEngine::createInstance(OSystem *syst, Engine **engine,
	const DetectedGame &gameDescriptor, const void *metaEngineDescriptor) {
	assert(syst);
	assert(engine);
	const char *gameid = ConfMan.get("gameid").c_str();

	// We start by checking whether the specified game ID is obsolete.
	// If that is the case, we automatically upgrade the target to use
	// the correct new game ID (and platform, if specified).
	Engines::upgradeTargetIfNecessary(obsoleteGameIDsTable);

	// Fetch the list of files in the current directory.
	Common::FSList fslist;
	Common::FSNode dir(ConfMan.getPath("path"));
	if (!dir.isDirectory())
		return Common::kPathNotDirectory;
	if (!dir.getChildren(fslist, Common::FSNode::kListAll))
		return Common::kNoGameDataFoundError;

	// Invoke the detector, but fixed to the specified gameid.
	Common::List<DetectorResult> results;
	::detectGames(fslist, results, gameid);

	// Unable to locate game data.
	if (results.empty())
		return Common::kNoGameDataFoundError;

	// No unique match found. If a platform override is present, try to
	// narrow down the list a bit more.
	if (results.size() > 1 && ConfMan.hasKey("platform")) {
		Common::Platform platform = Common::parsePlatform(ConfMan.get("platform"));
		Common::List<DetectorResult> tmp;

		// Copy only those candidates which match the platform setting.
		for (Common::List<DetectorResult>::iterator
		          x = results.begin(); x != results.end(); ++x) {
			if (x->game.platform == platform) {
				tmp.push_back(*x);
			}
		}

		// If we narrowed it down too much, print a warning, else use the list
		// we just computed as new candidates list.
		if (tmp.empty()) {
			warning("Engine_SCUMM_create: Game data inconsistent with platform override");
		} else {
			results = tmp;
		}
	}

	// Still no unique match found. Narrow down again excluding demos using extra
	// metadata
	if (results.size() > 1 && ConfMan.hasKey("extra")) {
		Common::String extra_cfg = ConfMan.get("extra");
		Common::List<DetectorResult> tmp;

		for (Common::List<DetectorResult>::iterator
				  x = results.begin(); x != results.end(); ++x) {

			// FIXME: there's no demo flag in confman, this is not 100% reliable
			if (Common::String(x->extra).contains("Demo") && extra_cfg.contains("Demo"))
				tmp.push_back(*x);
		}

		// If we narrowed it down too much, print a warning, else use the list
		// we just computed as new candidates list.
		if (tmp.empty()) {
			warning("Engine_SCUMM_create: Game data inconsistent with extra metadata settings");
		} else {
			results = tmp;
		}
	}

	// Still no unique match found -> print a warning.
	if (results.size() > 1)
		warning("Engine_SCUMM_create: No unique game candidate found, using first one");

	// Simply use the first match.
	DetectorResult res(*(results.begin()));
	debug(1, "Using gameid %s, variant %s, extra %s", res.game.gameid, res.game.variant, res.extra);
	debug(1, "  SCUMM version %d, HE version %d", res.game.version, res.game.heversion);

	// Print the MD5 of the game; either verbose using printf, in case of an
	// unknown MD5, or with a medium debug level in case of a known MD5 (for
	// debugging purposes).
	if (!findInMD5Table(res.md5.c_str())) {
		Common::String md5Warning;

		md5Warning = ("Your game version appears to be unknown. Please, report the following data to the\n"
		               "ScummVM team along with the name of the game you tried to add and its version,\n"
					   "language, etc.:\n");

		md5Warning += Common::String::format("  SCUMM gameid '%s', file '%s', MD5 '%s'\n\n",
				res.game.gameid,
				generateFilenameForDetection(res.fp.pattern, res.fp.genMethod, res.game.platform).c_str(),
				res.md5.c_str());

		g_system->logMessage(LogMessageType::kWarning, md5Warning.c_str());
	} else {
		debug(1, "Using MD5 '%s'", res.md5.c_str());
	}

	// We don't support the "Lite" version off puttzoo iOS because it contains
	// the full game.
	if (!strcmp(res.game.gameid, "puttzoo") && !strcmp(res.extra, "Lite")) {
		GUIErrorMessage(_("The Lite version of Putt-Putt Saves the Zoo iOS is not supported to avoid piracy.\n"
		                  "The full version is available for purchase from the iTunes Store."));
		return Common::kUnsupportedGameidError;
	}

	if (res.game.heversion != 0 && (res.extra && !strcmp(res.extra, "Steam"))) {
		if (!strcmp(res.game.gameid, "baseball") ||
			!strcmp(res.game.gameid, "soccer") ||
			!strcmp(res.game.gameid, "baseball2001") ||
			!strcmp(res.game.gameid, "basketball") ||
			!strcmp(res.game.gameid, "football")) {
			GUI::MessageDialog dialog(_("Warning: this re-release version contains patched game scripts,\n"
										"and therefore it might crash or not work properly for the time being."));
			dialog.runModal();
		}
	}

	// If the GUI options were updated, we catch this here and update them in the users config
	// file transparently.
	Common::updateGameGUIOptions(customizeGuiOptions(res), getGameGUIOptionsDescriptionLanguage(res.language));

	// If the game was added really long ago, it may be missing its "extra"
	// field. When adding game-specific options, it may be our only way of
	// telling certain versions apart, so make sure it's updated.
	if (res.game.variant && res.game.variant[0] && !ConfMan.hasKey("extra"))
		ConfMan.setAndFlush("extra", res.game.variant);

	// Check for a user override of the platform. We allow the user to override
	// the platform, to make it possible to add games which are not yet in
	// our MD5 database but require a specific platform setting.
	// TODO: Do we really still need/want the platform override?
	if (ConfMan.hasKey("platform"))
		res.game.platform = Common::parsePlatform(ConfMan.get("platform"));

	// Language override.
	if (ConfMan.hasKey("language"))
		res.language = Common::parseLanguage(ConfMan.get("language"));

	// V3 FM-TOWNS games *always* should use the corresponding music driver,
	// anything else makes no sense for them.
	// TODO: Maybe allow the null driver, too?
	if (res.game.platform == Common::kPlatformFMTowns && res.game.version == 3)
		res.game.midi = MDT_TOWNS;
	// Finally, we have massaged the GameDescriptor to our satisfaction, and can
	// instantiate the appropriate game engine. Hooray!
	switch (res.game.version) {
	case 0:
		*engine = new ScummEngine_v0(syst, res);
		break;
	case 1:
	case 2:
		*engine = new ScummEngine_v2(syst, res);
		break;
	case 3:
		if (res.game.features & GF_OLD256)
			*engine = new ScummEngine_v3(syst, res);
		else
			*engine = new ScummEngine_v3old(syst, res);
		break;
	case 4:
		*engine = new ScummEngine_v4(syst, res);
		break;
	case 5:
		*engine = new ScummEngine_v5(syst, res);
		break;
	case 6:
		switch (res.game.heversion) {
#ifdef ENABLE_HE
		case 200:
			*engine = new ScummEngine_vCUPhe(syst, res);
			break;
		case 101:
		case 100:
			*engine = new ScummEngine_v100he(syst, res);
			break;
		case 99:
			*engine = new ScummEngine_v99he(syst, res);
			break;
		case 98:
		case 95:
			*engine = new ScummEngine_v95he(syst, res);
			break;
		case 90:
			*engine = new ScummEngine_v90he(syst, res);
			break;
		case 85:
		case 80:
			*engine = new ScummEngine_v80he(syst, res);
			break;
		case 74:
		case 73:
		case 72:
			*engine = new ScummEngine_v72he(syst, res);
			break;
		case 71:
			*engine = new ScummEngine_v71he(syst, res);
			break;
#else
		case 200:
		case 101:
		case 100:
		case 99:
		case 98:
		case 95:
		case 90:
		case 85:
		case 80:
		case 74:
		case 73:
		case 72:
		case 71:
			return Common::Error(Common::kUnsupportedGameidError, _s("HE v71+ support is not compiled in"));
#endif
		case 70:
			*engine = new ScummEngine_v70he(syst, res);
			break;
		case 62:
		case 61:
		case 60:
			*engine = new ScummEngine_v60he(syst, res);
			break;
		default:
			*engine = new ScummEngine_v6(syst, res);
		}
		break;
#ifdef ENABLE_SCUMM_7_8
	case 7:
		*engine = new ScummEngine_v7(syst, res);
		break;
	case 8:
		*engine = new ScummEngine_v8(syst, res);
		break;
#else
	case 7:
	case 8:
		return Common::Error(Common::kUnsupportedGameidError, _s("SCUMM v7-8 support is not compiled in"));
#endif
	default:
		return Common::kUnsupportedGameidError;
	}

	return Common::kNoError;
}

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

namespace Scumm {
bool getSavegameName(Common::InSaveFile *in, Common::String &desc, int heversion);
} // End of namespace Scumm

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

	filenames = saveFileMan->listSavefiles(pattern);

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

		if (slotNum >= 0 && slotNum <= 99) {
			Common::InSaveFile *in = saveFileMan->openForLoading(*file);
			if (in) {
				Scumm::getSavegameName(in, saveDesc, 0);	// FIXME: heversion?!?
				saveList.push_back(SaveStateDescriptor(this, slotNum, saveDesc));
				delete in;
			}
		}
	}

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

bool ScummMetaEngine::removeSaveState(const char *target, int slot) const {
	Common::String filename = ScummEngine::makeSavegameName(target, slot, false);
	return g_system->getSavefileManager()->removeSavefile(filename);
}

SaveStateDescriptor ScummMetaEngine::querySaveMetaInfos(const char *target, int slot) const {
	Common::String saveDesc;
	Graphics::Surface *thumbnail = nullptr;
	SaveStateMetaInfos infos;
	memset(&infos, 0, sizeof(infos));
	SaveStateMetaInfos *infoPtr = &infos;

	// FIXME: heversion?!?
	if (!ScummEngine::querySaveMetaInfos(target, slot, 0, saveDesc, thumbnail, infoPtr)) {
		return SaveStateDescriptor();
	}

	SaveStateDescriptor desc(this, slot, saveDesc);
	desc.setThumbnail(thumbnail);

	if (infoPtr) {
		int day = (infos.date >> 24) & 0xFF;
		int month = (infos.date >> 16) & 0xFF;
		int year = infos.date & 0xFFFF;

		desc.setSaveDate(year, month, day);

		int hour = (infos.time >> 8) & 0xFF;
		int minutes = infos.time & 0xFF;

		desc.setSaveTime(hour, minutes);
		desc.setPlayTime(infos.playtime * 1000);
	}

	return desc;
}

GUI::OptionsContainerWidget *ScummMetaEngine::buildLoomOptionsWidget(GUI::GuiObject *boss, const Common::String &name, const Common::String &target) const {
	Common::Platform platform = Common::parsePlatform(ConfMan.get("platform", target));
	if (platform != Common::kPlatformUnknown && platform != Common::kPlatformDOS && platform != Common::kPlatformMacintosh)
		return nullptr;

	Common::String extra = ConfMan.get("extra", target);

	// The VGA Loom settings are only relevant for the DOS CD version, not
	// the Steam version (which is assumed to be well timed already).

	if (extra == "VGA")
		return new Scumm::LoomVgaGameOptionsWidget(boss, name, target);

	if (extra == "Steam" && platform != Common::kPlatformMacintosh)
		return MetaEngine::buildEngineOptionsWidget(boss, name, target);
	else if (extra == "Steam" && platform == Common::kPlatformMacintosh)
		return nullptr;
	else if (platform == Common::kPlatformMacintosh)
		return new Scumm::LoomMonkeyMacGameOptionsWidget(boss, name, target, GID_LOOM);

	// These EGA Loom settings are only relevant for the EGA
	// version, since that is the only one that has an overture.
	return new Scumm::LoomEgaGameOptionsWidget(boss, name, target);
}

GUI::OptionsContainerWidget *ScummMetaEngine::buildMI1OptionsWidget(GUI::GuiObject *boss, const Common::String &name, const Common::String &target) const {
	Common::String extra = ConfMan.get("extra", target);
	Common::Platform platform = Common::parsePlatform(ConfMan.get("platform", target));

	if (platform == Common::kPlatformMacintosh && extra != "Steam")
		return new Scumm::LoomMonkeyMacGameOptionsWidget(boss, name, target, GID_MONKEY);

	if (extra != "CD" && extra != "FM-TOWNS" && extra != "SEGA")
		return nullptr;

	return new Scumm::MI1CdGameOptionsWidget(boss, name, target);
}


GUI::OptionsContainerWidget *ScummMetaEngine::buildEngineOptionsWidget(GUI::GuiObject *boss, const Common::String &name, const Common::String &target) const {
	Common::String gameid = ConfMan.get("gameid", target);
	Common::String extra = ConfMan.get("extra", target);

	if (gameid == "loom") {
		GUI::OptionsContainerWidget *widget = buildLoomOptionsWidget(boss, name, target);
		if (widget)
			return widget;
	} else if (gameid == "monkey") {
		GUI::OptionsContainerWidget *widget = buildMI1OptionsWidget(boss, name, target);
		if (widget)
			return widget;
	}
#ifdef USE_ENET
	else if (gameid == "football" || gameid == "baseball2001" || gameid == "football2002" ||
		gameid == "moonbase")
		return new Scumm::HENetworkGameOptionsWidget(boss, name, target, Common::move(gameid));
#endif

	const ExtraGuiOptions engineOptions = getExtraGuiOptions(target);

	if (!engineOptions.empty())
		return new Scumm::ScummGameOptionsWidget(boss, name, target, engineOptions);

	return MetaEngine::buildEngineOptionsWidget(boss, name, target);
}

static const ExtraGuiOption comiObjectLabelsOption = {
	_s("Show Object Line"),
	_s("Show the names of objects at the bottom of the screen"),
	"object_labels",
	true,
	0,
	0
};

static const ExtraGuiOption mmnesClassicPaletteOption = {
	_s("Use NES Classic Palette"),
	_s("Use a more neutral color palette that closely emulates the NES Classic"),
	"mm_nes_classic_palette",
	false,
	0,
	0
};

static const ExtraGuiOption fmtownsTrimTo200 = {
	_s("Trim FM-TOWNS games to 200 pixels height"),
	_s("Cut the extra 40 pixels at the bottom of the screen, to make it standard 200 pixels height, allowing using 'aspect ratio correction'"),
	"trim_fmtowns_to_200_pixels",
	false,
	0,
	0
};

static const ExtraGuiOption fmtownsForceHiResMode = {
	_s("Run in original 640 x 480 resolution"),
	_s("This allows more accurate pause/restart banners, but might impact performance or shader/scaler usage."),
	"force_fmtowns_hires_mode",
	false,
	0,
	0};

static const ExtraGuiOption macV3LowQualityMusic = {
	_s("Play simplified music"),
	_s("This music was intended for low-end Macs, and uses only one channel."),
	"mac_v3_low_quality_music",
	false,
	0,
	0
};

static const ExtraGuiOption smoothScrolling = {
	_s("Enable smooth scrolling"),
	_s("(instead of the normal 8-pixels steps scrolling)"),
	"smooth_scroll",
	true,
	0,
	0
};

static const ExtraGuiOption semiSmoothScrolling = {
	_s("Allow semi-smooth scrolling"),
	_s("Allow scrolling to be less smooth during the fast camera movement in the intro."),
	"semi_smooth_scroll",
	false,
	0,
	0
};

static const ExtraGuiOption enableEnhancements {
	"",
	"",
	"enhancements",
	true,
	0,
	0
};

static const ExtraGuiOption audioOverride {
	_s("Load modded audio"),
	_s("Replace music, sound effects, and speech clips with modded audio files, if available."),
	"audio_override",
	true,
	0,
	0
};

static const ExtraGuiOption enableOriginalGUI = {
	_s("Enable the original GUI and Menu"),
	_s("Allow the game to use the in-engine graphical interface and the original save/load menu. "
	   "Use it together with the \"Ask for confirmation on exit\" for a more complete experience."),
	"original_gui",
	true,
	0,
	0
};

static const ExtraGuiOption enableLowLatencyAudio = {
	_s("Enable low latency audio mode"),
	_s("Allows the game to use low latency audio, at the cost of sound accuracy. "
	   "It is recommended to enable this feature only if you incur in audio latency issues during normal gameplay."),
	"dimuse_low_latency_mode",
	false,
	0,
	0
};

static const ExtraGuiOption enableCOMISong = {
	_s("Enable the \"A Pirate I Was Meant To Be\" song"),
	_s("Enable the song at the beginning of Part 3 of the game, \"A Pirate I Was Meant To Be\", "
	   "which was cut in international releases. Beware though: subtitles may not be fully translated."),
	"enable_song",
	false,
	0,
	0
};

static const ExtraGuiOption enableCopyProtection = {
	_s("Enable copy protection"),
	_s("Enable any copy protection that would otherwise be bypassed by default."),
	"copy_protection",
	false,
	0,
	0
};

static const ExtraGuiOption mmDemoModeOption = {
	_s("Enable demo/kiosk mode"),
	_s("Enable demo/kiosk mode in the full retail version of Maniac Mansion."),
	"enable_demo_mode",
	false,
	0,
	0
};

const ExtraGuiOptions ScummMetaEngine::getExtraGuiOptions(const Common::String &target) const {
	ExtraGuiOptions options;
	// Query the GUI options
	const Common::String guiOptionsString = ConfMan.get("guioptions", target);
	const Common::String gameid = ConfMan.get("gameid", target);
	const Common::String extra = ConfMan.get("extra", target);
	const Common::String guiOptions = parseGameGUIOptions(guiOptionsString);
	const Common::Platform platform = Common::parsePlatform(ConfMan.get("platform", target));
	const Common::String language = ConfMan.get("language", target);

	if (target.empty() || guiOptions.contains(GAMEOPTION_ORIGINALGUI)) {
		options.push_back(enableOriginalGUI);
	}
	if (target.empty() || guiOptions.contains(GAMEOPTION_COPY_PROTECTION)) {
		options.push_back(enableCopyProtection);
	}
	if (target.empty() || guiOptions.contains(GAMEOPTION_ENHANCEMENTS)) {
		options.push_back(enableEnhancements);
	}
	if (target.empty() || guiOptions.contains(GAMEOPTION_LOWLATENCYAUDIO)) {
		options.push_back(enableLowLatencyAudio);
	}
	if (target.empty() || guiOptions.contains(GAMEOPTION_AUDIO_OVERRIDE)) {
		options.push_back(audioOverride);
	}
	if (target.empty() || gameid == "comi") {
		options.push_back(comiObjectLabelsOption);

		if (!language.equals("en")) {
			options.push_back(enableCOMISong);
		}
	}
	if (target.empty() || platform == Common::kPlatformNES) {
		options.push_back(mmnesClassicPaletteOption);
	}
	if (target.empty() || platform == Common::kPlatformFMTowns) {
		options.push_back(smoothScrolling);
		if (target.empty() || gameid == "loom")
			options.push_back(semiSmoothScrolling);
		if (guiOptions.contains(GAMEOPTION_TRIM_FMTOWNS_TO_200_PIXELS))
			options.push_back(fmtownsTrimTo200);
#ifndef DISABLE_TOWNS_DUAL_LAYER_MODE
		if (platform == Common::kPlatformFMTowns && Common::parseLanguage(language) != Common::JA_JPN)
			options.push_back(fmtownsForceHiResMode);
#endif
	}
	if (target.empty() || gameid == "maniac") {
		// The kiosk demo script is in V1/V2 DOS, V2 Atari ST and V2 Amiga.
		bool isValidTarget = !extra.contains("Demo") &&
			(platform == Common::kPlatformDOS   ||
			 platform == Common::kPlatformAmiga ||
			 platform == Common::kPlatformAtariST) &&
			 !guiOptionsString.contains("lang_Italian");

		if (isValidTarget)
			options.push_back(mmDemoModeOption);
	}
	// The Steam Mac versions of Loom and Indy 3 are more akin to the VGA
	// DOS versions, and that's how ScummVM usually sees them. But that
	// rebranding does not happen until later.

	// The low quality music in Loom was probably intended for low-end
	// Macs. It plays only one channel, instead of three.

	if (target.empty() || (gameid == "indy3" && platform == Common::kPlatformMacintosh && extra != "Steam")) {
		options.push_back(macV3LowQualityMusic);
	}

	return options;
}

void ScummMetaEngine::registerDefaultSettings(const Common::String &) const {
	const ExtraGuiOptions engineOptions = getExtraGuiOptions("");
	for (uint i = 0; i < engineOptions.size(); i++) {
		if (strcmp(engineOptions[i].configOption, "enhancements") == 0)
			ConfMan.registerDefault(engineOptions[i].configOption, kEnhGameBreakingBugFixes | kEnhGrp1);
		else
			ConfMan.registerDefault(engineOptions[i].configOption, engineOptions[i].defaultState);
	}
}

Common::KeymapArray ScummMetaEngine::initKeymaps(const char *target) const {
	using namespace Common;
	using namespace Scumm;

	Common::KeymapArray keymaps = MetaEngine::initKeymaps(target);
	Common::String gameId = ConfMan.get("gameid", target);
	Action *act;

	if (gameId == "ft") {
		Keymap *insaneKeymap = new Keymap(Keymap::kKeymapTypeGame, insaneKeymapId, "SCUMM - Bike Fights");

		act = new Action("DOWNLEFT", _("Down Left"));
		act->setCustomEngineActionEvent(kScummActionInsaneDownLeft);
		act->addDefaultInputMapping("KP1");
		act->addDefaultInputMapping("END");
		insaneKeymap->addAction(act);

		act = new Action(kStandardActionMoveDown, _("Down"));
		act->setCustomEngineActionEvent(kScummActionInsaneDown);
		act->addDefaultInputMapping("DOWN");
		act->addDefaultInputMapping("KP2");
		act->addDefaultInputMapping("JOY_DOWN");
		insaneKeymap->addAction(act);

		act = new Action("DOWNRIGHT", _("Down Right"));
		act->setCustomEngineActionEvent(kScummActionInsaneDownRight);
		act->addDefaultInputMapping("KP3");
		act->addDefaultInputMapping("PAGEDOWN");
		insaneKeymap->addAction(act);

		act = new Action(kStandardActionMoveLeft, _("Left"));
		act->setCustomEngineActionEvent(kScummActionInsaneLeft);
		act->addDefaultInputMapping("LEFT");
		act->addDefaultInputMapping("KP4");
		act->addDefaultInputMapping("JOY_LEFT");
		insaneKeymap->addAction(act);

		act = new Action(kStandardActionMoveRight, _("Right"));
		act->setCustomEngineActionEvent(kScummActionInsaneRight);
		act->addDefaultInputMapping("RIGHT");
		act->addDefaultInputMapping("KP6");
		act->addDefaultInputMapping("JOY_RIGHT");
		insaneKeymap->addAction(act);

		act = new Action("UPLEFT", _("Up Left"));
		act->setCustomEngineActionEvent(kScummActionInsaneUpLeft);
		act->addDefaultInputMapping("KP7");
		act->addDefaultInputMapping("INSERT");
		insaneKeymap->addAction(act);

		act = new Action(kStandardActionMoveUp, _("Up"));
		act->setCustomEngineActionEvent(kScummActionInsaneUp);
		act->addDefaultInputMapping("UP");
		act->addDefaultInputMapping("KP8");
		act->addDefaultInputMapping("JOY_UP");
		insaneKeymap->addAction(act);

		act = new Action("UPRIGHT", _("Up Right"));
		act->setCustomEngineActionEvent(kScummActionInsaneUpRight);
		act->addDefaultInputMapping("KP9");
		act->addDefaultInputMapping("PAGEUP");
		insaneKeymap->addAction(act);

		act = new Action("ATTACK", _("Attack"));
		act->setCustomEngineActionEvent(kScummActionInsaneAttack);
		act->addDefaultInputMapping("RETURN");
		act->addDefaultInputMapping("JOY_A");
		insaneKeymap->addAction(act);

		act = new Action("SWITCH", _("Switch weapon"));
		act->setCustomEngineActionEvent(kScummActionInsaneSwitch);
		act->addDefaultInputMapping("TAB");
		act->addDefaultInputMapping("JOY_B");
		insaneKeymap->addAction(act);

		// TODO: Use a custom engine action here?
		act = new Action(kStandardActionSkip, _("Skip cutscene"));
		act->setKeyEvent(KeyState(KEYCODE_ESCAPE, ASCII_ESCAPE));
		act->addDefaultInputMapping("ESCAPE");
		act->addDefaultInputMapping("JOY_Y");
		insaneKeymap->addAction(act);

		// I18N: Lets one skip the bike/car fight sequences in Full Throttle
		act = new Action("CHEAT", _("Win the bike fight cheat"));
		act->setCustomEngineActionEvent(kScummActionInsaneCheat);
		act->addDefaultInputMapping("S+v");
		insaneKeymap->addAction(act);

		keymaps.push_back(insaneKeymap);
	}

	return keymaps;
}

#if PLUGIN_ENABLED_DYNAMIC(SCUMM)
	REGISTER_PLUGIN_DYNAMIC(SCUMM, PLUGIN_TYPE_ENGINE, ScummMetaEngine);
#else
	REGISTER_PLUGIN_STATIC(SCUMM, PLUGIN_TYPE_ENGINE, ScummMetaEngine);
#endif