File: engine.h

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 (715 lines) | stat: -rw-r--r-- 18,261 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
/* 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/>.
 *
 */

#ifndef ENGINES_ENGINE_H
#define ENGINES_ENGINE_H

#include "common/scummsys.h"
#include "common/str.h"
#include "common/language.h"
#include "common/platform.h"
#include "common/queue.h"
#include "common/singleton.h"
#include "engines/enhancements.h"

class OSystem;
class MetaEngineDetection;
class MetaEngine;

namespace Audio {
class Mixer;
}
namespace Common {
class Error;
class EventManager;
class SaveFileManager;
class TimerManager;
class FSNode;
class SeekableReadStream;
class WriteStream;
}
namespace GUI {
class Debugger;
class Dialog;
}

/**
 * @defgroup engines_engine Engine
 * @ingroup engines
 *
 * @brief API for managing various functions of the engine.
 *
 * @{
 */

/**
 * Initialize graphics and show an error message.
 */
void GUIErrorMessage(const Common::U32String &msg, const char *url = nullptr);
/**
 * Initialize graphics and show an error message.
 */
void GUIErrorMessage(const Common::String &msg, const char *url = nullptr);
/**
 * Initialize graphics and show an error message.
 */
void GUIErrorMessageWithURL(const Common::U32String &msg, const char *url);
/**
 * Initialize graphics and show an error message.
 */
void GUIErrorMessageWithURL(const Common::String &msg, const char *url);
/**
 * Initialize graphics and show an error message.
 */
void GUIErrorMessageFormatU32StringPtr(const Common::U32String *fmt, ...);
/**
 * Initialize graphics and show an error message.
 */
template<class... TParam>
inline void GUIErrorMessageFormat(const Common::U32String &fmt, TParam... param) {
	GUIErrorMessageFormatU32StringPtr(&fmt, Common::forward<TParam>(param)...);
}
/**
 * Initialize graphics and show an error message.
 */
void GUIErrorMessageFormat(MSVC_PRINTF const char *fmt, ...) GCC_PRINTF(1, 2);


class Engine;

/**
* Class for managing pausing by Engine::pauseEngine that hands out pause tokens.
*
* Each token represents one requested level of pause.
*/
class PauseToken {
public:
	PauseToken();
	/**
	 * Construct a pause token.
	 */
	PauseToken(const PauseToken &);
#if __cplusplus >= 201103L
	PauseToken(PauseToken &&);
#endif
	~PauseToken();
	/**
	 * Assign the pause token.
	 */
	void operator=(const PauseToken &);
#if __cplusplus >= 201103L
	void operator=(PauseToken &&);
#endif
	/**
	* Manually release the PauseToken.
	*
	* Only allowed if the token currently represents a pause request.
	*/
	void clear();

	/**
	 * Return true if the PauseToken represents a pause level,
	 * false if it is empty.
	 */
	bool isActive() const { return _engine != nullptr; }

private:
	PauseToken(Engine *);

	Engine *_engine;
	/**
	 * @see Engine
	 */
	friend class Engine;
};

class Engine {
public:
	/**
	 * The OSystem instance used by the engine.
	 */
	OSystem *_system;
	/**
	 * The Mixer instance used by the engine.
	 */
	Audio::Mixer *_mixer;

protected:
	/**
	 * The TimerManager instance used by the engine.
	 */
	Common::TimerManager *_timer;
	/**
	 * The EventManager instance used by the engine.
	 */
	Common::EventManager *_eventMan;
	/**
	 * The SaveFileManager used by the engine.
	 */
	Common::SaveFileManager *_saveFileMan;

	/**
	 * The Dialog instance used by the engine.
	 */
	GUI::Dialog *_mainMenuDialog;
	/**
	 * Run a GUI dialog.
	 */
	virtual int runDialog(GUI::Dialog &dialog);

	/**
	 * Target name for saves.
	 */
	const Common::String _targetName;

	int32 _activeEnhancements = kEnhGameBreakingBugFixes;

private:
	/**
	 * The associated metaengine
	 */
	MetaEngine *_metaEngine;

	/**
	 * The pause level.
	 *
	 * 0 means 'running'. A positive value indicates how often the engine
	 * has been paused and thus how many times it must be unpaused before
	 * it resumes running. This makes it possible to nest the code that pauses the engine.
	 */
	int _pauseLevel;

	/**
	 * The time when the pause was started.
	 */
	uint32 _pauseStartTime;

	/**
	 * The time when the engine was started.
	 *
	 * This value is used to calculate the current play time of the game running.
	 */
	int32 _engineStartTime;

