mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-20 16:59:36 +00:00
Store target info between compiles to support cross-project supermixins
This commit is contained in:
1 parent
34dab7d1d5
commit
5814ea1957
5 files changed
+411
-6
No files matched your search
@@ -225,6 +225,11 @@ class AnnotatedMixin {
|
||||
*/
|
||||
private final TypeElement mixin;
|
||||
|
||||
/**
|
||||
* Mixin class
|
||||
*/
|
||||
private final TypeHandle handle;
|
||||
|
||||
/**
|
||||
* Specified targets
|
||||
*/
|
||||
@@ -265,6 +270,7 @@ class AnnotatedMixin {
|
||||
this.annotation = MirrorUtils.getAnnotation(type, Mixin.class);
|
||||
this.mixins = mixins;
|
||||
this.mixin = type;
|
||||
this.handle = new TypeHandle(type);
|
||||
this.classRef = type.getQualifiedName().toString().replace('.', '/');
|
||||
|
||||
TypeHandle primaryTarget = this.initTargets();
|
||||
@@ -358,6 +364,13 @@ class AnnotatedMixin {
|
||||
return this.mixin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type handle for the mixin class
|
||||
*/
|
||||
public TypeHandle getHandle() {
|
||||
return this.handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mixin's targets
|
||||
*/
|
||||
|
||||
@@ -80,6 +80,8 @@ import net.minecraftforge.srg2source.rangeapplier.SrgContainer;
|
||||
*/
|
||||
class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider {
|
||||
|
||||
private static final String MAPID_SYSTEM_PROPERTY = "mixin.target.mapid";
|
||||
|
||||
static enum CompilerEnvironment {
|
||||
/**
|
||||
* Default environment
|
||||
@@ -144,6 +146,11 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider {
|
||||
*/
|
||||
private final Map<String, Integer> tokenCache = new HashMap<String, Integer>();
|
||||
|
||||
/**
|
||||
* Serialisable mixin target map
|
||||
*/
|
||||
private final TargetMap targets;
|
||||
|
||||
/**
|
||||
* SRG container for mcp->srg mappings
|
||||
*/
|
||||
@@ -167,6 +174,7 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider {
|
||||
private AnnotatedMixins(ProcessingEnvironment processingEnv) {
|
||||
this.env = this.detectEnvironment(processingEnv);
|
||||
this.processingEnv = processingEnv;
|
||||
this.targets = this.initTargetMap();
|
||||
|
||||
this.printMessage(Kind.NOTE, "SpongePowered Mixin Annotation Processor v" + MixinBootstrap.VERSION);
|
||||
|
||||
@@ -182,6 +190,12 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider {
|
||||
this.initTokenCache(this.getOption("tokens"));
|
||||
}
|
||||
|
||||
protected TargetMap initTargetMap() {
|
||||
TargetMap targets = TargetMap.create(System.getProperty(AnnotatedMixins.MAPID_SYSTEM_PROPERTY));
|
||||
System.setProperty(AnnotatedMixins.MAPID_SYSTEM_PROPERTY, targets.getSessionId());
|
||||
return targets;
|
||||
}
|
||||
|
||||
private void initTokenCache(String tokens) {
|
||||
if (tokens != null) {
|
||||
Pattern tokenPattern = Pattern.compile("^([A-Z0-9\\-_\\.]+)=([0-9]+)$");
|
||||
@@ -384,6 +398,7 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider {
|
||||
|
||||
if (!this.mixins.containsKey(name)) {
|
||||
AnnotatedMixin mixin = new AnnotatedMixin(this, mixinType);
|
||||
this.targets.registerTargets(mixin);
|
||||
mixin.runValidators(ValidationPass.EARLY, this.validators);
|
||||
this.mixins.put(name, mixin);
|
||||
this.mixinsForPass.add(mixin);
|
||||
@@ -411,12 +426,10 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider {
|
||||
public Collection<TypeMirror> getMixinsTargeting(TypeElement targetType) {
|
||||
List<TypeMirror> minions = new ArrayList<TypeMirror>();
|
||||
|
||||
for (AnnotatedMixin mixin : this.mixins.values()) {
|
||||
for (TypeHandle mixinTarget : mixin.getTargets()) {
|
||||
if (targetType.equals(mixinTarget.getElement())) {
|
||||
minions.add(mixin.getMixin().asType());
|
||||
break;
|
||||
}
|
||||
for (TypeReference mixin : this.targets.getMixinsTargeting(targetType)) {
|
||||
TypeHandle handle = mixin.getHandle(this.processingEnv);
|
||||
if (handle != null) {
|
||||
minions.add(handle.getType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -530,6 +543,10 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider {
|
||||
* Called from each AP when a pass is completed
|
||||
*/
|
||||
public void onPassCompleted() {
|
||||
if (!"true".equalsIgnoreCase(this.getOption("disableTargetExport"))) {
|
||||
this.targets.write(true);
|
||||
}
|
||||
|
||||
for (AnnotatedMixin mixin : this.mixinsForPass) {
|
||||
mixin.runValidators(ValidationPass.LATE, this.validators);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* 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.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.lang.model.element.TypeElement;
|
||||
|
||||
/**
|
||||
* Serialisable map of classes to their associated mixins, used so that we can
|
||||
* pass target information for supermixins from one compiler session to another
|
||||
*/
|
||||
public class TargetMap extends HashMap<TypeReference, Set<TypeReference>> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Session ID, used to identify the temp file
|
||||
*/
|
||||
private final String sessionId;
|
||||
|
||||
/**
|
||||
* Create a new TargetMap with a session ID based on the current system time
|
||||
*/
|
||||
private TargetMap() {
|
||||
this(String.valueOf(System.currentTimeMillis()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a TargetMap with the specified session ID
|
||||
*
|
||||
* @param sessionId
|
||||
*/
|
||||
private TargetMap(String sessionId) {
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the session ID
|
||||
*/
|
||||
public String getSessionId() {
|
||||
return this.sessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register target classes for the specified mixin
|
||||
*
|
||||
* @param mixin mixin to add targets for
|
||||
*/
|
||||
public void registerTargets(AnnotatedMixin mixin) {
|
||||
this.registerTargets(mixin.getTargets(), mixin.getHandle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Register target classes for the supplied mixin
|
||||
*
|
||||
* @param targets List of targets
|
||||
* @param mixin Mixin class
|
||||
*/
|
||||
public void registerTargets(List<TypeHandle> targets, TypeHandle mixin) {
|
||||
for (TypeHandle target : targets) {
|
||||
this.addMixin(target, mixin);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the specified mixin against the specified target
|
||||
*
|
||||
* @param target Target class
|
||||
* @param mixin Mixin class
|
||||
*/
|
||||
public void addMixin(TypeHandle target, TypeHandle mixin) {
|
||||
this.addMixin(target.getReference(), mixin.getReference());
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the specified mixin against the specified target
|
||||
*
|
||||
* @param target Target class
|
||||
* @param mixin Mixin class
|
||||
*/
|
||||
public void addMixin(TypeReference target, TypeReference mixin) {
|
||||
Set<TypeReference> mixins = this.getMixinsFor(target);
|
||||
mixins.add(mixin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mixin classes which target the specified class
|
||||
*
|
||||
* @param target Target class
|
||||
* @return Collection of mixins registered as targetting the specified class
|
||||
*/
|
||||
public Collection<TypeReference> getMixinsTargeting(TypeElement target) {
|
||||
return this.getMixinsTargeting(new TypeHandle(target));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mixin classes which target the specified class
|
||||
*
|
||||
* @param target Target class
|
||||
* @return Collection of mixins registered as targetting the specified class
|
||||
*/
|
||||
public Collection<TypeReference> getMixinsTargeting(TypeHandle target) {
|
||||
return this.getMixinsTargeting(target.getReference());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mixin classes which target the specified class
|
||||
*
|
||||
* @param target Target class
|
||||
* @return Collection of mixins registered as targetting the specified class
|
||||
*/
|
||||
public Collection<TypeReference> getMixinsTargeting(TypeReference target) {
|
||||
return Collections.unmodifiableCollection(this.getMixinsFor(target));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mixin set for specified class
|
||||
*
|
||||
* @param target Target class
|
||||
* @return Set of mixins registered as targetting the specified class
|
||||
*/
|
||||
private Set<TypeReference> getMixinsFor(TypeReference target) {
|
||||
Set<TypeReference> mixins = this.get(target);
|
||||
if (mixins == null) {
|
||||
mixins = new HashSet<TypeReference>();
|
||||
this.put(target, mixins);
|
||||
}
|
||||
return mixins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write this target map to temporary session file
|
||||
*
|
||||
* @param temp Set "delete on exit" for the file
|
||||
*/
|
||||
public void write(boolean temp) {
|
||||
ObjectOutputStream oos = null;
|
||||
FileOutputStream fout = null;
|
||||
try {
|
||||
File sessionFile = TargetMap.getSessionFile(this.sessionId);
|
||||
if (temp) {
|
||||
sessionFile.deleteOnExit();
|
||||
}
|
||||
fout = new FileOutputStream(sessionFile, true);
|
||||
oos = new ObjectOutputStream(fout);
|
||||
oos.writeObject(this);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (oos != null) {
|
||||
try {
|
||||
oos.close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attemp to deserialise a TargetMap from the specified file
|
||||
*
|
||||
* @param sessionFile File to read
|
||||
* @return deserialised map or null if deserialisation failed
|
||||
*/
|
||||
private static TargetMap read(File sessionFile) {
|
||||
ObjectInputStream objectinputstream = null;
|
||||
FileInputStream streamIn = null;
|
||||
try {
|
||||
streamIn = new FileInputStream(sessionFile);
|
||||
objectinputstream = new ObjectInputStream(streamIn);
|
||||
TargetMap map = (TargetMap)objectinputstream.readObject();
|
||||
return map;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if(objectinputstream != null){
|
||||
try {
|
||||
objectinputstream .close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a TargetMap for the specified session id. Generate new map if
|
||||
* the session id is invalid or the file cannot be read. Session ID can be
|
||||
* null
|
||||
*
|
||||
* @param sessionId session to deserialise, can be null to create new
|
||||
* session
|
||||
* @return new TargetMap
|
||||
*/
|
||||
public static TargetMap create(String sessionId) {
|
||||
if (sessionId != null) {
|
||||
File sessionFile = TargetMap.getSessionFile(sessionId);
|
||||
if (sessionFile.exists()) {
|
||||
TargetMap map = TargetMap.read(sessionFile);
|
||||
if (map != null) {
|
||||
return map;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new TargetMap();
|
||||
}
|
||||
|
||||
private static File getSessionFile(String sessionId) {
|
||||
File tempDir = new File(System.getProperty("java.io.tmpdir"));
|
||||
return new File(tempDir, String.format("mixin-targetdb-%s.tmp", sessionId));
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,11 @@ public class TypeHandle {
|
||||
* Actual type element, this is null for inaccessible classes
|
||||
*/
|
||||
private final TypeElement element;
|
||||
|
||||
/**
|
||||
* Reference to this handle, for serialisation
|
||||
*/
|
||||
private TypeReference reference;
|
||||
|
||||
/**
|
||||
* Ctor for imaginary elements, require the enclosing package and the FQ
|
||||
@@ -154,6 +159,13 @@ public class TypeHandle {
|
||||
public boolean isImaginary() {
|
||||
return this.element == null;
|
||||
}
|
||||
|
||||
public TypeReference getReference() {
|
||||
if (this.reference == null) {
|
||||
this.reference = new TypeReference(this);
|
||||
}
|
||||
return this.reference;
|
||||
}
|
||||
|
||||
public String findDescriptor(MemberInfo memberInfo) {
|
||||
String desc = memberInfo.desc;
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* 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.Serializable;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
|
||||
/**
|
||||
* Soft wrapper for a {@link TypeHandle} so that we can serialise it
|
||||
*/
|
||||
public class TypeReference implements Serializable, Comparable<TypeReference> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Class name, used to recreate the handle on the other side
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Transient handle, not serialised, regenerated when required
|
||||
*/
|
||||
private transient TypeHandle handle;
|
||||
|
||||
/**
|
||||
* Create a new soft wrapper for the specified type handle
|
||||
*
|
||||
* @param handle handle to wrap
|
||||
*/
|
||||
public TypeReference(TypeHandle handle) {
|
||||
this.name = handle.getName();
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the class name (internal format)
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the FQ class name (dotted format)
|
||||
*/
|
||||
public String getClassName() {
|
||||
return this.name.replace('/', '.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch or attempt to generate the type handle
|
||||
*
|
||||
* @param processingEnv environment to create handle if it needs to be
|
||||
* regenerated
|
||||
* @return type handle
|
||||
*/
|
||||
public TypeHandle getHandle(ProcessingEnvironment processingEnv) {
|
||||
if (this.handle == null) {
|
||||
TypeElement element = processingEnv.getElementUtils().getTypeElement(getClassName());
|
||||
try {
|
||||
this.handle = new TypeHandle(element);
|
||||
} catch (Exception ex) {
|
||||
// Class probably doesn't exist in scope :/
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return this.handle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("TypeReference[%s]", this.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(TypeReference other) {
|
||||
return other == null ? -1 : this.name.compareTo(other.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return other instanceof TypeReference && this.compareTo((TypeReference)other) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.name.hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user