forked from julixian/MyVisualNovelTransTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarbleMblArchiveTool.cpp
More file actions
255 lines (212 loc) · 7.67 KB
/
MarbleMblArchiveTool.cpp
File metadata and controls
255 lines (212 loc) · 7.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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include <filesystem>
#include <sstream>
#include <iomanip>
namespace fs = std::filesystem;
struct FileEntry16 {
char name[16];
uint32_t offset;
uint32_t size;
};
struct FileEntry56 {
char name[56];
uint32_t offset;
uint32_t size;
};
std::vector<uint8_t> parseKey(const std::string& keyStr) {
std::vector<uint8_t> key;
if (keyStr.substr(0, 2) == "0x") {
for (size_t i = 2; i < keyStr.length(); i += 2) {
uint8_t byte = std::stoi(keyStr.substr(i, 2), nullptr, 16);
key.push_back(byte);
}
}
else {
uint32_t decimalKey = std::stoul(keyStr);
key.resize(4);
for (int i = 0; i < 4; ++i) {
key[i] = (decimalKey >> (i * 8)) & 0xFF;
}
}
return key;
}
std::string processFileName(const char* name, size_t maxLen) {
std::string fileName;
size_t nameLen = 0;
while (nameLen < maxLen && name[nameLen] != '\0') {
nameLen++;
}
fileName = std::string(name, nameLen);
if (nameLen + 1 < maxLen) {
size_t extStart = nameLen + 1;
size_t extLen = 0;
while (extStart + extLen < maxLen && name[extStart + extLen] != '\0') {
extLen++;
}
if (extLen > 0) {
fileName += "." + std::string(name + extStart, extLen);
}
}
return fileName;
}
void processFileNamePack(const std::string& fullName, char* output, size_t maxLen) {
std::string name = fs::path(fullName).stem().string();
std::string ext = fs::path(fullName).extension().string();
if (ext.length() > 0 && ext[0] == '.') {
ext = ext.substr(1);
}
memset(output, 0, maxLen);
strncpy(output, name.c_str(), std::min(name.length(), maxLen - 1));
if (!ext.empty()) {
size_t nameLen = strlen(output);
if (nameLen < maxLen - 1) {
output[nameLen] = '\0';
strncpy(output + nameLen + 1, ext.c_str(), maxLen - nameLen - 2);
}
}
}
template<typename T>
void extractMBL(const std::string& mblPath, const std::string& outputDir, const std::vector<uint8_t>& key) {
std::ifstream mblFile(mblPath, std::ios::binary);
if (!mblFile) {
std::cerr << "Failed to open MBL file: " << mblPath << std::endl;
return;
}
std::string listFilePath = fs::path(outputDir).parent_path().string() + "/file_list.txt";
std::ofstream listFile(listFilePath);
if (!listFile) {
std::cerr << "Failed to create file list: " << listFilePath << std::endl;
return;
}
uint32_t fileCount;
mblFile.read(reinterpret_cast<char*>(&fileCount), sizeof(fileCount));
std::vector<T> entries(fileCount);
mblFile.read(reinterpret_cast<char*>(entries.data()), fileCount * sizeof(T));
fs::create_directories(outputDir);
for (const auto& entry : entries) {
std::string fileName = processFileName(entry.name, sizeof(entry.name));
std::string filePath = outputDir + "/" + fileName;
listFile << fileName << std::endl;
std::ofstream outFile(filePath, std::ios::binary);
if (!outFile) {
std::cerr << "Failed to create output file: " << filePath << std::endl;
continue;
}
mblFile.seekg(entry.offset);
std::vector<char> buffer(entry.size);
mblFile.read(buffer.data(), buffer.size());
if (!key.empty()) {
for (size_t i = 0; i < buffer.size(); ++i) {
buffer[i] ^= key[i % key.size()];
}
}
else {
for (char& byte : buffer) {
byte = ~byte + 1;
}
}
outFile.write(buffer.data(), buffer.size());
outFile.close();
std::cout << "Extracted: " << fileName << std::endl;
}
mblFile.close();
listFile.close();
std::cout << "Extraction complete. File list saved to: " << listFilePath << std::endl;
}
template<typename T>
void createMBL(const std::string& inputDir, const std::string& outputPath, const std::vector<uint8_t>& key) {
std::vector<fs::path> files;
for (const auto& entry : fs::recursive_directory_iterator(inputDir)) {
if (fs::is_regular_file(entry)) {
files.push_back(entry.path());
}
}
std::ofstream mblFile(outputPath, std::ios::binary);
if (!mblFile) {
std::cerr << "Failed to create MBL file: " << outputPath << std::endl;
return;
}
uint32_t fileCount = files.size();
mblFile.write(reinterpret_cast<char*>(&fileCount), sizeof(fileCount));
uint32_t currentOffset = 4 + fileCount * sizeof(T);
std::vector<T> entries(fileCount);
for (size_t i = 0; i < fileCount; ++i) {
processFileNamePack(files[i].filename().string(), entries[i].name, sizeof(entries[i].name));
entries[i].offset = currentOffset;
entries[i].size = fs::file_size(files[i]);
currentOffset += entries[i].size;
}
mblFile.write(reinterpret_cast<char*>(entries.data()), fileCount * sizeof(T));
for (const auto& file : files) {
std::ifstream inputFile(file, std::ios::binary);
std::vector<char> buffer(fs::file_size(file));
inputFile.read(buffer.data(), buffer.size());
if (!key.empty()) {
for (size_t i = 0; i < buffer.size(); ++i) {
buffer[i] ^= key[i % key.size()];
}
}
else {
for (char& byte : buffer) {
byte = ~(byte - 1);
}
}
mblFile.write(buffer.data(), buffer.size());
std::cout << "Packed: " << file.filename().string() << std::endl;
}
mblFile.close();
std::cout << "Packing complete. MBL file created: " << outputPath << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 5 || argc > 6) {
std::cerr << "Made by julixian 2025.01.30" << std::endl;
std::cerr << "Usage: " << std::endl;
std::cerr << "For extraction: " << argv[0] << " extract <mbl_file_path> <output_directory> <IndexLength> [key]" << std::endl;
std::cerr << "For packing: " << argv[0] << " pack <input_directory> <output_mbl_file> <IndexLength> [key]" << std::endl;
std::cerr << "Key can be empty, decimal or hexadecimal (prefixed with 0x)" << std::endl;
std::cerr << "Index length can be 24 or 64" << std::endl;
return 1;
}
std::string mode = argv[1];
std::string path1 = argv[2];
std::string path2 = argv[3];
int indexLength = std::stoi(argv[4]);
std::vector<uint8_t> key;
if (argc == 6) {
key = parseKey(argv[5]);
}
if (mode == "extract") {
if (indexLength == 24) {
extractMBL<FileEntry16>(path1, path2, key);
}
else if (indexLength == 64) {
extractMBL<FileEntry56>(path1, path2, key);
}
else {
std::cerr << "Invalid name length. Use 24 or 64." << std::endl;
return 1;
}
}
else if (mode == "pack") {
if (indexLength == 24) {
createMBL<FileEntry16>(path1, path2, key);
}
else if (indexLength == 64) {
createMBL<FileEntry56>(path1, path2, key);
}
else {
std::cerr << "Invalid name length. Use 24 or 64." << std::endl;
return 1;
}
}
else {
std::cerr << "Invalid mode. Use 'extract' or 'pack'." << std::endl;
return 1;
}
return 0;
}