2018-12-18 22:36:22 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016, 2017, 2018 FabricMC
|
|
|
|
*
|
|
|
|
* 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 net.fabricmc.loom.util;
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
2018-12-18 22:36:22 +00:00
|
|
|
import org.cadixdev.lorenz.MappingSet;
|
2018-12-22 13:37:16 +00:00
|
|
|
import org.cadixdev.lorenz.io.MappingsReader;
|
2018-12-18 22:36:22 +00:00
|
|
|
import org.cadixdev.mercury.Mercury;
|
|
|
|
import org.cadixdev.mercury.remapper.MercuryRemapper;
|
2018-12-22 13:37:16 +00:00
|
|
|
import org.zeroturnaround.zip.ZipUtil;
|
2019-11-02 20:23:27 +00:00
|
|
|
import org.gradle.api.Project;
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
|
|
|
import net.fabricmc.loom.providers.MappingsProvider;
|
|
|
|
import net.fabricmc.mappings.ClassEntry;
|
|
|
|
import net.fabricmc.mappings.EntryTriple;
|
|
|
|
import net.fabricmc.mappings.FieldEntry;
|
|
|
|
import net.fabricmc.mappings.Mappings;
|
|
|
|
import net.fabricmc.mappings.MethodEntry;
|
|
|
|
import net.fabricmc.stitch.util.StitchUtil;
|
2018-12-18 22:36:22 +00:00
|
|
|
|
|
|
|
public class SourceRemapper {
|
2018-12-22 13:37:16 +00:00
|
|
|
public static void remapSources(Project project, File source, File destination, boolean toNamed) throws Exception {
|
2018-12-28 21:08:36 +00:00
|
|
|
remapSourcesInner(project, source, destination, toNamed);
|
|
|
|
// TODO: FIXME - WORKAROUND https://github.com/FabricMC/fabric-loom/issues/45
|
|
|
|
System.gc();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void remapSourcesInner(Project project, File source, File destination, boolean toNamed) throws Exception {
|
2018-12-18 22:36:22 +00:00
|
|
|
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
|
|
|
MappingsProvider mappingsProvider = extension.getMappingsProvider();
|
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
MappingSet mappings = extension.getOrCreateSrcMappingCache(toNamed ? 1 : 0, () -> {
|
2019-04-24 17:32:35 +00:00
|
|
|
try {
|
|
|
|
Mappings m = mappingsProvider.getMappings();
|
2018-12-22 13:37:16 +00:00
|
|
|
project.getLogger().lifecycle(":loading " + (toNamed ? "intermediary -> named" : "named -> intermediary") + " source mappings");
|
2019-04-24 17:32:35 +00:00
|
|
|
return new TinyReader(m, toNamed ? "intermediary" : "named", toNamed ? "named" : "intermediary").read();
|
2018-12-22 13:37:16 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
});
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2019-11-07 12:56:43 +00:00
|
|
|
project.getLogger().info(":remapping source jar");
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2019-05-28 07:23:19 +00:00
|
|
|
Mercury mercury = extension.getOrCreateSrcMercuryCache(toNamed ? 1 : 0, () -> {
|
|
|
|
Mercury m = new Mercury();
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2019-05-28 07:23:19 +00:00
|
|
|
for (File file : project.getConfigurations().getByName(Constants.MINECRAFT_DEPENDENCIES).getFiles()) {
|
|
|
|
m.getClassPath().add(file.toPath());
|
2018-12-30 23:42:49 +00:00
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-05-28 07:23:19 +00:00
|
|
|
if (!toNamed) {
|
|
|
|
for (File file : project.getConfigurations().getByName("compileClasspath").getFiles()) {
|
|
|
|
m.getClassPath().add(file.toPath());
|
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-05-28 07:23:19 +00:00
|
|
|
for (Path file : extension.getUnmappedMods()) {
|
|
|
|
if (Files.isRegularFile(file)) {
|
|
|
|
m.getClassPath().add(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.getClassPath().add(extension.getMinecraftMappedProvider().MINECRAFT_MAPPED_JAR.toPath());
|
|
|
|
m.getClassPath().add(extension.getMinecraftMappedProvider().MINECRAFT_INTERMEDIARY_JAR.toPath());
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2019-05-28 07:23:19 +00:00
|
|
|
m.getProcessors().add(MercuryRemapper.create(mappings));
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2019-05-28 07:23:19 +00:00
|
|
|
return m;
|
|
|
|
});
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
if (source.equals(destination)) {
|
|
|
|
if (source.isDirectory()) {
|
|
|
|
throw new RuntimeException("Directories must differ!");
|
|
|
|
}
|
|
|
|
|
|
|
|
source = new File(destination.getAbsolutePath().substring(0, destination.getAbsolutePath().lastIndexOf('.')) + "-dev.jar");
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-12-23 18:18:40 +00:00
|
|
|
try {
|
|
|
|
com.google.common.io.Files.move(destination, source);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Could not rename " + destination.getName() + "!", e);
|
2018-12-22 13:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Path srcPath = source.toPath();
|
|
|
|
boolean isSrcTmp = false;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
if (!source.isDirectory()) {
|
|
|
|
// create tmp directory
|
|
|
|
isSrcTmp = true;
|
|
|
|
srcPath = Files.createTempDirectory("fabric-loom-src");
|
|
|
|
ZipUtil.unpack(source, srcPath.toFile());
|
|
|
|
}
|
|
|
|
|
2018-12-22 14:29:46 +00:00
|
|
|
if (!destination.isDirectory() && destination.exists()) {
|
|
|
|
if (!destination.delete()) {
|
|
|
|
throw new RuntimeException("Could not delete " + destination.getName() + "!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
StitchUtil.FileSystemDelegate dstFs = destination.isDirectory() ? null : StitchUtil.getJarFileSystem(destination, true);
|
|
|
|
Path dstPath = dstFs != null ? dstFs.get().getPath("/") : destination.toPath();
|
|
|
|
|
2018-12-22 14:29:46 +00:00
|
|
|
try {
|
|
|
|
mercury.rewrite(srcPath, dstPath);
|
|
|
|
} catch (Exception e) {
|
|
|
|
project.getLogger().warn("Could not remap " + source.getName() + " fully!", e);
|
|
|
|
}
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2019-08-23 10:59:16 +00:00
|
|
|
copyNonJavaFiles(srcPath, dstPath, project, source);
|
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
if (dstFs != null) {
|
|
|
|
dstFs.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSrcTmp) {
|
2018-12-22 14:35:36 +00:00
|
|
|
Files.walkFileTree(srcPath, new DeletingFileVisitor());
|
2018-12-22 13:37:16 +00:00
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
}
|
2019-08-23 10:59:16 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
private static void copyNonJavaFiles(Path from, Path to, Project project, File source) throws IOException {
|
|
|
|
Files.walk(from).forEach(path -> {
|
|
|
|
Path targetPath = to.resolve(from.relativize(path).toString());
|
|
|
|
|
|
|
|
if (!isJavaFile(path) && !Files.exists(targetPath)) {
|
|
|
|
try {
|
|
|
|
Files.copy(path, targetPath);
|
|
|
|
} catch (IOException e) {
|
|
|
|
project.getLogger().warn("Could not copy non-java sources '" + source.getName() + "' fully!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-12-18 22:36:22 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
private static boolean isJavaFile(Path path) {
|
|
|
|
String name = path.getFileName().toString();
|
|
|
|
// ".java" is not a valid java file
|
|
|
|
return name.endsWith(".java") && name.length() != 5;
|
|
|
|
}
|
2019-08-23 10:59:16 +00:00
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
public static class TinyReader extends MappingsReader {
|
2019-04-24 17:32:35 +00:00
|
|
|
private final Mappings m;
|
2019-04-21 09:29:19 +00:00
|
|
|
private final String from, to;
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2019-04-24 17:32:35 +00:00
|
|
|
public TinyReader(Mappings m, String from, String to) {
|
|
|
|
this.m = m;
|
2018-12-22 13:37:16 +00:00
|
|
|
this.from = from;
|
|
|
|
this.to = to;
|
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
@Override
|
2019-04-24 17:32:35 +00:00
|
|
|
public MappingSet read(final MappingSet mappings) {
|
2019-04-21 09:29:19 +00:00
|
|
|
for (ClassEntry entry : m.getClassEntries()) {
|
2019-11-02 20:23:27 +00:00
|
|
|
mappings.getOrCreateClassMapping(entry.get(from)).setDeobfuscatedName(entry.get(to));
|
2019-04-21 09:29:19 +00:00
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2019-04-21 09:29:19 +00:00
|
|
|
for (FieldEntry entry : m.getFieldEntries()) {
|
|
|
|
EntryTriple fromEntry = entry.get(from);
|
|
|
|
EntryTriple toEntry = entry.get(to);
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
mappings.getOrCreateClassMapping(fromEntry.getOwner()).getOrCreateFieldMapping(fromEntry.getName(), fromEntry.getDesc()).setDeobfuscatedName(toEntry.getName());
|
2019-04-21 09:29:19 +00:00
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2019-04-21 09:29:19 +00:00
|
|
|
for (MethodEntry entry : m.getMethodEntries()) {
|
|
|
|
EntryTriple fromEntry = entry.get(from);
|
|
|
|
EntryTriple toEntry = entry.get(to);
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
mappings.getOrCreateClassMapping(fromEntry.getOwner()).getOrCreateMethodMapping(fromEntry.getName(), fromEntry.getDesc()).setDeobfuscatedName(toEntry.getName());
|
2018-12-18 22:36:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
return mappings;
|
2018-12-18 22:36:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
@Override
|
2019-11-02 20:23:27 +00:00
|
|
|
public void close() throws IOException { }
|
2018-12-22 13:37:16 +00:00
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
}
|