1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "ags/ags.h"
24 #include "ags/detection.h"
25 #include "ags/events.h"
26 #include "ags/game_scanner.h"
27 #include "ags/music.h"
28 #include "ags/console.h"
29 #include "common/scummsys.h"
30 #include "common/config-manager.h"
31 #include "common/debug-channels.h"
32 #include "common/events.h"
33 #include "common/file.h"
34 #include "common/util.h"
35 #include "engines/util.h"
36 
37 #include "ags/shared/core/platform.h"
38 #define AGS_PLATFORM_DEFINES_PSP_VARS (AGS_PLATFORM_OS_IOS || AGS_PLATFORM_OS_ANDROID)
39 
40 #include "ags/lib/std/set.h"
41 #include "ags/shared/ac/common.h"
42 #include "ags/engine/ac/game.h"
43 #include "ags/globals.h"
44 #include "ags/engine/ac/game_setup.h"
45 #include "ags/engine/ac/game_state.h"
46 #include "ags/engine/ac/room.h"
47 #include "ags/shared/core/def_version.h"
48 #include "ags/engine/debugging/debugger.h"
49 #include "ags/engine/debugging/debug_log.h"
50 #include "ags/shared/debugging/out.h"
51 #include "ags/engine/game/savegame.h"
52 #include "ags/engine/main/config.h"
53 #include "ags/engine/main/engine.h"
54 #include "ags/engine/main/main.h"
55 #include "ags/engine/main/quit.h"
56 #include "ags/engine/platform/base/ags_platform_driver.h"
57 #include "ags/engine/script/script.h"
58 #include "ags/engine/ac/route_finder.h"
59 #include "ags/shared/core/asset_manager.h"
60 #include "ags/shared/util/directory.h"
61 #include "ags/shared/script/cc_options.h"
62 
63 #ifdef ENABLE_AGS_TESTS
64 #include "ags/tests/test_all.h"
65 #endif
66 
67 // Include translation.h last as some AGS classes have member such as _sc, which clash with
68 // macro defined in translation.h.
69 #include "common/translation.h"
70 
71 namespace AGS {
72 
73 AGSEngine *g_vm;
74 
AGSEngine(OSystem * syst,const AGSGameDescription * gameDesc)75 AGSEngine::AGSEngine(OSystem *syst, const AGSGameDescription *gameDesc) : Engine(syst),
76 	_gameDescription(gameDesc), _randomSource("AGS"), _events(nullptr), _music(nullptr),
77 	_gfxDriver(nullptr), _globals(nullptr), _forceTextAA(false) {
78 	g_vm = this;
79 
80 	_events = new EventsManager();
81 	_music = new Music();
82 	_globals = new ::AGS3::Globals();
83 
84 	Common::String forceAA;
85 	if (ConfMan.getActiveDomain()->tryGetVal("force_text_aa", forceAA))
86 		Common::parseBool(forceAA, _forceTextAA);
87 
88 	// WORKAROUND: Certain games need to force AA to render the text correctly
89 	if (_gameDescription->desc.flags & GAMEFLAG_FORCE_AA)
90 		_forceTextAA = true;
91 }
92 
~AGSEngine()93 AGSEngine::~AGSEngine() {
94 	if (_globals && _G(proper_exit) == 0) {
95 		_G(platform)->DisplayAlert("Error: the program has exited without requesting it.\n"
96 		                           "Program pointer: %+03d  (write this number down), ACI version %s\n"
97 		                           "If you see a list of numbers above, please write them down and contact\n"
98 		                           "developers. Otherwise, note down any other information displayed.",
99 		                           _G(our_eip), _G(EngineVersion).LongString.GetCStr());
100 	}
101 
102 	delete _events;
103 	delete _music;
104 	delete _globals;
105 }
106 
getFeatures() const107 uint32 AGSEngine::getFeatures() const {
108 	return _gameDescription->desc.flags;
109 }
110 
getNeededPlugins() const111 const PluginVersion *AGSEngine::getNeededPlugins() const {
112 	return _gameDescription->_plugins;
113 }
114 
getGameId() const115 Common::String AGSEngine::getGameId() const {
116 	return _gameDescription->desc.gameId;
117 }
118 
run()119 Common::Error AGSEngine::run() {
120 	if (debugChannelSet(-1, kDebugScan)) {
121 		// Scan the given folder and subfolders for unknown games
122 		AGS3::GameScanner scanner;
123 		scanner.scan(ConfMan.get("path"));
124 		return Common::kNoError;
125 	}
126 
127 	if (isUnsupportedPre25()) {
128 		GUIErrorMessage(_("The selected game uses a pre-2.5 version of the AGS engine, which is not supported."));
129 		return Common::kNoError;
130 	}
131 
132 	if (is64BitGame()) {
133 		// If the game file was opened and the engine started, but the
134 		// size is -1, then it must be a game like Strangeland where
135 		// the data file is > 2Gb
136 		GUIErrorMessage(_("The selected game has a data file greater than 2Gb, "
137 			"which isn't supported by your version of ScummVM yet."));
138 		return Common::kNoError;
139 	}
140 
141 	if (debugChannelSet(-1, kDebugScript))
142 		AGS3::ccSetOption(SCOPT_DEBUGRUN, 1);
143 
144 #ifdef ENABLE_AGS_TESTS
145 	AGS3::Test_DoAllTests();
146 	return Common::kNoError;
147 #endif
148 
149 	setDebugger(new AGSConsole(this));
150 
151 	const char *filename = _gameDescription->desc.filesDescriptions[0].fileName;
152 	const char *ARGV[] = { "scummvm.exe", filename };
153 	const int ARGC = 2;
154 	AGS3::main_init(ARGC, ARGV);
155 
156 	_G(debug_flags) = 0;
157 
158 	if (ConfMan.hasKey("display_fps"))
159 		_G(display_fps) = ConfMan.getBool("display_fps") ? AGS3::kFPS_Forced : AGS3::kFPS_Hide;
160 
161 	AGS3::ConfigTree startup_opts;
162 	int res = AGS3::main_process_cmdline(startup_opts, ARGC, ARGV);
163 	if (res != 0)
164 		return Common::kUnknownError;
165 
166 	if (_G(justDisplayVersion)) {
167 		_G(platform)->WriteStdOut(AGS3::get_engine_string().GetCStr());
168 		return Common::kNoError;
169 	}
170 
171 	if (_G(justDisplayHelp)) {
172 		AGS3::main_print_help();
173 		return Common::kNoError;
174 	}
175 
176 	if (!_G(justTellInfo))
177 		_G(platform)->SetGUIMode(true);
178 	AGS3::init_debug(startup_opts, _G(justTellInfo));
179 	AGS3::AGS::Shared::Debug::Printf("%s", AGS3::get_engine_string().GetCStr());
180 
181 	AGS3::main_set_gamedir(ARGC, ARGV);
182 
183 	// Update shell associations and exit
184 	if (_G(debug_flags) & DBG_REGONLY)
185 		return Common::kNoError;
186 
187 	_G(loadSaveGameOnStartup) = ConfMan.getInt("save_slot");
188 
189 	syncSoundSettings();
190 	AGS3::initialize_engine(startup_opts);
191 
192 	// Do shutdown stuff
193 	::AGS3::quit_free();
194 
195 	return Common::kNoError;
196 }
197 
listSaves() const198 SaveStateList AGSEngine::listSaves() const {
199 	return getMetaEngine()->listSaves(_targetName.c_str());
200 }
201 
getPixelFormat(int depth,Graphics::PixelFormat & format) const202 bool AGSEngine::getPixelFormat(int depth, Graphics::PixelFormat &format) const {
203 	Common::List<Graphics::PixelFormat> supportedFormatsList = g_system->getSupportedFormats();
204 
205 	if (depth == 8) {
206 		format = Graphics::PixelFormat::createFormatCLUT8();
207 		return true;
208 	}
209 
210 	// Prefer format with the requested color depth
211 	for (Common::List<Graphics::PixelFormat>::iterator it =
212 			supportedFormatsList.begin(); it != supportedFormatsList.end(); ++it) {
213 		if (it->bpp() == depth) {
214 			format = *it;
215 			return true;
216 		}
217 	}
218 
219 	// Allow using 16 bit <-> 32 bit conversions by using the preferred graphics mode
220 	if (!supportedFormatsList.empty()) {
221 		format = supportedFormatsList.front();
222 		return true;
223 	}
224 
225 	return false;
226 }
227 
228 
setGraphicsMode(size_t w,size_t h,int colorDepth)229 void AGSEngine::setGraphicsMode(size_t w, size_t h, int colorDepth) {
230 	Common::List<Graphics::PixelFormat> supportedFormatsList = g_system->getSupportedFormats();
231 	Graphics::PixelFormat format;
232 	if (!getPixelFormat(colorDepth, format))
233 		error("Unsupported color depth %d", colorDepth);
234 
235 	initGraphics(w, h, &format);
236 }
237 
isUnsupportedPre25() const238 bool AGSEngine::isUnsupportedPre25() const {
239 	return _gameDescription->desc.extra &&
240 		!strcmp(_gameDescription->desc.extra, "Pre 2.5");
241 }
242 
is64BitGame() const243 bool AGSEngine::is64BitGame() const {
244 	Common::File f;
245 	return f.open(_gameDescription->desc.filesDescriptions[0].fileName)
246 		&& f.size() == -1;
247 }
248 
getGameFolder()249 Common::FSNode AGSEngine::getGameFolder() {
250 	return Common::FSNode(ConfMan.get("path"));
251 }
252 
canLoadGameStateCurrently()253 bool AGSEngine::canLoadGameStateCurrently() {
254 	return !_GP(thisroom).Options.SaveLoadDisabled &&
255 	       !_G(inside_script) && !_GP(play).fast_forward && !_G(no_blocking_functions);
256 }
257 
canSaveGameStateCurrently()258 bool AGSEngine::canSaveGameStateCurrently() {
259 	return !_GP(thisroom).Options.SaveLoadDisabled &&
260 	       !_G(inside_script) && !_GP(play).fast_forward && !_G(no_blocking_functions);
261 }
262 
loadGameState(int slot)263 Common::Error AGSEngine::loadGameState(int slot) {
264 	(void)AGS3::try_restore_save(slot);
265 	return Common::kNoError;
266 }
267 
saveGameState(int slot,const Common::String & desc,bool isAutosave)268 Common::Error AGSEngine::saveGameState(int slot, const Common::String &desc, bool isAutosave) {
269 	(void)AGS3::save_game(slot, desc.c_str());
270 	return Common::kNoError;
271 }
272 
GUIError(const Common::String & msg)273 void AGSEngine::GUIError(const Common::String &msg) {
274 	GUIErrorMessage(msg);
275 }
276 
syncSoundSettings()277 void AGSEngine::syncSoundSettings() {
278 	// Digital audio
279 	Engine::syncSoundSettings();
280 	// MIDI
281 	_music->syncVolume();
282 }
283 
284 } // namespace AGS
285