forked from kenygia/MooseEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PakReader.cpp
220 lines (205 loc) · 6.34 KB
/
PakReader.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
#include "PakReader.h"
#include <fstream>
#include <sstream>
#include <boost/tokenizer.hpp>
#include <sys/stat.h>
#include <unistd.h>
#include <boost/filesystem/fstream.hpp>
long PakReader::getNextBlock(long offset) {
return offset - (offset % BLOCK_SIZE) + BLOCK_SIZE;
}
long PakReader::dataOffsetToAbsolute(long dataOffset, long pakNumber) {
if (pakNumber == 0) {
return dataStart + dataOffset;
}
return dataOffset;
}
long PakReader::getStreamLength(std::istream& input) {
long current = input.tellg();
input.seekg(0, std::ios_base::beg);
long fileStart = input.tellg();
input.seekg(0, std::ios_base::end);
long streamSize = input.tellg() - fileStart;
input.seekg(current);
return streamSize;
}
std::wstring PakReader::getFileNameByPakNumber(std::wstring& fileName, long pakNumber) {
if (pakNumber == 0) {
return fileName;
}
long lastDot = fileName.find_last_of(L".");
std::wstringstream result;
result<<fileName.substr(0, lastDot)<<L"_"<<pakNumber<<L".pak";
return result.str();
}
HEADER_PAK_FILEINFO_LSPK *PakReader::getHeaderForFileLspk(std::string& filePath) {
for (int i=0; i<fileInfoLspk.size(); ++i) {
if (fileInfoLspk[i]->fileName == filePath) {
return fileInfoLspk[i].get();
}
}
return 0;
}
std::wstring PakReader::getLastExtractPath() const
{
return lastExtractPath;
}
bool PakReader::loadFile(std::wstring fileName) {
fileInfoLspk.clear();
boost::filesystem::ifstream input(fileName, std::ios_base::binary);
if (input) {
int magicSampler = 0;
input.read((char *)&magicSampler, sizeof(int));
input.seekg(0, std::ios_base::beg);
if (magicSampler == 0x4B50534C) {
isLspk = true;
} else {
isLspk = false;
}
long fileCount = 0;
if (isLspk) {
HEADER_PAK_LSPK pakHeader;
input.read((char *)&pakHeader, sizeof(HEADER_PAK_LSPK));
dataStart = pakHeader.dataOffset;
fileCount = pakHeader.fileCount;
} else {
HEADER_PAK pakHeader;
input.read((char *)&pakHeader, sizeof(HEADER_PAK));
long dataHeaderSize = sizeof(HEADER_PAK_FILEINFO) * pakHeader.fileCount;
long headerEnd = sizeof(HEADER_PAK) + dataHeaderSize;
dataStart = getNextBlock(headerEnd);
fileCount = pakHeader.fileCount;
}
for (int i=0; i<fileCount; ++i) {
fileInfoLspk.push_back(boost::shared_ptr<HEADER_PAK_FILEINFO_LSPK>(new HEADER_PAK_FILEINFO_LSPK));
HEADER_PAK_FILEINFO_LSPK *currentInfo = fileInfoLspk[fileInfoLspk.size() - 1].get();
if (input) {
if (isLspk) {
input.read((char *)currentInfo, sizeof(HEADER_PAK_FILEINFO_LSPK));
} else {
HEADER_PAK_FILEINFO tempInfo;
input.read((char *)&tempInfo, sizeof(HEADER_PAK_FILEINFO));
strcpy(currentInfo->fileName, tempInfo.fileName);
currentInfo->dataSectionOffset = tempInfo.dataSectionOffset;
currentInfo->fileSize = tempInfo.fileSize;
currentInfo->pakNumber = tempInfo.pakNumber;
currentInfo->compressionMethod = 0;
currentInfo->unknown2 = 0;
currentInfo->decompressedSize = 0;
}
}
else {
input.close();
return false;
}
}
} else {
return false;
}
input.close();
return true;
}
std::vector<std::string> PakReader::getFileList() {
std::vector<std::string> fileList;
for (int i=0; i<fileInfoLspk.size(); ++i) {
fileList.push_back(fileInfoLspk[i]->fileName);
}
return fileList;
}
bool PakReader::extractFile(std::wstring fileName, std::string& filePath, std::wstring& destination, bool preservePath) {
unsigned long fileSize;
char *fileBytes = extractFileIntoMemory(fileName, filePath, destination, preservePath, &fileSize);
if (fileBytes == 0) {
return false;
}
boost::filesystem::ofstream outFile(lastExtractPath, std::ios_base::binary);
outFile.write(fileBytes, fileSize);
delete[] fileBytes;
if (!outFile) {
outFile.close();
return false;
}
outFile.close();
return true;
}
char *PakReader::extractFileIntoMemory(std::wstring fileName, std::string& filePath, std::wstring& destination, bool preservePath, unsigned long *fileSize) {
if (fileSize == 0) {
return 0;
}
HEADER_PAK_FILEINFO_LSPK *info = getHeaderForFileLspk(filePath);
if (info != 0) {
boost::filesystem::ifstream input(getFileNameByPakNumber(fileName, info->pakNumber).c_str(), std::ios_base::binary);
long fileStart = dataOffsetToAbsolute(info->dataSectionOffset, info->pakNumber);
input.seekg(fileStart);
char *alloc = new char[info->fileSize];
input.read(alloc, info->fileSize);
if (!input) {
delete []alloc;
input.close();
return 0;
}
long cwdSize = 1024;
wchar_t cwd[cwdSize];
_wgetcwd(cwd, cwdSize);
std::wstringstream ss;
if (!preservePath) {
long fileNameSize = strlen(info->fileName) + 1;
std::wstring extractFileName;
{
wchar_t alloc[fileNameSize];
mbstowcs(alloc, info->fileName, fileNameSize);
extractFileName = alloc;
}
long lastPathDelimiter = extractFileName.find_last_of(L'/');
if (lastPathDelimiter == std::string::npos) {
ss<<destination<<(destination[destination.size() - 1] == L'/' ? L"" : L"/")<<extractFileName;
}
else {
ss<<destination<<(destination[destination.size() - 1] == L'/' ? L"" : L"/")<<extractFileName.substr(lastPathDelimiter + 1);
}
} else {
boost::char_separator<wchar_t> sep(L"\\/");
typedef boost::tokenizer<boost::char_separator<wchar_t> , std::wstring::const_iterator, std::wstring> PathTokenizer;
long fileNameSize = strlen(info->fileName) + 1;
std::wstring name;
{
wchar_t alloc[fileNameSize];
mbstowcs(alloc, info->fileName, fileNameSize);
name = alloc;
}
PathTokenizer tok(name, sep);
std::wstring lastToken = L"";
_wchdir(destination.c_str());
ss<<destination<<(destination[destination.size() - 1] == '/' ? "" : "/");
for (PathTokenizer::iterator it = tok.begin(); it != tok.end(); ++it) {
std::wstring token = *it;
if (lastToken.length() != 0) {
_wmkdir(lastToken.c_str());
_wchdir(lastToken.c_str());
ss<<lastToken<<L"/";
}
lastToken = token;
}
ss<<lastToken;
}
lastExtractPath = ss.str();
if (info->decompressedSize > 0) {
char *decompressionBuffer = new char[info->decompressedSize];
int result = compressor.decompress((char *)decompressionBuffer, (char *)alloc, info->fileSize, info->decompressedSize);
if (result > 0) {
*fileSize = info->decompressedSize;
delete[] alloc;
alloc = decompressionBuffer;
}
} else {
*fileSize = info->fileSize;
}
input.close();
_wchdir(cwd);
return alloc;
}
return 0;
}
PakReader::PakReader()
{
}