forked from rarten/ooz
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbun_extract_file.cpp
295 lines (262 loc) · 8.22 KB
/
bun_extract_file.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <bun.h>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include "ggpk_vfs.h"
#include "path_rep.h"
#include "util.h"
#include <poe/util/utf.hpp>
using namespace std::string_view_literals;
static char const *const USAGE =
"bun_extract_file list-files GGPK_OR_STEAM_DIR\n"
"bun_extract_file extract-files [--regex] GGPK_OR_STEAM_DIR OUTPUT_DIR [FILE_PATHS...]\n\n"
"GGPK_OR_STEAM_DIR should be either a full path to a Standalone GGPK file or the Steam game directory.\n"
"If FILE_PATHS are omitted the file paths are taken from stdin.\n"
"If --regex is given, FILE_PATHS are interpreted as regular expressions to match.\n";
struct fs_node {
std::map<std::string_view, std::unique_ptr<fs_node>> children;
void record_file_path(std::string_view path);
void create_directories(std::filesystem::path const &base) const;
};
int main(int argc, char *argv[]) {
std::error_code ec;
if (argc < 2 || argv[1] == "--help"sv || argv[1] == "-h"sv) {
fprintf(stderr, USAGE);
return 1;
}
std::string command;
std::filesystem::path ggpk_or_steam_dir;
std::filesystem::path output_dir;
bool use_regex = false;
bool use_mmap = false;
std::vector<std::string> tail_args;
command = argv[1];
int argi = 2;
while (argi < argc) {
if (argv[argi] == "--regex"sv) {
use_regex = true;
++argi;
} else if (argv[argi] == "--mmap"sv) {
use_mmap = true;
++argi;
} else if (argv[argi] == "--no-mmap"sv) {
use_mmap = false;
++argi;
} else {
break;
}
}
if (command == "list-files"sv) {
if (argi < argc) {
ggpk_or_steam_dir = argv[argi++];
}
if (argi != argc) {
fprintf(stderr, USAGE);
return 1;
}
} else if (command == "extract-files"sv) {
if (argi + 1 < argc) {
ggpk_or_steam_dir = argv[argi++];
output_dir = argv[argi++];
while (argi < argc) {
tail_args.push_back(argv[argi++]);
}
}
} else {
fprintf(stderr, USAGE);
return 1;
}
std::shared_ptr<GgpkVfs> vfs;
if (ggpk_or_steam_dir.extension() == ".ggpk") {
vfs = open_ggpk(ggpk_or_steam_dir, use_mmap);
}
#if _WIN32
std::string ooz_dll = "libooz.dll";
#else
std::string ooz_dll = "liblibooz.so";
#endif
Bun *bun = BunNew(ooz_dll.c_str(), "Ooz_Decompress");
if (!bun) {
bun = BunNew(("./" + ooz_dll).c_str(), "Ooz_Decompress");
if (!bun) {
fprintf(stderr, "Could not initialize Bun library\n");
return 1;
}
}
BunIndex *idx = BunIndexOpen(bun, borrow_vfs(vfs), ggpk_or_steam_dir.string().c_str());
if (!idx) {
fprintf(stderr, "Could not open index\n");
return 1;
}
if (command == "list-files") {
auto rep_mem = BunIndexPathRepContents(idx);
for (size_t path_rep_id = 0;; ++path_rep_id) {
uint64_t hash;
uint32_t offset;
uint32_t size;
uint32_t recursive_size;
if (BunIndexPathRepInfo(idx, (int32_t)path_rep_id, &hash, &offset, &size, &recursive_size) < 0) {
break;
}
auto generated = generate_paths(rep_mem + offset, size);
for (auto &p : generated) {
std::cout << p << std::endl;
}
}
return 0;
}
std::vector<std::string> wanted_paths = tail_args;
if (wanted_paths.empty()) {
fprintf(stderr, "No patterns found in command line; reading from stdin, end with EOF (Ctrl-Z + Enter on Windows, Ctrl-D elsewhere):\n");
std::string line;
while (std::getline(std::cin, line)) {
wanted_paths.push_back(line);
}
}
for (auto &path : wanted_paths) {
if (BunIndexPathRepLowercase(idx)) {
for (auto &ch : path) {
ch = (char)std::tolower((int)(unsigned char)ch);
}
}
}
std::vector<std::regex> regexes;
if (use_regex) {
bool regexes_good = true;
for (auto &path : wanted_paths) {
try {
regexes.push_back(std::regex(path));
} catch (std::exception &e) {
fprintf(stderr, "Could not compile regex \"%s\": %s\n", path.c_str(), e.what());
regexes_good = false;
}
}
if (!regexes_good) {
return 1;
}
if (command == "extract-files") {
std::unordered_set<std::string> matching_paths;
auto rep_mem = BunIndexPathRepContents(idx);
for (size_t path_rep_id = 0;; ++path_rep_id) {
uint64_t hash;
uint32_t offset;
uint32_t size;
uint32_t recursive_size;
if (BunIndexPathRepInfo(idx, (int32_t)path_rep_id, &hash, &offset, &size, &recursive_size) < 0) {
break;
}
auto generated = generate_paths(rep_mem + offset, size);
for (auto &p : generated) {
if (BunIndexPathRepLowercase(idx)) {
for (auto &ch : p) {
ch = (char)std::tolower((int)(unsigned char)ch);
}
}
if (!matching_paths.count(p)) {
for (auto &r : regexes) {
if (std::regex_match(p, r)) {
matching_paths.insert(p);
}
}
}
}
}
wanted_paths.assign(matching_paths.begin(), matching_paths.end());
}
}
fs_node root;
struct extract_info {
std::string_view path;
uint32_t offset{};
uint32_t size{};
};
std::unordered_map<uint32_t, std::vector<extract_info>> bundle_parts_to_extract;
for (auto &path : wanted_paths) {
if (path.front() == '"' && path.back() == '"') {
path = path.substr(1, path.size() - 2);
}
int32_t file_id = BunIndexLookupFileByPath(idx, path.c_str());
if (file_id < 0) {
fprintf(stderr, "Could not find file \"%s\"\n", path.c_str());
continue;
}
uint32_t bundle_id{};
uint64_t path_hash{};
extract_info ei{};
ei.path = path;
BunIndexFileInfo(idx, file_id, &path_hash, &bundle_id, &ei.offset, &ei.size);
bundle_parts_to_extract[bundle_id].push_back(ei);
root.record_file_path(path);
}
fprintf(stderr, "Creating directories...\n");
std::filesystem::create_directories(output_dir, ec);
root.create_directories(output_dir);
fprintf(stderr, "Extracting files...\n");
size_t extracted = 0;
size_t missed = 0;
for (auto &[bundle_id, parts] : bundle_parts_to_extract) {
std::vector<uint8_t> bundle_data;
auto bundle_mem = BunIndexExtractBundle(idx, bundle_id);
if (!bundle_mem) {
missed += parts.size();
char const *name;
uint32_t cb;
BunIndexBundleInfo(idx, bundle_id, &name, &cb);
fprintf(stderr, "Could not open bundle \"%s\", missing %zu files.\n", name, parts.size());
continue;
}
for (auto &part : parts) {
std::filesystem::path output_path = output_dir / part.path;
if (!dump_file(output_path, bundle_mem + part.offset, part.size)) {
fprintf(stderr, "Could not write file \"%s\"\n", output_path.string().c_str());
++missed;
continue;
}
++extracted;
}
BunMemFree(bundle_mem);
}
fprintf(stderr, "Done, %zu/%zu extracted, %zu missed.\n", extracted, wanted_paths.size(), missed);
BunIndexClose(idx);
BunDelete(bun);
return 0;
}
void fs_node::record_file_path(std::string_view path) {
{
fs_node *cur_node = this;
size_t comp_start = 0;
while (true) {
std::string_view comp;
if (auto pos = path.find_first_of("/\\", comp_start); pos != path.npos) {
comp = path.substr(comp_start, pos - comp_start);
} else {
break; // if there's no more slashes we've reached the filename and should stop.
}
comp_start = comp_start + comp.size() + 1;
if (comp.empty())
continue;
auto &children = cur_node->children;
fs_node *next_node{};
if (auto next_iter = children.find(comp); next_iter != children.end()) {
next_node = next_iter->second.get();
} else {
auto new_node = std::make_unique<fs_node>();
next_node = new_node.get();
children.insert({comp, std::move(new_node)});
}
cur_node = next_node;
}
}
}
void fs_node::create_directories(std::filesystem::path const &base) const {
for (auto &[child_name, child_node] : children) {
std::filesystem::path child_path = base / child_name;
std::filesystem::create_directory(child_path);
child_node->create_directories(child_path);
}
}