aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/engine/guest_additions.h
blob: fd779098692e2b0484446dce7d6a18aa532dc923 (plain)
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
/* 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef SCI_ENGINE_GUEST_ADDITIONS_H
#define SCI_ENGINE_GUEST_ADDITIONS_H

#include "sci/engine/vm_types.h"

namespace Sci {

struct EngineState;
class GameFeatures;
class Kernel;
class Script;
class SegManager;

/**
 * The GuestAdditions class hooks into the SCI virtual machine to provide
 * enhanced interactions between the ScummVM GUI and the game engine. Currently,
 * this enhanced functionality encompasses synchronisation of audio volumes and
 * other audio-related settings, and integration of the ScummVM GUI when saving
 * and loading game states.
 *
 * NOTE: Some parts of the code used to manage audio sync are applied as script
 * patches using the normal ScriptPatcher mechanism. These patches prevent the
 * game from resetting audio volumes to defaults when starting up, and prevent
 * the game from restoring audio volumes stored inside of a save game.
 */
class GuestAdditions {
public:
	GuestAdditions(EngineState *state, GameFeatures *features, Kernel *kernel);

#pragma mark -

	/**
	 * Synchronises audio volume settings from ScummVM to the game. Called
	 * whenever the ScummVM global menu is dismissed.
	 */
	void syncSoundSettingsFromScummVM() const;

	/**
	 * Synchronises all audio settings from ScummVM to the game. Called when the
	 * game is first started, and when save games are loaded.
	 */
	void syncAudioOptionsFromScummVM() const;

	/**
	 * Clears audio settings synchronisation state.
	 */
	void reset();

private:
	EngineState *_state;
	GameFeatures *_features;
	Kernel *_kernel;
	SegManager *_segMan;

	/**
	 * Convenience function for invoking selectors that reduces boilerplate code
	 * required by Sci::invokeSelector.
	 */
	void invokeSelector(const reg_t objId, const Selector selector, const int argc = 0, const StackPtr argv = nullptr) const;

	/**
	 * Determines whether the current stack contains calls from audio controls
	 * that indicate a user-initiated change of audio settings.
	 */
	bool shouldSyncAudioToScummVM() const;

#pragma mark -
#pragma mark Hooks

public:
	/**
	 * Guest additions hook for SciEngine::runGame.
	 */
	void sciEngineRunGameHook();

	/**
	 * Guest additions hook for write_var.
	 */
	void writeVarHook(const int type, const int index, const reg_t value);

	/**
	 * Guest additions hook for kDoSoundMasterVolume.
	 *
	 * @returns true if the default action should be prevented
	 */
	bool kDoSoundMasterVolumeHook(const int volume) const;

#ifdef ENABLE_SCI32
	/**
	 * Guest additions hook for send_selector.
	 */
	void sendSelectorHook(const reg_t sendObj, Selector &selector, reg_t *argp);

	/**
	 * Guest additions hook for Audio32::setVolume.
	 *
	 * @returns true if the default action should be prevented
	 */
	bool audio32SetVolumeHook(const int16 channelIndex, const int16 volume) const;

	/**
	 * Guest additions hook for kDoSoundSetVolume.
	 */
	void kDoSoundSetVolumeHook(const reg_t soundObj, const int16 volume) const;

	/**
	 * Guest additions hook for SegManager::instantiateScript.
	 */
	void instantiateScriptHook(Script &script, const bool ignoreDelayedRestore = false) const;

	/**
	 * Guest additions hook for SegManager::saveLoadWithSerializer.
	 */
	void segManSaveLoadScriptHook(Script &script) const;
#endif

	/**
	 * Guest additions hook for kGetEvent.
	 */
	bool kGetEventHook() const;

	/**
	 * Guest additions hook for kWait.
	 */
	bool kWaitHook() const;

#pragma mark -
#pragma mark Integrated save & restore

public:
	/**
	 * Patches game scripts to use the ScummVM save/load dialogue instead of the
	 * game's native save/load dialogue when a user tries to save or restore a
	 * game from inside the game.
	 */
	void patchGameSaveRestore() const;

private:
	/**
	 * Patches the ScummVM save/load dialogue into the game for SCI16 games that
	 * use Game::save and Game::restore.
	 */
	void patchGameSaveRestoreSCI16() const;

#ifdef ENABLE_SCI32
public:
	/**
	 * Finds the correct save file number and description to save or load and
	 * returns it to the VM. For user-interactive save file lookup, this method
	 * displays the ScummVM save/load dialogue. For delayed restores, it returns
	 * the save game number sent by the ScummVM launcher without prompting the
	 * user.
	 */
	reg_t kScummVMSaveLoad(EngineState *s, int argc, reg_t *argv) const;

private:
	/**
	 * Patches the ScummVM save/load dialogue into SCI32 games that use
	 * SRDialog.
	 */
	void patchGameSaveRestoreSCI32(Script &script) const;

	/**
	 * Patches the ScummVM save/load dialogue into Torin.
	 */
	void patchGameSaveRestoreTorin(Script &script) const;

	/**
	 * Prompts for a save game and returns it to game scripts using default
	 * SRDialog game class semantics.
	 */
	reg_t promptSaveRestoreDefault(EngineState *s, int argc, reg_t *argv) const;

	/**
	 * Prompts for a save game and returns it to game scripts using Torin's
	 * custom NewGame class semantics.
	 */
	reg_t promptSaveRestoreTorin(EngineState *s, int argc, reg_t *argv) const;
#endif

#pragma mark -
#pragma mark Restore from launcher

private:
	/**
	 * Invokes the game's save restore mechanism to load a save game that was
	 * selected in the ScummVM launcher when the game was started.
	 */
	bool restoreFromLauncher() const;

#ifdef ENABLE_SCI32
	/**
	 * If true, GuestAdditions is in the process of handling a delayed game
	 * restore from the ScummVM launcher or global menu.
	 */
	mutable bool _restoring;
#endif

#pragma mark -
#pragma mark Message type sync

private:
	/**
	 * True if the message type (text/speech/text+speech) has been synchronised
	 * from ScummVM to the game.
	 */
	bool _messageTypeSynced;

