forked from julixian/MyVisualNovelTransTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLucifenSobScriptSimpleTool.cpp
More file actions
419 lines (381 loc) · 16.1 KB
/
LucifenSobScriptSimpleTool.cpp
File metadata and controls
419 lines (381 loc) · 16.1 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <iostream>
#include <fstream>
#include <windows.h>
#include <vector>
#include <string>
#include <iomanip>
#include <cstdint>
#include <filesystem>
#include <algorithm>
namespace fs = std::filesystem;
std::vector<uint8_t> stringToCP932(const std::string& str) {
std::vector<uint8_t> result;
for (char c : str) {
result.push_back(static_cast<uint8_t>(c));
}
return result;
}
bool isValidCP932(const std::string& str) {
if (str.empty()) return true;
std::vector<uint8_t> textBytes = stringToCP932(str);
//for (char ch : str) {
// /*if (*(uint8_t*)&ch < 0x20) {
// return false;
// }*/
// textBytes.push_back(*(uint8_t*)&ch);
//}
for (size_t i = 0; i < textBytes.size(); i++) {
if (textBytes[i] < 0x20 || (0x9f < textBytes[i] && textBytes[i] < 0xe0)) {
//std::cout << str << " :E1 " << i << std::endl;
return false;
}
else if ((0x81 <= textBytes[i] && textBytes[i] <= 0x9f) || (0xe0 <= textBytes[i] && textBytes[i] <= 0xef)) {
if (textBytes[i + 1] > 0xfc || textBytes[i + 1] < 0x40) {
//std::cout << str << " :E2 " << i << std::endl;
return false;
}
else {
i++;
continue;
}
}
}
return true;
/*int required = MultiByteToWideChar(
932,
MB_ERR_INVALID_CHARS,
str.c_str(),
str.length(),
nullptr,
0
);
if (required == 0) {
return false;
}
std::wstring wide(required, L'\0');
int result = MultiByteToWideChar(
932,
MB_ERR_INVALID_CHARS,
str.c_str(),
str.length(),
&wide[0],
required
);
return result != 0;*/
}
struct Sentence {
size_t offsetAddr;
uint16_t seq;
std::string str;
};
std::vector<Sentence> Sentences;
//DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
void dumpText(const fs::path& inputPath, const fs::path& outputPath) {
std::ifstream input(inputPath, std::ios::binary);
std::ofstream output(outputPath);
if (!input || !output) {
std::cerr << "Error opening files: " << inputPath << " or " << outputPath << std::endl;
return;
}
Sentences.clear();
std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(input), {});
size_t p = buffer.size() - 1;
while (buffer[p] == 0x00)p--;
while (buffer[p] != 0x00)p--;
p++;
size_t ScriptBegin = 0;
memcpy(&ScriptBegin, &buffer[0x4], sizeof(uint32_t));
ScriptBegin += 0x10;
size_t firstOffset = 0;
if (*(uint32_t*)&buffer[p - 4] != 0x00) {
firstOffset = p - ScriptBegin;
}
else {
firstOffset = p - 3 - ScriptBegin;
}
bool begin = false;
for (size_t i = 0x10; i < ScriptBegin; i += 4) {
uint32_t offset = 0;
memcpy(&offset, &buffer[i], sizeof(uint32_t));
if (!begin) {
if (offset == firstOffset) {
begin = true;
}
else {
continue;
}
}
size_t SeAddr = ScriptBegin + offset;
if (SeAddr >= buffer.size())continue;
if (buffer[SeAddr] == 0 && buffer[SeAddr + 3] > 0x20 && buffer[SeAddr + 3] != 0x40) {
uint16_t seq = 0;
memcpy(&seq, &buffer[SeAddr + 1], sizeof(uint16_t));
std::string str((char*)&buffer[SeAddr + 3]);
//std::cout << "offset: " << std::hex << i << " SeAddr: " << SeAddr << std::endl;
if (str.length() >= 4) {
size_t length = str.length();
size_t totalLength = 0;
for (size_t j = SeAddr + 3; j < SeAddr + 3 + length - 4; j++) {
if (buffer[j] == 0x20 && buffer[j + 1] == 0xbc && buffer[j + 2] == 0x80 && buffer[j + 3] == 0x01) {
//std::cout << "Fix find: " << std::hex << j << std::endl;
size_t fixoffset = j - ScriptBegin; //0x20的偏移,也就是偏移列表应该显示的一个偏移
size_t fixop = 0; //显示这个偏移的位置
for (size_t k = 0x10; k < ScriptBegin; k += 4) {
uint32_t foffset = 0;
memcpy(&foffset, &buffer[k], sizeof(uint32_t));
if (foffset == fixoffset) {
fixop = k;
}
}
std::string nameFix = "";
nameFix += "[Fix:";
nameFix += std::to_string(fixop);
nameFix += "]";
str.insert(str.length(), nameFix);
}
}
}
//if (!isValidCP932(str))continue;
Sentence se;
se.offsetAddr = i;
se.seq = seq;
se.str = str;
Sentences.push_back(se);
//std::cout << std::hex << i << std::endl;
}
else if (buffer[SeAddr] == 0x00 && buffer[SeAddr + 2] == 0x00 && buffer[SeAddr + 3] != 0x00 && buffer[SeAddr + 4] == 0x00) {
uint16_t selectCount = 0;
memcpy(&selectCount, &buffer[SeAddr + 3], sizeof(uint16_t));
if (selectCount == 0) {
//std::cout << "select0 at: " << std::hex << i << " : " << SeAddr << std::endl;
continue;
}
SeAddr += 5;
std::string str = "Select:";
for (size_t j = 0; j < selectCount; j++) {
uint16_t length = 0;
memcpy(&length, &buffer[SeAddr], sizeof(uint16_t));
std::string select((char*)&buffer[SeAddr + 2]);
str += select;
str += "|||||";
SeAddr += length;
}
Sentence se;
se.offsetAddr = i;
se.seq = selectCount;
se.str = str;
Sentences.push_back(se);
}
else if (buffer[SeAddr] >= 0x20 && buffer[SeAddr] <= 0xef) {
std::string str((char*)&buffer[SeAddr]);
//std::cout << str << std::endl;
//if (str.length() < 2 && str!="0")continue;
if (!isValidCP932(str))continue;
Sentence se;
se.offsetAddr = i;
se.seq = 0xffff;
se.str = str;
Sentences.push_back(se);
}
}
//reverse(Sentences.begin(), Sentences.end());
std::erase_if(Sentences, [&](Sentence& Se)
{
return *(uint32_t*)&buffer[Se.offsetAddr] < *(uint32_t*)&buffer[Sentences.back().offsetAddr];
});
for (auto it = Sentences.begin(); it != Sentences.end(); it++) {
output << std::hex << it->offsetAddr << ":::::" << (size_t)it->seq << ":::::" << it->str << std::endl;
}
input.close();
output.close();
std::cout << "Extraction complete. Output saved to " << outputPath << std::endl;
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
void injectText(const fs::path& inputBinPath, const fs::path& inputTxtPath, const fs::path& outputBinPath) {
std::ifstream inputBin(inputBinPath, std::ios::binary);
std::ifstream inputTxt(inputTxtPath);
std::ofstream outputBin(outputBinPath, std::ios::binary);
if (!inputBin || !inputTxt || !outputBin) {
std::cerr << "Error opening files: " << inputBinPath << " or " << inputTxtPath << " or " << outputBinPath << std::endl;
return;
}
Sentences.clear();
std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(inputBin), {});
std::vector<std::string> translations;
std::string line;
while (std::getline(inputTxt, line)) {
size_t first_sep = line.find(":::::");
size_t second_sep = line.find(":::::", first_sep + 5);
std::string first_str = line.substr(0, first_sep);
std::string second_str = line.substr(first_sep + 5, second_sep - (first_sep + 5));
std::string trans = line.substr(second_sep + 5);
size_t opoffset = std::stoul(first_str, nullptr, 16);
uint16_t seq = std::stoul(second_str, nullptr, 16);
Sentences.push_back({ opoffset, seq, trans });
}
size_t ScriptBegin = 0;
memcpy(&ScriptBegin, &buffer[0x4], sizeof(uint32_t));
ScriptBegin += 0x10;
std::vector<uint8_t> newBuffer;
reverse(Sentences.begin(), Sentences.end());
uint32_t ScriptChunckBegin = 0;
memcpy(&ScriptChunckBegin, &buffer[Sentences[0].offsetAddr], sizeof(uint32_t));
ScriptChunckBegin += ScriptBegin;
newBuffer.resize(ScriptChunckBegin);
memcpy(newBuffer.data(), buffer.data(), ScriptChunckBegin);
for (auto it = Sentences.begin(); it != Sentences.end(); it++) {
if (strncmp(it->str.c_str(), "Select:", 7) == 0) {
uint32_t newOffset = newBuffer.size() - ScriptBegin;
memcpy(&newBuffer[it->offsetAddr], &newOffset, sizeof(uint32_t));
newBuffer.push_back(0x00);
newBuffer.push_back(0x00);
newBuffer.push_back(0x00);
size_t orgiof = 0;
memcpy(&orgiof, &buffer[it->offsetAddr], sizeof(uint32_t));
memcpy(&newBuffer[newBuffer.size() - 2], &buffer[orgiof + ScriptBegin + 1], sizeof(uint16_t));
newBuffer.push_back(0x00);
newBuffer.push_back(0x00);
size_t stepf = 7;
size_t stepl = 0;
memcpy(&newBuffer[newBuffer.size() - 2], &it->seq, sizeof(uint16_t));
for (size_t j = 0; j < it->seq; j++) {
stepl = it->str.find("|||||", stepf);
std::string text = it->str.substr(stepf, stepl - stepf);
//std::cout << text << std::endl;
std::vector<uint8_t> textBytes = stringToCP932(text);
uint16_t newlength = textBytes.size() + 3;
newBuffer.push_back(0x00);
newBuffer.push_back(0x00);
memcpy(&newBuffer[newBuffer.size() - 2], &newlength, sizeof(uint16_t));
newBuffer.insert(newBuffer.end(), textBytes.begin(), textBytes.end());
newBuffer.push_back(0x00);
stepf = stepl + 5;
}
newBuffer.push_back(0x00);
}
else {
if (it->seq == 0xffff) {
uint32_t newOffset = newBuffer.size() - ScriptBegin;
memcpy(&newBuffer[it->offsetAddr], &newOffset, sizeof(uint32_t));
std::vector<uint8_t> textBytes = stringToCP932(it->str);
newBuffer.insert(newBuffer.end(), textBytes.begin(), textBytes.end());
newBuffer.push_back(0x00);
}
else {
std::vector<size_t> Fixops;
while (true) {
size_t pos = it->str.find("[Fix:");
if (pos != std::string::npos) {
//std::cout << it->str << std::endl;
size_t end = it->str.find("]", pos);
std::string Fixopstr = it->str.substr(pos + 5, end - pos - 5);
//std::cout << "Fixopstr: " << Fixopstr << std::endl;
size_t Fixop = std::stoul(Fixopstr, nullptr, 10);
Fixops.push_back(Fixop);
it->str.erase(pos, end - pos + 1);
//std::cout << it->str << std::endl;
}
else {
break;
}
}
uint32_t newOffset = newBuffer.size() - ScriptBegin;
memcpy(&newBuffer[it->offsetAddr], &newOffset, sizeof(uint32_t));
newBuffer.push_back(0x00);
newBuffer.push_back(0x00);
newBuffer.push_back(0x00);
memcpy(&newBuffer[newBuffer.size() - 2], &it->seq, sizeof(uint16_t));
std::vector<uint8_t> textBytes = stringToCP932(it->str);
newBuffer.insert(newBuffer.end(), textBytes.begin(), textBytes.end());
newBuffer.push_back(0x00);
if (!Fixops.empty()) {
size_t u = 0;
for (size_t j = newBuffer.size() - textBytes.size() - 1; j < newBuffer.size() - 1 - 4; j++) {
if (newBuffer[j] == 0x20 && newBuffer[j + 1] == 0xbc && newBuffer[j + 2] == 0x80 && newBuffer[j + 3] == 0x01) {
size_t fixedoffset = j - ScriptBegin;
if (u >= Fixops.size()) {
std::cout << "Error: Don't have enough Fix ops " << std::hex << it->seq << std::endl;
system("pause");
continue;
}
memcpy(&newBuffer[Fixops[u]], &fixedoffset, sizeof(uint32_t));
u++;
}
}
}
}
}
}
// 写入新文件
if (newBuffer.size() < buffer.size()) {
//newBuffer.insert(newBuffer.end(), buffer.begin() + newBuffer.size(), buffer.end());
newBuffer.resize(buffer.size());
}
outputBin.write(reinterpret_cast<const char*>(newBuffer.data()), newBuffer.size());
inputBin.close();
inputTxt.close();
outputBin.close();
std::cout << "Write-back complete. Output saved to " << outputBinPath << std::endl;
}
void printUsage() {
std::cout << "Made by julixian 2025.04.12" << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << " Dump: ./program dump <input_folder> <output_folder>" << std::endl;
std::cout << " Inject: ./program inject <input_orgi-bin_folder> <input_translated-txt_folder> <output_folder>" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printUsage();
return 1;
}
std::string mode = argv[1];
if (mode == "dump") {
if (argc != 4) {
std::cerr << "Error: Incorrect number of arguments for dump mode." << std::endl;
printUsage();
return 1;
}
std::string inputFolder = argv[2];
std::string outputFolder = argv[3];
fs::create_directories(outputFolder);
for (const auto& entry : fs::recursive_directory_iterator(inputFolder)) {
if (fs::is_regular_file(entry)) {
fs::path relativePath = fs::relative(entry.path(), inputFolder);
fs::path outputPath = fs::path(outputFolder) / relativePath;
fs::create_directories(outputPath.parent_path());
dumpText(entry.path(), outputPath);
}
}
}
else if (mode == "inject") {
if (argc != 5) {
std::cerr << "Error: Incorrect number of arguments for inject mode." << std::endl;
printUsage();
return 1;
}
std::string inputBinFolder = argv[2];
std::string inputTxtFolder = argv[3];
std::string outputFolder = argv[4];
fs::create_directories(outputFolder);
for (const auto& entry : fs::recursive_directory_iterator(inputBinFolder)) {
if (fs::is_regular_file(entry)) {
fs::path relativePath = fs::relative(entry.path(), inputBinFolder);
fs::path txtPath = fs::path(inputTxtFolder) / relativePath;
fs::path outputPath = fs::path(outputFolder) / relativePath;
fs::create_directories(outputPath.parent_path());
if (fs::exists(txtPath)) {
injectText(entry.path(), txtPath, outputPath);
}
else {
std::cerr << "Warning: No corresponding file found for " << relativePath << std::endl;
}
}
}
}
else {
std::cerr << "Error: Invalid mode selected." << std::endl;
printUsage();
return 1;
}
return 0;
}