forked from julixian/MyVisualNovelTransTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBndArchiveTool.cpp
More file actions
292 lines (242 loc) · 9.67 KB
/
BndArchiveTool.cpp
File metadata and controls
292 lines (242 loc) · 9.67 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
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <filesystem>
#include <algorithm>
#include <iomanip>
namespace fs = std::filesystem;
std::vector<uint8_t> compress(const std::vector<uint8_t>& input) {
std::vector<uint8_t> output;
for (size_t i = 0; i < input.size(); i += 8) {
output.push_back(0xFF);
for (size_t j = 0; j < 8 && i + j < input.size(); ++j) {
output.push_back(input[i + j]);
}
}
return output;
}
class LzssDecompressor {
public:
LzssDecompressor(int frameSize = 0x1000, uint8_t frameFill = 0, int frameInitPos = 0xFEE)
: m_frameSize(frameSize), m_frameFill(frameFill), m_frameInitPos(frameInitPos) {
}
std::vector<uint8_t> decompress(const std::vector<uint8_t>& input) {
std::vector<uint8_t> output;
std::vector<uint8_t> frame(m_frameSize, m_frameFill);
int framePos = m_frameInitPos;
int frameMask = m_frameSize - 1;
size_t inputPos = 0;
while (inputPos < input.size()) {
uint8_t ctrl = input[inputPos++];
for (int bit = 1; bit != 0x100 && inputPos < input.size(); bit <<= 1) {
if (ctrl & bit) {
uint8_t b = input[inputPos++];
frame[framePos++ & frameMask] = b;
output.push_back(b);
}
else {
if (inputPos + 1 >= input.size()) break;
uint8_t lo = input[inputPos++];
uint8_t hi = input[inputPos++];
int offset = ((hi & 0xf0) << 4) | lo;
int count = 3 + (hi & 0xF);
for (int i = 0; i < count; ++i) {
uint8_t v = frame[offset++ & frameMask];
frame[framePos++ & frameMask] = v;
output.push_back(v);
}
}
}
}
return output;
}
private:
int m_frameSize;
uint8_t m_frameFill;
int m_frameInitPos;
};
struct BndEntry {
uint32_t offset;
uint32_t decomprlen;
uint32_t size;
};
bool extractBndFiles(const std::string& bndFilePath, const std::string& outputDir) {
// 打开BND文件
std::ifstream bndFile(bndFilePath, std::ios::binary);
if (!bndFile) {
std::cerr << "无法打开BND文件: " << bndFilePath << std::endl;
return false;
}
// 创建输出目录(如果不存在)
if (!fs::exists(outputDir)) {
if (!fs::create_directories(outputDir)) {
std::cerr << "无法创建输出目录: " << outputDir << std::endl;
return false;
}
}
// 读取索引数目
uint32_t entryCount;
bndFile.read(reinterpret_cast<char*>(&entryCount), sizeof(entryCount));
if (!bndFile) {
std::cerr << "读取索引数目失败" << std::endl;
return false;
}
std::cout << "BND文件包含 " << entryCount << " 个文件" << std::endl;
// 读取所有索引
std::vector<BndEntry> entries(entryCount);
for (uint32_t i = 0; i < entryCount; ++i) {
bndFile.read(reinterpret_cast<char*>(&entries[i]), sizeof(BndEntry));
if (!bndFile) {
std::cerr << "读取索引 #" << i << " 失败" << std::endl;
return false;
}
}
// 提取所有文件
for (uint32_t i = 0; i < entryCount; ++i) {
const BndEntry& entry = entries[i];
// 生成输出文件名 (00000, 00001, ...)
std::ostringstream fileNameStream;
fileNameStream << std::setw(5) << std::setfill('0') << i;
std::string outputFilePath = outputDir + "\\" + fileNameStream.str();
// 创建输出文件
std::ofstream outputFile(outputFilePath, std::ios::binary);
if (!outputFile) {
std::cerr << "无法创建输出文件: " << outputFilePath << std::endl;
return false;
}
// 定位到文件数据
bndFile.seekg(entry.offset, std::ios::beg);
if (!bndFile) {
std::cerr << "定位到文件 #" << i << " 数据失败" << std::endl;
return false;
}
// 读取并写入文件数据
std::vector<uint8_t> buffer(entry.size);
bndFile.read((char*)buffer.data(), entry.size);
LzssDecompressor lzssdecompressor;
std::vector<uint8_t> decompressedData = lzssdecompressor.decompress(buffer);
if (decompressedData.size() != entry.decomprlen) {
std::cout << "Warning: unexpected data: " << i <<
" expect:" << entry.decomprlen << "bytes" <<
" Get:" << decompressedData.size() << "bytes" << std::endl;
}
outputFile.write((char*)decompressedData.data(), decompressedData.size());
if (!outputFile) {
std::cerr << "写入文件 #" << i << " 数据失败" << std::endl;
return false;
}
std::cout << "已提取文件 " << fileNameStream.str()
<< " (偏移: " << entry.offset
<< ", 大小: " << entry.size << " 字节)" << std::endl;
}
std::cout << "所有文件提取完成!" << std::endl;
return true;
}
// 新增封包功能
bool packBndFiles(const std::string& inputDir, const std::string& bndFilePath) {
// 检查输入目录
if (!fs::exists(inputDir) || !fs::is_directory(inputDir)) {
std::cerr << "输入目录不存在或不是有效目录: " << inputDir << std::endl;
return false;
}
// 收集目录中的所有文件
std::vector<fs::path> files;
for (const auto& entry : fs::directory_iterator(inputDir)) {
if (entry.is_regular_file()) {
files.push_back(entry.path());
}
}
// 按文件名排序 (00000, 00001, ...)
std::sort(files.begin(), files.end());
// 创建BND文件
std::ofstream bndFile(bndFilePath, std::ios::binary);
if (!bndFile) {
std::cerr << "无法创建BND文件: " << bndFilePath << std::endl;
return false;
}
// 写入文件数目
uint32_t fileCount = static_cast<uint32_t>(files.size());
bndFile.write(reinterpret_cast<const char*>(&fileCount), sizeof(fileCount));
// 计算头部大小 (4字节文件数 + 每个文件12字节的索引)
uint32_t headerSize = 4 + fileCount * sizeof(BndEntry);
// 构建索引表和文件数据
std::vector<BndEntry> entries(fileCount);
std::vector<std::vector<uint8_t>> compressedData(fileCount);
uint32_t currentOffset = headerSize;
// 第一遍:读取文件,压缩数据,计算偏移
for (uint32_t i = 0; i < fileCount; ++i) {
// 读取原始文件
std::ifstream inputFile(files[i], std::ios::binary);
if (!inputFile) {
std::cerr << "无法打开输入文件: " << files[i] << std::endl;
return false;
}
// 获取文件大小
inputFile.seekg(0, std::ios::end);
size_t fileSize = inputFile.tellg();
inputFile.seekg(0, std::ios::beg);
// 读取文件内容
std::vector<uint8_t> fileData(fileSize);
inputFile.read(reinterpret_cast<char*>(fileData.data()), fileSize);
// 压缩文件数据
compressedData[i] = compress(fileData);
// 设置索引信息
entries[i].offset = currentOffset;
entries[i].decomprlen = static_cast<uint32_t>(fileData.size());
entries[i].size = static_cast<uint32_t>(compressedData[i].size());
// 更新下一个文件的偏移
currentOffset += entries[i].size;
std::cout << "准备打包文件 " << files[i].filename().string()
<< " (原始大小: " << fileData.size()
<< " 字节, 压缩后: " << compressedData[i].size() << " 字节)" << std::endl;
}
// 写入索引表
for (uint32_t i = 0; i < fileCount; ++i) {
bndFile.write(reinterpret_cast<const char*>(&entries[i]), sizeof(BndEntry));
}
// 写入文件数据
for (uint32_t i = 0; i < fileCount; ++i) {
bndFile.write(reinterpret_cast<const char*>(compressedData[i].data()), compressedData[i].size());
std::cout << "已打包文件 " << files[i].filename().string()
<< " (偏移: " << entries[i].offset
<< ", 压缩大小: " << entries[i].size << " 字节)" << std::endl;
}
std::cout << "所有文件打包完成! 共 " << fileCount << " 个文件" << std::endl;
return true;
}
void printUsage(const char* programName) {
std::cout << "Usage:" << std::endl;
std::cout << "Made by julixian 2025.04.11" << std::endl;
std::cout << " extract: " << programName << " -e <BND_FILE> <OUTPUT_DIR>" << std::endl;
std::cout << " pack: " << programName << " -p <INPUT_DIR> <OUTPUT_BND_FILE>" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc != 4) {
printUsage(argv[0]);
return 1;
}
std::string mode = argv[1];
std::string path1 = argv[2];
std::string path2 = argv[3];
if (mode == "-e") {
// 解包模式
if (!extractBndFiles(path1, path2)) {
std::cerr << "BND文件提取失败" << std::endl;
return 1;
}
}
else if (mode == "-p") {
// 封包模式
if (!packBndFiles(path1, path2)) {
std::cerr << "BND文件打包失败" << std::endl;
return 1;
}
}
else {
std::cerr << "无效的模式: " << mode << std::endl;
printUsage(argv[0]);
return 1;
}
return 0;
}