	/**
	 * Autosave interval.
	 */
	int _autosaveInterval;

	/**
	 * The last time an autosave was done.
	 */
	int _lastAutosaveTime;

	/**
	 * Save slot selected via the global main menu.
	 *
	 * This slot will be loaded after the main menu execution (not from inside
	 * the menu loop, to avoid bugs like #4420).
	 */
	int _saveSlotToLoad;

	/**
	 * Used for preventing recursion during autosave.
	 */
	bool _autoSaving;

	/**
	 * Optional debugger for the engine.
	 */
	GUI::Debugger *_debugger;

	/**
	 * Flag for whether the quitGame method has been called
	 */
	static bool _quitRequested;

public:
	/**
	 * Engine features.
	 *
	 * A feature in this context means an ability of the engine
	 * that can be either available or not.
	 * @see Engine::hasFeature()
	 */
	enum EngineFeature {
		/**
		 * Enable the subtitle speed and toggle items in the Options section
		 * of the global main menu.
		 */
		kSupportsSubtitleOptions,

		/**
		 * The 'Return to launcher' feature is supported.
		 *
		 * This means that EVENT_RETURN_TO_LAUNCHER is handled
		 * either directly, or indirectly (the engine calls and honors
		 * the result of the Engine::shouldQuit() method appropriately).
		 */
		kSupportsReturnToLauncher,

		/**
		 * Loading save states during runtime is supported.
		 *
		 * This means that the engine implements loadGameState() and canLoadGameStateCurrently().
		 * If this feature is supported, then the corresponding MetaEngine *must*
		 * support the kSupportsListSaves feature.
		 */
		kSupportsLoadingDuringRuntime,

		/**
		 * Loading save states during runtime is supported.
		 *
		 * This means that the engine implements saveGameState() and canSaveGameStateCurrently().
		 * If this feature is supported, then the corresponding MetaEngine *must*
		 * support the kSupportsListSaves feature.
		 */
		kSupportsSavingDuringRuntime,

		/**
		 * Changing the game settings during runtime is supported.
		 *
		 * This enables showing the Engine Options tab in the config dialog
		 * accessed through the global main menu.
		 */
		kSupportsChangingOptionsDuringRuntime,

		/**
		 * Arbitrary resolutions are supported.
		 *
		 * This means that the engine allows the backend to override the resolution
		 * passed to OSystem::setupScreen.
		 * The engine will need to read the actual resolution used by the
		 * backend using OSystem::getWidth and OSystem::getHeight.
		 */
		kSupportsArbitraryResolutions,

		/**
		 * The game provides custom help.
		 *
		 * This enables the help button in the main menu.
		 */
		 kSupportsHelp,

		/**
		 * The engine provides overrides to the quit and exit to launcher dialogs.
		 */
		kSupportsQuitDialogOverride,
	};



	/** @name Overloadable methods
	 *
	 *  All Engine subclasses should consider overloading some or all of the following methods.
	 *  @{
	 */

	/**
	 * Create an instance of the engine.
	 */
	Engine(OSystem *syst);
	/**
	 * Destroy the engine instance.
	 */
	virtual ~Engine();

	/**
	 * Initialize SearchMan according to the game path.
	 *
	 * By default, this adds the directory in non-flat mode with a depth of 4 as
	 * priority 0 to SearchMan.
	 *
	 * @param gamePath The base directory of the game data.
	 */
	virtual void initializePath(const Common::FSNode &gamePath);

	/**
	 * Initialize the engine and start its main loop.
	 *
	 * @return kNoError on success, otherwise an error code.
	 */
	virtual Common::Error run() = 0;

	/**
	 * Prepare an error string that is printed by the error() function.
	 */
	virtual void errorString(const char *buf_input, char *buf_output, int buf_output_size);

	/**
	 * Return the engine's debugger instance, if any.
	 */
	virtual GUI::Debugger *getDebugger() final { return _debugger; }

	/**
	 * Set the engine's debugger.
	 *
	 * Once set, the Engine class is responsible for managing
	 * the debugger, and freeing it on exit.
	 */
	void setDebugger(GUI::Debugger *debugger) {
		assert(!_debugger);
		_debugger = debugger;
	}

	/**
	 * Return the engine's debugger instance, or create one if none is present.
	 *
	 * Used by error() to invoke the debugger when a severe error is reported.
	 */
	GUI::Debugger *getOrCreateDebugger();

	/**
	 * Determine whether the engine supports the specified feature.
	 */
	virtual bool hasFeature(EngineFeature f) const { return false; }

	bool enhancementEnabled(int32 cls);

