Skip to content

Commit d56e3f4

Browse files
committed
with offset
1 parent 589269c commit d56e3f4

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/rbun_ex.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@ struct Main {
1212
struct CLI {
1313
std::string output = {};
1414
std::vector<std::string> inputs = {};
15+
bool with_offset = {};
1516
bool force = {};
1617
bool no_hash = {};
1718
bool no_progress = {};
1819
} cli = {};
19-
std::unordered_map<ChunkID, std::size_t> seen = {};
20+
std::unordered_set<std::string> seen = {};
2021

2122
auto parse_args(int argc, char** argv) -> void {
2223
argparse::ArgumentParser program(fs::path(argv[0]).filename().generic_string());
2324
program.add_description("Extracts one or more bundles.");
2425
program.add_argument("output").help("Directory to write chunks into.").required();
2526
program.add_argument("input").help("Bundle file(s) or folder(s) to read from.").remaining().required();
2627

27-
program.add_argument("--force")
28+
program.add_argument("--with-offset").help("Put hex offset in name.").default_value(false).implicit_value(true);
29+
program.add_argument("-f", "--force")
2830
.help("Force overwrite existing files.")
2931
.default_value(false)
3032
.implicit_value(true);
@@ -36,6 +38,7 @@ struct Main {
3638

3739
program.parse_args(argc, argv);
3840

41+
cli.with_offset = program.get<bool>("--with-offset");
3942
cli.force = program.get<bool>("--force");
4043
cli.no_hash = program.get<bool>("--no-hash");
4144
cli.no_progress = program.get<bool>("--no-progress");
@@ -63,7 +66,7 @@ struct Main {
6366
}
6467
}
6568
}
66-
if (!paths.empty()) {
69+
if (!paths.empty() && !cli.force) {
6770
std::cerr << "Processing existing chunks ... " << std::endl;
6871
fs::create_directories(cli.output);
6972
for (auto const& entry : fs::directory_iterator(cli.output)) {
@@ -73,10 +76,7 @@ struct Main {
7376
if (entry.path().extension() != ".chunk") {
7477
continue;
7578
}
76-
auto name = entry.path().filename().replace_extension("").generic_string();
77-
if (auto id = from_hex<ChunkID>(name)) {
78-
seen[*id] = entry.file_size();
79-
}
79+
seen.insert(entry.path().filename().generic_string());
8080
}
8181
}
8282
std::cerr << "Processing input bundles ... " << std::endl;
@@ -95,17 +95,21 @@ struct Main {
9595
std::uint64_t offset = 0;
9696
progress_bar p("EXTRACTED", cli.no_progress, index, offset, bundle.toc_offset);
9797
for (auto const& chunk : bundle.chunks) {
98-
if (!seen.contains(chunk.chunkId)) {
98+
auto name = to_hex(chunk.chunkId) + ".chunk";
99+
if (cli.with_offset) {
100+
name = to_hex(offset) + "-" + name;
101+
}
102+
if (!seen.contains(name)) {
99103
auto src = infile.copy(offset, chunk.compressed_size);
100104
auto dst = zstd_decompress(src, chunk.uncompressed_size);
101105
if (!cli.no_hash) {
102106
auto hash_type = RChunk::hash_type(dst, chunk.chunkId);
103107
rlib_assert(hash_type != HashType::None);
104108
}
105-
auto outfile = IOFile(fs::path(cli.output) / (to_hex(chunk.chunkId) + ".chunk"), true);
106-
outfile.resize(0, 0);
109+
auto outfile = IOFile(fs::path(cli.output) / name, true);
110+
outfile.resize(0, dst.size());
107111
outfile.write(0, dst, true);
108-
seen[chunk.chunkId] = dst.size();
112+
seen.insert(std::move(name));
109113
}
110114
offset += chunk.compressed_size;
111115
p.update(offset);

0 commit comments

Comments
 (0)