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 | #include "fsCAssetPack.h"
#include <algorithm>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <map>
#include <set>
#include <unordered_map>
namespace
{
// Must match rustTools/project/src/engine/asset_pack.rs.
constexpr char kMagic[4] = {'F', 'P', 'K', '1'};
constexpr std::uint32_t kVersion = 1;
constexpr std::uint64_t kSecret = 0x5DEECE66D1CE4E5Bull;
constexpr std::size_t kHeaderBytes = 24;
struct sPack
{
std::filesystem::path mPath;
std::uint64_t mSalt;<--- Member variable 'sPack::mSalt' has no initializer. [+]Member variable 'sPack::mSalt' has no initializer. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
};
struct sEntry
{
std::size_t mPack;
std::uint64_t mOffset;
std::uint64_t mSize;
};
std::string sRoot;
std::vector<sPack> sPacks;
std::unordered_map<std::string, sEntry> sEntries;
// Every folder holding packed files, mapped to its immediate children.
std::map<std::string, std::set<std::string>> sFolders;
std::uint64_t splitmix64(std::uint64_t pValue)
{
std::uint64_t z = pValue + 0x9E3779B97F4A7C15ull;
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
return z ^ (z >> 31);
}
// XORs with the keystream the packer scrambled with: one splitmix64 per 8 bytes.
void unscramble(std::uint8_t* pData, std::uint64_t pSize, std::uint64_t pSeed)
{
for (std::uint64_t block = 0; block * 8 < pSize; ++block)
{
const std::uint64_t key = splitmix64(pSeed + block);
const std::uint64_t end = std::min<std::uint64_t>(pSize, block * 8 + 8);
for (std::uint64_t i = block * 8; i < end; ++i)
{
pData[i] ^= static_cast<std::uint8_t>(key >> ((i % 8) * 8));
}
}
}
template<typename T>
T littleEndianGet(const std::uint8_t* pBytes)
{
T value = 0;
for (std::size_t i = 0; i < sizeof(T); ++i)
{
value |= static_cast<T>(pBytes[i]) << (8 * i);
}
return value;
}
void folderChildAdd(const std::string& pKey)
{
std::string child = pKey;
for (;;)
{
const auto slash = child.find_last_of('/', child.size() - (child.back() == '/' ? 2 : 1));
const std::string folder = slash == std::string::npos ? std::string() : child.substr(0, slash + 1);
const bool known = sFolders.count(folder) != 0;
sFolders[folder].insert(child.substr(folder.size()));
if (known || folder.empty())
{
return;
}
child = folder;
}
}
bool packMount(const std::filesystem::path& pPath)
{
std::ifstream file(pPath, std::ios::binary);
std::uint8_t header[kHeaderBytes];
if (!file.read(reinterpret_cast<char*>(header), sizeof(header)) ||
std::memcmp(header, kMagic, sizeof(kMagic)) != 0 ||
littleEndianGet<std::uint32_t>(header + 4) != kVersion)
{
return false;
}
const auto salt = littleEndianGet<std::uint64_t>(header + 8);
const auto count = littleEndianGet<std::uint32_t>(header + 16);
std::vector<std::uint8_t> index(littleEndianGet<std::uint32_t>(header + 20));
if (!file.read(reinterpret_cast<char*>(index.data()), static_cast<std::streamsize>(index.size())))
{
return false;
}
unscramble(index.data(), index.size(), kSecret ^ salt);
const std::size_t pack = sPacks.size();
std::size_t at = 0;
for (std::uint32_t i = 0; i < count; ++i)
{
if (at + 2 > index.size())
{
return false;
}
const auto length = littleEndianGet<std::uint16_t>(&index[at]);
if (at + 2 + length + 16 > index.size())
{
return false;
}
std::string key(reinterpret_cast<const char*>(&index[at + 2]), length);
at += 2 + length;
const sEntry entry{pack, littleEndianGet<std::uint64_t>(&index[at]), littleEndianGet<std::uint64_t>(&index[at + 8])};
at += 16;
folderChildAdd(key);
sEntries.emplace(std::move(key), entry);
}
sPacks.push_back({pPath, salt});
return true;
}
const sEntry* entryFind(std::string_view pPath)
{
if (sEntries.empty())
{
return nullptr;
}
const auto entry = sEntries.find(fsCAssetPack::keyGet(pPath));
return entry == sEntries.end() ? nullptr : &entry->second;
}
}
std::string fsCAssetPack::keyGet(std::string_view pPath)
{
std::string key;
key.reserve(pPath.size());
for (const char ch : pPath)
{
const char normal = ch == '\\' ? '/' : ch;
if (normal == '/' && (key.empty() || key.back() == '/'))
{
continue;
}
key += (normal >= 'A' && normal <= 'Z') ? static_cast<char>(normal - 'A' + 'a') : normal;
}
while (key.compare(0, 2, "./") == 0)
{
key.erase(0, 2);
}
return key;
}
bool fsCAssetPack::mount(const std::string& pFolder)
{
std::error_code error;
std::vector<std::filesystem::path> packs;
for (const auto& file : std::filesystem::directory_iterator(std::filesystem::u8path(pFolder), error))
{
if (file.path().extension() == ".fpk")
{
packs.push_back(file.path());
}
}
// content0, content1, ... in order, so a later pack never shadows an earlier one.
std::sort(packs.begin(), packs.end());
sRoot = pFolder;
if (!sRoot.empty() && sRoot.back() != '/' && sRoot.back() != '\\')
{
sRoot += '/';
}
bool mounted = false;
for (const auto& pack : packs)
{
mounted = packMount(pack) || mounted;
}
return mounted;
}
bool fsCAssetPack::mountedGet()
{
return !sPacks.empty();
}
bool fsCAssetPack::fileExistsGet(std::string_view pPath)
{
return entryFind(pPath) != nullptr;
}
bool fsCAssetPack::folderExistsGet(std::string_view pPath)
{
if (sFolders.empty())
{
return false;
}
std::string key = keyGet(pPath);
if (!key.empty() && key.back() != '/')
{
key += '/';
}
return sFolders.count(key) != 0;
}
std::int64_t fsCAssetPack::fileSizeGet(std::string_view pPath)
{
const sEntry* const entry = entryFind(pPath);
return entry ? static_cast<std::int64_t>(entry->mSize) : -1;
}
bool fsCAssetPack::fileRead(std::string_view pPath, void* pDest)
{
const sEntry* const entry = entryFind(pPath);
if (!entry)
{
return false;
}
const sPack& pack = sPacks[entry->mPack];
std::ifstream file(pack.mPath, std::ios::binary);
if (!file.seekg(static_cast<std::streamoff>(entry->mOffset)) ||
!file.read(static_cast<char*>(pDest), static_cast<std::streamsize>(entry->mSize)))
{
return false;
}
unscramble(static_cast<std::uint8_t*>(pDest), entry->mSize, kSecret ^ pack.mSalt ^ splitmix64(entry->mOffset));
return true;
}
std::vector<std::string> fsCAssetPack::folderListGet(std::string_view pPath)
{
std::string key = keyGet(pPath);
if (!key.empty() && key.back() != '/')
{
key += '/';
}
const auto folder = sFolders.find(key);
if (folder == sFolders.end())
{
return {};
}
return {folder->second.begin(), folder->second.end()};
}
bool fsCAssetPack::fileGet(std::string_view pFullPath, std::string& pOut)
{
std::string_view path = pFullPath;
if (!sRoot.empty() && path.size() > sRoot.size() && keyGet(path.substr(0, sRoot.size())) == keyGet(sRoot))
{
path = path.substr(sRoot.size());
}
const std::int64_t size = fileSizeGet(path);
if (size < 0)
{
return false;
}
pOut.assign(static_cast<std::size_t>(size), '\0');
return size == 0 || fileRead(path, pOut.data());
}
|