From 7bcc6b98ff1f277f991a5c9a83e0cf4f38dbdadb Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Tue, 22 Oct 2019 20:04:38 +0100 Subject: [PATCH] Rewrite native handling, fixes support for gradle 5.6.3 Fixes #81 Fixes #134 This will require run configs to be regenerated Added cleanLoom, just a task that depends on cleanLoomBinaries and cleanLoomMappings --- build.gradle | 2 +- .../fabricmc/loom/LoomGradleExtension.java | 16 ++++++ .../net/fabricmc/loom/LoomGradlePlugin.java | 8 ++- .../providers/MinecraftLibraryProvider.java | 2 +- .../providers/MinecraftNativesProvider.java | 57 +++++++++++++++++++ .../fabricmc/loom/task/CleanLoomBinaries.java | 9 +++ .../loom/task/DownloadAssetsTask.java | 2 + .../loom/util/MinecraftVersionInfo.java | 4 ++ .../net/fabricmc/loom/util/RunConfig.java | 2 +- .../loom/util/SetupIntelijRunConfigs.java | 2 + 10 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/fabricmc/loom/providers/MinecraftNativesProvider.java diff --git a/build.gradle b/build.gradle index 423a8ad..dae48d8 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ targetCompatibility = 1.8 group = 'net.fabricmc' archivesBaseName = project.name -version = '0.2.5-SNAPSHOT' +version = '0.2.6-SNAPSHOT' def build = "local" def ENV = System.getenv() diff --git a/src/main/java/net/fabricmc/loom/LoomGradleExtension.java b/src/main/java/net/fabricmc/loom/LoomGradleExtension.java index 96d3322..31dcd59 100644 --- a/src/main/java/net/fabricmc/loom/LoomGradleExtension.java +++ b/src/main/java/net/fabricmc/loom/LoomGradleExtension.java @@ -145,6 +145,22 @@ public class LoomGradleExtension { return nestedModCache; } + public File getNativesJarStore(){ + File natives = new File(getUserCache(), "natives/jars"); + if(!natives.exists()) { + natives.mkdirs(); + } + return natives; + } + + public File getNativesDirectory(){ + File natives = new File(getUserCache(), "natives/" + getMinecraftProvider().minecraftVersion); + if(!natives.exists()) { + natives.mkdirs(); + } + return natives; + } + @Nullable private static Dependency findDependency(Project p, Collection configs, BiPredicate groupNameFilter) { for (Configuration config : configs) { diff --git a/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java b/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java index 35dbd3c..423827f 100644 --- a/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java +++ b/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java @@ -56,10 +56,16 @@ public class LoomGradlePlugin extends AbstractPlugin { super.apply(target); TaskContainer tasks = target.getTasks(); - + + tasks.register("cleanLoomBinaries", CleanLoomBinaries.class); tasks.register("cleanLoomMappings", CleanLoomMappings.class); + tasks.register("cleanLoom").configure(task -> { + task.dependsOn(tasks.getByName("cleanLoomBinaries")); + task.dependsOn(tasks.getByName("cleanLoomMappings")); + }); + tasks.register("migrateMappings", MigrateMappingsTask.class, t -> { t.getOutputs().upToDateWhen((o) -> false); }); diff --git a/src/main/java/net/fabricmc/loom/providers/MinecraftLibraryProvider.java b/src/main/java/net/fabricmc/loom/providers/MinecraftLibraryProvider.java index 8e1fd70..f218ddc 100644 --- a/src/main/java/net/fabricmc/loom/providers/MinecraftLibraryProvider.java +++ b/src/main/java/net/fabricmc/loom/providers/MinecraftLibraryProvider.java @@ -56,7 +56,7 @@ public class MinecraftLibraryProvider { initFiles(project, minecraftProvider); for (MinecraftVersionInfo.Library library : versionInfo.libraries) { - if (library.allowed() && library.getFile(MINECRAFT_LIBS) != null) { + if (library.allowed() && !library.isNative() && library.getFile(MINECRAFT_LIBS) != null) { // TODO: Add custom library locations // By default, they are all available on all sides diff --git a/src/main/java/net/fabricmc/loom/providers/MinecraftNativesProvider.java b/src/main/java/net/fabricmc/loom/providers/MinecraftNativesProvider.java new file mode 100644 index 0000000..c4c91fd --- /dev/null +++ b/src/main/java/net/fabricmc/loom/providers/MinecraftNativesProvider.java @@ -0,0 +1,57 @@ +/* + * 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.providers; + +import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.util.DownloadUtil; +import net.fabricmc.loom.util.MinecraftVersionInfo; +import org.gradle.api.Project; +import org.zeroturnaround.zip.ZipUtil; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +public class MinecraftNativesProvider { + + public static void provide(MinecraftProvider minecraftProvider, Project project) throws IOException { + LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); + MinecraftVersionInfo versionInfo = minecraftProvider.versionInfo; + + File nativesDir = extension.getNativesDirectory(); + File jarStore = extension.getNativesJarStore(); + + for (MinecraftVersionInfo.Library library : versionInfo.libraries) { + File libJarFile = library.getFile(jarStore); + if (library.allowed() && library.isNative() && libJarFile != null) { + DownloadUtil.downloadIfChanged(new URL(library.getURL()), libJarFile, project.getLogger()); + + //TODO possibly find a way to prevent needing to re-extract after each run, doesnt seem too slow + ZipUtil.unpack(libJarFile, nativesDir); + } + } + } + +} diff --git a/src/main/java/net/fabricmc/loom/task/CleanLoomBinaries.java b/src/main/java/net/fabricmc/loom/task/CleanLoomBinaries.java index a885aad..9c7f61d 100644 --- a/src/main/java/net/fabricmc/loom/task/CleanLoomBinaries.java +++ b/src/main/java/net/fabricmc/loom/task/CleanLoomBinaries.java @@ -25,9 +25,12 @@ package net.fabricmc.loom.task; import net.fabricmc.loom.LoomGradleExtension; +import org.apache.commons.io.FileUtils; import org.gradle.api.Project; import org.gradle.api.tasks.TaskAction; +import java.io.IOException; + public class CleanLoomBinaries extends AbstractLoomTask { @TaskAction public void run() { @@ -36,5 +39,11 @@ public class CleanLoomBinaries extends AbstractLoomTask { extension.getMinecraftProvider().getMergedJar().delete(); extension.getMinecraftMappedProvider().getIntermediaryJar().delete(); extension.getMinecraftMappedProvider().getMappedJar().delete(); + try { + FileUtils.deleteDirectory(extension.getNativesDirectory()); + FileUtils.deleteDirectory(extension.getNativesJarStore()); + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/src/main/java/net/fabricmc/loom/task/DownloadAssetsTask.java b/src/main/java/net/fabricmc/loom/task/DownloadAssetsTask.java index 3fce992..f43fc40 100644 --- a/src/main/java/net/fabricmc/loom/task/DownloadAssetsTask.java +++ b/src/main/java/net/fabricmc/loom/task/DownloadAssetsTask.java @@ -26,6 +26,7 @@ package net.fabricmc.loom.task; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.providers.MinecraftAssetsProvider; +import net.fabricmc.loom.providers.MinecraftNativesProvider; import org.gradle.api.Project; import org.gradle.api.tasks.TaskAction; @@ -38,5 +39,6 @@ public class DownloadAssetsTask extends AbstractLoomTask { LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); MinecraftAssetsProvider.provide(extension.getMinecraftProvider(), project); + MinecraftNativesProvider.provide(extension.getMinecraftProvider(), project); } } diff --git a/src/main/java/net/fabricmc/loom/util/MinecraftVersionInfo.java b/src/main/java/net/fabricmc/loom/util/MinecraftVersionInfo.java index d2f03c7..8ea9be5 100644 --- a/src/main/java/net/fabricmc/loom/util/MinecraftVersionInfo.java +++ b/src/main/java/net/fabricmc/loom/util/MinecraftVersionInfo.java @@ -100,6 +100,10 @@ public class MinecraftVersionInfo { } } + public boolean isNative(){ + return getClassifier().contains("natives"); + } + public boolean allowed() { if (this.rules == null || this.rules.length <= 0) { return true; diff --git a/src/main/java/net/fabricmc/loom/util/RunConfig.java b/src/main/java/net/fabricmc/loom/util/RunConfig.java index b192197..3935209 100644 --- a/src/main/java/net/fabricmc/loom/util/RunConfig.java +++ b/src/main/java/net/fabricmc/loom/util/RunConfig.java @@ -90,7 +90,7 @@ public class RunConfig { private static void populate(Project project, LoomGradleExtension extension, RunConfig runConfig, String mode) { runConfig.projectName = project.getName(); runConfig.runDir = "file://$PROJECT_DIR$/" + extension.runDir; - runConfig.vmArgs = "-Dfabric.development=true"; + runConfig.vmArgs = "-Dfabric.development=true -Djava.library.path=" + extension.getNativesDirectory().getAbsolutePath(); switch (extension.getLoaderLaunchMethod()) { case "launchwrapper": diff --git a/src/main/java/net/fabricmc/loom/util/SetupIntelijRunConfigs.java b/src/main/java/net/fabricmc/loom/util/SetupIntelijRunConfigs.java index 9847e7b..39d5222 100644 --- a/src/main/java/net/fabricmc/loom/util/SetupIntelijRunConfigs.java +++ b/src/main/java/net/fabricmc/loom/util/SetupIntelijRunConfigs.java @@ -26,6 +26,7 @@ package net.fabricmc.loom.util; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.providers.MinecraftAssetsProvider; +import net.fabricmc.loom.providers.MinecraftNativesProvider; import org.apache.commons.io.FileUtils; import org.gradle.api.Project; @@ -59,6 +60,7 @@ public class SetupIntelijRunConfigs { if(Boolean.parseBoolean(System.getProperty("idea.sync.active", "false"))){ LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); MinecraftAssetsProvider.provide(extension.getMinecraftProvider(), project); + MinecraftNativesProvider.provide(extension.getMinecraftProvider(), project); } File projectDir = project.file(".idea");