Repository navigation
Expand file tree
/
Copy pathG2CryptTool.cpp
More file actions
299 lines (264 loc) · 10.7 KB
/
Copy pathG2CryptTool.cpp
File metadata and controls
299 lines (264 loc) · 10.7 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
#include <iostream>
#include <fstream>
#include <windows.h>
#include <vector>
#include <string>
#include <regex>
#include <filesystem>
namespace fs = std::filesystem;
void decryptStr(uint8_t* data, int length) {
for (int i = 0; i < length; i++) {
data[i] = data[i] - (uint8_t)i - length;
}
}
void encryptStr(uint8_t* data, int length) {
for (int i = 0; i < length; i++) {
data[i] = data[i] + (uint8_t)i + length;
}
}
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 false;
std::vector<uint8_t> textBytes = stringToCP932(str);
for (size_t i = 0; i < textBytes.size(); i++) {
if ((textBytes[i] < 0x20 && textBytes[i] != 0x0d && textBytes[i] != 0x0a) || (0x9f < textBytes[i] && textBytes[i] < 0xe0)) {
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) {
return false;
}
else {
i++;
continue;
}
}
}
return true;
}
//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.string() << " or " << outputPath.string() << std::endl;
return;
}
std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(input), {});
for (size_t i = 0; i < buffer.size() - 6; i++) {
if ((*(uint16_t*)&buffer[i] == 0x0100 || *(uint16_t*)&buffer[i] == 0x0104 || *(uint16_t*)&buffer[i] == 0x0200)
&& *(uint32_t*)&buffer[i + 2] < 384) {
uint32_t length = *(uint32_t*)&buffer[i + 2];
std::string str((char*)&buffer[i + 6], length);
decryptStr((uint8_t*)str.data(), str.length());
if (!isValidCP932(str)) {
continue;
}
std::regex pattern(R"([\r])");
str = std::regex_replace(str, pattern, "[r]");
pattern = R"([\n])";
str = std::regex_replace(str, pattern, "[n]");
//output << std::hex << i << ": [" << str << "] length: " << std::hex << str.length() << std::endl;
output << str << std::endl;
i += 6 + length - 1;
}
}
input.close();
output.close();
std::cout << "Extraction complete. Output saved to " << outputPath.string() << std::endl;
}
struct Sentence {
size_t addr;
int offset;
};
//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.string() << " or "
<< inputTxtPath.string() << " or " << outputBinPath.string() << std::endl;
return;
}
std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(inputBin), {});
std::vector<std::string> translations;
int offset = 0;
uint32_t jmp1 = *(uint32_t*)&buffer[0x26];
uint32_t jmp2 = *(uint32_t*)&buffer[0x2a];
uint32_t jmp3 = *(uint32_t*)&buffer[0x2e];
uint32_t jmp4 = *(uint32_t*)&buffer[0x32];
std::vector<Sentence> Sentences;
// 读取翻译文本
std::string line;
while (std::getline(inputTxt, line)) {
std::regex pattern(R"(\[r\])");
line = std::regex_replace(line, pattern, "\r");
pattern = R"(\[n\])";
line = std::regex_replace(line, pattern, "\n");
translations.push_back(line);
}
size_t translationIndex = 0;
uint32_t ScriptBegin = 0;
std::vector<uint8_t> newBuffer;
std::vector<size_t>jmps;
for (size_t i = 0; i < buffer.size() - 4; i++) {
if (*(uint32_t*)&buffer[i] == 0x1201B) {
ScriptBegin = i;
break;
}
}
if (ScriptBegin == 0) {
std::cout << "Warning: ScriptBegin not found!" << std::endl;
system("pause");
}
for (size_t i = 0; i < buffer.size() - 6; i++) {
if ((*(uint16_t*)&buffer[i] == 0x0100 || *(uint16_t*)&buffer[i] == 0x0104 || *(uint16_t*)&buffer[i] == 0x0200)
&& *(uint32_t*)&buffer[i + 2] < 384) {
uint32_t length = *(uint32_t*)&buffer[i + 2];
std::string str((char*)&buffer[i + 6], length);
decryptStr((uint8_t*)str.data(), str.length());
if (!isValidCP932(str)) {
newBuffer.push_back(buffer[i]);
continue;
}
if (translationIndex >= translations.size()) {
std::cout << "Warning: Not enough translations!" << std::endl;
newBuffer.push_back(buffer[i]);
continue;
}
std::string text = translations[translationIndex++];
std::vector<uint8_t> textBytes = stringToCP932(text);
encryptStr(textBytes.data(), textBytes.size());
newBuffer.push_back(buffer[i]); newBuffer.push_back(buffer[i + 1]);
uint32_t newLen = textBytes.size();
Sentence Se;
Se.addr = i;
Se.offset = newLen - length;
Sentences.push_back(Se);
newBuffer.insert(newBuffer.end(), (uint8_t*)&newLen, (uint8_t*)&newLen + 4);
newBuffer.insert(newBuffer.end(), textBytes.begin(), textBytes.end());
i += 6 + length - 1;
}
else if (*(uint16_t*)&buffer[i] == 0x0700 //07 08 09
|| *(uint16_t*)&buffer[i] == 0x0704
|| *(uint16_t*)&buffer[i] == 0x0710
|| *(uint16_t*)&buffer[i] == 0x0711
|| *(uint16_t*)&buffer[i] == 0x0891
|| *(uint16_t*)&buffer[i] == 0x0892
|| *(uint16_t*)&buffer[i] == 0x0893
|| *(uint16_t*)&buffer[i] == 0x090d) {
if (*(uint32_t*)&buffer[i + 2] < buffer.size()) {
jmps.push_back(newBuffer.size() + 2);
}
newBuffer.push_back(buffer[i]);
}
else {
newBuffer.push_back(buffer[i]);
}
}
if (translationIndex != translations.size()) {
std::cout << "Warning: too many translations!" << std::endl;
system("pause");
}
newBuffer.insert(newBuffer.end(), buffer.end() - 6, buffer.end());
if (jmp1 < buffer.size() && jmp1 != 0x1201B) {
jmps.push_back(0x26);
if (jmp2 < buffer.size() && jmp2 != 0x1201B) {
jmps.push_back(0x2a);
if (jmp3 < buffer.size() && jmp3 != 0x1201B) {
jmps.push_back(0x2e);
if (jmp4 < buffer.size() && jmp4 != 0x1201B) {
jmps.push_back(0x32);
}
}
}
}
for (size_t i = 0; i < jmps.size(); i++) {
uint32_t jmp = *(uint32_t*)&newBuffer[jmps[i]];
offset = 0;
for (size_t j = 0; j < Sentences.size() && Sentences[j].addr < jmp + ScriptBegin; j++) {
offset += Sentences[j].offset;
}
jmp += offset;
*(uint32_t*)&newBuffer[jmps[i]] = jmp;
}
if (newBuffer.size() < buffer.size()) {
//newBuffer.insert(newBuffer.end(), buffer.begin() + newBuffer.size(), buffer.end());
}
// 写入新文件
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.string() << std::endl;
}
void printUsage() {
std::cout << "Made by julixian 2025.07.08" << 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[]) {
//system("chcp 932");
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.string() << std::endl;
}
}
}
}
else {
std::cerr << "Error: Invalid mode selected." << std::endl;
printUsage();
return 1;
}
return 0;
}