forked from Kanma/MPQExtractor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
403 lines (341 loc) · 13.6 KB
/
main.cpp
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <StormLib.h>
#include <SimpleOpt.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#ifdef WIN32
#include <io.h>
#include <direct.h>
#define mkdir(dirname, acl) _mkdir(dirname)
#else
#include <unistd.h>
#endif
#include <sys/stat.h>
using namespace std;
struct tSearchResult
{
string strFileName;
string strFullPath;
};
/**************************** COMMAND-LINE PARSING ****************************/
// The valid options
enum
{
OPT_HELP,
OPT_LISTFILE,
OPT_APPLYLIST,
OPT_PATCHPREFIX,
OPT_PATCHES,
OPT_SEARCH,
OPT_EXTRACT,
OPT_DEST,
OPT_FULLPATH,
OPT_LOWERCASE,
};
const CSimpleOpt::SOption COMMAND_LINE_OPTIONS[] = {
{ OPT_HELP, "-h", SO_NONE },
{ OPT_HELP, "--help", SO_NONE },
{ OPT_APPLYLIST, "-a", SO_REQ_SEP },
{ OPT_APPLYLIST, "--applylist", SO_REQ_SEP },
{ OPT_LISTFILE, "-l", SO_REQ_SEP },
{ OPT_LISTFILE, "--listfile", SO_REQ_SEP },
{ OPT_PATCHPREFIX, "--prefix", SO_REQ_SEP },
{ OPT_PATCHES, "-p", SO_MULTI },
{ OPT_PATCHES, "--patches", SO_MULTI },
{ OPT_SEARCH, "-s", SO_REQ_SEP },
{ OPT_SEARCH, "--search", SO_REQ_SEP },
{ OPT_EXTRACT, "-e", SO_REQ_SEP },
{ OPT_EXTRACT, "--extract", SO_REQ_SEP },
{ OPT_DEST, "-o", SO_REQ_SEP },
{ OPT_DEST, "--dest", SO_REQ_SEP },
{ OPT_FULLPATH, "-f", SO_NONE },
{ OPT_FULLPATH, "--fullpath", SO_NONE },
{ OPT_LOWERCASE, "-c", SO_NONE },
{ OPT_LOWERCASE, "--lowercase", SO_NONE },
SO_END_OF_OPTIONS
};
/********************************** FUNCTIONS *********************************/
void showUsage(const std::string& strApplicationName)
{
cout << "MPQExtractor" << endl
<< "Usage: " << strApplicationName << " [options] <filename>" << endl
<< endl
<< "This program can either:" << endl
<< " - retrieve the list of files in a MPQ archive" << endl
<< " - search for the list of files corresponding to a given pattern in a MPQ archive" << endl
<< " - extract some files from a MPQ archive" << endl
<< " - apply some patches to a MPQ archive before extracting some files" << endl
<< " - combine some of the above options" << endl
<< endl
<< "Options:" << endl
<< " --help, -h: Display this help" << endl
<< " --applylist <FILE>" << endl
<< " -a <FILE> Apply list file FILE to the archive" << endl
<< " --listfile <FILE>," << endl
<< " -l <FILE>: Retrieve the list of files in the archive and save it" << endl
<< " in FILE" << endl
<< " --search <PATTERN>," << endl
<< " -s <PATTERN>: Search for the list of files corresponding to a PATTERN" << endl
<< " in the archive" << endl
<< " --extract <PATTERN>," << endl
<< " -e <PATTERN>: Same as --search, but the found files are also extracted" << endl
<< " --dest <PATH>," << endl
<< " -o <PATH>: The folder where the files are extracted (default: the" << endl
<< " current one)" << endl
<< " --fullpath, -f: During extraction, preserve the path hierarchy found" << endl
<< " inside the archive" << endl
<< " --patches <FILE-1> <FILE-2> ... <FILE-N>," << endl
<< " -p <FILE-1> <FILE-2> ... <FILE-N>:" << endl
<< " The patches to apply to the archive before extracting files," << endl
<< " in their order of application. A per-patch prefix can be " << endl
<< " specified with a comma (e.g. -p wow-update-13164.MPQ,base)" << endl
<< " --prefix <PREFIX> Path prefix to apply to all patches which do not specify their " << endl
<< " own explicit prefix" << endl
<< " --lowercase, -c: Convert extracted file paths to lowercase" <<endl
<< endl
<< "Examples:" << endl
<< endl
<< " 1) Retrieve the list of files in a MPQ archive:" << endl
<< endl
<< " ./MPQExtractor -l list.txt archive.MPQ" << endl
<< endl
<< " 2) Search all the *.M2 files in a MPQ archive:" << endl
<< endl
<< " ./MPQExtractor -s *.M2 archive.MPQ" << endl
<< endl
<< " 3) Extract a specific file from a MPQ archive:" << endl
<< " IMPORTANT: The file name must be enclosed in \"\" to prevent the shell to" << endl
<< " interpret the \\ character as the start of an escape sequence." << endl
<< endl
<< " ./MPQExtractor -e \"Path\\To\\The\\File\" -o out archive.MPQ" << endl
<< endl
<< " 4) Extract some specific files from a MPQ archive, preserving the path hierarchy" << endl
<< " found inside the MPQ archive:" << endl
<< endl
<< " ./MPQExtractor -e \"Path\\To\\Extract\\*\" -f -o out archive.MPQ" << endl
<< endl
<< " 5) Apply some patches before extracting a specific file from a MPQ archive:" << endl
<< endl
<< " ./MPQExtractor -p patch-*.MPQ --prefix base -e \"Path\\To\\The\\File\" -o out archive.MPQ" << endl
<< endl;
}
int main(int argc, char** argv)
{
HANDLE hArchive;
string strApplyListFile;
string strListFile;
string strPatchPrefix;
vector<string> patches;
string strSearchPattern;
string strDestination = ".";
vector<tSearchResult> searchResults;
bool bExtraction = false;
bool bUseFullPath = false;
bool bLowerCase = false;
// Parse the command-line parameters
CSimpleOpt args(argc, argv, COMMAND_LINE_OPTIONS);
while (args.Next())
{
if (args.LastError() == SO_SUCCESS)
{
switch (args.OptionId())
{
case OPT_HELP:
showUsage(argv[0]);
return 0;
case OPT_APPLYLIST:
strApplyListFile = args.OptionArg();
break;
case OPT_LISTFILE:
strListFile = args.OptionArg();
break;
case OPT_PATCHPREFIX:
strPatchPrefix = args.OptionArg();
break;
case OPT_PATCHES:
{
unsigned int i = 1;
while (true)
{
char** val = args.MultiArg(i);
if (!val)
break;
patches.push_back(val[i - 1]);
}
break;
}
case OPT_SEARCH:
strSearchPattern = args.OptionArg();
break;
case OPT_EXTRACT:
strSearchPattern = args.OptionArg();
bExtraction = true;
break;
case OPT_DEST:
strDestination = args.OptionArg();
break;
case OPT_FULLPATH:
bUseFullPath = true;
break;
case OPT_LOWERCASE:
bLowerCase = true;
break;
}
}
else
{
cerr << "Invalid argument: " << args.OptionText() << endl;
return -1;
}
}
if (args.FileCount() != 1)
{
cerr << "No MPQ file specified" << endl;
return -1;
}
cout << "Opening '" << args.File(0) << "'..." << endl;
if (!SFileOpenArchive(args.File(0), 0, MPQ_OPEN_READ_ONLY, &hArchive))
{
cerr << "Failed to open the file '" << args.File(0) << "'" << endl;
return -1;
}
if (!strApplyListFile.empty())
{
if((SFileAddListFile(hArchive, strApplyListFile.c_str()))!= ERROR_SUCCESS)
{
cerr << "Failed to apply list file to archive" << endl;
SFileCloseArchive(hArchive);
return -1;
}
}
// List file extraction
if (!strListFile.empty())
{
if (!SFileExtractFile(hArchive, "(listfile)", strListFile.c_str(), SFILE_OPEN_FROM_MPQ))
{
cerr << "Failed to extract the list of files" << endl;
SFileCloseArchive(hArchive);
return -1;
}
}
// Apply the patches
if (!patches.empty())
{
vector<string>::iterator iter, iterEnd;
string prefix;
string patch;
size_t idx;
for (iter = patches.begin(), iterEnd = patches.end(); iter != iterEnd; ++iter)
{
prefix = strPatchPrefix;;
patch = *iter;
idx = patch.find_first_of(',');
if (idx != string::npos)
{
prefix = patch.substr(idx+1, string::npos);
patch = patch.substr(0, idx);
}
if (prefix.length())
{
cout << "Applying patch '" << patch << "' (prefix '" << prefix << "')..." << endl;
}else{
cout << "Applying patch '" << patch << "' (no prefix)..." << endl;
}
if (!SFileOpenPatchArchive(hArchive, patch.c_str(), prefix.c_str(), 0))
cerr << "Failed to apply the patch '" << patch << "'" << endl;
}
}
// Search the files
if (!strSearchPattern.empty())
{
if ((strSearchPattern.find("*") == string::npos) && (strSearchPattern.find("?") == string::npos))
{
tSearchResult r;
r.strFileName = strSearchPattern.substr(strSearchPattern.find_last_of("\\") + 1);
r.strFullPath = strSearchPattern;
searchResults.push_back(r);
}
else
{
cout << endl;
cout << "Searching for '" << strSearchPattern << "'..." << endl;
SFILE_FIND_DATA findData;
HANDLE handle = SFileFindFirstFile(hArchive, strSearchPattern.c_str(), &findData, 0);
if (handle)
{
cout << endl;
cout << "Found files:" << endl;
do {
cout << " - " << findData.cFileName << endl;
tSearchResult r;
r.strFileName = findData.szPlainName;
r.strFullPath = findData.cFileName;
searchResults.push_back(r);
} while (SFileFindNextFile(handle, &findData));
SFileFindClose(handle);
}
else
{
cout << "No file found!" << endl;
}
}
}
// Extraction
if (bExtraction && !searchResults.empty())
{
cout << endl;
cout << "Extracting files..." << endl;
cout << endl;
if (strDestination.at(strDestination.size() - 1) != '/')
strDestination += "/";
vector<tSearchResult>::iterator iter, iterEnd;
for (iter = searchResults.begin(), iterEnd = searchResults.end(); iter != iterEnd; ++iter)
{
string strDestName = strDestination;
if (bUseFullPath)
{
if (bLowerCase){
transform(iter->strFullPath.begin(), iter->strFullPath.end(), iter->strFullPath.begin(), ::tolower);
}
strDestName += iter->strFullPath;
size_t offset = strDestName.find("\\");
while (offset != string::npos)
{
strDestName = strDestName.substr(0, offset) + "/" + strDestName.substr(offset + 1);
offset = strDestName.find("\\");
}
offset = strDestName.find_last_of("/");
if (offset != string::npos)
{
string dest = strDestName.substr(0, offset + 1);
size_t start = dest.find("/", 0);
while (start != string::npos)
{
string dirname = dest.substr(0, start);
DIR* d = opendir(dirname.c_str());
if (!d)
mkdir(dirname.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
else
closedir(d);
start = dest.find("/", start + 1);
}
}
}
else
{
if (bLowerCase){
transform(iter->strFileName.begin(), iter->strFileName.end(), iter->strFileName.begin(), ::tolower);
}
strDestName += iter->strFileName;
}
if (!SFileExtractFile(hArchive, iter->strFullPath.c_str(), strDestName.c_str(), 0))
cerr << "Failed to extract the file '" << iter->strFullPath << "' in " << strDestName << endl;
}
}
SFileCloseArchive(hArchive);
return 0;
}