	/**
	 * Notify the engine that the sound settings in the config manager might have
	 * changed and that it should adjust any internal volume (and other) values
	 * accordingly.
	 *
	 * The default implementation sets the volume levels of all mixer sound
	 * types according to the config entries of the active domain.
	 * When overwriting, call the default implementation first, then adjust the
	 * volumes further (if required).
	 *
	 * @note When setting volume levels, respect the "mute" config entry.
	 *
	 * @note The volume for the plain sound type is reset to the maximum
	 *       volume. If the engine can associate its own value for this
	 *       type, it needs to overwrite this member and set it accordingly.
	 * @todo find a better name for this
	 */
	virtual void syncSoundSettings();

	/**
	 * Notify the engine that the settings editable from the Game tab in the
	 * in-game options dialog might have changed and that they need to be applied
	 * if necessary.
	 */
	virtual void applyGameSettings() {}

	/**
	 * Flip mute all sound options.
	 */
	virtual void flipMute();

	/**
	 * Generate the savegame filename.
	 */
	virtual Common::String getSaveStateName(int slot) const {
		return Common::String::format("%s.%03d", _targetName.c_str(), slot);
	}

	/**
	 * Load a game state.
	 *
	 * @param slot	The slot from which a save state should be loaded.
	 *
	 * @return kNoError on success, otherwise an error code.
	 */
	virtual Common::Error loadGameState(int slot);

	/**
	 * Load a game state.
	 *
	 * @param stream	The stream to load the save state from.
	 *
	 * @return kNoError on success, otherwise an error code.
	 */
	virtual Common::Error loadGameStream(Common::SeekableReadStream *stream);

	/**
	 * Set the game slot for a savegame to be loaded after the global
	 * main menu execution.
	 *
	 * This is to avoid loading a savegame from
	 * inside the menu loop which causes bugs like #4420.
	 *
	 * @param slot	The slot from which a save state should be loaded.
	 */
	void setGameToLoadSlot(int slot);

	/**
	 * Indicate whether a game state can be loaded.
	 *
	 * @param msg        Optional pointer to message explaining why it is disabled
	 */
	virtual bool canLoadGameStateCurrently(Common::U32String *msg = nullptr);

	/**
	 * Save a game state.
	 *
	 * @param slot        The slot into which the save state should be stored.
	 * @param desc        Description for the save state, entered by the user.
	 * @param isAutosave  Expected to be true if an autosave is being created.
	 *
	 * @return kNoError on success, otherwise an error code.
	 */
	virtual Common::Error saveGameState(int slot, const Common::String &desc, bool isAutosave = false);

	/**
	 * Save a game state.
	 *
	 * @param stream      The write stream to save the savegame data to.
	 * @param isAutosave  Expected to be true if an autosave is being created.
	 *
	 * @return kNoError on success, otherwise an error code.
	 */
	virtual Common::Error saveGameStream(Common::WriteStream *stream, bool isAutosave = false);

	/**
	 * Indicate whether a game state can be saved.
	 *
	 * @param msg        Optional pointer to message explaining why it is disabled
	 */
	virtual bool canSaveGameStateCurrently(Common::U32String *msg = nullptr);

	/**
	 * Show the ScummVM save dialog, allowing users to save their game.
	 */
	bool saveGameDialog();

	/**
	 * Show the ScummVM Restore dialog, allowing users to load a game.
	 */
	bool loadGameDialog();

protected:
	/**
	 * Actual implementation of pauseEngine by subclasses.
	 *
	 * @see Engine::pauseEngine
	 */
	virtual void pauseEngineIntern(bool pause);

	 /** @} */


public:

	/**
	 * Request the engine to quit.
	 *
	 * Sends an EVENT_QUIT event to the Event Manager.
	 */
	static void quitGame();

	/**
	 * Return whether the engine should quit.
	 *
	 * This can mean either quitting ScummVM altogether, or returning to the launcher.
	 */
	static bool shouldQuit();

	/**
	 * Return the MetaEngine instance used by this engine.
	 */
	inline MetaEngine *getMetaEngine() const { return _metaEngine; }

	/**
	 * Set the MetaEngine instance used by this engine.
	 */
	inline void setMetaEngine(MetaEngine *metaEngine) { _metaEngine = metaEngine; }

	/**
	 * Pause the engine.
	 *
	 * This should stop any audio playback and other operations.
	 * Called right before the system runs a global dialog
	 * (like a global pause, main menu, options, or 'confirm exit' dialog).
	 *
	 * Returns a PauseToken. Multiple pause tokens can exist. The engine will
	 * be resumed when all associated pause tokens reach the end of their lives.
	 */
	PauseToken pauseEngine();
private:
	/** Resume the engine.
	 *
	 * This should resume any audio playback and other operations.
	 * Only PauseToken is allowed to call this member function. Use the PauseToken
	 * that you got from pauseEngine to resume the engine.
	 */
	void resumeEngine();
	/**
	 * @see Engine::PauseToken
	 */
	friend class PauseToken;

