forked from SteamDatabase/FileDetectionRuleSets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileDetector.php
334 lines (286 loc) · 7.7 KB
/
FileDetector.php
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
<?php
declare(strict_types=1);
class FileDetector
{
public bool $FilterEvidenceMatches = true;
/** @var string[] */
public array $Map = [];
/** @var string[] */
public array $Regexes = [];
/**
* @param ?array<string, array<string, string|string[]>> $Rulesets
*/
public function __construct( ?array $Rulesets, ?string $Path )
{
if( $Rulesets === null )
{
if( $Path === null )
{
throw new RuntimeException( 'Pass in rulesets or path.' );
}
/** @var array<string, array<string, string|string[]>> $Rulesets */
$Rulesets = parse_ini_file( $Path, true, INI_SCANNER_RAW );
}
// This is a common regex to detect folders (or files in root folder),
// as there are enough of these rules, we combine these into a subregex
$CommonFolderPrefix = '(?:^|/)';
$MarkIndex = 0;
foreach( $Rulesets as $Type => $Rules )
{
$Regexes =
[
0 => [],
1 => [],
];
foreach( $Rules as $Name => $RuleRegexes )
{
if( !is_array( $RuleRegexes ) )
{
$RuleRegexes = [ $RuleRegexes ];
}
foreach( $RuleRegexes as $Regex )
{
$this->Map[ $MarkIndex ] = "$Type.$Name";
if( str_starts_with( $Regex, $CommonFolderPrefix ) )
{
$Regexes[ 0 ][] = substr( $Regex, strlen( $CommonFolderPrefix ) ) . '(*:' . $MarkIndex . ')';
}
else
{
$Regexes[ 1 ][] = $Regex . '(*:' . $MarkIndex . ')';
}
$MarkIndex++;
}
}
if( !empty( $Regexes[ 0 ] ) )
{
sort( $Regexes[ 0 ] );
$this->Regexes[] = '~' . $CommonFolderPrefix . '(?:' . implode( '|', $Regexes[ 0 ] ) . ')~i';
}
if( !empty( $Regexes[ 1 ] ) )
{
sort( $Regexes[ 1 ] );
$this->Regexes[] = '~' . implode( '|', $Regexes[ 1 ] ) . '~i';
}
}
}
/**
* @param string[] $Files
*
* @return array<array{File: string, Match: string}>
*/
public function GetMatchedFiles( array $Files ) : array
{
$Matches = [];
foreach( $Files as $Path )
{
foreach( $this->Regexes as $Regex )
{
if( preg_match( $Regex, $Path, $RegexMatches ) === 1 )
{
$Match = $this->Map[ $RegexMatches[ 'MARK' ] ];
$Matches[] =
[
'File' => $Path,
'Match' => $Match,
];
}
}
}
return $Matches;
}
/**
* @param string[] $Files
*
* @return array<string, int>
*/
public function GetMatchesForFileList( array $Files ) : array
{
$Matches = [];
foreach( $Files as $Path )
{
foreach( $this->Regexes as $Regex )
{
if( preg_match( $Regex, $Path, $RegexMatches ) === 1 )
{
$Match = $this->Map[ $RegexMatches[ 'MARK' ] ];
if( isset( $Matches[ $Match ] ) )
{
$Matches[ $Match ]++;
}
else
{
$Matches[ $Match ] = 1;
}
}
}
}
if( !empty( $Matches ) )
{
$EducatedGuess = self::TryDeduceEngine( $Files, $Matches );
if( $EducatedGuess !== null )
{
$Matches[ $EducatedGuess ] = 1;
}
if( $this->FilterEvidenceMatches )
{
$Matches = array_filter(
$Matches,
fn( string $Match ) : bool => !str_starts_with( $Match, 'Evidence.' ),
ARRAY_FILTER_USE_KEY
);
}
}
return $Matches;
}
/**
* @param string[] $Files
* @param array<string, int> $Matches
*/
private static function TryDeduceEngine( array $Files, array $Matches ) : ?string
{
// helper functions
$has = fn( string $Match ) : bool => isset( $Matches[ $Match ] );
$not = fn( string $Match ) : bool => !isset( $Matches[ $Match ] );
$count = function( array $Search ) use ( $Matches ) : int
{
$Count = 0;
foreach( $Search as $Match )
{
if( isset( $Matches[ $Match ] ) )
{
$Count++;
}
}
return $Count;
};
if( $has( 'Evidence.ARC' ) && $has( 'Evidence.TAB' ) )
{
return 'Engine.ApexEngine';
}
if( $has( 'Evidence.RPF' ) && $has( 'Evidence.METADATA_DAT' ) )
{
return 'Engine.RAGE';
}
if( $has( 'Evidence.HDLL' ) && $not( 'Engine.Lime_OR_OpenFL' ) )
{
return 'Engine.Heaps';
}
if( $has( 'Emulator.DOSBOX' ) )
{
//If it's a DOS game...
if( $has( 'Evidence.Build' ) )
{
//If it matches the pattern of a Build engine game (Duke Nukem 3D engine)
return 'Engine.Build';
}
else if( $has( 'Evidence.VSWAP' ) )
{
//If it's got VSWAP files it's probably idTech0 (Wolf3D engine)
return 'Engine.idTech0';
}
else if( $has( 'Evidence.CFG' ) && $has( 'Evidence.WAD' ) )
{
//If it's got CFG and WAD files it's probably idTech1 (DOOM engine)
return 'Engine.idTech1';
}
}
//.u files only turn up in idTech0 and UnrealEngine games -- if we haven't positively ID'd idTech0 so far, it's Unreal
if( $has( 'Evidence.U' ) && $not( 'Emulator.DOSBOX' ) )
{
return 'Engine.Unreal';
}
//If we have both BIF and TLK files it's probably a BioWare Engine
if( $count( [ 'Evidence.BIF', 'Evidence.TLK' ] ) > 1 )
{
if( $has('Evidence.RIM') || $has('Evidence.TGA') )
{
//RIM and TGA are found in Aurora but not in Infinity
return 'Engine.Aurora';
}
return 'Engine.Infinity';
}
//Any 2 of options.ini + data.win + snd_<whatever>.ogg is a good sign of a GameMaker Game
if( $count( [ 'Evidence.OPTIONS_INI', 'Evidence.DATA_WIN', 'Evidence.SND_OGG' ] ) > 1)
{
return 'Engine.GameMaker';
}
//If it's got the Sierra interpreter and also .SCR files
if( $has( 'Evidence.SIERRA_EXE' ) && $has( 'Evidence.SCR' ) )
{
return 'Engine.SCI';
}
//If I have a PCK file it might be Godot
if( $has( 'Evidence.PCK' ) && $count(['Engine.Unreal', 'Engine.idTech5', 'Engine.idTech6', 'Engine.idTech7', 'Emulator.DOSBOX']) == 0 && self::IsEngineGodot( $Files ) )
{
return 'Engine.Godot';
}
//If I have matched nothing so far and I have a PK3 file, it's likely idTech3 (Quake3 engine)
if( $has( 'Evidence.PK3' ) )
{
return 'Engine.idTech3';
}
return null;
}
/**
* @param string[] $Files
*/
private static function IsEngineGodot( array $Files ) : bool
{
//This is a really long and annoying check. Basically we have two things to look for:
//1. A single .pck file named exactly "data.pck", and NO other pck files
//2. For every executable, a correspondingly named pck file, and no other pck files
$swapExtension = fn( string $FileName, string $OldExtension, string $NewExtension ) : string => basename( $FileName, $OldExtension ) . $NewExtension;
$Pcks = [];
$Exes = [];
foreach( $Files as $File )
{
$Extension = strtolower( pathinfo( $File, PATHINFO_EXTENSION ) );
$BaseFile = basename( $File );
if( $Extension === 'pck' )
{
$Pcks[ $BaseFile ] = true;
}
if( $Extension === 'exe' )
{
$Exes[ $swapExtension( $BaseFile, ".exe", ".pck" ) ] = true;
}
else if( $Extension === 'x86' ) // 32-bit Linux in Godot 2.x/3.x
{
$Exes[ $swapExtension( $BaseFile, ".x86", ".pck" ) ] = true;
}
else if( $Extension === 'x86_32' ) // 32-bit Linux in Godot 4.x
{
$Exes[ $swapExtension( $BaseFile, ".x86_32", ".pck" ) ] = true;
}
else if( $Extension === 'x86_64' ) // 64-bit Linux in all versions
{
$Exes[ $swapExtension( $BaseFile, ".x86_64", ".pck" ) ] = true;
}
else
{
//Mac and Linux can have extensionless executables, make sure we don't have folders in the mix, just filenames though
$Exes[ $BaseFile . ".pck" ] = true;
}
}
// This can happen if Evidence.PCK finds "BASE.PCK", but the $Pcks will be empty due to case sensitivity
if( !empty( $Pcks ) )
{
//If we have exactly 1 PCK file and it is data.pck, we can skip all the fancy checks
if( count( $Pcks ) === 1 && array_key_first( $Pcks ) === 'data.pck' )
{
return true;
}
//Otherwise we have to match up exe & pck pairs
foreach ( array_keys($Pcks) as $pck )
{
//If we match an exe and a pck file pair, we're good
if( isset( $Exes[ $pck ] ) )
{
return true;
}
}
}
return false;
}
}