forked from skylot/jadx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJResource.java
More file actions
481 lines (429 loc) · 12.3 KB
/
JResource.java
File metadata and controls
481 lines (429 loc) · 12.3 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package jadx.gui.treemodel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.jetbrains.annotations.Nullable;
import jadx.api.ICodeInfo;
import jadx.api.ResourceFile;
import jadx.api.ResourceType;
import jadx.api.ResourcesLoader;
import jadx.api.impl.SimpleCodeInfo;
import jadx.api.resources.ResourceContentType;
import jadx.core.utils.ListUtils;
import jadx.core.utils.Utils;
import jadx.core.xmlgen.ResContainer;
import jadx.gui.jobs.SimpleTask;
import jadx.gui.ui.MainWindow;
import jadx.gui.ui.codearea.AbstractCodeArea;
import jadx.gui.ui.codearea.BinaryContentPanel;
import jadx.gui.ui.codearea.CodeContentPanel;
import jadx.gui.ui.panel.ContentPanel;
import jadx.gui.ui.panel.FontPanel;
import jadx.gui.ui.panel.ImagePanel;
import jadx.gui.ui.popupmenu.JResourcePopupMenu;
import jadx.gui.ui.tab.TabbedPane;
import jadx.gui.utils.Icons;
import jadx.gui.utils.NLS;
import jadx.gui.utils.UiUtils;
import jadx.gui.utils.res.ResTableHelper;
public class JResource extends JLoadableNode {
private static final long serialVersionUID = -201018424302612434L;
private static final ImageIcon ROOT_ICON = UiUtils.openSvgIcon("nodes/resourcesRoot");
private static final ImageIcon ARSC_ICON = UiUtils.openSvgIcon("nodes/resourceBundle");
private static final ImageIcon XML_ICON = UiUtils.openSvgIcon("nodes/xml");
private static final ImageIcon IMAGE_ICON = UiUtils.openSvgIcon("nodes/ImagesFileType");
private static final ImageIcon SO_ICON = UiUtils.openSvgIcon("nodes/binaryFile");
private static final ImageIcon MANIFEST_ICON = UiUtils.openSvgIcon("nodes/manifest");
private static final ImageIcon JAVA_ICON = UiUtils.openSvgIcon("nodes/java");
private static final ImageIcon APK_ICON = UiUtils.openSvgIcon("nodes/archiveApk");
private static final ImageIcon AUDIO_ICON = UiUtils.openSvgIcon("nodes/audioFile");
private static final ImageIcon VIDEO_ICON = UiUtils.openSvgIcon("nodes/videoFile");
private static final ImageIcon FONT_ICON = UiUtils.openSvgIcon("nodes/fontFile");
private static final ImageIcon HTML_ICON = UiUtils.openSvgIcon("nodes/html");
private static final ImageIcon JSON_ICON = UiUtils.openSvgIcon("nodes/json");
private static final ImageIcon TEXT_ICON = UiUtils.openSvgIcon("nodes/text");
private static final ImageIcon ARCHIVE_ICON = UiUtils.openSvgIcon("nodes/archive");
private static final ImageIcon UNKNOWN_ICON = UiUtils.openSvgIcon("nodes/unknown");
public static final Comparator<JResource> RESOURCES_COMPARATOR =
Comparator.<JResource>comparingInt(r -> r.type.ordinal())
.thenComparing(JResource::getName, String.CASE_INSENSITIVE_ORDER);
public enum JResType {
ROOT,
DIR,
FILE
}
private final transient String name;
private transient String shortName;
private final transient JResType type;
private final transient ResourceFile resFile;
private transient volatile boolean loaded;
private transient List<JResource> subNodes = Collections.emptyList();
private transient ICodeInfo content = ICodeInfo.EMPTY;
public JResource(@Nullable ResourceFile resFile, String name, JResType type) {
this(resFile, name, name, type);
}
public JResource(@Nullable ResourceFile resFile, String name, String shortName, JResType type) {
if (resFile == null && type == JResType.FILE) {
throw new IllegalArgumentException("Null resource file");
}
this.resFile = resFile;
this.name = name;
this.shortName = shortName;
this.type = type;
this.loaded = false;
}
public synchronized void update() {
removeAllChildren();
if (Utils.isEmpty(subNodes)) {
if (type == JResType.DIR || type == JResType.ROOT
|| resFile.getType() == ResourceType.ARSC) {
// fake leaf to force show expand button
// real sub nodes will load on expand in loadNode() method
add(new TextNode(NLS.str("tree.loading")));
}
} else {
for (JResource res : subNodes) {
res.update();
add(res);
}
if (type != JResType.FILE) {
// no content, nothing to load
loaded = true;
}
}
}
@Override
public synchronized void loadNode() {
getCodeInfo();
update();
}
@Override
public synchronized SimpleTask getLoadTask() {
if (loaded) {
return null;
}
return new SimpleTask(NLS.str("progress.load"), this::getCodeInfo, this::update);
}
@Override
public String getName() {
return name;
}
public String getShortName() {
return shortName;
}
public JResType getType() {
return type;
}
public List<JResource> getSubNodes() {
return subNodes;
}
public void addSubNode(JResource node) {
subNodes = ListUtils.safeAdd(subNodes, node);
}
public void sortSubNodes() {
sortResNodes(subNodes);
}
private static void sortResNodes(List<JResource> nodes) {
if (Utils.notEmpty(nodes)) {
nodes.forEach(JResource::sortSubNodes);
nodes.sort(RESOURCES_COMPARATOR);
}
}
/**
* Collapse single-child DIR chains into one node with a slash-joined display name (GitHub-style).
*/
public static void mergeMiddleDirs(JResource root) {
mergeChildren(root.subNodes);
}
public static void mergeMiddleDirs(List<JResource> roots) {
mergeChildren(roots);
}
private static void mergeChildren(List<JResource> children) {
for (int i = 0; i < children.size(); i++) {
JResource sub = children.get(i);
JResource replaced = mergeChain(sub, new ArrayList<>());
if (replaced != sub) {
children.set(i, replaced);
}
mergeChildren(replaced.subNodes);
}
}
private static JResource mergeChain(JResource node, List<JResource> merged) {
if (node.type == JResType.DIR) {
List<JResource> subs = node.subNodes;
if (subs.size() == 1 && subs.get(0).type == JResType.DIR) {
merged.add(node);
return mergeChain(subs.get(0), merged);
}
}
if (!merged.isEmpty()) {
merged.add(node);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < merged.size(); i++) {
if (i > 0) {
sb.append('/');
}
sb.append(merged.get(i).shortName);
}
node.shortName = sb.toString();
}
return node;
}
@Override
public boolean hasContent() {
return resFile != null;
}
@Override
public @Nullable ContentPanel getContentPanel(TabbedPane tabbedPane) {
if (resFile == null) {
return null;
}
// TODO: allow to register custom viewers
switch (resFile.getType()) {
case IMG:
return new ImagePanel(tabbedPane, this);
case FONT:
return new FontPanel(tabbedPane, this);
}
if (getContentType() == ResourceContentType.CONTENT_BINARY) {
return new BinaryContentPanel(tabbedPane, this, false);
}
if (getSyntaxByExtension(resFile.getDeobfName()) != null) {
return new CodeContentPanel(tabbedPane, this);
}
// unknown file type, show both text and binary
return new BinaryContentPanel(tabbedPane, this, true);
}
@Override
public JPopupMenu onTreePopupMenu(MainWindow mainWindow) {
return new JResourcePopupMenu(mainWindow, this);
}
@Override
public synchronized ICodeInfo getCodeInfo() {
if (loaded) {
return content;
}
ICodeInfo codeInfo = loadContent();
content = codeInfo;
loaded = true;
return codeInfo;
}
@Override
public ResourceContentType getContentType() {
if (type == JResType.FILE) {
return resFile.getType().getContentType();
}
return ResourceContentType.CONTENT_NONE;
}
private ICodeInfo loadContent() {
if (resFile == null || type != JResType.FILE) {
return ICodeInfo.EMPTY;
}
ResContainer rc = resFile.loadContent();
if (rc == null) {
return ICodeInfo.EMPTY;
}
if (rc.getDataType() == ResContainer.DataType.RES_TABLE) {
ICodeInfo codeInfo = loadCurrentSingleRes(rc);
List<JResource> nodes = ResTableHelper.buildTree(this, rc);
sortResNodes(nodes);
subNodes = nodes;
UiUtils.uiRun(this::update);
return codeInfo;
}
// single node
return loadCurrentSingleRes(rc);
}
private ICodeInfo loadCurrentSingleRes(ResContainer rc) {
switch (rc.getDataType()) {
case TEXT:
case RES_TABLE:
return rc.getText();
case RES_LINK:
try {
ResourceFile resourceFile = rc.getResLink();
return ResourcesLoader.decodeStream(resourceFile, (size, is) -> {
// TODO: check size before loading
if (size > 10 * 1024 * 1024L) {
return new SimpleCodeInfo("File too large for view");
}
Charset charset;
if (resourceFile.getType().getContentType() == ResourceContentType.CONTENT_TEXT) {
charset = StandardCharsets.UTF_8;
} else {
// force one byte charset for binary data to have the same offsets as in a byte array
charset = StandardCharsets.US_ASCII;
}
return ResourcesLoader.loadToCodeWriter(is, charset);
});
} catch (Exception e) {
return new SimpleCodeInfo("Failed to load resource file:\n" + Utils.getStackTrace(e));
}
case DECODED_DATA:
default:
return new SimpleCodeInfo("Unexpected resource type: " + rc);
}
}
@Override
public String getSyntaxName() {
if (resFile == null) {
return null;
}
switch (resFile.getType()) {
case CODE:
return super.getSyntaxName();
case MANIFEST:
case XML:
case ARSC:
return SyntaxConstants.SYNTAX_STYLE_XML;
default:
String syntax = getSyntaxByExtension(resFile.getDeobfName());
if (syntax != null) {
return syntax;
}
return super.getSyntaxName();
}
}
private static final Map<String, String> EXTENSION_TO_FILE_SYNTAX = jadx.core.utils.Utils.newConstStringMap(
"java", SyntaxConstants.SYNTAX_STYLE_JAVA,
"smali", AbstractCodeArea.SYNTAX_STYLE_SMALI,
"js", SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT,
"ts", SyntaxConstants.SYNTAX_STYLE_TYPESCRIPT,
"json", SyntaxConstants.SYNTAX_STYLE_JSON,
"css", SyntaxConstants.SYNTAX_STYLE_CSS,
"less", SyntaxConstants.SYNTAX_STYLE_LESS,
"html", SyntaxConstants.SYNTAX_STYLE_HTML,
"xml", SyntaxConstants.SYNTAX_STYLE_XML,
"yaml", SyntaxConstants.SYNTAX_STYLE_YAML,
"properties", SyntaxConstants.SYNTAX_STYLE_PROPERTIES_FILE,
"ini", SyntaxConstants.SYNTAX_STYLE_INI,
"sql", SyntaxConstants.SYNTAX_STYLE_SQL);
private String getSyntaxByExtension(String name) {
int dot = name.lastIndexOf('.');
if (dot == -1) {
return null;
}
String ext = name.substring(dot + 1);
return EXTENSION_TO_FILE_SYNTAX.get(ext);
}
@Override
public Icon getIcon() {
switch (type) {
case ROOT:
return ROOT_ICON;
case DIR:
return Icons.FOLDER;
case FILE:
ResourceType resType = resFile.getType();
switch (resType) {
case MANIFEST:
return MANIFEST_ICON;
case ARSC:
return ARSC_ICON;
case XML:
return XML_ICON;
case IMG:
return IMAGE_ICON;
case LIB:
return SO_ICON;
case CODE:
return JAVA_ICON;
case APK:
return APK_ICON;
case VIDEOS:
return VIDEO_ICON;
case SOUNDS:
return AUDIO_ICON;
case FONT:
return FONT_ICON;
case HTML:
return HTML_ICON;
case JSON:
return JSON_ICON;
case TEXT:
return TEXT_ICON;
case ARCHIVE:
return ARCHIVE_ICON;
case UNKNOWN:
return UNKNOWN_ICON;
}
return UNKNOWN_ICON;
}
return Icons.FILE;
}
public static boolean isSupportedForView(ResourceType type) {
switch (type) {
case SOUNDS:
case VIDEOS:
case ARCHIVE:
case APK:
return false;
case MANIFEST:
case XML:
case ARSC:
case IMG:
case LIB:
case FONT:
case TEXT:
case JSON:
case HTML:
case UNKNOWN:
return true;
}
return true;
}
public static boolean isOpenInExternalTool(ResourceType type) {
switch (type) {
case SOUNDS:
case VIDEOS:
return true;
default:
return false;
}
}
public ResourceFile getResFile() {
return resFile;
}
@Override
public JClass getJParent() {
return null;
}
@Override
public String getID() {
if (type == JResType.ROOT) {
return "JResources";
}
return makeString();
}
@Override
public String makeString() {
return shortName;
}
@Override
public String makeLongString() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
JResource other = (JResource) o;
return name.equals(other.name) && type.equals(other.type);
}
@Override
public int hashCode() {
return name.hashCode() + 31 * type.ordinal();
}
}