forked from rarten/ooz
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathggpk_vfs.cpp
More file actions
116 lines (108 loc) · 3.52 KB
/
ggpk_vfs.cpp
File metadata and controls
116 lines (108 loc) · 3.52 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
#include "ggpk_vfs.h"
#include <poe/util/utf.hpp>
#include <filesystem>
#include <fstream>
struct GgpkVfs {
Vfs vfs;
std::filesystem::path path;
std::unique_ptr<poe::format::ggpk::parsed_ggpk> pack;
};
std::shared_ptr<GgpkVfs> open_ggpk(std::filesystem::path ggpk_path, bool mmap_data) {
auto ret = std::make_shared<GgpkVfs>();
ret->path = ggpk_path;
ret->pack = poe::format::ggpk::index_ggpk(ggpk_path);
if (!ret->pack) {
return {};
}
ret->vfs.open = [](Vfs* vfs, char const* c_path) -> VfsFile* {
auto* gvfs = reinterpret_cast<GgpkVfs*>(vfs);
ggpk::parsed_directory const* dir = gvfs->pack->root_;
std::u16string path = poe::util::lowercase(poe::util::to_u16string(c_path));
std::u16string_view tail(path);
while (!tail.empty() && dir) {
size_t delim = tail.find(u'/');
if (delim == 0) {
continue;
}
std::u16string_view head = tail.substr(0, delim);
bool next_found = false;
for (auto& child : dir->entries_) {
if (poe::util::lowercase(child->name_) == head) {
if (delim == std::string_view::npos) {
return (VfsFile*)dynamic_cast<ggpk::parsed_file const*>(child.get());
}
else {
dir = dynamic_cast<ggpk::parsed_directory const*>(child.get());
tail = tail.substr(delim + 1);
next_found = true;
}
}
}
if (!next_found) {
return nullptr;
}
}
return nullptr;
};
ret->vfs.close = [](Vfs* vfs, VfsFile* file) {};
ret->vfs.size = [](Vfs*, VfsFile* file) -> int64_t {
auto* f = reinterpret_cast<ggpk::parsed_file const*>(file);
return f ? f->data_size_ : -1;
};
if (mmap_data) {
auto const read_via_mmap = [](Vfs* vfs, VfsFile* file, uint8_t* out, int64_t offset, int64_t size) -> int64_t {
auto* gvfs = reinterpret_cast<GgpkVfs*>(vfs);
auto* f = reinterpret_cast<ggpk::parsed_file const*>(file);
if (offset + size > (int64_t)f->data_size_) {
return -1;
}
memcpy(out, gvfs->pack->mapping_.data() + f->data_offset_ + offset, size);
return size;
};
ret->vfs.read = read_via_mmap;
} else {
auto const read_via_file = [](Vfs* vfs, VfsFile* file, uint8_t* out, int64_t offset, int64_t size) -> int64_t {
auto* gvfs = reinterpret_cast<GgpkVfs*>(vfs);
auto* f = reinterpret_cast<ggpk::parsed_file const*>(file);
if (offset + size > (int64_t)f->data_size_) {
return -1;
}
#if _WIN32
HANDLE fh = CreateFileW(gvfs->path.wstring().c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, 0 /*FILE_FLAG_SEQUENTIAL_SCAN*/, nullptr);
if (!fh)
return -1;
uint8_t *out_ptr = out;
LARGE_INTEGER seek_pos{};
seek_pos.QuadPart = (uint64_t)(f->data_offset_ + offset);
if (!SetFilePointerEx(fh, seek_pos, nullptr, FILE_BEGIN)) {
CloseHandle(fh);
return -1;
}
size_t remaining = size;
while (remaining) {
DWORD read_size = (DWORD)(std::min)(remaining, 1ull << 20);
DWORD bytes_read{};
BOOL res = ReadFile(fh, out_ptr, read_size, &bytes_read, nullptr);
if (!res) {
CloseHandle(fh);
return 1;
}
out_ptr += bytes_read;
remaining -= bytes_read;
}
CloseHandle(fh);
#else
std::ifstream ifs(gvfs->path, std::ios::binary);
ifs.seekg(f->data_offset_ + offset);
ifs.read((char *)out, size);
#endif
return size;
};
ret->vfs.read = read_via_file;
}
return ret;
}
Vfs* borrow_vfs(std::shared_ptr<GgpkVfs>& vfs) {
return vfs ? &vfs->vfs : nullptr;
}