-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcasc.php
213 lines (175 loc) · 5.81 KB
/
casc.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Erorus\CASC;
function getHomeDir() {
$home = getenv('HOME');
if (!empty($home)) {
// home should never end with a trailing slash.
$home = rtrim($home, '/');
}
elseif (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
// home on windows
$home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
// If HOMEPATH is a root directory the path can end with a slash. Make sure
// that doesn't happen.
$home = rtrim($home, '\\/');
}
return empty($home) ? false : $home;
}
function main()
{
ini_set('memory_limit', '512M');
$dest = null;
$cachePath = $defaultCachePath = (getHomeDir() ?? __DIR__) . DIRECTORY_SEPARATOR . '.casc-cache';
$wowPath = null;
$program = 'wow';
$region = 'us';
$locale = 'enUS';
$listfile = '';
$shortOpts = 'o:c:p:r:l:f:w:hi';
$longOpts = [
'out:',
'cache:',
'program:',
'region:',
'locale:',
'files:',
'wow:',
'help',
'ignore',
'no-check-certificate',
];
$opts = getopt($shortOpts, $longOpts);
foreach ($opts as $k => $v) {
switch ($k) {
case 'h':
case 'help':
printHelp($defaultCachePath);
return 0;
case 'i':
case 'ignore':
CASC\DataSource::ignoreErrors(true);
break;
case 'o':
case 'out':
$dest = $v;
break;
case 'w':
case 'wow':
$wowPath = $v;
break;
case 'c':
case 'cache':
$cachePath = $v;
break;
case 'p':
case 'program':
$program = $v;
break;
case 'r':
case 'region':
$region = $v;
break;
case 'l':
case 'locale':
$locale = $v;
break;
case 'f':
case 'files':
$listfile = $v;
break;
case 'no-check-certificate':
CASC\HTTP::setCertVerification(false);
break;
}
}
if (is_null($dest)) {
echo "Required option 'out' not found, aborting.\n";
printHelp($defaultCachePath);
return 1;
}
if (is_null($listfile)) {
echo "Required option 'files' not found, aborting.\n";
printHelp($defaultCachePath);
return 1;
}
$dest = rtrim($dest, DIRECTORY_SEPARATOR);
if (!is_dir($dest) || !is_writable($dest)) {
echo "Out directory $dest is not found/writable, aborting.\n";
return 1;
}
if ($wowPath) {
$wowPath = rtrim($wowPath, DIRECTORY_SEPARATOR);
if (!is_dir($wowPath)) {
echo "WoW directory $wowPath is not found, aborting.\n";
return 1;
}
}
$cachePath = rtrim($cachePath, DIRECTORY_SEPARATOR);
if (!file_exists($listfile) || !is_readable($listfile)) {
echo "Files list $listfile is not readable, aborting.\n";
return 1;
}
$files = file($listfile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
echo "Output dir: $dest\n";
echo "Cache dir: $cachePath\n";
try {
$ngdp = new CASC\NGDP($cachePath, $wowPath, $program, $region, $locale);
} catch (\Exception $e) {
echo $e->getMessage(), "\n";
return 1;
}
echo "\n";
$successCount = 0;
$totalCount = 0;
foreach ($files as $file) {
$file = trim($file);
if (!$file) {
continue;
}
if (preg_match('/^(\d+)\W*([\w\W]+)$/', $file, $res)) {
$destName = $res[2];
$file = $res[1];
} else {
$destName = $file;
}
$slash = DIRECTORY_SEPARATOR;
$destPath = $dest . $slash . strtr($destName, ['/' => $slash, '\\' => $slash]);
$totalCount++;
echo $destName;
$success = $ngdp->fetchFile($file, $destPath);
$successCount += $success ? 1 : 0;
$success = $success ? sprintf('OK (%s)', $success) : 'Failed';
echo sprintf(" -> %s\n", $success);
}
echo "\n";
echo sprintf("%d file%s out of %d extracted successfully\n", $successCount, $successCount == 1 ? '' : 's', $totalCount);
$httpStats = CASC\HTTP::getStats();
echo sprintf("%d remote connection%s made for %d remote request%s\n",
$httpStats['connections'], $httpStats['connections'] == 1 ? '' : 's',
$httpStats['requests'], $httpStats['requests'] == 1 ? '' : 's');
return $successCount > 0 ? 1 : 0;
}
function printHelp($cachePath) {
global $argv;
$me = $argv[0];
$locales = CASC\Manifest\Root::LOCALE_FLAGS;
ksort($locales);
$locales = implode(", ", array_keys($locales));
echo <<<EOF
Usage:
php $me --files <path> --out <path> [--wow <path>] [--cache <path>] [--program <wow>] [--region <us>] [--locale <enUS>] [--ignore]
php $me --help
-f, --files Required. Path of a text file listing the files to extract, one per line.
-o, --out Required. Destination path for extracted files.
-w, --wow Recommended. Path to World of Warcraft's install directory.
-c, --cache Optional. Path for CASC's file cache. [default: $cachePath]
-p, --program Optional. The NGDP program code (wow, wow_beta, wowt). [default: wow]
-r, --region Optional. Region. (us, eu, cn, tw, kr) [default: us]
-l, --locale Optional. Locale. ($locales) [default: enUS]
-i, --ignore Optional. Ignore extraction errors (useful to keep partially encrypted files)
--no-check-certificate Optional. Ignores SSL certificate issues when connecting to HTTPS hosts.
-h, --help This help message.
EOF;
}
exit(main());