-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.cpp
More file actions
278 lines (229 loc) · 10.8 KB
/
Cache.cpp
File metadata and controls
278 lines (229 loc) · 10.8 KB
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
#include "Cache.h"
#include "Helper.h"
#include "PazFile.h"
#include <filesystem>
#include <fstream>
#include <vector>
#include <Windows.h>
namespace kukdh1 {
// ── Helpers ───────────────────────────────────────────────────────────────────
// Cache lives next to the exe — never inside the game folder (anti-cheat risk).
static std::wstring ExeDir() {
WCHAR buf[MAX_PATH] = {};
GetModuleFileNameW(nullptr, buf, MAX_PATH);
return std::filesystem::path(buf).parent_path().wstring();
}
std::wstring CachePath(const std::wstring & /*pazFolder*/) {
return ExeDir() + L"\\paz_cache.bin";
}
static std::wstring MetaPath(const std::wstring &pazFolder) {
return pazFolder + L"\\pad00000.meta";
}
// Returns FILETIME as uint64 and file size for pad00000.meta.
static bool GetMetaStat(const std::wstring &pazFolder, uint64_t &mtime, uint64_t &size) {
WIN32_FILE_ATTRIBUTE_DATA fa = {};
if (!GetFileAttributesExW(MetaPath(pazFolder).c_str(), GetFileExInfoStandard, &fa))
return false;
ULARGE_INTEGER t;
t.LowPart = fa.ftLastWriteTime.dwLowDateTime;
t.HighPart = fa.ftLastWriteTime.dwHighDateTime;
ULARGE_INTEGER s;
s.LowPart = fa.nFileSizeLow;
s.HighPart = fa.nFileSizeHigh;
mtime = t.QuadPart;
size = s.QuadPart;
return true;
}
// Extract numeric PAZ index from a full wsPazFullPath (e.g. "...\\PAD03471.PAZ" → 3471).
static uint32_t PazIdxFromPath(const std::wstring &fullPath) {
size_t slash = fullPath.rfind(L'\\');
std::wstring fname = (slash != std::wstring::npos) ? fullPath.substr(slash + 1) : fullPath;
// fname = "PAD%05u.PAZ"
if (fname.size() < 12) return 0;
return static_cast<uint32_t>(std::stoul(fname.substr(3, 5)));
}
// ── Binary format ─────────────────────────────────────────────────────────────
//
// Header (variable):
// char[4] magic "PAZC"
// uint32_t version CACHE_VERSION
// uint64_t meta_mtime pad00000.meta FILETIME as uint64
// uint64_t meta_size pad00000.meta byte size
// uint32_t paz_version Meta::uiVersion
// uint32_t paz_file_count Meta::uiPAZFileCount
// uint32_t record_count total file nodes
// uint16_t folder_len wchar_t count of paz folder path
// wchar_t[] folder_path paz folder (UTF-16LE, no null) — invalidates if folder changes
//
// Per record (variable):
// uint32_t paz_idx PAZ file index → reconstructs wsPazFullPath
// uint32_t offset FileInfo::uiOffset
// uint32_t comp_size FileInfo::uiCompressedSize
// uint32_t orig_size FileInfo::uiOriginalSize
// uint16_t path_len byte length of sFullPath (no null terminator)
// char[] path sFullPath (ASCII/UTF-8)
static const char CACHE_MAGIC[4] = {'P','A','Z','C'};
// ── Public API ────────────────────────────────────────────────────────────────
// Reads and validates the fixed + folder parts of the header.
// Leaves f positioned at the start of per-record data on success.
static bool ReadAndValidateHeader(std::ifstream &f, const std::wstring &pazFolder,
uint64_t curMtime, uint64_t curSize,
uint32_t &outPazVersion, uint32_t &outPazFileCount,
uint32_t &outRecordCount) {
char magic[4] = {};
uint32_t version = 0;
uint64_t cachedMtime = 0;
uint64_t cachedSize = 0;
uint32_t pazVersion = 0;
uint32_t pazFileCount = 0;
uint32_t recordCount = 0;
uint16_t folderLen = 0;
f.read(magic, 4);
f.read(reinterpret_cast<char*>(&version), 4);
f.read(reinterpret_cast<char*>(&cachedMtime), 8);
f.read(reinterpret_cast<char*>(&cachedSize), 8);
f.read(reinterpret_cast<char*>(&pazVersion), 4);
f.read(reinterpret_cast<char*>(&pazFileCount), 4);
f.read(reinterpret_cast<char*>(&recordCount), 4);
f.read(reinterpret_cast<char*>(&folderLen), 2);
if (!f) return false;
if (memcmp(magic, CACHE_MAGIC, 4) != 0) return false;
if (version != CACHE_VERSION) return false;
if (cachedMtime != curMtime || cachedSize != curSize) return false;
if (folderLen == 0) return false;
std::wstring cachedFolder(folderLen, L'\0');
f.read(reinterpret_cast<char*>(cachedFolder.data()), folderLen * sizeof(wchar_t));
if (!f) return false;
// Normalise both paths to lowercase for comparison
std::wstring a = pazFolder, b = cachedFolder;
std::transform(a.begin(), a.end(), a.begin(), ::towlower);
std::transform(b.begin(), b.end(), b.begin(), ::towlower);
if (a != b) return false;
outPazVersion = pazVersion;
outPazFileCount = pazFileCount;
outRecordCount = recordCount;
return true;
}
bool IsCacheValid(const std::wstring &pazFolder) {
uint64_t curMtime = 0, curSize = 0;
if (!GetMetaStat(pazFolder, curMtime, curSize)) return false;
std::ifstream f(CachePath(pazFolder), std::ios::binary);
if (!f) return false;
uint32_t dummy1, dummy2, dummy3;
return ReadAndValidateHeader(f, pazFolder, curMtime, curSize, dummy1, dummy2, dummy3);
}
bool LoadCache(const std::wstring &pazFolder, Tree *root,
uint32_t &outPazVersion, uint32_t &outPazFileCount) {
uint64_t curMtime = 0, curSize = 0;
if (!GetMetaStat(pazFolder, curMtime, curSize)) return false;
std::ifstream f(CachePath(pazFolder), std::ios::binary);
if (!f) return false;
// ── Header ────────────────────────────────────────────────────────────────
uint32_t recordCount = 0;
if (!ReadAndValidateHeader(f, pazFolder, curMtime, curSize,
outPazVersion, outPazFileCount, recordCount))
return false;
// ── Records ───────────────────────────────────────────────────────────────
std::vector<std::string> pathParts;
pathParts.reserve(8);
for (uint32_t i = 0; i < recordCount; i++) {
uint32_t pazIdx = 0;
uint32_t offset = 0;
uint32_t compSz = 0;
uint32_t origSz = 0;
uint16_t pathLen = 0;
f.read(reinterpret_cast<char*>(&pazIdx), 4);
f.read(reinterpret_cast<char*>(&offset), 4);
f.read(reinterpret_cast<char*>(&compSz), 4);
f.read(reinterpret_cast<char*>(&origSz), 4);
f.read(reinterpret_cast<char*>(&pathLen), 2);
if (!f || pathLen == 0) return false;
std::string sPath(pathLen, '\0');
f.read(sPath.data(), pathLen);
if (!f) return false;
// Reconstruct wsPazFullPath from folder + PAZ index
wchar_t pazName[32];
swprintf_s(pazName, L"PAD%05u.PAZ", pazIdx);
std::wstring wsPazPath = pazFolder + L"\\" + pazName;
// Build FileInfo
FileInfo fi;
fi.uiOffset = offset;
fi.uiCompressedSize = compSz;
fi.uiOriginalSize = origSz;
fi.sFullPath = sPath;
fi.wsPazFullPath = wsPazPath;
// Insert into tree — same logic as FileThread
ParsePath(sPath, pathParts);
if (pathParts.empty()) continue;
Tree *ptr = root;
for (auto it = pathParts.begin(); it != pathParts.end() - 1; ++it) {
Tree *found = ptr->GetChildFolderWithName(*it);
if (found) {
ptr = found;
} else {
auto node = std::make_unique<Tree>(Tree::TREE_TYPE_FOLDER);
node->SetFolderInfo(ptr, *it);
ptr = ptr->AddChild(std::move(node));
}
}
auto fileNode = std::make_unique<Tree>(Tree::TREE_TYPE_FILE);
fileNode->SetFileInfo(ptr, pathParts.back(), fi);
ptr->AddChild(std::move(fileNode));
}
return f.good() || f.eof();
}
void WriteCache(const std::wstring &pazFolder, Tree *root,
uint32_t pazVersion, uint32_t pazFileCount) {
uint64_t mtime = 0, size = 0;
if (!GetMetaStat(pazFolder, mtime, size)) return;
// Collect all file nodes
std::vector<Tree*> files;
root->GetFileNodeList(files);
std::ofstream f(CachePath(pazFolder), std::ios::binary | std::ios::trunc);
if (!f) return;
// ── Header ────────────────────────────────────────────────────────────────
uint32_t version = CACHE_VERSION;
uint32_t recordCount = static_cast<uint32_t>(files.size());
f.write(CACHE_MAGIC, 4);
f.write(reinterpret_cast<const char*>(&version), 4);
f.write(reinterpret_cast<const char*>(&mtime), 8);
f.write(reinterpret_cast<const char*>(&size), 8);
f.write(reinterpret_cast<const char*>(&pazVersion), 4);
f.write(reinterpret_cast<const char*>(&pazFileCount), 4);
f.write(reinterpret_cast<const char*>(&recordCount), 4);
uint16_t folderLen = static_cast<uint16_t>(pazFolder.size());
f.write(reinterpret_cast<const char*>(&folderLen), 2);
f.write(reinterpret_cast<const char*>(pazFolder.data()), folderLen * sizeof(wchar_t));
// ── Records ───────────────────────────────────────────────────────────────
for (const Tree *node : files) {
const FileInfo &fi = node->GetFileInfo();
uint32_t pazIdx = PazIdxFromPath(fi.wsPazFullPath);
uint32_t offset = fi.uiOffset;
uint32_t compSz = fi.uiCompressedSize;
uint32_t origSz = fi.uiOriginalSize;
uint16_t pathLen = static_cast<uint16_t>(fi.sFullPath.size());
f.write(reinterpret_cast<const char*>(&pazIdx), 4);
f.write(reinterpret_cast<const char*>(&offset), 4);
f.write(reinterpret_cast<const char*>(&compSz), 4);
f.write(reinterpret_cast<const char*>(&origSz), 4);
f.write(reinterpret_cast<const char*>(&pathLen), 2);
f.write(fi.sFullPath.data(), pathLen);
}
// f closes on destruction
}
int ClearPreviewTempFiles() {
WCHAR tempDir[MAX_PATH] = {};
GetTempPathW(MAX_PATH, tempDir);
std::wstring pattern = std::wstring(tempDir) + L"paz_preview_*.*";
WIN32_FIND_DATAW fd = {};
HANDLE h = FindFirstFileW(pattern.c_str(), &fd);
if (h == INVALID_HANDLE_VALUE) return 0;
int count = 0;
do {
std::wstring fullPath = std::wstring(tempDir) + fd.cFileName;
if (DeleteFileW(fullPath.c_str())) ++count;
} while (FindNextFileW(h, &fd));
FindClose(h);
return count;
}
} // namespace kukdh1