-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathReferenceManager.java
247 lines (216 loc) · 9.29 KB
/
ReferenceManager.java
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
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.tools.obfuscation;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorRemappable;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.asm.mixin.refmap.ReferenceMapper;
import org.spongepowered.asm.obfuscation.mapping.common.MappingField;
import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import org.spongepowered.tools.obfuscation.interfaces.IReferenceManager;
import org.spongepowered.tools.obfuscation.interfaces.IMessagerEx.MessageType;
/**
* Implementation of the reference manager
*/
public class ReferenceManager implements IReferenceManager {
/**
* Exception thrown when a reference conflict occurs
*/
public static class ReferenceConflictException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final String oldReference, newReference;
public ReferenceConflictException(String oldReference, String newReference) {
this.oldReference = oldReference;
this.newReference = newReference;
}
public String getOld() {
return this.oldReference;
}
public String getNew() {
return this.newReference;
}
}
/**
* Annotation processor
*/
private final IMixinAnnotationProcessor ap;
/**
* Name of the resource to write remapped refs to
*/
private final String outRefMapFileName;
/**
* Available obfuscation environments
*/
private final List<ObfuscationEnvironment> environments;
/**
* Reference mapper for reference mapping
*/
private final ReferenceMapper refMapper = new ReferenceMapper();
private boolean allowConflicts;
public ReferenceManager(IMixinAnnotationProcessor ap, List<ObfuscationEnvironment> environments) {
this.ap = ap;
this.environments = environments;
this.outRefMapFileName = this.ap.getOption(SupportedOptions.OUT_REFMAP_FILE);
}
/* (non-Javadoc)
* @see org.spongepowered.tools.obfuscation.interfaces.IReferenceManager
* #getAllowConflicts()
*/
@Override
public boolean getAllowConflicts() {
return this.allowConflicts;
}
/* (non-Javadoc)
* @see org.spongepowered.tools.obfuscation.interfaces.IReferenceManager
* #setAllowConflicts(boolean)
*/
@Override
public void setAllowConflicts(boolean allowConflicts) {
this.allowConflicts = allowConflicts;
}
/**
* Write out stored mappings
*/
@Override
public void write() {
if (this.outRefMapFileName == null) {
return;
}
PrintWriter writer = null;
try {
writer = this.newWriter(this.outRefMapFileName, "refmap");
this.refMapper.write(writer);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (Exception ex) {
// oh well
}
}
}
}
/**
* Open a writer for an output file
*/
private PrintWriter newWriter(String fileName, String description) throws IOException {
if (fileName.matches("^.*[\\\\/:].*$")) {
File outFile = new File(fileName);
outFile.getParentFile().mkdirs();
this.ap.printMessage(MessageType.INFO, "Writing " + description + " to " + outFile.getAbsolutePath());
return new PrintWriter(outFile);
}
FileObject outResource = this.ap.getProcessingEnvironment().getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", fileName);
this.ap.printMessage(MessageType.INFO, "Writing " + description + " to " + new File(outResource.toUri()).getAbsolutePath());
return new PrintWriter(outResource.openWriter());
}
/* (non-Javadoc)
* @see org.spongepowered.tools.obfuscation.IObfuscationManager
* #getReferenceMapper()
*/
@Override
public ReferenceMapper getMapper() {
return this.refMapper;
}
/* (non-Javadoc)
* @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager
* #addMethodMapping(java.lang.String, java.lang.String,
* org.spongepowered.tools.obfuscation.ObfuscationData)
*/
@Override
public void addMethodMapping(String className, String reference, ObfuscationData<MappingMethod> obfMethodData) {
for (ObfuscationEnvironment env : this.environments) {
MappingMethod obfMethod = obfMethodData.get(env.getType());
if (obfMethod != null) {
MemberInfo remappedReference = new MemberInfo(obfMethod);
this.addMapping(env.getType(), className, reference, remappedReference.toString());
}
}
}
/* (non-Javadoc)
* @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager
* #addMethodMapping(java.lang.String, java.lang.String,
* org.spongepowered.asm.mixin.injection.struct.MemberInfo,
* org.spongepowered.tools.obfuscation.ObfuscationData)
*/
@Override
public void addMethodMapping(String className, String reference, ITargetSelectorRemappable context,
ObfuscationData<MappingMethod> obfMethodData) {
for (ObfuscationEnvironment env : this.environments) {
MappingMethod obfMethod = obfMethodData.get(env.getType());
if (obfMethod != null) {
ITargetSelectorRemappable remappedReference = context.remapUsing(obfMethod, true);
this.addMapping(env.getType(), className, reference, remappedReference.toString());
}
}
}
/* (non-Javadoc)
* @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager
* #addFieldMapping(java.lang.String, java.lang.String,
* org.spongepowered.asm.mixin.injection.struct.MemberInfo,
* org.spongepowered.tools.obfuscation.ObfuscationData)
*/
@Override
public void addFieldMapping(String className, String reference, ITargetSelectorRemappable context, ObfuscationData<MappingField> obfFieldData) {
for (ObfuscationEnvironment env : this.environments) {
MappingField obfField = obfFieldData.get(env.getType());
if (obfField != null) {
MemberInfo remappedReference = MemberInfo.fromMapping(obfField.transform(env.remapDescriptor(context.getDesc())));
this.addMapping(env.getType(), className, reference, remappedReference.toString());
}
}
}
/* (non-Javadoc)
* @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager
* #addClassMapping(java.lang.String, java.lang.String,
* org.spongepowered.tools.obfuscation.ObfuscationData)
*/
@Override
public void addClassMapping(String className, String reference, ObfuscationData<String> obfClassData) {
for (ObfuscationEnvironment env : this.environments) {
String remapped = obfClassData.get(env.getType());
if (remapped != null) {
this.addMapping(env.getType(), className, reference, remapped);
}
}
}
protected void addMapping(ObfuscationType type, String className, String reference, String newReference) {
String oldReference = this.refMapper.addMapping(type.getKey(), className, reference, newReference);
if (type.isDefault()) {
this.refMapper.addMapping(null, className, reference, newReference);
}
if (!this.allowConflicts && oldReference != null && !oldReference.equals(newReference)) {
throw new ReferenceConflictException(oldReference, newReference);
}
}
}