File: game_scanner.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 (187 lines) | stat: -rw-r--r-- 5,657 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
/* 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/detection.h"
#include "ags/game_scanner.h"
#include "ags/shared/ac/game_setup_struct.h"
#include "ags/shared/core/asset_manager.h"
#include "ags/shared/util/multi_file_lib.h"
#include "ags/shared/util/string.h"
#include "ags/engine/main/game_file.h"
#include "ags/globals.h"
#include "common/config-manager.h"
#include "common/file.h"
#include "common/hashmap.h"
#include "common/md5.h"

namespace AGS3 {
/**
 * When detection is compiled dynamically, detection tables end up in detection plugin and
 * engine cannot link to them so don't include the feature instead of duplicating the data
 */
#ifdef DETECTION_STATIC

extern bool define_gamedata_location(const AGS::Shared::String &exe_path);
extern bool engine_try_init_gamedata(AGS::Shared::String gamepak_path);


void GameScanner::scan(const Common::Path &startFolder) {
	detectClashes();

	Common::FSNode folder(startFolder);
	scanFolder(folder);

	if (!_oldGames.empty()) {
		debug("// Pre 2.5 games that aren't supported");
		for (EntryArray::iterator it = _oldGames.begin(); it != _oldGames.end(); ++it) {
			debug("UNSUPPORTED_GAME_ENTRY(\"\", \"%s\", \"%s\", %u),",
			      it->_filename.c_str(), it->_md5.c_str(), (uint32)it->_filesize);
		}
		debugN("\n");
	}

	debug("// 2.5+ games that should be supported");
	Common::HashMap<Common::String, bool> gameDescs;
	for (EntryArray::iterator it = _games.begin(); it != _games.end(); ++it) {
		if (!gameDescs.contains(it->_id))
			debug("{ \"%s\", \"%s\" },", it->_id.c_str(), it->_gameName.c_str());
		gameDescs[it->_id] = true;
	}
	debugN("\n");

	for (EntryArray::iterator it = _games.begin(); it != _games.end(); ++it) {
		debug("GAME_ENTRY(\"%s\", \"%s\", \"%s\", %u),",
		      it->_id.c_str(), it->_filename.c_str(),
		      it->_md5.c_str(), (uint32)it->_filesize);
	}
	debugN("\n");
}

void GameScanner::scanFolder(const Common::FSNode &folder) {
	Common::FSList fslist;
	folder.getChildren(fslist, Common::FSNode::kListAll);

	for (uint idx = 0; idx < fslist.size(); ++idx) {
		Common::FSNode node = fslist[idx];
		Common::String filename = node.getName();

		if (node.isDirectory()) {
			scanFolder(node);
		} else if (filename.hasSuffixIgnoreCase(".exe") ||
		           filename.hasSuffixIgnoreCase(".ags") ||
		           filename.equalsIgnoreCase("ac2game.dat")) {
			Common::Path path = node.getPath();
			scanFile(path);
		}
	}
}

void GameScanner::scanFile(const Common::Path &filename) {
#ifdef TODO
	Common::File f;
	Common::FSNode fsNode(filename);
	if (!f.open(fsNode))
		return;

	int32 size = f.size();
	Common::String md5 = Common::computeStreamMD5AsString(f, 5000);

	// Check if it's an already known game
	const ::AGS::AGSGameDescription *gameP = ::AGS::GAME_DESCRIPTIONS;
	for (; gameP->desc.gameId; ++gameP) {
		if (size == gameP->desc.filesDescriptions[0].fileSize &&
		        md5 == gameP->desc.filesDescriptions[0].md5)
			// Known game, so skip
			return;
	}

	// Check the game file
	AGS::Shared::AssetLibInfo lib;
	AGS::Shared::StreamScummVMFile *stream = new AGS::Shared::StreamScummVMFile(&f);
	if (AGS::Shared::AssetManager::ReadDataFileTOC(stream, lib) != AGS::Shared::kAssetNoError)
		return;
	f.close();

	AGS::Shared::AssetManager::DestroyInstance();
	AGS::Shared::AssetManager::CreateInstance();
	AGS::Shared::AssetManager::SetDataFile(filename);

	if (!engine_try_init_gamedata(filename))
		return;

	AGS::Shared::HError err = preload_game_data();
	if (!err) {
		if (err->Code() == AGS::Shared::kMGFErr_FormatVersionTooOld) {
			Entry e;
			e._filename = fsNode.getName();
			e._filename.toLowercase();
			e._filesize = size;
			e._md5 = md5;

			_oldGames.push_back(e);
		}
	} else {
		// Add an entry for the found game
		Entry e;
		e._filename = fsNode.getName();
		e._filename.toLowercase();
		e._filesize = size;
		e._gameName = _GP(game).gamename;
		e._id = convertGameNameToId(e._gameName);
		e._md5 = md5;

		_games.push_back(e);
	}
#else
	error("TODO: GameScanner::scanFile");
#endif
}

Common::String GameScanner::convertGameNameToId(const Common::String &name) {
	Common::String result;

	for (uint idx = 0; idx < name.size(); ++idx) {
		char c = name[idx];
		if (Common::isDigit(c) || (tolower(c) >= 'a' && tolower(c) <= 'z'))
			result += tolower(c);
	}

	return result;
}

void GameScanner::detectClashes() {
	Common::HashMap<Common::String, bool> gameIds;
	Common::HashMap<Common::String, bool> gameNames;

	const PlainGameDescriptor *nameP = ::AGS::GAME_NAMES;
	for (; nameP->gameId; ++nameP) {
		if (gameIds.contains(nameP->gameId))
			debug("Duplicate game Id: %s", nameP->gameId);
		gameIds[nameP->gameId] = true;

		if (gameNames.contains(nameP->description))
			debug("Duplicate game name: %s", nameP->description);
		gameNames[nameP->description] = true;
	}
}
#endif

} // namespace AGS3