diff options
author | Jonas Herzig <me@johni0702.de> | 2019-08-30 16:48:00 +0200 |
---|---|---|
committer | Jonas Herzig <me@johni0702.de> | 2019-08-30 16:48:00 +0200 |
commit | cfdc125366b756a7d164502ebcde22e2976c9319 (patch) | |
tree | 03559547dd1cc52ec9a3f2a13c490da3d02182e6 /src/main/java | |
parent | 06279d658496a3fb0a1909bad1ebdb9a60a37aed (diff) | |
download | Remap-cfdc125366b756a7d164502ebcde22e2976c9319.tar.gz Remap-cfdc125366b756a7d164502ebcde22e2976c9319.tar.bz2 Remap-cfdc125366b756a7d164502ebcde22e2976c9319.zip |
Convert implementation and build script to Kotlin
Diffstat (limited to 'src/main/java')
6 files changed, 0 insertions, 861 deletions
diff --git a/src/main/java/com/replaymod/gradle/remap/PsiMapper.java b/src/main/java/com/replaymod/gradle/remap/PsiMapper.java deleted file mode 100644 index c1b2d41..0000000 --- a/src/main/java/com/replaymod/gradle/remap/PsiMapper.java +++ /dev/null @@ -1,429 +0,0 @@ -package com.replaymod.gradle.remap; - -import com.intellij.openapi.util.TextRange; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.psi.JavaRecursiveElementVisitor; -import com.intellij.psi.PsiAnnotation; -import com.intellij.psi.PsiAnnotationMemberValue; -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiClassObjectAccessExpression; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiField; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiIdentifier; -import com.intellij.psi.PsiJavaCodeReferenceElement; -import com.intellij.psi.PsiMethod; -import com.intellij.psi.PsiNameValuePair; -import com.intellij.psi.PsiPackage; -import com.intellij.psi.PsiQualifiedNamedElement; -import com.intellij.psi.PsiSwitchLabelStatement; -import com.intellij.psi.PsiTypeElement; -import org.cadixdev.bombe.type.signature.MethodSignature; -import org.cadixdev.lorenz.MappingSet; -import org.cadixdev.lorenz.model.ClassMapping; -import org.cadixdev.lorenz.model.Mapping; -import org.cadixdev.lorenz.model.MethodMapping; - -import java.util.ArrayDeque; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Map; -import java.util.TreeMap; - -import static com.replaymod.gradle.remap.PsiUtils.getSignature; - -class PsiMapper { - private static final String CLASS_MIXIN = "org.spongepowered.asm.mixin.Mixin"; - private static final String CLASS_ACCESSOR = "org.spongepowered.asm.mixin.gen.Accessor"; - private static final String CLASS_AT = "org.spongepowered.asm.mixin.injection.At"; - private static final String CLASS_INJECT = "org.spongepowered.asm.mixin.injection.Inject"; - private static final String CLASS_REDIRECT = "org.spongepowered.asm.mixin.injection.Redirect"; - - private final MappingSet map; - private final Map<String, ClassMapping<?, ?>> mixinMappings = new HashMap<>(); - private final PsiFile file; - private boolean error; - private TreeMap<TextRange, String> changes = new TreeMap<>(Comparator.comparing(TextRange::getStartOffset)); - - PsiMapper(MappingSet map, PsiFile file) { - this.map = map; - this.file = file; - } - - private void error(PsiElement at, String message) { - int line = StringUtil.offsetToLineNumber(file.getText(), at.getTextOffset()); - System.err.println(file.getName() + ":" + line + ": " + message); - error = true; - } - - private void replace(PsiElement e, String with) { - changes.put(e.getTextRange(), with); - } - - private void replaceIdentifier(PsiElement parent, String with) { - for (PsiElement child : parent.getChildren()) { - if (child instanceof PsiIdentifier) { - replace(child, with); - return; - } - } - } - - private boolean valid(PsiElement e) { - TextRange range = e.getTextRange(); - TextRange before = changes.ceilingKey(range); - return before == null || !before.intersects(range); - } - - private String getResult(String text) { - if (error) { - return null; - } - for (Map.Entry<TextRange, String> change : changes.descendingMap().entrySet()) { - text = change.getKey().replace(text, change.getValue()); - } - return text; - } - - private static boolean isSwitchCase(PsiElement e) { - if (e instanceof PsiSwitchLabelStatement) { - return true; - } - PsiElement parent = e.getParent(); - return parent != null && isSwitchCase(parent); - } - - private void map(PsiElement expr, PsiField field) { - PsiClass declaringClass = field.getContainingClass(); - if (declaringClass == null) return; - String name = declaringClass.getQualifiedName(); - if (name == null) return; - ClassMapping<?, ?> mapping = this.mixinMappings.get(name); - if (mapping == null) { - mapping = map.getClassMapping(name).orElse(null); - } - if (mapping == null) return; - String mapped = mapping.getFieldMapping(field.getName()).map(Mapping::getDeobfuscatedName).orElse(null); - if (mapped == null || mapped.equals(field.getName())) return; - replaceIdentifier(expr, mapped); - - if (expr instanceof PsiJavaCodeReferenceElement - && !((PsiJavaCodeReferenceElement) expr).isQualified() // qualified access is fine - && !isSwitchCase(expr) // referencing constants in case statements is fine - ) { - error(expr, "Implicit member reference to remapped field \"" + field.getName() + "\". " + - "This can cause issues if the remapped reference becomes shadowed by a local variable and is therefore forbidden. " + - "Use \"this." + field.getName() + "\" instead."); - } - } - - private void map(PsiElement expr, PsiMethod method) { - if (method.isConstructor()) return; - - PsiClass declaringClass = method.getContainingClass(); - if (declaringClass == null) return; - ArrayDeque<PsiClass> parentQueue = new ArrayDeque<>(); - parentQueue.offer(declaringClass); - ClassMapping<?, ?> mapping = null; - - String name = declaringClass.getQualifiedName(); - if (name != null) { - mapping = mixinMappings.get(name); - } - while (true) { - if (mapping != null) { - String mapped = mapping.getMethodMapping(getSignature(method)).map(Mapping::getDeobfuscatedName).orElse(null); - if (mapped != null) { - if (!mapped.equals(method.getName())) { - replaceIdentifier(expr, mapped); - } - return; - } - mapping = null; - } - while (mapping == null) { - declaringClass = parentQueue.poll(); - if (declaringClass == null) return; - - PsiClass superClass = declaringClass.getSuperClass(); - if (superClass != null) { - parentQueue.offer(superClass); - } - for (PsiClass anInterface : declaringClass.getInterfaces()) { - parentQueue.offer(anInterface); - } - - name = declaringClass.getQualifiedName(); - if (name == null) continue; - mapping = map.getClassMapping(name).orElse(null); - } - } - } - - private void map(PsiElement expr, PsiQualifiedNamedElement resolved) { - String name = resolved.getQualifiedName(); - if (name == null) return; - ClassMapping<?, ?> mapping = map.getClassMapping(name).orElse(null); - if (mapping == null) return; - String mapped = mapping.getDeobfuscatedName(); - if (mapped.equals(name)) return; - mapped = mapped.replace('/', '.'); - - if (expr.getText().equals(name)) { - replace(expr, mapped); - return; - } - replaceIdentifier(expr, mapped.substring(mapped.lastIndexOf('.') + 1)); - } - - private void map(PsiElement expr, PsiElement resolved) { - if (resolved instanceof PsiField) { - map(expr, (PsiField) resolved); - } else if (resolved instanceof PsiMethod) { - map(expr, (PsiMethod) resolved); - } else if (resolved instanceof PsiClass || resolved instanceof PsiPackage) { - map(expr, (PsiQualifiedNamedElement) resolved); - } - } - - // Note: Supports only Mixins with a single target (ignores others) and only ones specified via class literals - private PsiClass getMixinTarget(PsiAnnotation annotation) { - for (PsiNameValuePair pair : annotation.getParameterList().getAttributes()) { - String name = pair.getName(); - if (name != null && !"value".equals(name)) continue; - PsiAnnotationMemberValue value = pair.getValue(); - if (!(value instanceof PsiClassObjectAccessExpression)) continue; - PsiTypeElement type = ((PsiClassObjectAccessExpression) value).getOperand(); - PsiJavaCodeReferenceElement reference = type.getInnermostComponentReferenceElement(); - if (reference == null) continue; - return (PsiClass) reference.resolve(); - } - return null; - } - - private void remapAccessors(ClassMapping<?, ?> mapping) { - file.accept(new JavaRecursiveElementVisitor() { - @Override - public void visitMethod(PsiMethod method) { - PsiAnnotation annotation = method.getAnnotation(CLASS_ACCESSOR); - if (annotation == null) return; - - String targetByName = method.getName(); - if (targetByName.startsWith("is")) { - targetByName = targetByName.substring(2); - } else if (targetByName.startsWith("get") || targetByName.startsWith("set")) { - targetByName = targetByName.substring(3); - } else { - targetByName = null; - } - if (targetByName != null) { - targetByName = targetByName.substring(0, 1).toLowerCase() + targetByName.substring(1); - } - - String target = Arrays.stream(annotation.getParameterList().getAttributes()) - .filter(it -> it.getName() == null || it.getName().equals("value")) - .map(PsiNameValuePair::getLiteralValue) - .findAny() - .orElse(targetByName); - - if (target == null) { - throw new IllegalArgumentException("Cannot determine accessor target for " + method); - } - - String mapped = mapping.getFieldMapping(target).map(Mapping::getDeobfuscatedName).orElse(null); - if (mapped != null && !mapped.equals(target)) { - // Update accessor target - String parameterList; - if (mapped.equals(targetByName)) { - // Mapped name matches implied target, can just remove the explict target - parameterList = ""; - } else { - // Mapped name does not match implied target, need to set the target as annotation value - parameterList = "(\"" + StringUtil.escapeStringCharacters(mapped) + "\")"; - } - replace(annotation.getParameterList(), parameterList); - } - } - }); - } - - private void remapInjectsAndRedirects(ClassMapping<?, ?> mapping) { - file.accept(new JavaRecursiveElementVisitor() { - @Override - public void visitMethod(PsiMethod method) { - PsiAnnotation annotation = method.getAnnotation(CLASS_INJECT); - if (annotation == null) { - annotation = method.getAnnotation(CLASS_REDIRECT); - } - if (annotation == null) return; - - for (PsiNameValuePair attribute : annotation.getParameterList().getAttributes()) { - if (!"method".equals(attribute.getName())) continue; - // Note: mixin supports multiple targets, we do not (yet) - String literalValue = attribute.getLiteralValue(); - if (literalValue == null) continue; - String mapped; - if (literalValue.contains("(")) { - mapped = mapping.getMethodMapping(MethodSignature.of(literalValue)).map(Mapping::getDeobfuscatedName).orElse(null); - } else { - mapped = null; - for (MethodMapping methodMapping : mapping.getMethodMappings()) { - if (methodMapping.getObfuscatedName().equals(literalValue)) { - String name = methodMapping.getDeobfuscatedName(); - if (mapped != null && !mapped.equals(name)) { - error(attribute, "Ambiguous mixin method \"" + literalValue + "\" maps to \"" + mapped + "\" and \"" + name + "\""); - } - mapped = name; - } - } - } - if (mapped != null && !mapped.equals(literalValue)) { - PsiAnnotationMemberValue value = attribute.getValue(); - assert value != null; - replace(value, '"' + mapped + '"'); - } - } - } - }); - } - - private ClassMapping<?, ?> remapInternalType(String internalType, StringBuilder result) { - if (internalType.charAt(0) == 'L') { - String type = internalType.substring(1, internalType.length() - 1).replace('/', '.'); - ClassMapping<?, ?> mapping = map.getClassMapping(type).orElse(null); - if (mapping != null) { - result.append('L').append(mapping.getFullDeobfuscatedName()).append(';'); - return mapping; - } - } - result.append(internalType); - return null; - } - - private String remapFullyQualifiedMethodOrField(String signature) { - int ownerEnd = signature.indexOf(';'); - int argsBegin = signature.indexOf('('); - int argsEnd = signature.indexOf(')'); - boolean method = argsBegin != -1; - if (!method) { - argsBegin = argsEnd = signature.indexOf(':'); - } - String owner = signature.substring(0, ownerEnd + 1); - String name = signature.substring(ownerEnd + 1, argsBegin); - String returnType = signature.substring(argsEnd + 1); - - StringBuilder builder = new StringBuilder(signature.length() + 32); - ClassMapping<?, ?> mapping = remapInternalType(owner, builder); - String mapped = null; - if (mapping != null) { - mapped = (method - ? mapping.getMethodMapping(MethodSignature.of(signature.substring(ownerEnd + 1))) - : mapping.getFieldMapping(name)) - .map(Mapping::getDeobfuscatedName) - .orElse(null); - } - builder.append(mapped != null ? mapped : name); - if (method) { - builder.append('('); - String args = signature.substring(argsBegin + 1, argsEnd); - for (int i = 0; i < args.length(); i++) { - char c = args.charAt(i); - if (c != 'L') { - builder.append(c); - continue; - } - int end = args.indexOf(';', i); - String arg = args.substring(i, end + 1); - remapInternalType(arg, builder); - i = end; - } - builder.append(')'); - } else { - builder.append(':'); - } - remapInternalType(returnType, builder); - return builder.toString(); - } - - private void remapAtTargets() { - file.accept(new JavaRecursiveElementVisitor() { - @Override - public void visitAnnotation(PsiAnnotation annotation) { - if (!CLASS_AT.equals(annotation.getQualifiedName())) { - super.visitAnnotation(annotation); - return; - } - - for (PsiNameValuePair attribute : annotation.getParameterList().getAttributes()) { - if (!"target".equals(attribute.getName())) continue; - String signature = attribute.getLiteralValue(); - if (signature == null) continue; - String newSignature = remapFullyQualifiedMethodOrField(signature); - if (!newSignature.equals(signature)) { - PsiAnnotationMemberValue value = attribute.getValue(); - assert value != null; - replace(value, '"' + newSignature + '"'); - } - } - } - }); - } - - String remapFile() { - file.accept(new JavaRecursiveElementVisitor() { - @Override - public void visitClass(PsiClass psiClass) { - PsiAnnotation annotation = psiClass.getAnnotation(CLASS_MIXIN); - if (annotation == null) return; - - remapAtTargets(); - - PsiClass target = getMixinTarget(annotation); - if (target == null) return; - String qualifiedName = target.getQualifiedName(); - if (qualifiedName == null) return; - - ClassMapping<?, ?> mapping = map.getClassMapping(qualifiedName).orElse(null); - if (mapping == null) return; - - mixinMappings.put(psiClass.getQualifiedName(), mapping); - - if (!mapping.getFieldMappings().isEmpty()) { - remapAccessors(mapping); - } - if (!mapping.getMethodMappings().isEmpty()) { - remapInjectsAndRedirects(mapping); - } - } - }); - - file.accept(new JavaRecursiveElementVisitor() { - @Override - public void visitField(PsiField field) { - if (valid(field)) { - map(field, field); - } - super.visitField(field); - } - - @Override - public void visitMethod(PsiMethod method) { - if (valid(method)) { - map(method, method); - } - super.visitMethod(method); - } - - @Override - public void visitReferenceElement(PsiJavaCodeReferenceElement reference) { - if (valid(reference)) { - map(reference, reference.resolve()); - } - super.visitReferenceElement(reference); - } - }); - - return getResult(file.getText()); - } -} diff --git a/src/main/java/com/replaymod/gradle/remap/PsiUtils.java b/src/main/java/com/replaymod/gradle/remap/PsiUtils.java deleted file mode 100644 index ec4d1c8..0000000 --- a/src/main/java/com/replaymod/gradle/remap/PsiUtils.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.replaymod.gradle.remap; - -import com.intellij.psi.PsiArrayType; -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiClassType; -import com.intellij.psi.PsiMethod; -import com.intellij.psi.PsiPrimitiveType; -import com.intellij.psi.PsiType; -import com.intellij.psi.util.TypeConversionUtil; -import org.cadixdev.bombe.type.ArrayType; -import org.cadixdev.bombe.type.FieldType; -import org.cadixdev.bombe.type.MethodDescriptor; -import org.cadixdev.bombe.type.ObjectType; -import org.cadixdev.bombe.type.Type; -import org.cadixdev.bombe.type.VoidType; -import org.cadixdev.bombe.type.signature.MethodSignature; - -import java.util.Arrays; -import java.util.stream.Collectors; - -class PsiUtils { - static MethodSignature getSignature(PsiMethod method) { - return new MethodSignature(method.getName(), getDescriptor(method)); - } - - private static MethodDescriptor getDescriptor(PsiMethod method) { - return new MethodDescriptor( - Arrays.stream(method.getParameterList().getParameters()) - .map(it -> getFieldType(it.getType())) - .collect(Collectors.toList()), - getType(method.getReturnType()) - ); - } - - private static FieldType getFieldType(PsiType type) { - type = TypeConversionUtil.erasure(type); - if (type instanceof PsiPrimitiveType) { - return FieldType.of(((PsiPrimitiveType) type).getKind().getBinaryName()); - } else if (type instanceof PsiArrayType) { - PsiArrayType array = (PsiArrayType) type; - return new ArrayType(array.getArrayDimensions(), getFieldType(array.getDeepComponentType())); - } else if (type instanceof PsiClassType) { - PsiClass resolved = ((PsiClassType) type).resolve(); - if (resolved == null) throw new NullPointerException("Failed to resolve type " + type); - String qualifiedName = resolved.getQualifiedName(); - if (qualifiedName == null) throw new NullPointerException("Type " + type + " has no qualified name."); - return new ObjectType(qualifiedName); - } else { - throw new IllegalArgumentException("Cannot translate type " + type); - } - } - - private static Type getType(PsiType type) { - if (TypeConversionUtil.isVoidType(type)) { - return VoidType.INSTANCE; - } else { - return getFieldType(type); - } - } - -} diff --git a/src/main/java/com/replaymod/gradle/remap/Transformer.java b/src/main/java/com/replaymod/gradle/remap/Transformer.java deleted file mode 100644 index 5b25856..0000000 --- a/src/main/java/com/replaymod/gradle/remap/Transformer.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.replaymod.gradle.remap; - -import com.intellij.codeInsight.CustomExceptionHandler; -import com.intellij.mock.MockProject; -import com.intellij.openapi.extensions.ExtensionPoint; -import com.intellij.openapi.extensions.Extensions; -import com.intellij.openapi.extensions.ExtensionsArea; -import com.intellij.openapi.util.Disposer; -import com.intellij.openapi.vfs.StandardFileSystems; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.VirtualFileManager; -import com.intellij.openapi.vfs.local.CoreLocalFileSystem; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiManager; -import com.replaymod.gradle.remap.legacy.LegacyMapping; -import org.cadixdev.lorenz.MappingSet; -import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys; -import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot; -import org.jetbrains.kotlin.cli.common.messages.MessageRenderer; -import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector; -import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles; -import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment; -import org.jetbrains.kotlin.cli.jvm.compiler.NoScopeRecordCliBindingTrace; -import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM; -import org.jetbrains.kotlin.cli.jvm.config.JavaSourceRoot; -import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot; -import org.jetbrains.kotlin.config.CommonConfigurationKeys; -import org.jetbrains.kotlin.config.CompilerConfiguration; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardOpenOption; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -public class Transformer { - private MappingSet map; - private String[] classpath; - private boolean fail; - - public static void main(String[] args) throws IOException { - MappingSet mappings; - if (args[0].isEmpty()) { - mappings = MappingSet.create(); - } else { - mappings = LegacyMapping.readMappingSet(new File(args[0]).toPath(), args[1].equals("true")); - } - Transformer transformer = new Transformer(mappings); - - BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); - - String[] classpath = new String[Integer.parseInt(args[2])]; - for (int i = 0; i < classpath.length; i++) { - classpath[i] = reader.readLine(); - } - transformer.setClasspath(classpath); - - Map<String, String> sources = new HashMap<>(); - while (true) { - String name = reader.readLine(); - if (name == null || name.isEmpty()) { - break; - } - - String[] lines = new String[Integer.parseInt(reader.readLine())]; - for (int i = 0; i < lines.length; i++) { - lines[i] = reader.readLine(); - } - String source = String.join("\n", lines); - - sources.put(name, source); - } - - Map<String, String> results = transformer.remap(sources); - - for (String name : sources.keySet()) { - System.out.println(name); - String[] lines = results.get(name).split("\n"); - System.out.println(lines.length); - for (String line : lines) { - System.out.println(line); - } - } - - if (transformer.fail) { - System.exit(1); - } - } - - public Transformer(MappingSet mappings) { - this.map = mappings; - } - - public String[] getClasspath() { - return classpath; - } - - public void setClasspath(String[] classpath) { - this.classpath = classpath; - } - - public Map<String, String> remap(Map<String, String> sources) throws IOException { - Path tmpDir = Files.createTempDirectory("remap"); - try { - for (Entry<String, String> entry : sources.entrySet()) { - String unitName = entry.getKey(); - String source = entry.getValue(); - - Path path = tmpDir.resolve(unitName); - Files.createDirectories(path.getParent()); - Files.write(path, source.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE); - } - - CompilerConfiguration config = new CompilerConfiguration(); - config.put(CommonConfigurationKeys.MODULE_NAME, "main"); - config.add(CLIConfigurationKeys.CONTENT_ROOTS, new JavaSourceRoot(tmpDir.toFile(), "")); - config.add(CLIConfigurationKeys.CONTENT_ROOTS, new KotlinSourceRoot(tmpDir.toAbsolutePath().toString(), false)); - config.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, new PrintingMessageCollector(System.err, MessageRenderer.GRADLE_STYLE, true)); - - KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForProduction( - Disposer.newDisposable(), - config, - EnvironmentConfigFiles.JVM_CONFIG_FILES - ); - ExtensionsArea rootArea = Extensions.getRootArea(); - if (!rootArea.hasExtensionPoint(CustomExceptionHandler.KEY)) { - rootArea.registerExtensionPoint(CustomExceptionHandler.KEY.getName(), CustomExceptionHandler.class.getName(), ExtensionPoint.Kind.INTERFACE); - } - - MockProject project = (MockProject) environment.getProject(); - - environment.updateClasspath(Stream.of(getClasspath()).map(it -> new JvmClasspathRoot(new File(it))).collect(Collectors.toList())); - - TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration( - project, - Collections.emptyList(), - new NoScopeRecordCliBindingTrace(), - environment.getConfiguration(), - environment::createPackagePartProvider - ); - - CoreLocalFileSystem vfs = (CoreLocalFileSystem) VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL); - Map<String, String> results = new HashMap<>(); - for (String name : sources.keySet()) { - VirtualFile file = vfs.findFileByIoFile(tmpDir.resolve(name).toFile()); - assert file != null; - PsiFile psiFile = PsiManager.getInstance(project).findFile(file); - assert psiFile != null; - - String mapped = new PsiMapper(map, psiFile).remapFile(); - if (mapped == null) { - fail = true; - continue; - } - results.put(name, mapped); - } - return results; - } finally { - Files.walk(tmpDir).map(Path::toFile).sorted(Comparator.reverseOrder()).forEach(File::delete); - } - } - -} diff --git a/src/main/java/com/replaymod/gradle/remap/legacy/LegacyMapping.java b/src/main/java/com/replaymod/gradle/remap/legacy/LegacyMapping.java deleted file mode 100644 index 05a85d9..0000000 --- a/src/main/java/com/replaymod/gradle/remap/legacy/LegacyMapping.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.replaymod.gradle.remap.legacy; - -import org.cadixdev.lorenz.MappingSet; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -public class LegacyMapping { - public static MappingSet readMappingSet(Path mappingFile, boolean invert) throws IOException { - return new LegacyMappingsReader(readMappings(mappingFile, invert)).read(); - } - - public static Map<String, LegacyMapping> readMappings(Path mappingFile, boolean invert) throws IOException { - Map<String, LegacyMapping> mappings = new HashMap<>(); - Map<String, LegacyMapping> revMappings = new HashMap<>(); - int lineNumber = 0; - for (String line : Files.readAllLines(mappingFile, StandardCharsets.UTF_8)) { - lineNumber++; - if (line.trim().startsWith("#") || line.trim().isEmpty()) continue; - - String[] parts = line.split(" "); - if (parts.length < 2 || line.contains(";")) { - throw new IllegalArgumentException("Failed to parse line " + lineNumber + " in " + mappingFile + "."); - } - - LegacyMapping mapping = mappings.get(parts[0]); - if (mapping == null) { - mapping = new LegacyMapping(); - mapping.oldName = mapping.newName = parts[0]; - mappings.put(mapping.oldName, mapping); - } - - if (parts.length == 2) { - // Class mapping - mapping.newName = parts[1]; - // Possibly merge with reverse mapping - LegacyMapping revMapping = revMappings.remove(mapping.newName); - if (revMapping != null) { - mapping.fields.putAll(revMapping.fields); - mapping.methods.putAll(revMapping.methods); - } - revMappings.put(mapping.newName, mapping); - } else if (parts.length == 3 || parts.length == 4) { - String fromName = parts[1]; - String toName; - LegacyMapping revMapping; - if (parts.length == 4) { - toName = parts[3]; - revMapping = revMappings.get(parts[2]); - if (revMapping == null) { - revMapping = new LegacyMapping(); - revMapping.oldName = revMapping.newName = parts[2]; - revMappings.put(revMapping.newName, revMapping); - } - } else { - toName = parts[2]; - revMapping = mapping; - } - if (fromName.endsWith("()")) { - // Method mapping - fromName = fromName.substring(0, fromName.length() - 2); - toName = toName.substring(0, toName.length() - 2); - mapping.methods.put(fromName, toName); - revMapping.methods.put(fromName, toName); - } else { - // Field mapping - mapping.fields.put(fromName, toName); - revMapping.fields.put(fromName, toName); - } - } else { - throw new IllegalArgumentException("Failed to parse line " + lineNumber + " in " + mappingFile + "."); - } - } - if (invert) { - Stream.concat( - mappings.values().stream(), - revMappings.values().stream() - ).distinct().forEach(it -> { - String oldName = it.oldName; - it.oldName = it.newName; - it.newName = oldName; - it.fields = it.fields.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); - it.methods = it.methods.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); - }); - } - return Stream.concat( - mappings.values().stream(), - revMappings.values().stream() - ).collect(Collectors.toMap(mapping -> mapping.oldName, Function.identity(), (mapping, other) -> { - if (!other.oldName.equals(other.newName)) { - if (!mapping.oldName.equals(mapping.newName) - && !other.oldName.equals(mapping.oldName) - && !other.newName.equals(mapping.newName)) { - throw new IllegalArgumentException("Conflicting mappings: " - + mapping.oldName + " -> " + mapping.newName - + " and " + other.oldName + " -> " + other.newName); - } - mapping.oldName = other.oldName; - mapping.newName = other.newName; - } - mapping.fields.putAll(other.fields); - mapping.methods.putAll(other.methods); - return mapping; - })); - } - - public String oldName; - public String newName; - public Map<String, String> fields = new HashMap<>(); - public Map<String, String> methods = new HashMap<>(); -} diff --git a/src/main/java/com/replaymod/gradle/remap/legacy/LegacyMappingSetModelFactory.java b/src/main/java/com/replaymod/gradle/remap/legacy/LegacyMappingSetModelFactory.java deleted file mode 100644 index eb72788..0000000 --- a/src/main/java/com/replaymod/gradle/remap/legacy/LegacyMappingSetModelFactory.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.replaymod.gradle.remap.legacy; - -import org.cadixdev.bombe.type.signature.MethodSignature; -import org.cadixdev.lorenz.MappingSet; -import org.cadixdev.lorenz.impl.MappingSetModelFactoryImpl; -import org.cadixdev.lorenz.impl.model.TopLevelClassMappingImpl; -import org.cadixdev.lorenz.model.MethodMapping; -import org.cadixdev.lorenz.model.TopLevelClassMapping; - -import java.util.Optional; - -public class LegacyMappingSetModelFactory extends MappingSetModelFactoryImpl { - @Override - public TopLevelClassMapping createTopLevelClassMapping(MappingSet parent, String obfuscatedName, String deobfuscatedName) { - return new TopLevelClassMappingImpl(parent, obfuscatedName, deobfuscatedName) { - private MethodSignature stripDesc(MethodSignature signature) { - // actual descriptor isn't included in legacy format - return MethodSignature.of(signature.getName(), "()V"); - } - - @Override - public boolean hasMethodMapping(MethodSignature signature) { - return super.hasMethodMapping(signature) || super.hasMethodMapping(stripDesc(signature)); - } - - @Override - public Optional<MethodMapping> getMethodMapping(MethodSignature signature) { - Optional<MethodMapping> maybeMapping = super.getMethodMapping(signature); - if (!maybeMapping.isPresent() || !maybeMapping.get().hasMappings()) { - maybeMapping = super.getMethodMapping(stripDesc(signature)); - } - return maybeMapping; - } - }; - } -} diff --git a/src/main/java/com/replaymod/gradle/remap/legacy/LegacyMappingsReader.java b/src/main/java/com/replaymod/gradle/remap/legacy/LegacyMappingsReader.java deleted file mode 100644 index 502f7ba..0000000 --- a/src/main/java/com/replaymod/gradle/remap/legacy/LegacyMappingsReader.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.replaymod.gradle.remap.legacy; - -import org.cadixdev.lorenz.MappingSet; -import org.cadixdev.lorenz.io.MappingsReader; -import org.cadixdev.lorenz.model.ClassMapping; - -import java.util.Map; - -public class LegacyMappingsReader extends MappingsReader { - private final Map<String, LegacyMapping> map; - - public LegacyMappingsReader(Map<String, LegacyMapping> map) { - this.map = map; - } - - @Override - public MappingSet read() { - return read(MappingSet.create(new LegacyMappingSetModelFactory())); - } - - @Override - public MappingSet read(MappingSet mappings) { - if (!(mappings.getModelFactory() instanceof LegacyMappingSetModelFactory)) { - throw new IllegalArgumentException("legacy mappings must use legacy model factory, use read() instead"); - } - for (LegacyMapping legacyMapping : map.values()) { - ClassMapping classMapping = mappings.getOrCreateClassMapping(legacyMapping.oldName) - .setDeobfuscatedName(legacyMapping.newName); - for (Map.Entry<String, String> entry : legacyMapping.fields.entrySet()) { - classMapping.getOrCreateFieldMapping(entry.getKey()) - .setDeobfuscatedName(entry.getValue()); - } - for (Map.Entry<String, String> entry : legacyMapping.methods.entrySet()) { - classMapping.getOrCreateMethodMapping(entry.getKey(), "()V") - .setDeobfuscatedName(entry.getValue()); - } - } - return mappings; - } - - @Override - public void close() { - } -} |