-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTranslator.java
More file actions
212 lines (187 loc) · 6.15 KB
/
Translator.java
File metadata and controls
212 lines (187 loc) · 6.15 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
package roj.plugins;
import roj.archive.zip.ZipEditor;
import roj.archive.zip.ZipEntry;
import roj.asm.AsmCache;
import roj.asm.ClassNode;
import roj.asm.cp.Constant;
import roj.asm.cp.CstString;
import roj.collect.BitSet;
import roj.collect.HashMap;
import roj.collect.IntMap;
import roj.io.IOUtil;
import roj.plugin.Plugin;
import roj.plugin.SimplePlugin;
import roj.text.*;
import roj.ui.Argument;
import roj.util.ByteList;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.zip.ZipFile;
import static roj.ui.CommandNode.argument;
import static roj.ui.CommandNode.literal;
/**
* @author Roj234
* @since 2020/10/18 14:46
*/
@SimplePlugin(id = "translator", desc = """
Jar和Class常量字符串替换(翻译)工具
生成翻译映射表: translate 映射表 jar/class/文件夹
...使用文本编辑器修改映射表中的字符串...
应用翻译映射表: translate apply 映射表 jar/class/文件夹""")
public class Translator extends Plugin {
private static Map<String, IntMap<String>> value;
private static TextWriter out1;
@Override
@SuppressWarnings("unchecked")
protected void onEnable() throws Exception {
registerCommand(literal("translate").then(literal("apply").then(argument("dict", Argument.file()).then(argument("jar", Argument.files(0)).executes(ctx -> {
value = new HashMap<>();
Tokenizer wr = new Tokenizer();
wr.tokenIds(Tokenizer.generate(": 11\n= 12"))
.literalEnd(BitSet.from(":="))
.init(TextReader.auto(ctx.argument("dict", File.class)));
Token w = wr.except(Token.LITERAL, "类名");
while (wr.hasNext()) {
String name = w.text();
wr.except(11, ":");
IntMap<String> map = new IntMap<>();
w = wr.except(Token.INTEGER, "常量池索引");
while (wr.hasNext()) {
int pos = w.asInt();
wr.except(12, "=");
w = wr.except(Token.STRING, "翻译字符串");
map.put(pos, w.text());
w = wr.readWord();
if (w.type() != Token.INTEGER) break;
}
value.put(name, map);
}
List<File> jar = ctx.argument("jar", List.class);
for (File file : jar) apply(file);
})))).then(argument("dict", Argument.fileOptional(true)).then(argument("jar", Argument.files(0)).executes(ctx -> {
try (TextWriter out = TextWriter.to(ctx.argument("dict", File.class))) {
List<File> jar = ctx.argument("jar", List.class);
out1 = out;
for (File file : jar) read(file);
out1 = null;
}
getLogger().info("字符映射表保存成功");
}))));
}
private static void apply(File f) throws IOException {
if (f.isDirectory()) {
IOUtil.listFiles(f, file -> {
String name = file.getName().toLowerCase(Locale.ROOT);
if (name.endsWith(".zip") || name.endsWith(".jar") || name.endsWith(".class")) {
try {
apply(file);
} catch (IOException e) {
System.out.println("错误,这个文件没法读取: " + file.getAbsolutePath());
e.printStackTrace();
}
}
return false;
});
} else if (f.isFile()) {
ByteList ib = IOUtil.getSharedByteBuf();
if (f.getName().endsWith(".class")) {
byte[] mod = apply("", ib.readStreamFully(new FileInputStream(f)));
if (mod != null) {
System.out.println("已修改的内容: " + f.getName());
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(mod);
}
}
} else {
try (ZipEditor mzf = new ZipEditor(f)) {
mzf.setComment("Roj234's class translator");
String path = f.getName()+'/';
for (ZipEntry file : mzf.entries()) {
String name = file.getName();
if (name.endsWith(".class")) {
ib.clear();
byte[] mod = apply(path, mzf.get(file, ib));
if (mod != null) {
System.out.println("已修改的内容: " + path+name);
mzf.put(name, new ByteList(mod));
}
} else if (name.endsWith("/")) {
mzf.put(name, null);
}
}
mzf.save();
}
}
}
}
private static byte[] apply(String path, ByteList ib) {
ClassNode data = ClassNode.parseSkeleton(ib);
IntMap<String> map = value.get(path+data.name());
if (map == null) return null;
boolean any = false;
List<Constant> array = data.cp.constants();
for (IntMap.Entry<String> entry : map.selfEntrySet()) {
CstString ref = (CstString) array.get(entry.getIntKey()-1);
if (!ref.value().str().equals(entry.getValue())) {
ref.setValue(data.cp.getUtf(entry.getValue()));
any = true;
}
}
return any ? AsmCache.toByteArray(data) : null;
}
private static void read(File f) throws IOException {
if (f.isDirectory()) {
IOUtil.listFiles(f, file -> {
String name = file.getName().toLowerCase(Locale.ROOT);
if (name.endsWith(".zip") || name.endsWith(".jar") || name.endsWith(".class")) {
try {
read(file);
} catch (IOException e) {
System.out.println("错误,这个文件没法读取: " + file.getAbsolutePath());
e.printStackTrace();
}
}
return false;
});
} else if (f.isFile()) {
ByteList buf = IOUtil.getSharedByteBuf();
if (f.getName().endsWith(".class")) {
read("", buf.readStreamFully(new FileInputStream(f)));
} else {
try (ZipFile zf = new ZipFile(f)) {
String path = f.getName()+'/';
Enumeration<? extends java.util.zip.ZipEntry> e = zf.entries();
while (e.hasMoreElements()) {
java.util.zip.ZipEntry ze = e.nextElement();
if (ze.getName().endsWith(".class")) {
buf.clear();
read(path, buf.readStreamFully(zf.getInputStream(ze)));
}
}
}
}
}
}
private static void read(String path, ByteList ib) {
ClassNode data = ClassNode.parseSkeleton(ib);
CharList sb = IOUtil.getSharedCharBuf();
sb.append(path).append(data.name()).append(':').append('\n');
boolean any = false;
List<Constant> array = data.cp.constants();
for (int i = 0; i < array.size(); i++) {
Constant s = array.get(i);
if (s.type() == Constant.STRING) {
sb.append('\t').append(i+1).append('=').append('"');
Tokenizer.escape(sb, ((CstString) s).value().str()).append('"').append('\n');
any = true;
}
}
if (any) out1.append(sb);
}
}