-
Notifications
You must be signed in to change notification settings - Fork 3
/
mpq.cpp
492 lines (400 loc) · 15 KB
/
mpq.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/***************************************************************************
* Copyright (C) 2010 by Tamino Dauth *
* *
* 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 2 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, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include "../mpq.hpp"
#include "../utilities.hpp"
#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <boost/scoped_ptr.hpp>
using namespace wc3lib;
using namespace wc3lib::mpq;
namespace
{
void extract(Archive &mpq, std::string &entry, const boost::program_options::variables_map &vm)
{
mpq::File file = mpq.findFile(entry);
if (!file.isValid())
{
std::cerr << boost::format(_("Error occured while extracting file \"%1%\": File doesn't exist.")) % entry << std::endl;
return;
}
/*
* Always use the first occurence of directory as dir path.
* Otherwise all files would use different dir names which are case sensitively listed in the (listfile).
*/
string dirPath = Listfile::dirPath(entry);
#ifdef DEBUG
std::string oldEntry = entry;
#endif
#ifdef UNIX
// (listfile) entries usually have Windows path format
Listfile::toNativePath(entry);
Listfile::toNativePath(dirPath);
#endif
// output directory is archive's basename in the current working directory (name without extension)
boost::filesystem::path outputDirectoryPath = boost::filesystem::current_path() / boost::filesystem::basename(mpq.path());
boost::filesystem::path entryPath = outputDirectoryPath / boost::filesystem::path(dirPath);
if (!vm.count("overwrite") && boost::filesystem::exists(entryPath))
{
std::cerr << boost::format(_("Error occured while extracting file \"%1%\": \"Directory exists %2% already (use --overwrite to create it anyway\".")) % entry % entryPath << std::endl;
return;
}
if (!boost::filesystem::is_directory(entryPath) && !boost::filesystem::create_directories(entryPath))
{
std::cerr << boost::format(_("Error occured while extracting file \"%1%\": Unable to create output directory %2%.")) % entry %
entryPath << std::endl;
return;
}
/*
* Now construct the whole file path with the file name from the directory path.
*/
entryPath /= boost::filesystem::path(entry).filename();
if (!vm.count("overwrite") && boost::filesystem::exists(entryPath))
{
std::cerr << boost::format(_("Error occured while extracting file \"%1%\": \"File exists already (use --overwrite to extract it anyway\".")) % entry << std::endl;
return;
}
ofstream out(entryPath, std::ios::out | std::ios::binary);
try
{
checkStream(out);
file.decompress(out);
}
catch (const Exception &exception)
{
std::cerr << boost::format(_("Error occured while extracting file \"%1%\": \"%2%\".")) % entry % exception.what() << std::endl;
}
}
}
int main(int argc, char *argv[])
{
// Set the current locale.
setlocale(LC_ALL, "");
// Set the text message domain.
bindtextdomain("mpq", LOCALE_DIR);
textdomain("mpq");
std::string format;
typedef std::vector<boost::filesystem::path> Paths;
typedef std::vector<std::string> Strings;
// TODO throws exception when using escaped white spaces! - string work around
Strings listfileStrings;
Strings archiveStrings;
Strings fileStrings;
Paths listfiles;
Paths archivePaths;
Paths filePaths;
boost::filesystem::path dir;
uint32 hashes = 0;
uint32 blocks = 0;
uint32 sectorSize = 0;
uint32 startPosition = 0;
const boost::program_options::command_line_style::style_t pstyle = boost::program_options::command_line_style::style_t(
boost::program_options::command_line_style::unix_style
);
// boost::format(_("mpq %1%.\n\n")) % version << _("Usage: mpq [Options] [MPQ files]\n\n") <<
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("version,V", _("Shows current version of mpq."))
("help,h",_("Shows this text."))
// options
("human-readable", boost::program_options::value<bool>()->default_value(true), _("Shows output sizes in an human-readable format."))
("decimal,d", _("Shows decimal sizes (factor 1000 not 1024)"))
("format,F", boost::program_options::value<std::string>(&format)->default_value("1"), _("Selects the format of the created MPQ archive and modified files: <format:format:format>\nHere's a list of valid expressions:\n* \"1\"\n* \"2\"\n* \"listfile\"\n* \"attributes\""))
("list-files,L", boost::program_options::value<Strings>(&listfileStrings), _("Uses given listfiles to detect file paths of MPQ archives."))
("overwrite", _("Overwrites existing files and directories when creating or extracting files."))
("remove-files", _("Removes files/archives after adding them to the MPQ archives."))
("interactive", _("Asks for confirmation for every action."))
#ifdef DEBUG
("debug", _("Creates info files for each extracted file."))
#endif
("directory,C", boost::program_options::value<boost::filesystem::path>(&dir)->default_value(""), _("Changes directory to arg."))
("hashes", boost::program_options::value<uint32>(&hashes)->default_value(4096), _("Sets the number of hash entries when creating an archive."))
("blocks", boost::program_options::value<uint32>(&blocks)->default_value(4096), _("Sets the number of block entries when creating an archive."))
("sectorsize", boost::program_options::value<uint32>(§orSize)->default_value(4096), _("Sets the size of file sectors in bytes when creating an archive."))
("startposition", boost::program_options::value<uint32>(&startPosition)->default_value(0), _("Sets the start offset in the the archive file when creating one."))
// operations
("add,a", _("Adds files of MPQ archives or from hard disk to another archive."))
("create,c", _("Creates new MPQ archives."))
("diff,d", _("Finds differences between archives."))
("list,t", _("Lists all contained files of all read MPQ archives."))
("update,u", _("Only append files that are newer than the existing archives."))
("extract,x", _("Extract files from MPQ archives. If no files are specified via -f all files are extracted from given MPQ archives."))
("delete", _("Deletes files from MPQ archives."))
("info,i", _("Shows some basic information about all read MPQ archives. If any files are specified via -f their information will be shown as well."))
// input
("archives,A", boost::program_options::value<Strings>(&archiveStrings), _("Expected MPQ archives."))
("files,f", boost::program_options::value<Strings>(&fileStrings), _("Expected files."))
;
boost::program_options::positional_options_description p;
p.add("archives", -1);
boost::program_options::variables_map vm;
try
{
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).style(pstyle).options(desc).positional(p).run(), vm);
}
catch (std::exception &exception)
{
std::cerr << boost::format(_("Error while parsing program options: \"%1%\"")) % exception.what() << std::endl;
return EXIT_FAILURE;
}
boost::program_options::notify(vm);
if (vm.count("version"))
{
static const char *version = "0.1";
std::cout << boost::format(_("mpq %1%")) % version
<< std::endl
<< wc3libCopyright()
<< std::endl;
return EXIT_SUCCESS;
}
if (vm.count("help"))
{
std::cout << _("Usage: mpq [options] [archives]") << std::endl << std::endl;
std::cout << desc << std::endl;
std::cout << wc3libReportBugs() << std::endl;
return EXIT_SUCCESS;
}
wc3lib::mpq::Archive::Format mpqFormat = wc3lib::mpq::Archive::Format::Mpq1;
if (!format.empty())
{
if (format == "1")
{
mpqFormat = wc3lib::mpq::Archive::Format::Mpq1;
}
else if (format == "2")
{
mpqFormat = wc3lib::mpq::Archive::Format::Mpq2;
}
else
{
std::cerr << boost::format(_("Unknown MPQ format: %1%.")) % format << std::endl;
return EXIT_FAILURE;
}
}
// WORKAROUND
listfiles.resize(listfileStrings.size());
listfiles.assign(listfileStrings.begin(), listfileStrings.end());
archivePaths.resize(archiveStrings.size());
archivePaths.assign(archiveStrings.begin(), archiveStrings.end());
filePaths.resize(fileStrings.size());
filePaths.assign(fileStrings.begin(), fileStrings.end());
if (!dir.empty())
{
std::cout << boost::format(_("Changing to directory %1%.")) % dir << std::endl;
boost::filesystem::current_path(dir);
if (boost::filesystem::current_path() != dir)
{
std::cerr << _("Error on changing to directory.") << std::endl;
return EXIT_FAILURE;
}
}
// contains all file entries
Listfile::Entries listfileEntries;
BOOST_FOREACH(Paths::const_reference path, listfiles)
{
ifstream in(path, std::ios::in);
if (!in)
{
std::cerr << boost::format(_("Unable to open listfile %1%.")) % path << std::endl;
continue;
}
const string::size_type size = (string::size_type)(endPosition(in)) + 1;
string content(size, '0');
in.read(&content[0], size);
Listfile::Entries entries = Listfile::entries(content);
BOOST_FOREACH(Listfile::Entries::const_reference entry, entries)
{
listfileEntries.push_back(entry);
}
}
if (archivePaths.empty())
{
std::cerr << _("Missing archive arguments.") << std::endl;
return EXIT_FAILURE;
}
if (vm.count("info"))
{
BOOST_FOREACH(Paths::const_reference path, archivePaths)
{
if (!boost::filesystem::is_regular_file(path))
{
std::cerr << boost::format(_("File %1% does not seem to be a regular file and will be skipped.")) % path << std::endl;
continue;
}
boost::scoped_ptr<Archive> mpq(new Archive());
try
{
mpq->open(path);
}
catch (wc3lib::Exception &exception)
{
std::cerr << boost::format(_("Error occured while opening file %1%:\n\"%2%\"")) % path % exception.what() << std::endl;
continue;
}
std::cout
<< archiveInfo(*mpq, vm.count("human-readable"), vm.count("decimal")) << std::endl;
if (!filePaths.empty())
{
BOOST_FOREACH(Paths::const_reference filePath, filePaths)
{
File file = mpq->findFile(filePath);
if (file.isValid())
{
std::cout << fileInfo(file, vm.count("human-readable"), vm.count("decimal")) << std::endl;
}
else
{
std::cerr << boost::format(_("Error occured while getting info of file %1%: File doesn't exist.")) % filePath << std::endl;
}
}
}
}
}
if (vm.count("list"))
{
std::cout << _("Listing contained files:") << std::endl;
BOOST_FOREACH(Paths::const_reference path, archivePaths)
{
if (!boost::filesystem::is_regular_file(path))
{
std::cerr << boost::format(_("File %1% does not seem to be a regular file and will be skipped.")) % path << std::endl;
continue;
}
boost::scoped_ptr<Archive> mpq(new Archive());
try
{
mpq->open(path);
}
catch (wc3lib::Exception &exception)
{
std::cerr << boost::format(_("Error occured while opening file %1%: \"%2%\"")) % path % exception.what() << std::endl;
continue;
}
if (mpq->containsListfileFile())
{
Listfile listfile = mpq->listfileFile();
if (listfile.isValid())
{
Listfile::Entries entries = listfile.entries();
std::sort(entries.begin(), entries.end()); // sort alphabetically
BOOST_FOREACH(Listfile::Entries::const_reference entry, entries)
{
std::cout << entry << std::endl;
}
}
}
}
}
if (vm.count("create"))
{
BOOST_FOREACH(Paths::const_reference path, archivePaths)
{
if (boost::filesystem::exists(path))
{
std::cerr << boost::format(_("File %1% does already exist.")) % path << std::endl;
continue;
}
boost::scoped_ptr<Archive> mpq(new Archive());
try
{
mpq->create(path, hashes, blocks, mpqFormat, sectorSize, startPosition);
// import all specified files on creation
BOOST_FOREACH(Paths::reference entry, filePaths)
{
ifstream in(entry);
if (!in)
{
std::cerr << boost::format(_("Error on importing file %1%.")) % entry << std::endl;
continue;
}
const uint64 dataSize = std::streamoff(endPosition(in)) + 1;
boost::scoped_array<byte> data(new byte[dataSize]);
in.read(data.get(), dataSize);
in.close();
// TODO allow setting flags for each file
mpq->addFile(entry, data.get(), dataSize);
}
}
catch (wc3lib::Exception &exception)
{
std::cerr << boost::format(_("Error occured while creating file %1%: \"%2%\"")) % path % exception.what() << std::endl;
continue;
}
}
}
if (vm.count("extract"))
{
BOOST_FOREACH(Paths::const_reference path, archivePaths)
{
if (!boost::filesystem::is_regular_file(path))
{
std::cerr << boost::format(_("File %1% does not seem to be a regular file and will be skipped.")) % path << std::endl;
continue;
}
boost::scoped_ptr<Archive> mpq(new Archive());
try
{
mpq->open(path);
}
catch (wc3lib::Exception &exception)
{
std::cerr << boost::format(_("Error occured while opening file %1%: \"%2%\"")) % path % exception.what() << std::endl;
continue;
}
std::set<boost::filesystem::path> newlyCreatedDirsForExtraction;
if (filePaths.empty())
{
if (mpq->containsListfileFile())
{
const Listfile::Entries entries = mpq->listfileFile().entries();
BOOST_FOREACH(Listfile::Entries::const_reference entry, entries)
{
listfileEntries.push_back(entry);
}
}
// usually does not list itself
// TODO really search for it?
if (std::find(listfileEntries.begin(), listfileEntries.end(), "(listfile)") == listfileEntries.end())
{
listfileEntries.push_back("(listfile)");
}
/*
* Filter case sensitive existing.
* Case sensitive means that the first appearance of a directory name is used from a listfile for the actual directory name.
*/
listfileEntries = Listfile::caseSensitiveFileEntries(Listfile::existingEntries(listfileEntries, *mpq));
BOOST_FOREACH(Listfile::Entries::reference entry, listfileEntries)
{
extract(*mpq, entry, vm);
}
}
else
{
BOOST_FOREACH(Paths::reference entry, filePaths)
{
string stringEntry = entry.string();
extract(*mpq, stringEntry, vm);
}
}
}
}
return EXIT_SUCCESS;
}