-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
246 lines (208 loc) · 8.28 KB
/
Copy pathmain.cpp
File metadata and controls
246 lines (208 loc) · 8.28 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
/* SPDX-FileCopyrightText: Copyright 2026 Azamat H. Hackimov <azamat.hackimov@gmail.com> */
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <format>
#include <fstream>
#include <iostream>
#include <yaml-cpp/yaml.h>
#include "manhunt2fsbanker_version.h"
#include "CLI11.hpp"
#include "ContextMapBin.h"
#include "Dir.h"
#include "Fsb.h"
#include "SpeechLst.h"
#include "Wav.h"
#include "WavCodec.h"
void extract(const std::filesystem::path &input_path, const std::filesystem::path &output_path, const bool &recode_wav) {
const std::filesystem::path& fsb_file = input_path;
const std::filesystem::path& dir_path = output_path / fsb_file.stem();
std::fstream fsb_stream(fsb_file, std::fstream::in | std::fstream::binary);
if (!fsb_stream.is_open()) {
std::cerr << "Cannot open .fsb file " << fsb_file << std::endl;
exit(-1);
}
MH2FSB::Fsb fsb;
fsb_stream >> fsb;
std::filesystem::path dir_file = fsb_file;
dir_file.replace_extension(".dir");
std::filesystem::path speech_list_file = fsb_file.parent_path() / "../../../levels" / fsb_file.stem() / "speech.lst" ;
// This is DIR-controlled FSB-file
if (std::filesystem::is_regular_file(dir_file)) {
std::fstream dir_stream(dir_file, std::fstream::in | std::fstream::binary);
if (!dir_stream.is_open()) {
std::cerr << "Cannot open .dir file " << dir_file << std::endl;
exit(-1);
}
MH2FSB::Dir dir;
dir_stream >> dir;
if (dir_file.stem() == "Executions") {
fsb.ResolveExecutionsNames(dir);
}
if (dir_file.stem() == "Scripted") {
fsb.ResolveScriptedNames(dir);
}
dir_stream.close();
} else if (std::filesystem::is_regular_file(speech_list_file)) {
// This is Speech-like FSB file
std::fstream speech_list_stream(speech_list_file, std::fstream::in);
if (!speech_list_stream.is_open()) {
std::cerr << "Cannot open speech.lst file " << speech_list_file << std::endl;
exit(-1);
}
MH2FSB::SpeechLst speech_lst;
speech_list_stream >> speech_lst;
if (!MH2FSB::SpeechLstQuirks::Resolve(fsb_file.stem()).empty()) {
speech_lst.SetEntries(MH2FSB::SpeechLstQuirks::Resolve(fsb_file.filename().stem()));
}
std::filesystem::path contextmap_bin_parent = fsb_file.parent_path();
uint32_t count = 0;
for (auto &entry : fsb.GetSamples()) {
entry.SetRealName(std::format("UNKNOWN/{:04}.wav", count));
count++;
}
auto it = fsb.GetSamples().begin();
for (auto const &speech : speech_lst.GetEntries()) {
if (it == fsb.GetSamples().end()) {
break;
}
std::filesystem::path contextmap_file = contextmap_bin_parent / speech / "context_map.bin";
std::fstream contextmap_stream(contextmap_file, std::fstream::in | std::fstream::binary);
if (!contextmap_stream.is_open()) {
std::cerr << "Cannot open context_map.bin file " << contextmap_file << std::endl;
exit(-1);
}
MH2FSB::ContextMapBin contextmap;
contextmap_stream >> contextmap;
uint32_t prev_entry = 0;
for (uint32_t i = 0; i < 58; i++) {
uint32_t entry = contextmap.GetEntries()[i];
for (uint32_t j = 0; j < (entry - prev_entry); j++) {
std::string filename = std::format("{:04}.wav", j);
std::filesystem::path real_path = std::filesystem::path(speech) / MH2FSB::ContextMapResolve::getContextMapName(i) / filename;
if (it == fsb.GetSamples().end()) {
std::cerr << "WARNING: PREMATURE END OF FSB SAMPLES!" << std::endl;
exit(-1);
} else {
it.operator*().SetRealName(real_path);
++it;
}
}
prev_entry = entry;
}
contextmap_stream.close();
}
} else {
std::cout << "NOT IMPLEMENTED" << std::endl;
exit(1);
}
for (const auto& sample : fsb.GetSamples()) {
std::filesystem::path wav_file = dir_path / sample.GetRealName();
std::error_code ec;
std::filesystem::create_directories(wav_file.parent_path(), ec);
if (ec) {
std::cerr << "Cannot create directory: " << ec.message() << std::endl;
exit(-1);
}
std::fstream wav_stream(wav_file, std::fstream::out | std::fstream::binary);
if (!wav_stream.is_open()) {
std::cerr << "Cannot open .wav file " << wav_file << std::endl;
exit(-1);
}
auto mode = sample.GetMode();
auto sample_size = sample.GetSize();
auto format = (mode | MH2FSB::FSOUND_IMAADPCM) ? MH2FSB::WAVE_FORMAT_IMA_XBOX : MH2FSB::WAVE_FORMAT_PCM;
MH2FSB::WavHeader wav_header(format, sample.GetChannels(), sample.GetFrequency(), sample_size);
if (recode_wav && format == MH2FSB::WAVE_FORMAT_IMA_XBOX) {
// Update format to recalculate inner variables
wav_header.SetAudioFormat(MH2FSB::WAVE_FORMAT_PCM);
}
wav_stream << wav_header;
fsb_stream.seekg(sample.GetOffset(), std::ios::beg);
if (recode_wav) {
UTILS::WavCodec::encode(fsb_stream, wav_stream, wav_header.GetNumChannels(), sample_size);
} else {
std::vector<char> buffer(sample_size);
fsb_stream.read(buffer.data(), sample_size);
wav_stream.write(buffer.data(), sample_size);
}
wav_stream.close();
}
fsb_stream.close();
YAML::Node node;
node["fsb"] = fsb;
std::fstream yaml_stream(dir_path / "fileinfo.yaml", std::fstream::out);
if (!yaml_stream.is_open()) {
std::cerr << "Cannot open fileinfo.yaml file " << dir_path / "fileinfo.yaml" << std::endl;
exit(-1);
}
yaml_stream << node << std::endl;
yaml_stream.close();
}
void pack(const std::filesystem::path &input_path, const std::filesystem::path &output_path) {
std::filesystem::path fsb_file = output_path / input_path.stem();
fsb_file.replace_extension(".fsb");
std::error_code ec;
std::filesystem::create_directories(fsb_file.parent_path(), ec);
if (ec) {
std::cerr << "Cannot create directory: " << ec.message() << std::endl;
exit(-1);
}
MH2FSB::Fsb fsb;
try {
fsb = MH2FSB::Fsb(input_path);
} catch (const std::exception& e) {
std::cerr << "Failed open input directory " << input_path << ": " << e.what() << std::endl;
exit(-1);
}
std::fstream fsb_stream(fsb_file, std::fstream::out | std::fstream::binary);
if (!fsb_stream.is_open()) {
std::cerr << "Cannot open .fsb file " << fsb_file << std::endl;
exit(-1);
}
fsb_stream << fsb;
for (auto& sample : fsb.GetSamples()) {
MH2FSB::WavHeader wav_header;
std::fstream wav_stream(input_path / sample.GetRealName(), std::fstream::in | std::fstream::binary);
if (!wav_stream.is_open()) {
std::cerr << "Cannot open .wav file " << input_path / sample.GetRealName() << std::endl;
exit(-1);
}
wav_stream >> wav_header;
auto sample_size = wav_header.GetRawDataSize();
if (wav_header.GetAudioFormat() == MH2FSB::WAVE_FORMAT_PCM) {
UTILS::WavCodec::decode(wav_stream, fsb_stream, wav_header.GetNumChannels(), sample_size);
} else {
std::vector<char> buffer(sample_size);
wav_stream.read(buffer.data(), sample_size);
fsb_stream.write(buffer.data(), sample_size);
}
wav_stream.close();
}
fsb_stream.close();
}
int main(int argc, char *argv[]) {
std::filesystem::path input_path;
std::filesystem::path output_path;
bool recode_wav = false;
CLI::App app{"Manhunt2FSBanker - unpack/pack FSB files from Rockstar's Manhunt 2 PC game\n"
"Copyright (C) 2026 Azamat H. Hackimov, licensed under LGPL-2.1 or later license"};
app.set_version_flag("-v", MANHUNT2FSBANKER_VERSION);
argv = app.ensure_utf8(argv);
auto unpack_cmd =
app.add_subcommand("unpack", "Extract FSB file")->callback(
[&]() {
extract(input_path, output_path, recode_wav);
});
unpack_cmd->add_option("input", input_path, "Input FSB file(s)")->required()->check(CLI::ExistingFile);
unpack_cmd->add_option("output", output_path, "Output directory");
unpack_cmd->add_flag("-r", recode_wav, "Recode samples to WAV PCM format")->default_val(recode_wav);
auto pack_cmd =
app.add_subcommand("pack", "Pack WAV files to FSB file")->callback(
[&]() {
pack(input_path, output_path);
});
pack_cmd->add_option("input", input_path, "Input directory")->required()->check(CLI::ExistingDirectory);
pack_cmd->add_option("output", output_path, "Output directory");
app.require_subcommand(1, 1);
CLI11_PARSE(app, argc, argv);
return 0;
}