Merge remote-tracking branch 'upstream/dev/0.2.6' into dev/0.2.6

This commit is contained in:
modmuss50 2019-11-18 22:14:55 +00:00
commit 414f41ab50
13 changed files with 134 additions and 164 deletions

View file

@ -48,7 +48,6 @@ import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.DependencyProvider;
import net.fabricmc.loom.util.DownloadUtil;
import net.fabricmc.loom.util.Version;
import net.fabricmc.mapping.reader.v2.TinyV2Factory;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.stitch.Command;
@ -93,9 +92,8 @@ public class MappingsProvider extends DependencyProvider {
boolean isV2 = doesJarContainV2Mappings(mappingsJar.toPath());
Version mappingsVersion = new Version(version);
this.minecraftVersion = mappingsVersion.getMinecraftVersion();
this.mappingsVersion = mappingsVersion.getMappingsVersion() + (isV2 ? "-v2" : "");
this.minecraftVersion = minecraftProvider.minecraftVersion;
this.mappingsVersion = version + (isV2 ? "-v2" : "");
initFiles(project);

View file

@ -26,8 +26,14 @@ package net.fabricmc.loom.task;
import org.gradle.api.DefaultTask;
import net.fabricmc.loom.LoomGradleExtension;
public abstract class AbstractLoomTask extends DefaultTask {
public AbstractLoomTask() {
setGroup("fabric");
}
protected LoomGradleExtension getExtension() {
return getProject().getExtensions().getByType(LoomGradleExtension.class);
}
}

View file

@ -27,7 +27,6 @@ package net.fabricmc.loom.task;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;
import net.fabricmc.loom.LoomGradleExtension;
@ -35,8 +34,7 @@ import net.fabricmc.loom.LoomGradleExtension;
public class CleanLoomBinaries extends AbstractLoomTask {
@TaskAction
public void run() {
Project project = this.getProject();
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
LoomGradleExtension extension = getExtension();
extension.getMinecraftProvider().getMergedJar().delete();
extension.getMinecraftMappedProvider().getIntermediaryJar().delete();
extension.getMinecraftMappedProvider().getMappedJar().delete();

View file

@ -27,7 +27,6 @@ package net.fabricmc.loom.task;
import java.io.IOException;
import java.nio.file.Files;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;
import net.fabricmc.loom.LoomGradleExtension;
@ -37,8 +36,7 @@ public class CleanLoomMappings extends AbstractLoomTask {
@TaskAction
public void run() {
try {
Project project = this.getProject();
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
LoomGradleExtension extension = getExtension();
extension.getMappingsProvider().clean();
extension.getMinecraftMappedProvider().getIntermediaryJar().delete();
extension.getMinecraftMappedProvider().getMappedJar().delete();

View file

@ -37,7 +37,7 @@ public class DownloadAssetsTask extends AbstractLoomTask {
@TaskAction
public void downloadAssets() throws IOException {
Project project = this.getProject();
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
LoomGradleExtension extension = getExtension();
MinecraftAssetsProvider.provide(extension.getMinecraftProvider(), project);
MinecraftNativesProvider.provide(extension.getMinecraftProvider(), project);

View file

@ -31,7 +31,6 @@ import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.gradle.api.tasks.TaskAction;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.util.RunConfig;
public class GenEclipseRunsTask extends AbstractLoomTask {
@ -51,7 +50,7 @@ public class GenEclipseRunsTask extends AbstractLoomTask {
FileUtils.writeStringToFile(serverRunConfigs, serverRunConfig, StandardCharsets.UTF_8);
}
File runDir = new File(getProject().getRootDir(), this.getProject().getExtensions().getByType(LoomGradleExtension.class).runDir);
File runDir = new File(getProject().getRootDir(), getExtension().runDir);
if (!runDir.exists()) {
runDir.mkdirs();

View file

@ -58,7 +58,7 @@ public class GenIdeaProjectTask extends AbstractLoomTask {
return;
}
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
LoomGradleExtension extension = getExtension();
project.getLogger().lifecycle(":Building idea workspace");
File file = project.file(project.getName() + ".iws");

View file

@ -33,6 +33,7 @@ import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.FileUtils;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;
import net.fabricmc.loom.LoomGradleExtension;
@ -45,8 +46,9 @@ import net.fabricmc.loom.util.RunConfig;
public class GenVsCodeProjectTask extends AbstractLoomTask {
@TaskAction
public void genRuns() {
LoomGradleExtension extension = getProject().getExtensions().getByType(LoomGradleExtension.class);
File projectDir = getProject().file(".vscode");
Project project = getProject();
LoomGradleExtension extension = getExtension();
File projectDir = project.file(".vscode");
if (!projectDir.exists()) {
projectDir.mkdir();
@ -59,8 +61,8 @@ public class GenVsCodeProjectTask extends AbstractLoomTask {
}
VsCodeLaunch launch = new VsCodeLaunch();
launch.add(RunConfig.clientRunConfig(getProject()));
launch.add(RunConfig.serverRunConfig(getProject()));
launch.add(RunConfig.clientRunConfig(project));
launch.add(RunConfig.serverRunConfig(project));
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(launch);
@ -71,7 +73,7 @@ public class GenVsCodeProjectTask extends AbstractLoomTask {
throw new RuntimeException("Failed to write launch.json", e);
}
File runDir = new File(getProject().getRootDir(), extension.runDir);
File runDir = new File(project.getRootDir(), extension.runDir);
if (!runDir.exists()) {
runDir.mkdirs();

View file

@ -27,27 +27,30 @@ package net.fabricmc.loom.task;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import com.google.common.net.UrlEscapers;
import org.apache.commons.io.FileUtils;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.lorenz.io.MappingsReader;
import org.cadixdev.lorenz.model.ClassMapping;
import org.cadixdev.lorenz.model.Mapping;
import org.cadixdev.mercury.Mercury;
import org.cadixdev.mercury.remapper.MercuryRemapper;
import org.gradle.api.GradleException;
import org.gradle.api.IllegalDependencyNotation;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.providers.MappingsProvider;
@ -61,88 +64,99 @@ import net.fabricmc.mapping.tree.TinyMappingFactory;
import net.fabricmc.mapping.tree.TinyTree;
public class MigrateMappingsTask extends AbstractLoomTask {
private Path inputDir;
private Path outputDir;
private String mappings;
public MigrateMappingsTask() {
inputDir = getProject().file("src/main/java").toPath();
outputDir = getProject().file("remappedSrc").toPath();
}
@Option(option = "input", description = "Java source file directory")
public void setInputDir(String inputDir) {
this.inputDir = getProject().file(inputDir).toPath();
}
@Option(option = "output", description = "Remapped source output directory")
public void setOutputDir(String outputDir) {
this.outputDir = getProject().file(outputDir).toPath();
}
@Option(option = "mappings", description = "Target mappings")
public void setMappings(String mappings) {
this.mappings = mappings;
}
@TaskAction
public void doTask() throws Throwable {
Project project = getProject();
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
Map<String, ?> properties = project.getProperties();
LoomGradleExtension extension = getExtension();
project.getLogger().lifecycle(":loading mappings");
String inputDir = (String) properties.get("inputDir");
if (inputDir == null) inputDir = "src/main/java";
String outputDir = (String) properties.get("outputDir");
if (outputDir == null) outputDir = "remappedSrc";
String officialMappingsVersion = (String) properties.get("targetMappings");
String localMappingsPath = (String) properties.get("targetLocalMappings");
if (officialMappingsVersion != null && localMappingsPath != null) {
throw new IllegalArgumentException("targetMappings and targetLocalMappings are mutually exclusive;"
+ " you either specify an official yarn mappings version with targetMappings, "
+ "or a local one with targetLocalMappings.");
if (!Files.exists(inputDir) || !Files.isDirectory(inputDir)) {
throw new IllegalArgumentException("Could not find input directory: " + inputDir.toAbsolutePath());
}
if (officialMappingsVersion == null && localMappingsPath == null) {
throw new IllegalArgumentException("You must specify a new mappings version with -PtargetMappings (or local mappings with -PtargetLocalMappings).");
}
boolean useLocalMappings = localMappingsPath != null;
if (useLocalMappings && !Files.exists(Paths.get(localMappingsPath))) {
throw new IllegalArgumentException("Can't find local mappings in specified location: " + localMappingsPath);
}
String targetMappingsName = useLocalMappings ? localMappingsPath : officialMappingsVersion;
Path inputDirPath = Paths.get(System.getProperty("user.dir"), inputDir);
Path outputDirPath = Paths.get(System.getProperty("user.dir"), outputDir);
if (!Files.exists(inputDirPath) || !Files.isDirectory(inputDirPath)) {
throw new IllegalArgumentException("Could not find input directory: " + inputDirPath.toAbsolutePath());
}
Files.createDirectories(outputDirPath);
Files.createDirectories(outputDir);
File mappings = loadMappings();
MappingsProvider mappingsProvider = extension.getMappingsProvider();
try {
TinyTree currentMappings = mappingsProvider.getMappings();
TinyTree targetMappings = getMappings(project, targetMappingsName, useLocalMappings);
migrateMappings(project, extension.getMinecraftMappedProvider(), inputDirPath, outputDirPath, currentMappings, targetMappings, extension);
project.getLogger().lifecycle(":remapped project written to " + outputDirPath.toAbsolutePath());
TinyTree targetMappings = getMappings(mappings);
migrateMappings(project, extension.getMinecraftMappedProvider(), inputDir, outputDir, currentMappings, targetMappings);
project.getLogger().lifecycle(":remapped project written to " + outputDir.toAbsolutePath());
} catch (IOException e) {
throw new IllegalArgumentException("Could not find mappings for version " + officialMappingsVersion, e);
throw new IllegalArgumentException("Error while loading mappings", e);
}
}
private TinyTree getMappings(Project project, String mappingsVersionOrPath, boolean useLocalMappings) throws IOException {
Path migrateMappingsDir = Files.createTempDirectory("migrate");
Path localMappingsOfVersion = migrateMappingsDir.resolve(mappingsVersionOrPath + ".tiny");
private File loadMappings() {
Project project = getProject();
if (!Files.exists(localMappingsOfVersion)) {
if (useLocalMappings) {
Files.copy(Paths.get(mappingsVersionOrPath), localMappingsOfVersion);
} else {
String versionRelativePath = UrlEscapers.urlFragmentEscaper().escape(mappingsVersionOrPath);
String artifactUrl = "https://maven.fabricmc.net/net/fabricmc/yarn/" + versionRelativePath + "/yarn-" + versionRelativePath + ".jar";
File mappingsJar = File.createTempFile("migrateMappingsJar", ".jar");
project.getLogger().lifecycle(":downloading new mappings from " + artifactUrl);
FileUtils.copyURLToFile(new URL(artifactUrl), mappingsJar);
if (mappings == null || mappings.isEmpty()) {
throw new IllegalArgumentException("No mappings were specified. Use --mappings=\"\" to specify target mappings");
}
try (FileSystem jar = FileSystems.newFileSystem(mappingsJar.toPath(), null)) {
if (!Files.exists(migrateMappingsDir)) Files.createDirectory(migrateMappingsDir);
MappingsProvider.extractMappings(jar, localMappingsOfVersion);
}
Set<File> files;
try {
files = project.getConfigurations().detachedConfiguration(project.getDependencies().create(mappings)).resolve();
} catch (IllegalDependencyNotation ignored) {
project.getLogger().info("Could not locate mappings, presuming V2 Yarn");
try {
files = project.getConfigurations().detachedConfiguration(project.getDependencies().module(ImmutableMap.of("group", "net.fabricmc", "name", "yarn", "version", mappings, "classifier", "v2"))).resolve();
} catch (GradleException ignored2) {
project.getLogger().info("Could not locate mappings, presuming V1 Yarn");
files = project.getConfigurations().detachedConfiguration(project.getDependencies().module(ImmutableMap.of("group", "net.fabricmc", "name", "yarn", "version", mappings))).resolve();
}
}
try (BufferedReader reader = Files.newBufferedReader(localMappingsOfVersion)) {
if (files.isEmpty()) {
throw new IllegalArgumentException("Mappings could not be found");
}
return Iterables.getOnlyElement(files);
}
private static TinyTree getMappings(File mappings) throws IOException {
Path temp = Files.createTempFile("mappings", ".tiny");
try (FileSystem fileSystem = FileSystems.newFileSystem(mappings.toPath(), null)) {
Files.copy(fileSystem.getPath("mappings/mappings.tiny"), temp, StandardCopyOption.REPLACE_EXISTING);
}
try (BufferedReader reader = Files.newBufferedReader(temp)) {
return TinyMappingFactory.loadWithDetection(reader);
}
}
private static void migrateMappings(Project project, MinecraftMappedProvider minecraftMappedProvider,
Path inputDir, Path outputDir, TinyTree currentMappings, TinyTree targetMappings, LoomGradleExtension extension
Path inputDir, Path outputDir, TinyTree currentMappings, TinyTree targetMappings
) throws IOException {
project.getLogger().lifecycle(":joining mappings");
MappingSet mappingSet = new MappingsJoiner(currentMappings, targetMappings,
@ -166,7 +180,7 @@ public class MigrateMappingsTask extends AbstractLoomTask {
System.gc();
}
public static class MappingsJoiner extends MappingsReader {
private static class MappingsJoiner extends MappingsReader {
private final TinyTree sourceMappings, targetMappings;
private final String fromNamespace, toNamespace;
@ -179,7 +193,7 @@ public class MigrateMappingsTask extends AbstractLoomTask {
* Since we only use intermediary names (and not descriptors) to match, and intermediary names are unique,
* this will migrate methods that have had their signature changed too.
*/
public MappingsJoiner(TinyTree sourceMappings, TinyTree targetMappings, String fromNamespace, String toNamespace) {
private MappingsJoiner(TinyTree sourceMappings, TinyTree targetMappings, String fromNamespace, String toNamespace) {
this.sourceMappings = sourceMappings;
this.targetMappings = targetMappings;
this.fromNamespace = fromNamespace;
@ -234,4 +248,3 @@ public class MigrateMappingsTask extends AbstractLoomTask {
}
}
}

View file

@ -39,13 +39,13 @@ import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.io.FilenameUtils;
import org.zeroturnaround.zip.ZipUtil;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ResolvedDependency;
import org.gradle.api.artifacts.SelfResolvingDependency;
import org.zeroturnaround.zip.ZipUtil;
import net.fabricmc.loom.LoomGradleExtension;
@ -154,7 +154,7 @@ public abstract class DependencyProvider {
public static class FileDependencyInfo extends DependencyInfo {
protected final Map<String, File> classifierToFile = new HashMap<>();
protected final String group = "net.fabricmc.synthetic", name, version;
protected final String group, name, version;
FileDependencyInfo(Project project, SelfResolvingDependency dependency, Configuration configuration) {
super(project, dependency, configuration);
@ -196,27 +196,34 @@ public abstract class DependencyProvider {
}
}
File root = classifierToFile.get(""); //We've built the classifierToFile map, now to try find a name and version for our dependency
if ("jar".equals(FilenameUtils.getExtension(root.getName())) && ZipUtil.containsEntry(root, "fabric.mod.json")) {
//It's a Fabric mod, see how much we can extract out
JsonObject json = new Gson().fromJson(new String(ZipUtil.unpackEntry(root, "fabric.mod.json"), StandardCharsets.UTF_8), JsonObject.class);
if (json == null || !json.has("id") || !json.has("version")) {
throw new IllegalArgumentException("Invalid Fabric mod jar: " + root + " (malformed json: " + json + ')');
}
if (json.has("name")) { //Go for the name field if it's got one
name = json.get("name").getAsString();
} else {
name = json.get("id").getAsString();
}
version = json.get("version").getAsString();
if (dependency.getGroup() != null && dependency.getVersion() != null) {
group = dependency.getGroup();
name = dependency.getName();
version = dependency.getVersion();
} else {
//Not a Fabric mod, just have to make something up
name = FilenameUtils.removeExtension(root.getName());
version = "1.0";
group = "net.fabricmc.synthetic";
File root = classifierToFile.get(""); //We've built the classifierToFile map, now to try find a name and version for our dependency
if ("jar".equals(FilenameUtils.getExtension(root.getName())) && ZipUtil.containsEntry(root, "fabric.mod.json")) {
//It's a Fabric mod, see how much we can extract out
JsonObject json = new Gson().fromJson(new String(ZipUtil.unpackEntry(root, "fabric.mod.json"), StandardCharsets.UTF_8), JsonObject.class);
if (json == null || !json.has("id") || !json.has("version")) {
throw new IllegalArgumentException("Invalid Fabric mod jar: " + root + " (malformed json: " + json + ')');
}
if (json.has("name")) { //Go for the name field if it's got one
name = json.get("name").getAsString();
} else {
name = json.get("id").getAsString();
}
version = json.get("version").getAsString();
} else {
//Not a Fabric mod, just have to make something up
name = FilenameUtils.removeExtension(root.getName());
version = "1.0";
}
}
}

View file

@ -85,7 +85,7 @@ public class DownloadUtil {
if ((code < 200 || code > 299) && code != HttpURLConnection.HTTP_NOT_MODIFIED) {
//Didn't get what we expected
throw new IOException(connection.getResponseMessage());
throw new IOException(connection.getResponseMessage() + " for " + from);
}
long modifyTime = connection.getHeaderFieldDate("Last-Modified", -1);

View file

@ -106,7 +106,7 @@ public class RunConfig {
default:
runConfig.mainClass = "net.fabricmc.devlaunchinjector.Main";
runConfig.programArgs = "";
runConfig.vmArgs = "-Dfabric.dli.config=\"" + extension.getDevLauncherConfig().getAbsolutePath() + "\" -Dfabric.dli.env=" + mode.toLowerCase();
runConfig.vmArgs = "-Dfabric.dli.config=" + quoteIfNeeded(extension.getDevLauncherConfig().getAbsolutePath()) + " -Dfabric.dli.env=" + mode.toLowerCase();
break;
}
@ -218,4 +218,12 @@ public class RunConfig {
throw new RuntimeException("Failed to find mainclass");
}
private static String quoteIfNeeded(String input) {
if (!input.contains(" ")) {
return input;
}
return String.format("\"%s\"", input);
}
}

View file

@ -1,59 +0,0 @@
/*
* 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;
public class Version {
private String mappingsVersion;
private String minecraftVersion;
private String version;
public Version(String version) {
this.version = version;
if (version.contains("+build.")) {
this.minecraftVersion = version.substring(0, version.lastIndexOf('+'));
this.mappingsVersion = version.substring(version.lastIndexOf('.') + 1);
} else {
//TODO legacy remove when no longer needed
char verSep = version.contains("-") ? '-' : '.';
this.minecraftVersion = version.substring(0, version.lastIndexOf(verSep));
this.mappingsVersion = version.substring(version.lastIndexOf(verSep) + 1);
}
}
public String getMappingsVersion() {
return mappingsVersion;
}
public String getMinecraftVersion() {
return minecraftVersion;
}
@Override
public String toString() {
return version;
}
}