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 "sword1/sword1.h"
24 #include "sword1/control.h"
25 
26 #include "base/plugins.h"
27 #include "common/fs.h"
28 #include "common/gui_options.h"
29 #include "common/savefile.h"
30 #include "common/system.h"
31 #include "graphics/thumbnail.h"
32 #include "graphics/surface.h"
33 
34 #include "engines/metaengine.h"
35 
36 /* Broken Sword */
37 static const PlainGameDescriptor sword1FullSettings =
38 	{"sword1", "Broken Sword: The Shadow of the Templars"};
39 static const PlainGameDescriptor sword1DemoSettings =
40 	{"sword1demo", "Broken Sword: The Shadow of the Templars (Demo)"};
41 static const PlainGameDescriptor sword1MacFullSettings =
42 	{"sword1mac", "Broken Sword: The Shadow of the Templars (Mac)"};
43 static const PlainGameDescriptor sword1MacDemoSettings =
44 	{"sword1macdemo", "Broken Sword: The Shadow of the Templars (Mac demo)"};
45 static const PlainGameDescriptor sword1PSXSettings =
46 	{"sword1psx", "Broken Sword: The Shadow of the Templars (PlayStation)"};
47 static const PlainGameDescriptor sword1PSXDemoSettings =
48 	{"sword1psxdemo", "Broken Sword: The Shadow of the Templars (PlayStation demo)"};
49 
50 
51 // check these subdirectories (if present)
52 static const char *const g_dirNames[] = { "clusters", "speech", "english", "italian"};
53 
54 #define NUM_COMMON_FILES_TO_CHECK 1
55 #define NUM_PC_FILES_TO_CHECK 3
56 #define NUM_MAC_FILES_TO_CHECK 4
57 #define NUM_PSX_FILES_TO_CHECK 1
58 #define NUM_PSX_DEMO_FILES_TO_CHECK 2
59 #define NUM_DEMO_FILES_TO_CHECK 1
60 #define NUM_MAC_DEMO_FILES_TO_CHECK 1
61 
62 #define NUM_FILES_TO_CHECK NUM_COMMON_FILES_TO_CHECK + NUM_PC_FILES_TO_CHECK + NUM_MAC_FILES_TO_CHECK + NUM_PSX_FILES_TO_CHECK + NUM_DEMO_FILES_TO_CHECK + NUM_MAC_DEMO_FILES_TO_CHECK + NUM_PSX_DEMO_FILES_TO_CHECK
63 static const char *const g_filesToCheck[NUM_FILES_TO_CHECK] = { // these files have to be found
64 	"swordres.rif", // Mac, PC and PSX version
65 	"general.clu", // PC and PSX version
66 	"compacts.clu", // PC and PSX version
67 	"scripts.clu", // PC and PSX version
68 	"general.clm", // Mac version only
69 	"compacts.clm", // Mac version only
70 	"scripts.clm", // Mac version only
71 	"paris2.clm", // Mac version (full game only)
72 	"cows.mad", // this one should only exist in the demo version
73 	"scripts.clm", // Mac version both demo and full game
74 	"train.plx", // PSX version only
75 	"speech.dat", // PSX version only
76 	"tunes.dat", // PSX version only
77 	// the engine needs several more files to work, but checking these should be sufficient
78 };
79 
80 class SwordMetaEngine : public MetaEngine {
81 public:
getName() const82 	virtual const char *getName() const {
83 		return "Broken Sword: The Shadow of the Templars";
84 	}
getOriginalCopyright() const85 	virtual const char *getOriginalCopyright() const {
86 		return "Broken Sword: The Shadow of the Templars (C) Revolution";
87 	}
88 
89 	virtual bool hasFeature(MetaEngineFeature f) const;
90 	PlainGameList getSupportedGames() const override;
91 	PlainGameDescriptor findGame(const char *gameId) const override;
92 	DetectedGames detectGames(const Common::FSList &fslist) const override;
93 	virtual SaveStateList listSaves(const char *target) const;
94 	virtual int getMaximumSaveSlot() const;
95 	virtual void removeSaveState(const char *target, int slot) const;
96 	SaveStateDescriptor querySaveMetaInfos(const char *target, int slot) const;
97 
98 	virtual Common::Error createInstance(OSystem *syst, Engine **engine) const;
99 };
100 
hasFeature(MetaEngineFeature f) const101 bool SwordMetaEngine::hasFeature(MetaEngineFeature f) const {
102 	return
103 	    (f == kSupportsListSaves) ||
104 	    (f == kSupportsLoadingDuringStartup) ||
105 	    (f == kSupportsDeleteSave) ||
106 	    (f == kSavesSupportMetaInfo) ||
107 	    (f == kSavesSupportThumbnail) ||
108 	    (f == kSavesSupportCreationDate) ||
109 	    (f == kSavesSupportPlayTime);
110 }
111 
hasFeature(EngineFeature f) const112 bool Sword1::SwordEngine::hasFeature(EngineFeature f) const {
113 	return
114 	    (f == kSupportsRTL) ||
115 	    (f == kSupportsSavingDuringRuntime) ||
116 	    (f == kSupportsLoadingDuringRuntime);
117 }
118 
getSupportedGames() const119 PlainGameList SwordMetaEngine::getSupportedGames() const {
120 	PlainGameList games;
121 	games.push_back(sword1FullSettings);
122 	games.push_back(sword1DemoSettings);
123 	games.push_back(sword1MacFullSettings);
124 	games.push_back(sword1MacDemoSettings);
125 	games.push_back(sword1PSXSettings);
126 	games.push_back(sword1PSXDemoSettings);
127 	return games;
128 }
129 
findGame(const char * gameId) const130 PlainGameDescriptor SwordMetaEngine::findGame(const char *gameId) const {
131 	if (0 == scumm_stricmp(gameId, sword1FullSettings.gameId))
132 		return sword1FullSettings;
133 	if (0 == scumm_stricmp(gameId, sword1DemoSettings.gameId))
134 		return sword1DemoSettings;
135 	if (0 == scumm_stricmp(gameId, sword1MacFullSettings.gameId))
136 		return sword1MacFullSettings;
137 	if (0 == scumm_stricmp(gameId, sword1MacDemoSettings.gameId))
138 		return sword1MacDemoSettings;
139 	if (0 == scumm_stricmp(gameId, sword1PSXSettings.gameId))
140 		return sword1PSXSettings;
141 	if (0 == scumm_stricmp(gameId, sword1PSXDemoSettings.gameId))
142 		return sword1PSXDemoSettings;
143 	return PlainGameDescriptor::empty();
144 }
145 
Sword1CheckDirectory(const Common::FSList & fslist,bool * filesFound,bool recursion=false)146 void Sword1CheckDirectory(const Common::FSList &fslist, bool *filesFound, bool recursion = false) {
147 	for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
148 		if (!file->isDirectory()) {
149 			// The required game data files can be located in the game directory, or in
150 			// a subdirectory called "clusters". In the latter case, we don't want to
151 			// detect the game in that subdirectory, as this will detect the game twice
152 			// when mass add is searching inside a directory. In this case, the first
153 			// result (the game directory) will be correct, but the second result (the
154 			// clusters subdirectory) will be wrong, as the optional speech, music and
155 			// video data files will be ignored. Note that this fix will skip the game
156 			// data files if the user has placed them inside a "clusters" subdirectory,
157 			// or if he/she points ScummVM directly to the "clusters" directory of the
158 			// game CD. Fixes bug #3049346.
159 			Common::String directory = file->getParent().getName();
160 			directory.toLowercase();
161 			if (directory.hasPrefix("clusters") && directory.size() <= 9 && !recursion)
162 				continue;
163 
164 			for (int cnt = 0; cnt < NUM_FILES_TO_CHECK; cnt++)
165 				if (scumm_stricmp(file->getName().c_str(), g_filesToCheck[cnt]) == 0)
166 					filesFound[cnt] = true;
167 		} else {
168 			for (int cnt = 0; cnt < ARRAYSIZE(g_dirNames); cnt++)
169 				if (scumm_stricmp(file->getName().c_str(), g_dirNames[cnt]) == 0) {
170 					Common::FSList fslist2;
171 					if (file->getChildren(fslist2, Common::FSNode::kListFilesOnly))
172 						Sword1CheckDirectory(fslist2, filesFound, true);
173 				}
174 		}
175 	}
176 }
177 
detectGames(const Common::FSList & fslist) const178 DetectedGames SwordMetaEngine::detectGames(const Common::FSList &fslist) const {
179 	int i, j;
180 	DetectedGames detectedGames;
181 	bool filesFound[NUM_FILES_TO_CHECK];
182 	for (i = 0; i < NUM_FILES_TO_CHECK; i++)
183 		filesFound[i] = false;
184 
185 	Sword1CheckDirectory(fslist, filesFound);
186 	bool mainFilesFound = true;
187 	bool pcFilesFound = true;
188 	bool macFilesFound = true;
189 	bool demoFilesFound = true;
190 	bool macDemoFilesFound = true;
191 	bool psxFilesFound = true;
192 	bool psxDemoFilesFound = true;
193 	for (i = 0; i < NUM_COMMON_FILES_TO_CHECK; i++)
194 		if (!filesFound[i])
195 			mainFilesFound = false;
196 	for (j = 0; j < NUM_PC_FILES_TO_CHECK; i++, j++)
197 		if (!filesFound[i])
198 			pcFilesFound = false;
199 	for (j = 0; j < NUM_MAC_FILES_TO_CHECK; i++, j++)
200 		if (!filesFound[i])
201 			macFilesFound = false;
202 	for (j = 0; j < NUM_DEMO_FILES_TO_CHECK; i++, j++)
203 		if (!filesFound[i])
204 			demoFilesFound = false;
205 	for (j = 0; j < NUM_DEMO_FILES_TO_CHECK; i++, j++)
206 		if (!filesFound[i])
207 			macDemoFilesFound = false;
208 	for (j = 0; j < NUM_PSX_FILES_TO_CHECK; i++, j++)
209 		if (!filesFound[i])
210 			psxFilesFound = false;
211 	for (j = 0; j < NUM_PSX_DEMO_FILES_TO_CHECK; i++, j++)
212 		if (!filesFound[i] || psxFilesFound)
213 			psxDemoFilesFound = false;
214 
215 	DetectedGame game;
216 	if (mainFilesFound && pcFilesFound && demoFilesFound)
217 		game = DetectedGame(sword1DemoSettings);
218 	else if (mainFilesFound && pcFilesFound && psxFilesFound)
219 		game = DetectedGame(sword1PSXSettings);
220 	else if (mainFilesFound && pcFilesFound && psxDemoFilesFound)
221 		game = DetectedGame(sword1PSXDemoSettings);
222 	else if (mainFilesFound && pcFilesFound && !psxFilesFound)
223 		game = DetectedGame(sword1FullSettings);
224 	else if (mainFilesFound && macFilesFound)
225 		game = DetectedGame(sword1MacFullSettings);
226 	else if (mainFilesFound && macDemoFilesFound)
227 		game = DetectedGame(sword1MacDemoSettings);
228 	else
229 		return detectedGames;
230 
231 	game.setGUIOptions(GUIO2(GUIO_NOMIDI, GUIO_NOASPECT));
232 
233 	game.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(Common::EN_ANY));
234 	game.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(Common::DE_DEU));
235 	game.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(Common::FR_FRA));
236 	game.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(Common::IT_ITA));
237 	game.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(Common::ES_ESP));
238 	game.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(Common::PT_BRA));
239 	game.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(Common::CZ_CZE));
240 
241 	detectedGames.push_back(game);
242 
243 	return detectedGames;
244 }
245 
createInstance(OSystem * syst,Engine ** engine) const246 Common::Error SwordMetaEngine::createInstance(OSystem *syst, Engine **engine) const {
247 	assert(engine);
248 	*engine = new Sword1::SwordEngine(syst);
249 	return Common::kNoError;
250 }
251 
listSaves(const char * target) const252 SaveStateList SwordMetaEngine::listSaves(const char *target) const {
253 	Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
254 	SaveStateList saveList;
255 	char saveName[40];
256 
257 	Common::StringArray filenames = saveFileMan->listSavefiles("sword1.###");
258 
259 	int slotNum = 0;
260 	for (Common::StringArray::const_iterator file = filenames.begin(); file != filenames.end(); ++file) {
261 		// Obtain the last 3 digits of the filename, since they correspond to the save slot
262 		slotNum = atoi(file->c_str() + file->size() - 3);
263 
264 		if (slotNum >= 0 && slotNum <= 999) {
265 			Common::InSaveFile *in = saveFileMan->openForLoading(*file);
266 			if (in) {
267 				in->readUint32LE(); // header
268 				in->read(saveName, 40);
269 				saveList.push_back(SaveStateDescriptor(slotNum, saveName));
270 				delete in;
271 			}
272 		}
273 	}
274 
275 	// Sort saves based on slot number.
276 	Common::sort(saveList.begin(), saveList.end(), SaveStateDescriptorSlotComparator());
277 	return saveList;
278 }
279 
getMaximumSaveSlot() const280 int SwordMetaEngine::getMaximumSaveSlot() const { return 999; }
281 
removeSaveState(const char * target,int slot) const282 void SwordMetaEngine::removeSaveState(const char *target, int slot) const {
283 	g_system->getSavefileManager()->removeSavefile(Common::String::format("sword1.%03d", slot));
284 }
285 
querySaveMetaInfos(const char * target,int slot) const286 SaveStateDescriptor SwordMetaEngine::querySaveMetaInfos(const char *target, int slot) const {
287 	Common::String fileName = Common::String::format("sword1.%03d", slot);
288 	char name[40];
289 	uint32 playTime = 0;
290 	byte versionSave;
291 
292 	Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(fileName);
293 
294 	if (in) {
295 		in->skip(4);        // header
296 		in->read(name, sizeof(name));
297 		in->read(&versionSave, 1);      // version
298 
299 		SaveStateDescriptor desc(slot, name);
300 
301 		if (versionSave < 2) // These older version of the savegames used a flag to signal presence of thumbnail
302 			in->skip(1);
303 
304 		if (Graphics::checkThumbnailHeader(*in)) {
305 			Graphics::Surface *thumbnail;
306 			if (!Graphics::loadThumbnail(*in, thumbnail)) {
307 				delete in;
308 				return SaveStateDescriptor();
309 			}
310 			desc.setThumbnail(thumbnail);
311 		}
312 
313 		uint32 saveDate = in->readUint32BE();
314 		uint16 saveTime = in->readUint16BE();
315 		if (versionSave > 1) // Previous versions did not have playtime data
316 			playTime = in->readUint32BE();
317 
318 		int day = (saveDate >> 24) & 0xFF;
319 		int month = (saveDate >> 16) & 0xFF;
320 		int year = saveDate & 0xFFFF;
321 
322 		desc.setSaveDate(year, month, day);
323 
324 		int hour = (saveTime >> 8) & 0xFF;
325 		int minutes = saveTime & 0xFF;
326 
327 		desc.setSaveTime(hour, minutes);
328 
329 		if (versionSave > 1) {
330 			desc.setPlayTime(playTime * 1000);
331 		} else { //We have no playtime data
332 			desc.setPlayTime(0);
333 		}
334 
335 		delete in;
336 
337 		return desc;
338 	}
339 
340 	return SaveStateDescriptor();
341 }
342 
343 #if PLUGIN_ENABLED_DYNAMIC(SWORD1)
344 	REGISTER_PLUGIN_DYNAMIC(SWORD1, PLUGIN_TYPE_ENGINE, SwordMetaEngine);
345 #else
346 	REGISTER_PLUGIN_STATIC(SWORD1, PLUGIN_TYPE_ENGINE, SwordMetaEngine);
347 #endif
348 
349 namespace Sword1 {
350 
loadGameState(int slot)351 Common::Error SwordEngine::loadGameState(int slot) {
352 	_systemVars.forceRestart = false;
353 	_systemVars.controlPanelMode = CP_NORMAL;
354 	_control->restoreGameFromFile(slot);
355 	reinitialize();
356 	_control->doRestore();
357 	reinitRes();
358 	return Common::kNoError;    // TODO: return success/failure
359 }
360 
canLoadGameStateCurrently()361 bool SwordEngine::canLoadGameStateCurrently() {
362 	return (mouseIsActive() && !_control->isPanelShown()); // Disable GMM loading when game panel is shown
363 }
364 
saveGameState(int slot,const Common::String & desc)365 Common::Error SwordEngine::saveGameState(int slot, const Common::String &desc) {
366 	_control->setSaveDescription(slot, desc.c_str());
367 	_control->saveGameToFile(slot);
368 	return Common::kNoError;    // TODO: return success/failure
369 }
370 
canSaveGameStateCurrently()371 bool SwordEngine::canSaveGameStateCurrently() {
372 	return (mouseIsActive() && !_control->isPanelShown());
373 }
374 
375 } // End of namespace Sword1
376