	/**
	 * Warns before overwriting autosave.
	 *
	 * @return true if it is safe to save, false to avoid saving.
	 */
	bool warnBeforeOverwritingAutosave();

public:

	/**
	 * Return whether the engine is currently paused or not.
	 */
	bool isPaused() const { return _pauseLevel != 0; }

	/**
	 * Run the global main menu dialog.
	 */
	void openMainMenuDialog();

	/**
	 * Display a warning to the user that the game is not fully supported.
	 *
	 * @param msg  A message that will be presented to user. If empty, then
	 *             generic message regarding unsupported game is presented
	 *
	 * @return True if the user chooses to start anyway, false otherwise.
	 */
	static bool warnUserAboutUnsupportedGame(Common::String msg = Common::String());

	/**
	 * Display an error message to the user that the game is not supported.
	 *
	 * @param extraMsg  An extra message that will be appended to the default message.
	 */
	static void errorUnsupportedGame(Common::String extraMsg);

	/**
	 * Get the total play time.
	 *
	 * @return How long the player has been playing in ms.
	 */
	uint32 getTotalPlayTime() const;

	/**
	 * Set the game time counter to the specified time.
	 *
	 * This can be used to set the play time counter after loading a savegame,
	 * for example. Another use case is when the engine wants to exclude from
	 * the counter the time that the user spent in original engine dialogs.
	 *
	 * @param time Play time to set up in ms.
	 */
	void setTotalPlayTime(uint32 time = 0);

	/**
	 * Return the TimerManager instance used by this engine.
	 */
	inline Common::TimerManager *getTimerManager() { return _timer; }
	/**
	 * Return the EventManager instance used by this engine.
	 */
	inline Common::EventManager *getEventManager() { return _eventMan; }
	/**
	 * Return the SaveFileManager instance used by this engine.
	 */
	inline Common::SaveFileManager *getSaveFileManager() { return _saveFileMan; }

public:
	/**
	 * Check if extracted CD Audio files are found.
	 */
	bool existExtractedCDAudioFiles(uint track = 1);
	/**
	 * On some systems, check whether the game appears to be run
	 * from the same CD drive, which also should play CD audio.
	 */
	bool isDataAndCDAudioReadFromSameCD();
	/**
	 *Display a warning for no extracted CD Audio files found.
	 */
	void warnMissingExtractedCDAudio();

	/**
	 * Check whether it is time to autosave, and if so, do it.
	 */
	void handleAutoSave();

	/**
	 * Autosave immediately if autosaves are enabled.
	 */
	void saveAutosaveIfEnabled();

	/**
	 * Indicate whether an autosave can currently be done.
	 */
	virtual bool canSaveAutosaveCurrently() {
		return canSaveGameStateCurrently();
	}

	/**
	 * Return the slot that should be used for autosaves, or -1 for engines that
	 * don't support autosave.
	 *
	 * @note	This should match the meta engine getAutosaveSlot() method.
	 */
	virtual int getAutosaveSlot() const {
		return 0;
	}

protected:
	/**
	 * Syncs the engine's mixer using the default volume syncing behavior.
	 */
	void defaultSyncSoundSettings();
};


/** @name Chained games
*
*  @{
*/

/**
 * Singleton class which manages chained games.
 *
 * A chained game is one that starts automatically, optionally loading
 * a saved game, instead of returning to the launcher.
 */
class ChainedGamesManager : public Common::Singleton<ChainedGamesManager> {
private:
	struct Game {
		Common::String target;
		int slot;
	};

	Common::Queue<Game> _chainedGames;

public:
	ChainedGamesManager();
	/** Clear the chained games manager of any games. */
	void clear();
	/** Load a game into a slot in the chained games manager. */
	void push(const Common::String target, const int slot = -1);
	/** Pop the last game loaded into the chained games manager. */
	bool pop(Common::String &target, int &slot);
	/** Returns true if the chained games manager has no elements in the queue. */
	bool empty() { return _chainedGames.empty(); }
};

/** Convenience shortcut for accessing the chained games manager. */
#define ChainedGamesMan ChainedGamesManager::instance()
/** @} */
/** Current workaround for MidiEmu & error(). */ //FIXME
extern Engine *g_engine;
/** @} */
#endif