forked from julixian/MyVisualNovelTransTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextonLikeCScriptSimpleTool.cpp
More file actions
195 lines (167 loc) · 7.12 KB
/
NextonLikeCScriptSimpleTool.cpp
File metadata and controls
195 lines (167 loc) · 7.12 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
#include <iostream>
#include <fstream>
#include <windows.h>
#include <vector>
#include <string>
#include <regex>
#include <filesystem>
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;
}
struct LC_FUNC {
uint32_t func;
uint32_t param1;
uint32_t param2;
};
//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;
}
std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(input), {});
uint32_t funcCount = *(uint32_t*)&buffer[0];
uint32_t scriptBegin = funcCount * 12 + 8;
for (size_t i = 0; i < funcCount; i++) {
LC_FUNC lc_func = *(LC_FUNC*)&buffer[8 + i * 12];
if (lc_func.func == 0x11 && lc_func.param1 == 0x02) {
uint32_t offset = scriptBegin + lc_func.param2 + 4;
std::string str((char*)&buffer[offset]);
std::regex pattern_1(R"([\x01])");
std::regex pattern_2(R"([\x02])");
std::regex pattern_3(R"([\x03])");
str = std::regex_replace(str, pattern_1, "\\x01");
str = std::regex_replace(str, pattern_2, "\\x02");
str = std::regex_replace(str, pattern_3, "\\x03");
output << 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;
}
std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(inputBin), {});
std::vector<std::string> translations;
std::string line;
while (std::getline(inputTxt, line)) {
translations.push_back(line);
}
size_t translationIndex = 0;
uint32_t funcCount = *(uint32_t*)&buffer[0];
uint32_t scriptBegin = funcCount * 12 + 8;
std::vector<uint8_t> newBuffer(scriptBegin);
memcpy(newBuffer.data(), buffer.data(), scriptBegin);
for (size_t i = 0; i < funcCount; i++) {
LC_FUNC lc_func = *(LC_FUNC*)&buffer[8 + i * 12];
if (lc_func.func == 0x11 && lc_func.param1 == 0x02) {
uint32_t offset = newBuffer.size() - scriptBegin;
lc_func.param2 = offset;
*(LC_FUNC*)&newBuffer[8 + i * 12] = lc_func;
if (translationIndex >= translations.size()) {
std::cout << "Not have enough translations!" << std::endl;
continue;
}
std::string str = translations[translationIndex];
translationIndex++;
std::regex pattern_1(R"(\\x01)");
std::regex pattern_2(R"(\\x02)");
std::regex pattern_3(R"(\\x03)");
str = std::regex_replace(str, pattern_1, "\x01");
str = std::regex_replace(str, pattern_2, "\x02");
str = std::regex_replace(str, pattern_3, "\x03");
std::vector<uint8_t> textBytes = stringToCP932(str);
textBytes.push_back(0x00);
uint32_t length = textBytes.size();
newBuffer.insert(newBuffer.end(), (uint8_t*)&length, (uint8_t*)&length + 4);
newBuffer.insert(newBuffer.end(), textBytes.begin(), textBytes.end());
}
}
if (translationIndex != translations.size()) {
std::cout << "Warning: translations too much" << std::endl;
}
*(uint32_t*)&newBuffer[4] = newBuffer.size() - scriptBegin;
// 写入新文件
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.11" << 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;
}