-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.cpp
305 lines (253 loc) · 9.71 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
#include <CascLib.h>
#include <SimpleOpt.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#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_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_LISTFILE, "-l", SO_REQ_SEP },
{ OPT_LISTFILE, "--listfile", SO_REQ_SEP },
{ 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 << "CASCExtractor" << endl
<< "Usage: " << strApplicationName << " [options] <CASC_ROOT> <PATTERN>" << endl
<< endl
<< "This program can extract files from a CASC storage" << endl
<< endl
<< "Options:" << endl
<< " --help, -h: Display this help" << endl
<< " --listfile <FILE>" << endl
<< " -l <FILE> Use the list file FILE [required if PATTERN is not a full path]" << 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 storage" << endl
<< " --lowercase, -c: Convert extracted file paths to lowercase" <<endl
<< endl
<< "Examples:" << endl
<< endl
<< " 1) Extract all the *.M2 files in a CASC storage:" << endl
<< endl
<< " ./CASCExtractor -l listfile-wow6.txt \"/Applications/World of Warcraft Beta/Data/\" *.M2" << endl
<< endl
<< " 2) Extract a specific file from a CASC storage:" << 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
<< " ./CASCExtractor -o out \"/Applications/World of Warcraft Beta/Data/\" \"Path\\To\\The\\File\"" << endl
<< endl
<< " 3) Extract some specific files from a CASC storage, preserving the path hierarchy:" << endl
<< endl
<< " ./CASCExtractor -f -o out -l listfile-wow6.txt \"/Applications/World of Warcraft Beta/Data/\" \"Path\\To\\Extract\\*\"" << endl
<< endl;
}
int main(int argc, char** argv)
{
HANDLE hStorage;
string strListFile;
string strSearchPattern;
string strStorage;
string strDestination = ".";
vector<tSearchResult> searchResults;
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_LISTFILE:
strListFile = args.OptionArg();
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() != 2)
{
cerr << "Must specify both the path to a CASC storage and a search pattern" << endl;
return -1;
}
strStorage = args.File(0);
strSearchPattern = args.File(1);
// Remove trailing slashes at the end of the storage path (CascLib doesn't like that)
if ((strStorage[strStorage.size() - 1] == '/') || (strStorage[strStorage.size() - 1] == '\\'))
strStorage = strStorage.substr(0, strStorage.size() - 1);
cout << "Opening '" << strStorage << "'..." << endl;
if (!CascOpenStorage(strStorage.c_str(), 0, &hStorage))
{
cerr << "Failed to open the storage '" << strStorage << "'" << endl;
return -1;
}
// Search the files
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
{
if (strListFile.empty())
{
cerr << "No listfile specified, use the --listfile option" << endl;
return -1;
}
cout << endl;
cout << "Searching for '" << strSearchPattern << "'..." << endl;
CASC_FIND_DATA findData;
HANDLE handle = CascFindFirstFile(hStorage, strSearchPattern.c_str(), &findData, strListFile.c_str());
if (handle)
{
cout << endl;
cout << "Found files:" << endl;
do {
cout << " - " << findData.szFileName << endl;
tSearchResult r;
r.strFileName = findData.szPlainName;
r.strFullPath = findData.szFileName;
searchResults.push_back(r);
} while (CascFindNextFile(handle, &findData) && findData.szPlainName);
CascFindClose(handle);
}
else
{
cout << "No file found!" << endl;
}
}
// Extraction
if (!searchResults.empty())
{
char buffer[1000000];
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;
}
HANDLE hFile;
if (CascOpenFile(hStorage, iter->strFullPath.c_str(), CASC_LOCALE_ALL, 0, &hFile))
{
DWORD read;
FILE* dest = fopen(strDestName.c_str(), "wb");
if (dest)
{
do {
if (CascReadFile(hFile, &buffer, 1000000, &read))
fwrite(&buffer, read, 1, dest);
} while (read > 0);
fclose(dest);
}
else
{
cerr << "Failed to extract the file '" << iter->strFullPath << "' in " << strDestName << endl;
}
CascCloseFile(hFile);
}
else
{
cerr << "Failed to extract the file '" << iter->strFullPath << "' in " << strDestName << endl;
}
}
}
CascCloseStorage(hStorage);
return 0;
}