	/**
	 * Synchronises the message type (speech/text/speech+text) from ScummVM to
	 * a game.
	 */
	void syncMessageTypeFromScummVM() const;

	/**
	 * Synchronises the message type from ScummVM using the default strategy
	 * (global90).
	 */
	void syncMessageTypeFromScummVMUsingDefaultStrategy() const;

#ifdef ENABLE_SCI32
	/**
	 * Synchronises the message type from ScummVM using the strategy used by
	 * Shivers (global211).
	 */
	void syncMessageTypeFromScummVMUsingShiversStrategy() const;

	/**
	 * Synchronises the message type from ScummVM using the strategy used by
	 * LSL6hires (gameFlags).
	 */
	void syncMessageTypeFromScummVMUsingLSL6HiresStrategy() const;
#endif

	/**
	 * Synchronises the message type (speech/text/speech+text) from a game to
	 * ScummVM.
	 */
	void syncMessageTypeToScummVM(const int index, const reg_t value);

	/**
	 * Synchronises the message type to ScummVM using the default strategy
	 * (global90).
	 */
	void syncMessageTypeToScummVMUsingDefaultStrategy(const int index, const reg_t value);

#ifdef ENABLE_SCI32
	/**
	 * Synchronises the message type to ScummVM using the strategy used by
	 * Shivers (global211).
	 */
	void syncMessageTypeToScummVMUsingShiversStrategy(const int index, const reg_t value);

	/**
	 * Synchronises the message type to ScummVM using the strategy used by
	 * LSL6hires (gameFlags).
	 */
	void syncMessageTypeToScummVMUsingLSL6HiresStrategy(const reg_t sendObj, Selector &selector, reg_t *argp);
#endif

#pragma mark -
#pragma mark Master volume sync

private:
	/**
	 * Synchronises audio volume settings from ScummVM to the game, for games
	 * that do not store volume themselves and just call to the kernel.
	 */
	void syncMasterVolumeFromScummVM() const;

	/**
	 * Synchronises audio volume settings from the game to ScummVM, for games
	 * that do not store volume themselves and just call to the kernel.
	 */
	void syncMasterVolumeToScummVM(const int16 masterVolume) const;

#ifdef ENABLE_SCI32
#pragma mark -
#pragma mark Globals volume sync

private:
	/**
	 * Synchronises audio volume settings from ScummVM to the game, for games
	 * that store volumes in globals.
	 */
	void syncAudioVolumeGlobalsFromScummVM() const;

	/**
	 * Synchronises audio volume settings from ScummVM to GK1 at game startup
	 * time.
	 */
	void syncGK1StartupVolumeFromScummVM(const int index, const reg_t value) const;

	/**
	 * Synchronises audio volume settings from ScummVM to GK1 when the game is
	 * running.
	 */
	void syncGK1VolumeFromScummVM(const int16 musicVolume, const int16 dacVolume) const;

	void syncGK2VolumeFromScummVM(const int16 musicVolume) const;
	void syncLSL6HiresVolumeFromScummVM(const int16 musicVolume) const;
	void syncPhant2VolumeFromScummVM(const int16 masterVolume) const;
	void syncTorinVolumeFromScummVM(const int16 musicVolume, const int16 sfxVolume, const int16 speechVolume) const;

	/**
	 * Synchronises audio volume settings from a game to ScummVM, for games
	 * that store volumes in globals.
	 */
	void syncAudioVolumeGlobalsToScummVM(const int index, const reg_t value) const;

	/**
	 * Synchronises audio volume settings from GK1 to ScummVM.
	 */
	void syncGK1AudioVolumeToScummVM(const reg_t soundObj, const int16 volume) const;

#pragma mark -
#pragma mark Audio UI sync

private:
	/**
	 * Synchronises the in-game control panel UI in response to a change of
	 * volume from the ScummVM GUI. The values of the volume parameters passed
	 * to this function are game-specific.
	 */
	void syncInGameUI(const int16 musicVolume, const int16 sfxVolume) const;

	void syncGK1UI() const;
	void syncGK2UI() const;
	void syncLSL6HiresUI(const int16 musicVolume) const;
	void syncMGDXUI(const int16 musicVolume) const;
	void syncPhant1UI(const int16 oldMusicVolume, const int16 musicVolume, reg_t &musicGlobal, const int16 oldDacVolume, const int16 dacVolume, reg_t &dacGlobal) const;
	void syncPhant2UI(const int16 masterVolume) const;
	void syncPQ4UI(const int16 musicVolume) const;
	void syncPQSWATUI() const;
	void syncQFG4UI(const int16 musicVolume) const;
	void syncShivers1UI(const int16 dacVolume) const;
	void syncSQ6UI() const;
	void syncTorinUI(const int16 musicVolume, const int16 sfxVolume, const int16 speechVolume) const;

#pragma mark -
#pragma mark Talk speed sync

private:
	/**
	 * Synchronises text speed settings from ScummVM to a game.
	 */
	void syncTextSpeedFromScummVM() const;

	/**
	 * Synchronises text speed settings from a game to ScummVM.
	 */
	void syncTextSpeedToScummVM(const int index, const reg_t value) const;
#endif
};

} // End of namespace Sci

#endif // SCI_ENGINE_GUEST_ADDITIONS_H