-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
141 lines (129 loc) · 4.58 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
#include <iostream>
#include <string>
#include <cstring>
#include <StormLib/StormLib.h>
using std::cerr;
using std::endl;
using std::string;
int main(int argc, const char *argv[])
{
if (argc < 3)
{
cerr << "min 2 params" << endl;
return 1;
}
auto mode = argv[1];
auto isListMode = !strcmp(mode, "-l") || !strcmp(mode, "--list");
DWORD mpqFlag;
if (isListMode || !strcmp(mode, "-r") || !strcmp(mode, "--read"))
mpqFlag = STREAM_FLAG_READ_ONLY;
else if (!strcmp(mode, "-w") || !strcmp(mode, "--write"))
mpqFlag = STREAM_FLAG_WRITE_SHARE;
else
{
cerr << "unknown mode: " << mode << endl;
return 2;
}
if (!isListMode && argc < 5)
{
cerr << "not enough params for non-list mode" << endl;
return 3;
}
HANDLE mpqHandle = nullptr;
if (!SFileOpenArchive(argv[2], 0, BASE_PROVIDER_FILE | STREAM_PROVIDER_FLAT | mpqFlag, &mpqHandle))
{
cerr << "failed to open MPQ, error: " << GetLastError() << endl;
return 4;
}
auto closeMpq = [&mpqHandle]{ SFileCloseArchive(mpqHandle); };
if (mpqFlag == STREAM_FLAG_READ_ONLY)
{
// for list mode path mask is required
const auto extraListFileArgIndex = isListMode ? 4 : 5;
if (extraListFileArgIndex < argc)
SFileAddListFile(mpqHandle, argv[extraListFileArgIndex]);
auto mpqInternalPathMask = (!isListMode || argc > 3) ? argv[3] : "*";
SFILE_FIND_DATA findData;
if (auto findHandle = SFileFindFirstFile(mpqHandle, mpqInternalPathMask, &findData, nullptr))
{
string extractPath;
if (!isListMode)
{
extractPath = string{argv[4]};
if (extractPath.back() != '/')
#ifdef PLATFORM_WINDOWS
if (extractPath.back() != '\\')
#endif
extractPath += '/';
}
bool nextFileFound;
do
{
auto fileInternalPath = findData.cFileName;
if (isListMode)
std::cout << findData.dwFileSize << " " << fileInternalPath << endl;
else
{
auto fileExtractPath = extractPath + findData.szPlainName;
if (!SFileExtractFile(mpqHandle, fileInternalPath, fileExtractPath.c_str(), SFILE_OPEN_FROM_MPQ))
cerr << "failed to extract file '" << fileInternalPath << "', error: " << GetLastError() << endl;
}
nextFileFound = SFileFindNextFile(findHandle, &findData);
} while (nextFileFound);
SFileFindClose(findHandle);
}
else
{
auto err = GetLastError();
cerr << "no files found with mask '" << mpqInternalPathMask << "'";
if (err != ERROR_NO_MORE_FILES)
cerr << ", error: " << err;
cerr << endl;
}
}
else
{
string thirdParam{argv[3]}, prefixParam{"--prefix="}, internalPathPrefix;
int filesStartIndex;
bool isSinglePrefix;
if ((isSinglePrefix = thirdParam.find(prefixParam) == 0))
{
internalPathPrefix = thirdParam.substr(prefixParam.length());
filesStartIndex = 4;
}
else
{
filesStartIndex = 3;
if ((argc - filesStartIndex) % 2)
{
cerr << "each input file must be balanced with an output one" << endl;
closeMpq();
return 5;
}
}
for (int i = filesStartIndex; i < argc; ++i)
{
string filePath{argv[i]}, internalPath;
if (isSinglePrefix)
{
string fileName;
auto lastSlashIndex = filePath.rfind('/');
#ifdef PLATFORM_WINDOWS
if (lastSlashIndex == string::npos)
lastSlashIndex = filePath.rfind('\\');
#endif
internalPath = internalPathPrefix + filePath.substr(lastSlashIndex != string::npos ? lastSlashIndex + 1 : 0);
}
else
internalPath = argv[++i];
auto s = "'" + filePath + "' => '" + internalPath + "'";
if (SFileAddFileEx(mpqHandle, filePath.c_str(), internalPath.c_str(), MPQ_FILE_COMPRESS | MPQ_FILE_REPLACEEXISTING, MPQ_COMPRESSION_PKWARE, MPQ_COMPRESSION_NEXT_SAME))
std::cout << s << endl;
else
cerr << s << " error: " << GetLastError() << endl;
}
SFileCompactArchive(mpqHandle, nullptr, false);
}
closeMpq();
return 0;
}