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
|
/*******************************************************************************
* Copyright (c) 2015 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public
* License v3.0 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.translation.mapping;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.mapping.tree.EntryTreeNode;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.translation.representation.entry.Entry;
import cuchaz.enigma.translation.representation.entry.FieldEntry;
import cuchaz.enigma.translation.representation.entry.LocalVariableEntry;
import cuchaz.enigma.translation.representation.entry.MethodEntry;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class MappingsChecker {
private final JarIndex index;
private final EntryTree<EntryMapping> mappings;
public MappingsChecker(JarIndex index, EntryTree<EntryMapping> mappings) {
this.index = index;
this.mappings = mappings;
}
public Dropped dropBrokenMappings(ProgressListener progress) {
Dropped dropped = new Dropped();
Collection<Entry<?>> obfEntries = mappings.getAllEntries()
.filter(e -> e instanceof ClassEntry || e instanceof MethodEntry || e instanceof FieldEntry || e instanceof LocalVariableEntry)
.collect(Collectors.toList());
progress.init(obfEntries.size(), "Checking for dropped mappings");
int steps = 0;
for (Entry<?> entry : obfEntries) {
progress.step(steps++, entry.toString());
tryDropEntry(dropped, entry);
}
dropped.apply(mappings);
return dropped;
}
private void tryDropEntry(Dropped dropped, Entry<?> entry) {
if (shouldDropEntry(entry)) {
EntryMapping mapping = mappings.get(entry);
if (mapping != null) {
dropped.drop(entry, mapping);
}
}
}
private boolean shouldDropEntry(Entry<?> entry) {
if (!index.getEntryIndex().hasEntry(entry)) {
return true;
}
Collection<Entry<?>> resolvedEntries = index.getEntryResolver().resolveEntry(entry, ResolutionStrategy.RESOLVE_ROOT);
return !resolvedEntries.contains(entry);
}
public static class Dropped {
private final Map<Entry<?>, String> droppedMappings = new HashMap<>();
public void drop(Entry<?> entry, EntryMapping mapping) {
droppedMappings.put(entry, mapping.getTargetName());
}
void apply(EntryTree<EntryMapping> mappings) {
for (Entry<?> entry : droppedMappings.keySet()) {
EntryTreeNode<EntryMapping> node = mappings.findNode(entry);
if (node == null) {
continue;
}
for (Entry<?> childEntry : node.getChildrenRecursively()) {
mappings.remove(childEntry);
}
}
}
public Map<Entry<?>, String> getDroppedMappings() {
return droppedMappings;
}
}
}
|