Repository navigation
Expand file tree
/
Copy pathIceArchiveTool.cpp
More file actions
298 lines (249 loc) · 9.74 KB
/
Copy pathIceArchiveTool.cpp
File metadata and controls
298 lines (249 loc) · 9.74 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
293
294
295
296
297
298
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <filesystem>
#include <algorithm>
#include <cstdint>
#include <stdexcept>
namespace fs = std::filesystem;
struct Entry {
uint32_t offset;
uint32_t size;
std::string name;
};
class BinManager {
private:
std::vector<Entry> m_entries;
fs::path m_binPath;
fs::path m_folderPath;
fs::path m_outputPath; // 新增:输出文件路径
void logError(const std::string& message) {
std::cerr << "Error: " << message << std::endl;
}
void logInfo(const std::string& message) {
std::cout << "Info: " << message << std::endl;
}
bool readBinStructure() {
std::ifstream file(m_binPath, std::ios::binary);
if (!file) {
logError("Failed to open bin file: " + m_binPath.string());
return false;
}
uint32_t first_offset;
file.read(reinterpret_cast<char*>(&first_offset), sizeof(first_offset));
if (first_offset < 8 || first_offset >= fs::file_size(m_binPath) || 0 != (first_offset & 3)) {
logError("Invalid bin file structure. First offset: " + std::to_string(first_offset));
return false;
}
int count = (first_offset - 4) / 4;
if (count <= 0 || count > 10000) {
logError("Invalid file count: " + std::to_string(count));
return false;
}
m_entries.reserve(count);
uint32_t prevOffset = first_offset;
for (int i = 0; i < count; ++i) {
uint32_t nextOffset;
file.read(reinterpret_cast<char*>(&nextOffset), sizeof(nextOffset));
if (nextOffset < prevOffset) {
logError("Invalid offset order at index " + std::to_string(i));
return false;
}
Entry entry;
entry.offset = prevOffset;
entry.size = nextOffset - prevOffset;
entry.name = "file_" + std::to_string(i) + ".bin";
m_entries.push_back(entry);
prevOffset = nextOffset;
}
logInfo("Successfully read " + std::to_string(m_entries.size()) + " entries from bin file");
return true;
}
bool extractFiles() {
std::ifstream binFile(m_binPath, std::ios::binary);
if (!binFile) {
logError("Failed to open bin file for reading: " + m_binPath.string());
return false;
}
for (const auto& entry : m_entries) {
fs::path outputPath = m_folderPath / entry.name;
std::ofstream outFile(outputPath, std::ios::binary);
if (!outFile) {
logError("Failed to create output file: " + outputPath.string());
return false;
}
binFile.seekg(entry.offset);
std::vector<char> buffer(entry.size);
binFile.read(buffer.data(), entry.size);
outFile.write(buffer.data(), entry.size);
logInfo("Extracted: " + entry.name);
}
return true;
}
std::vector<fs::path> getUpdateFiles() {
std::vector<fs::path> updateFiles;
for (const auto& entry : fs::directory_iterator(m_folderPath)) {
if (entry.is_regular_file()) {
updateFiles.push_back(entry.path());
}
}
std::sort(updateFiles.begin(), updateFiles.end(), [this](const fs::path& a, const fs::path& b) {
std::string nameA = a.stem().string();
std::string nameB = b.stem().string();
logInfo("Sorting file: " + nameA);
auto extractNumber = [this](const std::string& name) -> int {
size_t pos = name.find_last_of('_');
if (pos != std::string::npos && pos + 1 < name.length()) {
try {
return std::stoi(name.substr(pos + 1));
}
catch (const std::exception& e) {
logError("Failed to extract number from filename: " + name + ". Error: " + e.what());
return -1;
}
}
logError("Invalid filename format: " + name);
return -1;
};
int numA = extractNumber(nameA);
int numB = extractNumber(nameB);
if (numA == -1 || numB == -1) {
return nameA < nameB;
}
return numA < numB;
});
logInfo("Found " + std::to_string(updateFiles.size()) + " update files");
for (const auto& file : updateFiles) {
logInfo("Update file: " + file.filename().string());
}
return updateFiles;
}
bool updateBinFile() {
std::ifstream originalBin(m_binPath, std::ios::binary);
std::ofstream updatedBin(m_outputPath, std::ios::binary); // 使用新的输出路径
if (!originalBin) {
logError("Failed to open original bin file for reading: " + m_binPath.string());
return false;
}
if (!updatedBin) {
logError("Failed to open output bin file for writing: " + m_outputPath.string());
return false;
}
auto updateFiles = getUpdateFiles();
// Write the initial offset table
uint32_t currentOffset = 4 + m_entries.size() * 4;
updatedBin.write(reinterpret_cast<char*>(¤tOffset), sizeof(currentOffset));
for (size_t i = 0; i < m_entries.size(); ++i) {
if (i < updateFiles.size()) {
uint32_t newSize = static_cast<uint32_t>(fs::file_size(updateFiles[i]));
currentOffset += newSize;
}
else {
currentOffset += m_entries[i].size;
}
updatedBin.write(reinterpret_cast<char*>(¤tOffset), sizeof(currentOffset));
}
// Write file contents
for (size_t i = 0; i < m_entries.size(); ++i) {
if (i < updateFiles.size()) {
std::ifstream updateFile(updateFiles[i], std::ios::binary);
if (!updateFile) {
logError("Failed to open update file: " + updateFiles[i].string());
return false;
}
updatedBin << updateFile.rdbuf();
logInfo("Updated file " + std::to_string(i) + " with " + updateFiles[i].string());
}
else {
originalBin.seekg(m_entries[i].offset);
std::vector<char> buffer(m_entries[i].size);
originalBin.read(buffer.data(), m_entries[i].size);
updatedBin.write(buffer.data(), m_entries[i].size);
logInfo("Kept original file " + std::to_string(i));
}
}
logInfo("Bin file update completed successfully. Output: " + m_outputPath.string());
return true;
}
public:
BinManager(const fs::path& binPath, const fs::path& folderPath, const fs::path& outputPath = "")
: m_binPath(binPath), m_folderPath(folderPath), m_outputPath(outputPath) {}
bool Extract() {
try {
if (!readBinStructure()) {
return false;
}
return extractFiles();
}
catch (const std::exception& e) {
logError("Unexpected error during extraction: " + std::string(e.what()));
return false;
}
}
bool Update() {
try {
if (!readBinStructure()) {
return false;
}
return updateBinFile();
}
catch (const std::exception& e) {
logError("Unexpected error during update: " + std::string(e.what()));
return false;
}
}
};
int main(int argc, char* argv[]) {
if (argc < 4 || argc > 5) {
std::cerr << "Made by julixian 2025.01.06" << std::endl;
std::cerr << "Usage:" << std::endl;
std::cerr << " Extract: " << argv[0] << " extract <bin_file> <output_folder>" << std::endl;
std::cerr << " Pack: " << argv[0] << " pack <orgi_bin_file> <input_folder> <output_bin_file>" << std::endl;
return 1;
}
std::string mode = argv[1];
fs::path binPath = argv[2];
fs::path folderPath = argv[3];
fs::path outputPath;
if (!fs::exists(binPath) || !fs::is_regular_file(binPath)) {
std::cerr << "Error: Bin file does not exist or is not a regular file." << std::endl;
return 1;
}
if (mode == "extract") {
if (!fs::exists(folderPath)) {
fs::create_directories(folderPath);
}
}
else if (mode == "pack") {
if (!fs::exists(folderPath) || !fs::is_directory(folderPath)) {
std::cerr << "Error: Pack folder does not exist or is not a directory." << std::endl;
return 1;
}
if (argc != 5) {
std::cerr << "Error: Pack mode requires an output bin file path." << std::endl;
return 1;
}
outputPath = argv[4];
}
else {
std::cerr << "Error: Invalid mode. Use 'extract' or 'pack'." << std::endl;
return 1;
}
BinManager manager(binPath, folderPath, outputPath);
bool success;
if (mode == "extract") {
success = manager.Extract();
}
else {
success = manager.Update();
}
if (success) {
std::cout << "Operation completed successfully." << std::endl;
return 0;
}
else {
std::cerr << "Operation failed. Check the error messages above for details." << std::endl;
return 1;
}
}