summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/convert/MemberMatches.java
blob: bd74311501c096aab87cb9454e75ed1315abef92 (plain) (blame)
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
/*******************************************************************************
 * 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.convert;

import com.google.common.collect.*;
import cuchaz.enigma.Deobfuscator;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.Entry;

import java.util.Collection;
import java.util.Set;

public class MemberMatches<T extends Entry> {

	private BiMap<T, T> matches;
	private Multimap<ClassEntry, T> matchedSourceEntries;
	private Multimap<ClassEntry, T> unmatchedSourceEntries;
	private Multimap<ClassEntry, T> unmatchedDestEntries;
	private Multimap<ClassEntry, T> unmatchableSourceEntries;

	public MemberMatches() {
		matches = HashBiMap.create();
		matchedSourceEntries = HashMultimap.create();
		unmatchedSourceEntries = HashMultimap.create();
		unmatchedDestEntries = HashMultimap.create();
		unmatchableSourceEntries = HashMultimap.create();
	}

	public void addMatch(T srcEntry, T destEntry) {
		boolean wasAdded = matches.put(srcEntry, destEntry) == null;
		assert (wasAdded);
		wasAdded = matchedSourceEntries.put(srcEntry.getClassEntry(), srcEntry);
		assert (wasAdded);
	}

	public void addUnmatchedSourceEntry(T sourceEntry) {
		boolean wasAdded = unmatchedSourceEntries.put(sourceEntry.getClassEntry(), sourceEntry);
		assert (wasAdded);
	}

	public void addUnmatchedSourceEntries(Iterable<T> sourceEntries) {
		for (T sourceEntry : sourceEntries) {
			addUnmatchedSourceEntry(sourceEntry);
		}
	}

	public void addUnmatchedDestEntry(T destEntry) {
		if (destEntry.getName().equals("<clinit>") || destEntry.getName().equals("<init>"))
			return;
		boolean wasAdded = unmatchedDestEntries.put(destEntry.getClassEntry(), destEntry);
		assert (wasAdded);
	}

	public void addUnmatchedDestEntries(Iterable<T> destEntriesntries) {
		for (T entry : destEntriesntries) {
			addUnmatchedDestEntry(entry);
		}
	}

	public void addUnmatchableSourceEntry(T sourceEntry) {
		boolean wasAdded = unmatchableSourceEntries.put(sourceEntry.getClassEntry(), sourceEntry);
		assert (wasAdded);
	}

	public Set<ClassEntry> getSourceClassesWithUnmatchedEntries() {
		return unmatchedSourceEntries.keySet();
	}

	public Collection<ClassEntry> getSourceClassesWithoutUnmatchedEntries() {
		Set<ClassEntry> out = Sets.newHashSet();
		out.addAll(matchedSourceEntries.keySet());
		out.removeAll(unmatchedSourceEntries.keySet());
		return out;
	}

	public Collection<T> getUnmatchedSourceEntries() {
		return unmatchedSourceEntries.values();
	}

	public Collection<T> getUnmatchedSourceEntries(ClassEntry sourceClass) {
		return unmatchedSourceEntries.get(sourceClass);
	}

	public Collection<T> getUnmatchedDestEntries() {
		return unmatchedDestEntries.values();
	}

	public Collection<T> getUnmatchedDestEntries(ClassEntry destClass) {
		return unmatchedDestEntries.get(destClass);
	}

	public Collection<T> getUnmatchableSourceEntries() {
		return unmatchableSourceEntries.values();
	}

	public boolean hasSource(T sourceEntry) {
		return matches.containsKey(sourceEntry) || unmatchedSourceEntries.containsValue(sourceEntry);
	}

	public boolean hasDest(T destEntry) {
		return matches.containsValue(destEntry) || unmatchedDestEntries.containsValue(destEntry);
	}

	public BiMap<T, T> matches() {
		return matches;
	}

	public boolean isMatchedSourceEntry(T sourceEntry) {
		return matches.containsKey(sourceEntry);
	}

	public boolean isMatchedDestEntry(T destEntry) {
		return matches.containsValue(destEntry);
	}

	public boolean isUnmatchableSourceEntry(T sourceEntry) {
		return unmatchableSourceEntries.containsEntry(sourceEntry.getClassEntry(), sourceEntry);
	}

	public void makeMatch(T sourceEntry, T destEntry) {
		makeMatch(sourceEntry, destEntry, null, null);
	}

	public void makeMatch(T sourceEntry, T destEntry, Deobfuscator sourceDeobfuscator, Deobfuscator destDeobfuscator) {
		if (sourceDeobfuscator != null && destDeobfuscator != null) {
			makeMatch(sourceEntry, destEntry);
			sourceEntry = (T) sourceEntry.cloneToNewClass(sourceDeobfuscator.getJarIndex().getTranslationIndex().resolveEntryClass(sourceEntry, true));
			destEntry = (T) destEntry.cloneToNewClass(destDeobfuscator.getJarIndex().getTranslationIndex().resolveEntryClass(destEntry, true));
		}
		boolean wasRemoved = unmatchedSourceEntries.remove(sourceEntry.getClassEntry(), sourceEntry);
		assert (wasRemoved);
		wasRemoved = unmatchedDestEntries.remove(destEntry.getClassEntry(), destEntry);
		assert (wasRemoved);
		addMatch(sourceEntry, destEntry);
	}

	public boolean isMatched(T sourceEntry, T destEntry) {
		T match = matches.get(sourceEntry);
		return match != null && match.equals(destEntry);
	}

	public void unmakeMatch(T sourceEntry, T destEntry, Deobfuscator sourceDeobfuscator, Deobfuscator destDeobfuscator) {
		if (sourceDeobfuscator != null && destDeobfuscator != null) {
			unmakeMatch(sourceEntry, destEntry, null, null);
			sourceEntry = (T) sourceEntry.cloneToNewClass(
				sourceDeobfuscator.getJarIndex().getTranslationIndex().resolveEntryClass(sourceEntry, true));
			destEntry = (T) destEntry.cloneToNewClass(
				destDeobfuscator.getJarIndex().getTranslationIndex().resolveEntryClass(destEntry, true));
		}

		boolean wasRemoved = matches.remove(sourceEntry) != null;
		assert (wasRemoved);
		wasRemoved = matchedSourceEntries.remove(sourceEntry.getClassEntry(), sourceEntry);
		assert (wasRemoved);
		addUnmatchedSourceEntry(sourceEntry);
		addUnmatchedDestEntry(destEntry);
	}

	public void makeSourceUnmatchable(T sourceEntry, Deobfuscator sourceDeobfuscator) {
		if (sourceDeobfuscator != null) {
			makeSourceUnmatchable(sourceEntry, null);
			sourceEntry = (T) sourceEntry.cloneToNewClass(
				sourceDeobfuscator.getJarIndex().getTranslationIndex().resolveEntryClass(sourceEntry, true));
		}
		assert (!isMatchedSourceEntry(sourceEntry));
		boolean wasRemoved = unmatchedSourceEntries.remove(sourceEntry.getClassEntry(), sourceEntry);
		assert (wasRemoved);
		addUnmatchableSourceEntry(sourceEntry);
	}
}