File: ags.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 (389 lines) | stat: -rw-r--r-- 12,940 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
/* 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 "ags/ags.h"
#include "ags/detection.h"
#include "ags/events.h"
#include "ags/game_scanner.h"
#include "ags/music.h"
#include "ags/console.h"
#include "common/scummsys.h"
#include "common/config-manager.h"
#include "common/compression/clickteam.h"
#include "common/debug-channels.h"
#include "common/events.h"
#include "common/file.h"
#include "common/tokenizer.h"
#include "common/util.h"
#include "engines/util.h"

#include "ags/shared/core/platform.h"

#include "common/std/set.h"
#include "ags/shared/ac/common.h"
#include "ags/engine/ac/game.h"
#include "ags/globals.h"
#include "ags/engine/ac/game_setup.h"
#include "ags/engine/ac/game_state.h"
#include "ags/engine/ac/room.h"
#include "ags/shared/core/def_version.h"
#include "ags/engine/debugging/debugger.h"
#include "ags/engine/debugging/debug_log.h"
#include "ags/shared/debugging/out.h"
#include "ags/engine/game/savegame.h"
#include "ags/engine/game/savegame_components.h"
#include "ags/engine/main/config.h"
#include "ags/engine/main/engine.h"
#include "ags/engine/main/main.h"
#include "ags/engine/main/quit.h"
#include "ags/engine/platform/base/ags_platform_driver.h"
#include "ags/engine/script/cc_instance.h"
#include "ags/engine/script/script.h"
#include "ags/engine/ac/route_finder.h"
#include "ags/shared/core/asset_manager.h"
#include "ags/shared/util/directory.h"
#include "ags/shared/script/cc_common.h"

#ifdef ENABLE_AGS_TESTS
#include "ags/tests/test_all.h"
#endif

// Include translation.h last as some AGS classes have member such as _sc, which clash with
// macro defined in translation.h.
#include "common/translation.h"

namespace AGS {

AGSEngine *g_vm;

AGSEngine::AGSEngine(OSystem *syst, const AGSGameDescription *gameDesc) : Engine(syst),
	_gameDescription(gameDesc), _randomSource("AGS"), _events(nullptr), _music(nullptr),
	_gfxDriver(nullptr), _globals(nullptr), _forceTextAA(false) {
	g_vm = this;

	AGS3::script_commands_init();
	AGS3::Engine::SavegameComponents::component_handlers_init();
	_events = new EventsManager();
	_globals = new ::AGS3::Globals();

	Common::String forceAA;
	if (ConfMan.getActiveDomain()->tryGetVal("force_text_aa", forceAA))
		Common::parseBool(forceAA, _forceTextAA);

	// WORKAROUND: Certain games need to force AA to render the text correctly
	if (_gameDescription->features & GAMEFLAG_FORCE_AA)
		_forceTextAA = true;
}

AGSEngine::~AGSEngine() {
	if (_globals && _G(proper_exit) == 0) {
		_G(platform)->DisplayAlert("Error: the program has exited without requesting it.\n"
		                           "Program pointer: %+03d  (write this number down), engine version %s\n"
		                           "If you see a list of numbers above, please write them down and contact\n"
		                           "developers. Otherwise, note down any other information displayed.",
		                           _G(our_eip), _G(EngineVersion).LongString.GetCStr());
	}

	delete _events;
	delete _music;
	delete _globals;
	AGS3::Engine::SavegameComponents::component_handlers_free();
	AGS3::script_commands_free();
}

uint32 AGSEngine::getFeatures() const {
	return _gameDescription->desc.flags;
}

static const PluginVersion AGSTEAM_WADJETEYE[] = { { "agsteam", kWadjetEye }, { nullptr, 0 } };
static const PluginVersion AGS_FLASHLIGHT[] = { { "agsflashlight", 0 }, { nullptr, 0 } };
static const PluginVersion AGSSPRITEFONT_CLIFFTOP[] = { { "agsspritefont", kClifftopGames }, { "agsplugin.spritefont", kClifftopGames }, { nullptr, 0 } };

static const PluginVersion *const PLUGIN_VERSIONS[] = {
	nullptr,
	AGSTEAM_WADJETEYE,
	AGS_FLASHLIGHT,
	AGSSPRITEFONT_CLIFFTOP
};

const PluginVersion *AGSEngine::getNeededPlugins() const {
	uint index = (_gameDescription->features & GAMEFLAG_PLUGINS_MASK);

	if (index >= ARRAYSIZE(PLUGIN_VERSIONS))
		return nullptr;
	else
		return PLUGIN_VERSIONS[index];
}

Common::String AGSEngine::getGameId() const {
	return _gameDescription->desc.gameId;
}

Common::Error AGSEngine::run() {
#ifdef DETECTION_STATIC
	// The game scanner is not available when detection is dynamic
	if (debugChannelSet(-1, kDebugScan)) {
		// Scan the given folder and subfolders for unknown games
		AGS3::GameScanner scanner;
		scanner.scan(ConfMan.getPath("path"));
		return Common::kNoError;
	}
#endif

	if (isUnsupportedPre25()) {
		GUIErrorMessage(_("The selected game uses a pre-2.5 version of the AGS engine, which is not supported."));
		return Common::kNoError;
	}

	if (is64BitGame()) {
		// If the game file was opened and the engine started, but the
		// size is -1, then it must be a game like Strangeland where
		// the data file is > 2Gb
		GUIErrorMessage(_("The selected game has a data file greater than 2Gb, "
			"which isn't supported by your version of ScummVM yet."));
		return Common::kNoError;
	}

	if (debugChannelSet(-1, kDebugScript))
		AGS3::ccSetOption(SCOPT_DEBUGRUN, 1);

#ifdef ENABLE_AGS_TESTS
	AGS3::Test_DoAllTests();
	return Common::kNoError;
#endif

	setDebugger(new AGSConsole(this));

	Common::String filename(_gameDescription->desc.filesDescriptions[0].fileName);
	Common::StringTokenizer tok(filename, ":");

	Common::String type = tok.nextToken();
	if (type.equals("clk")) {
		Common::File *f = new Common::File();
		f->open(tok.nextToken().c_str());
		SearchMan.add("installer", Common::ClickteamInstaller::open(f, DisposeAfterUse::YES));
		filename = tok.nextToken();
	}

	const char *ARGV[] = { "scummvm.exe", filename.c_str() };
	const int ARGC = 2;
	AGS3::main_init(ARGC, ARGV);

	_G(debug_flags) = 0;

	if (ConfMan.hasKey("display_fps"))
		_G(display_fps) = ConfMan.getBool("display_fps") ? AGS3::kFPS_Forced : AGS3::kFPS_Hide;

	Common::String saveOverrideOption;
	bool saveOverride = false;
	ConfMan.getActiveDomain()->tryGetVal("save_override", saveOverrideOption);
	if (!saveOverrideOption.empty())
		parseBool(saveOverrideOption, saveOverride);
	_G(noScummAutosave) = (Common::checkGameGUIOption(GAMEOPTION_NO_AUTOSAVE, ConfMan.get("guioptions"))) && !saveOverride;
	_G(noScummSaveLoad) = (Common::checkGameGUIOption(GAMEOPTION_NO_SAVELOAD, ConfMan.get("guioptions"))) && !saveOverride;
	if (_G(noScummSaveLoad))
		_G(noScummAutosave) = true;

	_G(saveThumbnail) = !(Common::checkGameGUIOption(GAMEOPTION_NO_SAVE_THUMBNAIL, ConfMan.get("guioptions")));

	AGS3::ConfigTree startup_opts;
	int res = AGS3::main_process_cmdline(startup_opts, ARGC, ARGV);
	if (res != 0)
		return Common::kUnknownError;

	if (_G(justDisplayVersion)) {
		_G(platform)->WriteStdOut(AGS3::get_engine_string().GetCStr());
		return Common::kNoError;
	}

	if (_G(justDisplayHelp)) {
		AGS3::main_print_help();
		return Common::kNoError;
	}

	if (!_G(justTellInfo))
		_G(platform)->SetGUIMode(true);
	AGS3::init_debug(startup_opts, _G(justTellInfo));
	AGS3::AGS::Shared::Debug::Printf("%s", AGS3::get_engine_string().GetCStr());

	AGS3::main_set_gamedir(ARGC, ARGV);

	// Update shell associations and exit
	if (_G(debug_flags) & DBG_REGONLY)
		return Common::kNoError;

	_music = new Music();

	_G(loadSaveGameOnStartup) = ConfMan.getInt("save_slot");

	syncSoundSettings();
	AGS3::initialize_engine(startup_opts);

	// Do shutdown stuff
	::AGS3::quit_free();

	return Common::kNoError;
}

SaveStateList AGSEngine::listSaves() const {
	return getMetaEngine()->listSaves(_targetName.c_str());
}

bool AGSEngine::getPixelFormat(int depth, Graphics::PixelFormat &format) const {
	if (depth == 8) {
		format = Graphics::PixelFormat::createFormatCLUT8();
		return true;
	}

	Common::List<Graphics::PixelFormat> supportedFormatsList = g_system->getSupportedFormats();

	// Internally the engine uses the following format (see create_bitmap_ex())
	//  16 bit: RGB565   / Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0)
	//  32 bit: ARGB8888 / Graphics::PixelFormat(4, 8, 8, 8, 8, 16, 8, 0, 24)
	// If supported by the backend, use it so that we can do direct rendering.
	Graphics::PixelFormat engine_format = depth == 16 ? Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0) : Graphics::PixelFormat(4, 8, 8, 8, 8, 16, 8, 0, 24);
	if (Common::find(supportedFormatsList.begin(), supportedFormatsList.end(), engine_format) != supportedFormatsList.end()) {
		format = engine_format;
		return true;
	}

	// For 32 bit color depth, the next best formats for which there is some optimization in the
	// engine are RGBA8888 or ABGR8888 (see ScummVMRendererGraphicsDriver::Present()).
	if (depth == 32) {
		Graphics::PixelFormat preferred_format1 = Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
		Graphics::PixelFormat preferred_format2 = Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
		for (Common::List<Graphics::PixelFormat>::iterator it =
				supportedFormatsList.begin(); it != supportedFormatsList.end(); ++it) {
			if (*it == preferred_format1 || *it == preferred_format2) {
				format = *it;
				return true;
			}
		}
	}

	// Otherwise prefer another format with the same color depth
	for (Common::List<Graphics::PixelFormat>::iterator it =
			supportedFormatsList.begin(); it != supportedFormatsList.end(); ++it) {
		if (it->bpp() == depth) {
			format = *it;
			return true;
		}
	}

	// Allow using 16 bit <-> 32 bit conversions by using the preferred graphics mode
	if (!supportedFormatsList.empty()) {
		format = supportedFormatsList.front();
		return true;
	}

	return false;
}


void AGSEngine::setGraphicsMode(size_t w, size_t h, int colorDepth) {
	Common::List<Graphics::PixelFormat> supportedFormatsList = g_system->getSupportedFormats();
	Graphics::PixelFormat format;
	if (!getPixelFormat(colorDepth, format))
		error("Unsupported color depth %d", colorDepth);

	initGraphics(w, h, &format);
}

bool AGSEngine::isUnsupportedPre25() const {
	return _gameDescription->desc.extra &&
		!strcmp(_gameDescription->desc.extra, "Pre 2.5");
}

bool AGSEngine::is64BitGame() const {
	Common::File f;

	// TODO: There are no more entries in the tables with -1 filesize, so this check doesn't really do anything.
	// Maybe find a more reliable way to detect if the system can't handle these files?

	if (_gameDescription->desc.filesDescriptions[0].fileName[0] == '\0')
		return false;
	else
		return f.open(_gameDescription->desc.filesDescriptions[0].fileName) && f.size() == -1;
}

Common::FSNode AGSEngine::getGameFolder() {
	return Common::FSNode(ConfMan.getPath("path"));
}

bool AGSEngine::canLoadGameStateCurrently(Common::U32String *msg) {
	if (msg) {
		if (ConfMan.get("gameid") == "strangeland") {
			*msg = _("This game does not support loading from the menu. Use in-game interface");
			return false;
		}
		if (_G(noScummSaveLoad))
			*msg = _("To preserve the original experience, this game should be loaded using the in-game interface.\nYou can, however, override this setting in Game Options.");
	}

	return !_GP(thisroom).Options.SaveLoadDisabled &&
		   !_G(inside_script) && !_GP(play).fast_forward && !_G(no_blocking_functions) &&
		   !_G(noScummSaveLoad);
}

bool AGSEngine::canSaveGameStateCurrently(Common::U32String *msg) {
	if (msg) {
		if (ConfMan.get("gameid") == "strangeland") {
			*msg = _("This game does not support saving from the menu. Use in-game interface");
			return false;
		}
		if (_G(noScummSaveLoad))
			*msg = _("To preserve the original experience, this game should be saved using the in-game interface.\nYou can, however, override this setting in Game Options.");
	}

	return !_GP(thisroom).Options.SaveLoadDisabled &&
		   !_G(inside_script) && !_GP(play).fast_forward && !_G(no_blocking_functions) &&
		   !_G(noScummSaveLoad);
}

Common::Error AGSEngine::loadGameState(int slot) {
	(void)AGS3::try_restore_save(slot);
	return Common::kNoError;
}

Common::Error AGSEngine::saveGameState(int slot, const Common::String &desc, bool isAutosave) {
	(void)AGS3::save_game(slot, desc.c_str());
	return Common::kNoError;
}

int AGSEngine::getAutosaveSlot() const {
	if (!g_engine || !_G(noScummAutosave))
		return 0;
	else
		return -1;
}

void AGSEngine::GUIError(const Common::String &msg) {
	GUIErrorMessage(msg);
}

void AGSEngine::syncSoundSettings() {
	// Digital audio
	Engine::syncSoundSettings();
	// MIDI
	_music->syncVolume();
}

} // namespace AGS