-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbun_extract_file.cpp
226 lines (201 loc) · 5.57 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
#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";
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;
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 {
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);
}
#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()) {
std::string line;
while (std::getline(std::cin, line)) {
wanted_paths.push_back(line);
}
}
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 (!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());
}
}
struct extract_info {
std::string 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());
}
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);
}
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;
std::filesystem::create_directories(output_path.parent_path(), ec);
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;
}