From 65e2a76d7027183bb83f669c88f333d6344d7579 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Wed, 17 Aug 2016 17:38:54 +0100 Subject: [PATCH] Jar merging + reformat --- build.gradle | 1 + .../net/fabricmc/loom/AbstractPlugin.java | 258 +++++++------ .../fabricmc/loom/LoomGradleExtension.java | 6 +- .../net/fabricmc/loom/LoomGradlePlugin.java | 24 +- .../fabricmc/loom/task/DownloadNewTask.java | 178 ++++----- .../net/fabricmc/loom/task/DownloadTask.java | 182 ++++----- .../loom/task/ExtractNativesTask.java | 16 +- .../loom/task/GenIdeaProjectTask.java | 238 ++++++------ .../net/fabricmc/loom/task/MapJarsTask.java | 68 ++-- .../net/fabricmc/loom/task/MergeJarsTask.java | 59 +++ .../java/net/fabricmc/loom/util/Checksum.java | 32 +- .../net/fabricmc/loom/util/Constants.java | 58 +-- .../net/fabricmc/loom/util/IdeaRunConfig.java | 64 ++-- .../fabricmc/loom/util/ManifestVersion.java | 8 +- .../fabricmc/loom/util/OperatingSystem.java | 40 +- .../java/net/fabricmc/loom/util/Version.java | 218 +++++------ .../fabricmc/loom/util/assets/AssetIndex.java | 28 +- .../loom/util/assets/AssetObject.java | 50 +-- .../loom/util/delayed/DelayedFile.java | 24 +- .../fabricmc/loom/util/delayed/IDelayed.java | 2 +- .../loom/util/progress/ProgressLogger.java | 350 +++++++++--------- 21 files changed, 980 insertions(+), 924 deletions(-) create mode 100644 src/main/java/net/fabricmc/loom/task/MergeJarsTask.java diff --git a/build.gradle b/build.gradle index 952b25c..a04e3f7 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,7 @@ dependencies { shade 'com.google.code.gson:gson:2.6.2' shade 'commons-io:commons-io:1.4' shade 'com.google.guava:guava:19.0' + shade 'net.fabricmc:blending-jar:0.1.0' shade 'net.fabricmc:enigma:0.11.+' shade 'org.javassist:javassist:3.+' diff --git a/src/main/java/net/fabricmc/loom/AbstractPlugin.java b/src/main/java/net/fabricmc/loom/AbstractPlugin.java index d71354d..e4679dd 100644 --- a/src/main/java/net/fabricmc/loom/AbstractPlugin.java +++ b/src/main/java/net/fabricmc/loom/AbstractPlugin.java @@ -24,16 +24,14 @@ package net.fabricmc.loom; +import com.google.common.collect.ImmutableMap; import com.google.gson.Gson; import net.fabricmc.loom.task.DownloadTask; import net.fabricmc.loom.util.Constants; -import com.google.common.collect.ImmutableMap; import net.fabricmc.loom.util.Version; -import org.gradle.api.Action; import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.Task; -import org.gradle.api.artifacts.repositories.FlatDirectoryArtifactRepository; import org.gradle.api.artifacts.repositories.MavenArtifactRepository; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginConvention; @@ -42,161 +40,159 @@ import org.gradle.api.tasks.javadoc.Javadoc; import org.gradle.plugins.ide.eclipse.model.EclipseModel; import org.gradle.plugins.ide.idea.model.IdeaModel; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class AbstractPlugin implements Plugin { - protected Project project; + protected Project project; - @Override - public void apply(Project target) { - this.project = target; + @Override + public void apply(Project target) { + this.project = target; - // Apply default plugins - project.apply(ImmutableMap.of("plugin", "java")); - project.apply(ImmutableMap.of("plugin", "eclipse")); - project.apply(ImmutableMap.of("plugin", "idea")); + // Apply default plugins + project.apply(ImmutableMap.of("plugin", "java")); + project.apply(ImmutableMap.of("plugin", "eclipse")); + project.apply(ImmutableMap.of("plugin", "idea")); - project.getExtensions().create("minecraft", LoomGradleExtension.class); + project.getExtensions().create("minecraft", LoomGradleExtension.class); - // Force add Mojang repository - addMavenRepo(target, "Mojang", "https://libraries.minecraft.net/"); + // Force add Mojang repository + addMavenRepo(target, "Mojang", "https://libraries.minecraft.net/"); - // Minecraft libraries configuration - project.getConfigurations().maybeCreate(Constants.CONFIG_MC_DEPENDENCIES); - project.getConfigurations().maybeCreate(Constants.CONFIG_MC_DEPENDENCIES_CLIENT); - project.getConfigurations().maybeCreate(Constants.CONFIG_NATIVES); + // Minecraft libraries configuration + project.getConfigurations().maybeCreate(Constants.CONFIG_MC_DEPENDENCIES); + project.getConfigurations().maybeCreate(Constants.CONFIG_MC_DEPENDENCIES_CLIENT); + project.getConfigurations().maybeCreate(Constants.CONFIG_NATIVES); - // Common libraries extends from client libraries, CONFIG_MC_DEPENDENCIES will contains all MC dependencies - project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES).extendsFrom(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES_CLIENT)); + // Common libraries extends from client libraries, CONFIG_MC_DEPENDENCIES will contains all MC dependencies + project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES).extendsFrom(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES_CLIENT)); - configureIDEs(); - configureCompile(); - } + configureIDEs(); + configureCompile(); + } - /** - * Permit to create a Task instance of the type in the project - * - * @param name The name of the task - * @param type The type of the task that will be used to create an instance - * @return The created task object for the project - */ - public T makeTask(String name, Class type) { - return makeTask(project, name, type); - } + /** + * Permit to create a Task instance of the type in the project + * + * @param name The name of the task + * @param type The type of the task that will be used to create an instance + * @return The created task object for the project + */ + public T makeTask(String name, Class type) { + return makeTask(project, name, type); + } - /** - * Permit to create a Task instance of the type in a project - * - * @param target The target project - * @param name The name of the task - * @param type The type of the task that will be used to create an instance - * @return The created task object for the specified project - */ - public static T makeTask(Project target, String name, Class type) { - return target.getTasks().create(name, type); - } + /** + * Permit to create a Task instance of the type in a project + * + * @param target The target project + * @param name The name of the task + * @param type The type of the task that will be used to create an instance + * @return The created task object for the specified project + */ + public static T makeTask(Project target, String name, Class type) { + return target.getTasks().create(name, type); + } - /** - * Permit to add a Maven repository to a target project - * - * @param target The garget project - * @param name The name of the repository - * @param url The URL of the repository - * @return An object containing the name and the URL of the repository that can be modified later - */ - public MavenArtifactRepository addMavenRepo(Project target, final String name, final String url) { - return target.getRepositories().maven(repo -> { - repo.setName(name); - repo.setUrl(url); - }); - } + /** + * Permit to add a Maven repository to a target project + * + * @param target The garget project + * @param name The name of the repository + * @param url The URL of the repository + * @return An object containing the name and the URL of the repository that can be modified later + */ + public MavenArtifactRepository addMavenRepo(Project target, final String name, final String url) { + return target.getRepositories().maven(repo -> { + repo.setName(name); + repo.setUrl(url); + }); + } - /** - * Add Minecraft dependencies to IDE dependencies - */ - protected void configureIDEs() { - // IDEA - IdeaModel ideaModule = (IdeaModel) project.getExtensions().getByName("idea"); + /** + * Add Minecraft dependencies to IDE dependencies + */ + protected void configureIDEs() { + // IDEA + IdeaModel ideaModule = (IdeaModel) project.getExtensions().getByName("idea"); - ideaModule.getModule().getExcludeDirs().addAll(project.files(".gradle", "build", ".idea", "out").getFiles()); - ideaModule.getModule().setDownloadJavadoc(true); - ideaModule.getModule().setDownloadSources(true); - ideaModule.getModule().setInheritOutputDirs(true); - ideaModule.getModule().getScopes().get("COMPILE").get("plus").add(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES)); + ideaModule.getModule().getExcludeDirs().addAll(project.files(".gradle", "build", ".idea", "out").getFiles()); + ideaModule.getModule().setDownloadJavadoc(true); + ideaModule.getModule().setDownloadSources(true); + ideaModule.getModule().setInheritOutputDirs(true); + ideaModule.getModule().getScopes().get("COMPILE").get("plus").add(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES)); - // ECLIPSE - EclipseModel eclipseModule = (EclipseModel) project.getExtensions().getByName("eclipse"); - eclipseModule.getClasspath().getPlusConfigurations().add(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES)); - } + // ECLIPSE + EclipseModel eclipseModule = (EclipseModel) project.getExtensions().getByName("eclipse"); + eclipseModule.getClasspath().getPlusConfigurations().add(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES)); + } - /** - * Add Minecraft dependencies to compile time - */ - protected void configureCompile() { - JavaPluginConvention javaModule = (JavaPluginConvention) project.getConvention().getPlugins().get("java"); + /** + * Add Minecraft dependencies to compile time + */ + protected void configureCompile() { + JavaPluginConvention javaModule = (JavaPluginConvention) project.getConvention().getPlugins().get("java"); - SourceSet main = javaModule.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME); - SourceSet test = javaModule.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME); + SourceSet main = javaModule.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME); + SourceSet test = javaModule.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME); - main.setCompileClasspath(main.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES))); - test.setCompileClasspath(test.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES))); - main.setRuntimeClasspath(main.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES))); - test.setCompileClasspath(test.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES))); + main.setCompileClasspath(main.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES))); + test.setCompileClasspath(test.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES))); + main.setRuntimeClasspath(main.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES))); + test.setCompileClasspath(test.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES))); - Javadoc javadoc = (Javadoc) project.getTasks().getByName(JavaPlugin.JAVADOC_TASK_NAME); - javadoc.setClasspath(main.getOutput().plus(main.getCompileClasspath())); + Javadoc javadoc = (Javadoc) project.getTasks().getByName(JavaPlugin.JAVADOC_TASK_NAME); + javadoc.setClasspath(main.getOutput().plus(main.getCompileClasspath())); - project.afterEvaluate(project1 -> { - LoomGradleExtension extension = project1.getExtensions().getByType(LoomGradleExtension.class); - project1.getRepositories().flatDir(flatDirectoryArtifactRepository -> { - flatDirectoryArtifactRepository.dir(Constants.CACHE_FILES); - flatDirectoryArtifactRepository.setName("LoomCacheFiles"); - }); + project.afterEvaluate(project1 -> { + LoomGradleExtension extension = project1.getExtensions().getByType(LoomGradleExtension.class); + project1.getRepositories().flatDir(flatDirectoryArtifactRepository -> { + flatDirectoryArtifactRepository.dir(Constants.CACHE_FILES); + flatDirectoryArtifactRepository.setName("LoomCacheFiles"); + }); - project1.getRepositories().maven(mavenArtifactRepository -> { - mavenArtifactRepository.setName("FabricMC"); - mavenArtifactRepository.setUrl("http://maven.fabricmc.net/"); - }); + project1.getRepositories().maven(mavenArtifactRepository -> { + mavenArtifactRepository.setName("FabricMC"); + mavenArtifactRepository.setUrl("http://maven.fabricmc.net/"); + }); - project1.getRepositories().maven(mavenArtifactRepository -> { - mavenArtifactRepository.setName("SpongePowered"); - mavenArtifactRepository.setUrl("http://repo.spongepowered.org/maven"); - }); + project1.getRepositories().maven(mavenArtifactRepository -> { + mavenArtifactRepository.setName("SpongePowered"); + mavenArtifactRepository.setUrl("http://repo.spongepowered.org/maven"); + }); - project1.getRepositories().maven(mavenArtifactRepository -> { - mavenArtifactRepository.setName("Mojang"); - mavenArtifactRepository.setUrl("https://libraries.minecraft.net/"); - }); + project1.getRepositories().maven(mavenArtifactRepository -> { + mavenArtifactRepository.setName("Mojang"); + mavenArtifactRepository.setUrl("https://libraries.minecraft.net/"); + }); - project1.getRepositories().mavenCentral(); - project1.getRepositories().jcenter(); + project1.getRepositories().mavenCentral(); + project1.getRepositories().jcenter(); - Gson gson = new Gson(); - try { - DownloadTask.downloadMcJson(extension, project1.getLogger()); - Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class); - for (Version.Library library : version.libraries) { - if (library.allowed() && library.getFile() != null) { - String configName = Constants.CONFIG_MC_DEPENDENCIES; - if (library.name.contains("java3d") || library.name.contains("paulscode") || library.name.contains("lwjgl") || library.name.contains("twitch") || library.name.contains("jinput")) { - configName = Constants.CONFIG_MC_DEPENDENCIES_CLIENT; - } - project1.getDependencies().add(configName, library.getArtifactName()); - } - } - } catch (IOException e) { - e.printStackTrace(); - } - project1.getDependencies().add(Constants.CONFIG_MC_DEPENDENCIES, "net.minecraft:" + Constants.MINECRAFT_CLIENT_MAPPED_JAR.get(extension).getName().replace(".jar", "")); + Gson gson = new Gson(); + try { + DownloadTask.downloadMcJson(extension, project1.getLogger()); + Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class); + for (Version.Library library : version.libraries) { + if (library.allowed() && library.getFile() != null) { + String configName = Constants.CONFIG_MC_DEPENDENCIES; + if (library.name.contains("java3d") || library.name.contains("paulscode") || library.name.contains("lwjgl") || library.name.contains("twitch") || library.name.contains("jinput")) { + configName = Constants.CONFIG_MC_DEPENDENCIES_CLIENT; + } + project1.getDependencies().add(configName, library.getArtifactName()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + project1.getDependencies().add(Constants.CONFIG_MC_DEPENDENCIES, "net.minecraft:" + Constants.MINECRAFT_MAPPED_JAR.get(extension).getName().replace(".jar", "")); - if(extension.fabricVersion != null && !extension.fabricVersion.isEmpty()){ - //only add this when not in a fabric dev env - project1.getDependencies().add(Constants.CONFIG_MC_DEPENDENCIES, "net.fabricmc:fabric-base:" + extension.version + "-" + extension.fabricVersion); - } - }); + if (extension.fabricVersion != null && !extension.fabricVersion.isEmpty()) { + //only add this when not in a fabric dev env + project1.getDependencies().add(Constants.CONFIG_MC_DEPENDENCIES, "net.fabricmc:fabric-base:" + extension.version + "-" + extension.fabricVersion); + } + }); - - } + } } diff --git a/src/main/java/net/fabricmc/loom/LoomGradleExtension.java b/src/main/java/net/fabricmc/loom/LoomGradleExtension.java index 1684ef5..8f1d585 100644 --- a/src/main/java/net/fabricmc/loom/LoomGradleExtension.java +++ b/src/main/java/net/fabricmc/loom/LoomGradleExtension.java @@ -25,7 +25,7 @@ package net.fabricmc.loom; public class LoomGradleExtension { - public String version; - public String runDir = "run"; - public String fabricVersion; + public String version; + public String runDir = "run"; + public String fabricVersion; } diff --git a/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java b/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java index 935950d..2d85b14 100644 --- a/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java +++ b/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java @@ -24,23 +24,21 @@ package net.fabricmc.loom; -import net.fabricmc.loom.task.DownloadTask; -import net.fabricmc.loom.task.ExtractNativesTask; -import net.fabricmc.loom.task.GenIdeaProjectTask; -import net.fabricmc.loom.task.MapJarsTask; +import net.fabricmc.loom.task.*; import org.gradle.api.DefaultTask; import org.gradle.api.Project; public class LoomGradlePlugin extends AbstractPlugin { - @Override - public void apply(Project target) { - super.apply(target); + @Override + public void apply(Project target) { + super.apply(target); - makeTask("download", DownloadTask.class); - makeTask("mapJars", MapJarsTask.class).dependsOn("download"); - makeTask("setupFabric", DefaultTask.class).dependsOn("mapJars"); + makeTask("download", DownloadTask.class); + makeTask("mergeJars", MergeJarsTask.class).dependsOn("download"); + makeTask("mapJars", MapJarsTask.class).dependsOn("mergeJars"); + makeTask("setupFabric", DefaultTask.class).dependsOn("mapJars"); - makeTask("extractNatives", ExtractNativesTask.class).dependsOn("download"); - makeTask("genIdeaRuns", GenIdeaProjectTask.class).dependsOn("cleanIdea").dependsOn("idea").dependsOn("extractNatives"); - } + makeTask("extractNatives", ExtractNativesTask.class).dependsOn("download"); + makeTask("genIdeaRuns", GenIdeaProjectTask.class).dependsOn("cleanIdea").dependsOn("idea").dependsOn("extractNatives"); + } } diff --git a/src/main/java/net/fabricmc/loom/task/DownloadNewTask.java b/src/main/java/net/fabricmc/loom/task/DownloadNewTask.java index 6b21d01..b0a12bb 100644 --- a/src/main/java/net/fabricmc/loom/task/DownloadNewTask.java +++ b/src/main/java/net/fabricmc/loom/task/DownloadNewTask.java @@ -46,111 +46,111 @@ import java.nio.channels.ReadableByteChannel; */ public class DownloadNewTask extends DefaultTask { - @Input - private Object output; + @Input + private Object output; - @OutputFile - private String url; - private String taskName; + @OutputFile + private String url; + private String taskName; - @TaskAction - public void download() throws IOException { - File outputFile = getProject().file(getOutput()); - outputFile.getParentFile().mkdirs(); - outputFile.createNewFile(); + @TaskAction + public void download() throws IOException { + File outputFile = getProject().file(getOutput()); + outputFile.getParentFile().mkdirs(); + outputFile.createNewFile(); - getLogger().info("Downloading " + getURL()); + getLogger().info("Downloading " + getURL()); - URL url = new URL(getURL()); - HttpURLConnection connect = (HttpURLConnection) url.openConnection(); - connect.setRequestProperty("User-Agent", Constants.USER_AGENT); - connect.setInstanceFollowRedirects(true); + URL url = new URL(getURL()); + HttpURLConnection connect = (HttpURLConnection) url.openConnection(); + connect.setRequestProperty("User-Agent", Constants.USER_AGENT); + connect.setInstanceFollowRedirects(true); - ProgressLogger progressLogger = ProgressLogger.getProgressFactory(getProject(), getClass().getName()); - progressLogger.setDescription("Downloading " + getURL()); - ReadableByteChannel inChannel = new DownloadChannel(Channels.newChannel(connect.getInputStream()), getContentLength(url), progressLogger); - FileChannel outChannel = new FileOutputStream(outputFile).getChannel(); - outChannel.transferFrom(inChannel, 0, Long.MAX_VALUE); - outChannel.close(); - inChannel.close(); - progressLogger.completed(); - getLogger().info("Download complete"); - } + ProgressLogger progressLogger = ProgressLogger.getProgressFactory(getProject(), getClass().getName()); + progressLogger.setDescription("Downloading " + getURL()); + ReadableByteChannel inChannel = new DownloadChannel(Channels.newChannel(connect.getInputStream()), getContentLength(url), progressLogger); + FileChannel outChannel = new FileOutputStream(outputFile).getChannel(); + outChannel.transferFrom(inChannel, 0, Long.MAX_VALUE); + outChannel.close(); + inChannel.close(); + progressLogger.completed(); + getLogger().info("Download complete"); + } - private int getContentLength(URL url) { - HttpURLConnection connection; - int contentLength = -1; - try { - connection = (HttpURLConnection) url.openConnection(); - contentLength = connection.getContentLength(); - } catch (Exception e) { - } - return contentLength; - } + private int getContentLength(URL url) { + HttpURLConnection connection; + int contentLength = -1; + try { + connection = (HttpURLConnection) url.openConnection(); + contentLength = connection.getContentLength(); + } catch (Exception e) { + } + return contentLength; + } - public File getOutput() { - return getProject().file(output); - } + public File getOutput() { + return getProject().file(output); + } - public void setOutput(Object output) { - this.output = output; - } + public void setOutput(Object output) { + this.output = output; + } - public String getURL() { - return url; - } + public String getURL() { + return url; + } - public void setURL(String url) { - this.url = url; - } + public void setURL(String url) { + this.url = url; + } - public void setTaskName(String taskName) { - this.taskName = taskName; - } + public void setTaskName(String taskName) { + this.taskName = taskName; + } - public String getTaskName() { - return taskName; - } + public String getTaskName() { + return taskName; + } - class DownloadChannel implements ReadableByteChannel { - ProgressLogger logger; - String formattedSize; - ReadableByteChannel rbc; - long totalBytes; + class DownloadChannel implements ReadableByteChannel { + ProgressLogger logger; + String formattedSize; + ReadableByteChannel rbc; + long totalBytes; - DownloadChannel(ReadableByteChannel rbc, long expectedSize, ProgressLogger logger) { - this.logger = logger; - this.formattedSize = toHumanReadableLength(expectedSize); - this.rbc = rbc; - } + DownloadChannel(ReadableByteChannel rbc, long expectedSize, ProgressLogger logger) { + this.logger = logger; + this.formattedSize = toHumanReadableLength(expectedSize); + this.rbc = rbc; + } - public void close() throws IOException { - rbc.close(); - } + public void close() throws IOException { + rbc.close(); + } - public boolean isOpen() { - return rbc.isOpen(); - } + public boolean isOpen() { + return rbc.isOpen(); + } - public int read(ByteBuffer buffer) throws IOException { - int processedBytes; - if ((processedBytes = rbc.read(buffer)) > 0) { - totalBytes += processedBytes; - logger.progress(toHumanReadableLength(totalBytes) + "/" + formattedSize + " downloaded"); - } - return processedBytes; - } + public int read(ByteBuffer buffer) throws IOException { + int processedBytes; + if ((processedBytes = rbc.read(buffer)) > 0) { + totalBytes += processedBytes; + logger.progress(toHumanReadableLength(totalBytes) + "/" + formattedSize + " downloaded"); + } + return processedBytes; + } - private String toHumanReadableLength(long bytes) { - if (bytes < 1024) { - return bytes + " B"; - } else if (bytes < 1024 * 1024) { - return (bytes / 1024) + " KB"; - } else if (bytes < 1024 * 1024 * 1024) { - return String.format("%.2f MB", bytes / (1024.0 * 1024.0)); - } else { - return String.format("%.2f GB", bytes / (1024.0 * 1024.0 * 1024.0)); - } - } - } + private String toHumanReadableLength(long bytes) { + if (bytes < 1024) { + return bytes + " B"; + } else if (bytes < 1024 * 1024) { + return (bytes / 1024) + " KB"; + } else if (bytes < 1024 * 1024 * 1024) { + return String.format("%.2f MB", bytes / (1024.0 * 1024.0)); + } else { + return String.format("%.2f GB", bytes / (1024.0 * 1024.0 * 1024.0)); + } + } + } } diff --git a/src/main/java/net/fabricmc/loom/task/DownloadTask.java b/src/main/java/net/fabricmc/loom/task/DownloadTask.java index 0fb55be..87dce28 100644 --- a/src/main/java/net/fabricmc/loom/task/DownloadTask.java +++ b/src/main/java/net/fabricmc/loom/task/DownloadTask.java @@ -24,13 +24,13 @@ package net.fabricmc.loom.task; -import net.fabricmc.loom.util.Constants; -import net.fabricmc.loom.util.Version; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.util.Checksum; +import net.fabricmc.loom.util.Constants; import net.fabricmc.loom.util.ManifestVersion; +import net.fabricmc.loom.util.Version; import net.fabricmc.loom.util.assets.AssetIndex; import net.fabricmc.loom.util.assets.AssetObject; import net.fabricmc.loom.util.progress.ProgressLogger; @@ -44,111 +44,115 @@ import org.gradle.api.tasks.TaskAction; import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.net.MalformedURLException; import java.net.URL; import java.util.Map; import java.util.Optional; public class DownloadTask extends DefaultTask { - @TaskAction - public void download() { - try { - LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class); + @TaskAction + public void download() { + try { + LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class); - downloadMcJson(extension, getLogger()); + downloadMcJson(extension, getLogger()); - Gson gson = new Gson(); - Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class); + Gson gson = new Gson(); + Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class); - if (!Constants.MINECRAFT_CLIENT_JAR.get(extension).exists() || !Checksum.equals(Constants.MINECRAFT_CLIENT_JAR.get(extension), version.downloads.get("client").sha1)) { - this.getLogger().lifecycle(":downloading client"); - FileUtils.copyURLToFile(new URL(version.downloads.get("client").url), Constants.MINECRAFT_CLIENT_JAR.get(extension)); - } + if (!Constants.MINECRAFT_CLIENT_JAR.get(extension).exists() || !Checksum.equals(Constants.MINECRAFT_CLIENT_JAR.get(extension), version.downloads.get("client").sha1)) { + this.getLogger().lifecycle(":downloading client"); + FileUtils.copyURLToFile(new URL(version.downloads.get("client").url), Constants.MINECRAFT_CLIENT_JAR.get(extension)); + } - if (Constants.MAPPINGS_ZIP.exists()) { - Constants.MAPPINGS_ZIP.delete(); - } + if (!Constants.MINECRAFT_SERVER_JAR.get(extension).exists() || !Checksum.equals(Constants.MINECRAFT_SERVER_JAR.get(extension), version.downloads.get("server").sha1)) { + this.getLogger().lifecycle(":downloading server"); + FileUtils.copyURLToFile(new URL(version.downloads.get("server").url), Constants.MINECRAFT_SERVER_JAR.get(extension)); + } - this.getLogger().lifecycle(":downloading mappings"); - FileUtils.copyURLToFile(new URL("https://github.com/FabricMC/pomf/archive/master.zip"), Constants.MAPPINGS_ZIP); + if (Constants.MAPPINGS_ZIP.exists()) { + Constants.MAPPINGS_ZIP.delete(); + } - DependencyHandler dependencyHandler = getProject().getDependencies(); + this.getLogger().lifecycle(":downloading mappings"); + FileUtils.copyURLToFile(new URL("https://github.com/FabricMC/pomf/archive/master.zip"), Constants.MAPPINGS_ZIP); - if (getProject().getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES).getState() == Configuration.State.UNRESOLVED) { - for (Version.Library library : version.libraries) { - if (library.allowed() && library.getFile() != null) { - // By default, they are all available on all sides - String configName = Constants.CONFIG_MC_DEPENDENCIES; - if (library.name.contains("java3d") || library.name.contains("paulscode") || library.name.contains("lwjgl") || library.name.contains("twitch") || library.name.contains("jinput")) { - configName = Constants.CONFIG_MC_DEPENDENCIES_CLIENT; - } - dependencyHandler.add(configName, library.getArtifactName()); - } - } - } + DependencyHandler dependencyHandler = getProject().getDependencies(); - if (getProject().getConfigurations().getByName(Constants.CONFIG_NATIVES).getState() == Configuration.State.UNRESOLVED) { - version.libraries.stream().filter(lib -> lib.natives != null).forEach(lib -> dependencyHandler.add(Constants.CONFIG_NATIVES, lib.getArtifactName())); - } + if (getProject().getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES).getState() == Configuration.State.UNRESOLVED) { + for (Version.Library library : version.libraries) { + if (library.allowed() && library.getFile() != null) { + // By default, they are all available on all sides + String configName = Constants.CONFIG_MC_DEPENDENCIES; + if (library.name.contains("java3d") || library.name.contains("paulscode") || library.name.contains("lwjgl") || library.name.contains("twitch") || library.name.contains("jinput")) { + configName = Constants.CONFIG_MC_DEPENDENCIES_CLIENT; + } + dependencyHandler.add(configName, library.getArtifactName()); + } + } + } - // Force add LaunchWrapper - dependencyHandler.add(Constants.CONFIG_MC_DEPENDENCIES, "net.minecraft:launchwrapper:1.11"); + if (getProject().getConfigurations().getByName(Constants.CONFIG_NATIVES).getState() == Configuration.State.UNRESOLVED) { + version.libraries.stream().filter(lib -> lib.natives != null).forEach(lib -> dependencyHandler.add(Constants.CONFIG_NATIVES, lib.getArtifactName())); + } - Version.AssetIndex assetIndex = version.assetIndex; + // Force add LaunchWrapper + dependencyHandler.add(Constants.CONFIG_MC_DEPENDENCIES, "net.minecraft:launchwrapper:1.11"); - File assets = new File(Constants.CACHE_FILES, "assets"); - if (!assets.exists()) { - assets.mkdirs(); - } + Version.AssetIndex assetIndex = version.assetIndex; - File assetsInfo = new File(assets, "indexes" + File.separator + assetIndex.id + ".json"); - if (!assetsInfo.exists() || !Checksum.equals(assetsInfo, assetIndex.sha1)) { - this.getLogger().lifecycle(":downloading asset index"); - FileUtils.copyURLToFile(new URL(assetIndex.url), assetsInfo); - } + File assets = new File(Constants.CACHE_FILES, "assets"); + if (!assets.exists()) { + assets.mkdirs(); + } - ProgressLogger progressLogger = ProgressLogger.getProgressFactory(getProject(), getClass().getName()); - progressLogger.start("Downloading assets...", "assets"); - AssetIndex index = new Gson().fromJson(new FileReader(assetsInfo), AssetIndex.class); - Map parent = index.getFileMap(); - final int totalSize = parent.size(); - int position = 0; - this.getLogger().lifecycle(":downloading assets..."); - for (Map.Entry entry : parent.entrySet()) { - AssetObject object = entry.getValue(); - String sha1 = object.getHash(); - File file = new File(assets, "objects" + File.separator + sha1.substring(0, 2) + File.separator + sha1); - if (!file.exists() || !Checksum.equals(file, sha1)) { - this.getLogger().debug(":downloading asset " + entry.getKey()); - FileUtils.copyURLToFile(new URL(Constants.RESOURCES_BASE + sha1.substring(0, 2) + "/" + sha1), file); - } - String assetName = entry.getKey(); - int end = assetName.lastIndexOf("/") + 1; - if (end > 0) { - assetName = assetName.substring(end, assetName.length()); - } - progressLogger.progress(assetName + " - " + position + "/" + totalSize + " (" + (int) ((position / (double) totalSize) * 100) + "%) assets downloaded"); - position++; - } - progressLogger.completed(); - } catch (IOException e) { - e.printStackTrace(); - } - } + File assetsInfo = new File(assets, "indexes" + File.separator + assetIndex.id + ".json"); + if (!assetsInfo.exists() || !Checksum.equals(assetsInfo, assetIndex.sha1)) { + this.getLogger().lifecycle(":downloading asset index"); + FileUtils.copyURLToFile(new URL(assetIndex.url), assetsInfo); + } - public static void downloadMcJson(LoomGradleExtension extension, Logger logger) throws IOException { - if (!Constants.MINECRAFT_JSON.get(extension).exists()) { - logger.lifecycle(":downloading minecraft json"); - FileUtils.copyURLToFile(new URL("https://launchermeta.mojang.com/mc/game/version_manifest.json"), Constants.VERSION_MANIFEST); - ManifestVersion mcManifest = new GsonBuilder().create().fromJson(FileUtils.readFileToString(Constants.VERSION_MANIFEST), ManifestVersion.class); + ProgressLogger progressLogger = ProgressLogger.getProgressFactory(getProject(), getClass().getName()); + progressLogger.start("Downloading assets...", "assets"); + AssetIndex index = new Gson().fromJson(new FileReader(assetsInfo), AssetIndex.class); + Map parent = index.getFileMap(); + final int totalSize = parent.size(); + int position = 0; + this.getLogger().lifecycle(":downloading assets..."); + for (Map.Entry entry : parent.entrySet()) { + AssetObject object = entry.getValue(); + String sha1 = object.getHash(); + File file = new File(assets, "objects" + File.separator + sha1.substring(0, 2) + File.separator + sha1); + if (!file.exists() || !Checksum.equals(file, sha1)) { + this.getLogger().debug(":downloading asset " + entry.getKey()); + FileUtils.copyURLToFile(new URL(Constants.RESOURCES_BASE + sha1.substring(0, 2) + "/" + sha1), file); + } + String assetName = entry.getKey(); + int end = assetName.lastIndexOf("/") + 1; + if (end > 0) { + assetName = assetName.substring(end, assetName.length()); + } + progressLogger.progress(assetName + " - " + position + "/" + totalSize + " (" + (int) ((position / (double) totalSize) * 100) + "%) assets downloaded"); + position++; + } + progressLogger.completed(); + } catch (IOException e) { + e.printStackTrace(); + } + } - Optional optionalVersion = mcManifest.versions.stream().filter(versions -> versions.id.equalsIgnoreCase(extension.version)).findFirst(); - if (optionalVersion.isPresent()) { - FileUtils.copyURLToFile(new URL(optionalVersion.get().url), Constants.MINECRAFT_JSON.get(extension)); - } else { - logger.info(":failed downloading minecraft json"); - throw new RuntimeException("Failed downloading Minecraft json"); - } - } - } + public static void downloadMcJson(LoomGradleExtension extension, Logger logger) throws IOException { + if (!Constants.MINECRAFT_JSON.get(extension).exists()) { + logger.lifecycle(":downloading minecraft json"); + FileUtils.copyURLToFile(new URL("https://launchermeta.mojang.com/mc/game/version_manifest.json"), Constants.VERSION_MANIFEST); + ManifestVersion mcManifest = new GsonBuilder().create().fromJson(FileUtils.readFileToString(Constants.VERSION_MANIFEST), ManifestVersion.class); + + Optional optionalVersion = mcManifest.versions.stream().filter(versions -> versions.id.equalsIgnoreCase(extension.version)).findFirst(); + if (optionalVersion.isPresent()) { + FileUtils.copyURLToFile(new URL(optionalVersion.get().url), Constants.MINECRAFT_JSON.get(extension)); + } else { + logger.info(":failed downloading minecraft json"); + throw new RuntimeException("Failed downloading Minecraft json"); + } + } + } } diff --git a/src/main/java/net/fabricmc/loom/task/ExtractNativesTask.java b/src/main/java/net/fabricmc/loom/task/ExtractNativesTask.java index 35a8ca4..ed660db 100644 --- a/src/main/java/net/fabricmc/loom/task/ExtractNativesTask.java +++ b/src/main/java/net/fabricmc/loom/task/ExtractNativesTask.java @@ -33,12 +33,12 @@ import java.io.File; import java.io.FileNotFoundException; public class ExtractNativesTask extends DefaultTask { - @TaskAction - public void extractNatives() throws FileNotFoundException { - if (!Constants.MINECRAFT_NATIVES.exists()) { - for (File source : getProject().getConfigurations().getByName(Constants.CONFIG_NATIVES)) { - ZipUtil.unpack(source, Constants.MINECRAFT_NATIVES); - } - } - } + @TaskAction + public void extractNatives() throws FileNotFoundException { + if (!Constants.MINECRAFT_NATIVES.exists()) { + for (File source : getProject().getConfigurations().getByName(Constants.CONFIG_NATIVES)) { + ZipUtil.unpack(source, Constants.MINECRAFT_NATIVES); + } + } + } } diff --git a/src/main/java/net/fabricmc/loom/task/GenIdeaProjectTask.java b/src/main/java/net/fabricmc/loom/task/GenIdeaProjectTask.java index fdf3ebc..44a24fe 100644 --- a/src/main/java/net/fabricmc/loom/task/GenIdeaProjectTask.java +++ b/src/main/java/net/fabricmc/loom/task/GenIdeaProjectTask.java @@ -24,11 +24,11 @@ package net.fabricmc.loom.task; +import com.google.gson.Gson; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.util.Constants; import net.fabricmc.loom.util.IdeaRunConfig; import net.fabricmc.loom.util.Version; -import com.google.gson.Gson; import org.gradle.api.DefaultTask; import org.gradle.api.tasks.TaskAction; import org.w3c.dom.Document; @@ -51,142 +51,142 @@ import java.io.FileReader; import java.io.IOException; public class GenIdeaProjectTask extends DefaultTask { - @TaskAction - public void genIdeaRuns() throws IOException, ParserConfigurationException, SAXException, TransformerException { - LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class); + @TaskAction + public void genIdeaRuns() throws IOException, ParserConfigurationException, SAXException, TransformerException { + LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class); - File file = new File(getProject().getName() + ".iml"); - DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - Document doc = docBuilder.parse(file); + File file = new File(getProject().getName() + ".iml"); + DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); + Document doc = docBuilder.parse(file); - Node component = null; - NodeList module = doc.getElementsByTagName("module").item(0).getChildNodes(); - for (int i = 0; i < module.getLength(); i++) { - if (module.item(i).getNodeName().equals("component")) { - component = module.item(i); - break; - } - } + Node component = null; + NodeList module = doc.getElementsByTagName("module").item(0).getChildNodes(); + for (int i = 0; i < module.getLength(); i++) { + if (module.item(i).getNodeName().equals("component")) { + component = module.item(i); + break; + } + } - if (component == null) { - this.getLogger().lifecycle(":failed to generate intellij run configurations"); - return; - } + if (component == null) { + this.getLogger().lifecycle(":failed to generate intellij run configurations"); + return; + } - Node content = null; - NodeList moduleList = component.getChildNodes(); + Node content = null; + NodeList moduleList = component.getChildNodes(); - for (int i = 0; i < moduleList.getLength(); i++) { - if (moduleList.item(i).getNodeName().equals("content")) { - content = moduleList.item(i); - } - } + for (int i = 0; i < moduleList.getLength(); i++) { + if (moduleList.item(i).getNodeName().equals("content")) { + content = moduleList.item(i); + } + } - if (content == null) { - this.getLogger().lifecycle(":failed to generate intellij run configurations"); - return; - } + if (content == null) { + this.getLogger().lifecycle(":failed to generate intellij run configurations"); + return; + } - Element sourceFolder = doc.createElement("sourceFolder"); - sourceFolder.setAttribute("url", "file://$MODULE_DIR$/minecraft/src/main/java"); - sourceFolder.setAttribute("isTestSource", "false"); - content.appendChild(sourceFolder); + Element sourceFolder = doc.createElement("sourceFolder"); + sourceFolder.setAttribute("url", "file://$MODULE_DIR$/minecraft/src/main/java"); + sourceFolder.setAttribute("isTestSource", "false"); + content.appendChild(sourceFolder); - sourceFolder = doc.createElement("sourceFolder"); - sourceFolder.setAttribute("url", "file://$MODULE_DIR$/minecraft/src/main/resources"); - sourceFolder.setAttribute("type", "java-resource"); - content.appendChild(sourceFolder); + sourceFolder = doc.createElement("sourceFolder"); + sourceFolder.setAttribute("url", "file://$MODULE_DIR$/minecraft/src/main/resources"); + sourceFolder.setAttribute("type", "java-resource"); + content.appendChild(sourceFolder); - Gson gson = new Gson(); + Gson gson = new Gson(); - Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class); + Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class); - for (Version.Library library : version.libraries) { - if (library.allowed() && library.getFile() != null && library.getFile().exists()) { - Element node = doc.createElement("orderEntry"); - node.setAttribute("type", "module-library"); - Element libraryElement = doc.createElement("library"); - Element classes = doc.createElement("CLASSES"); - Element javadoc = doc.createElement("JAVADOC"); - Element sources = doc.createElement("SOURCES"); - Element root = doc.createElement("root"); - root.setAttribute("url", "jar://" + library.getFile().getAbsolutePath() + "!/"); - classes.appendChild(root); - libraryElement.appendChild(classes); - libraryElement.appendChild(javadoc); - libraryElement.appendChild(sources); - node.appendChild(libraryElement); - component.appendChild(node); - } else if (!library.allowed()) { - this.getLogger().info(":" + library.getFile().getName() + " is not allowed on this os"); - } - } + for (Version.Library library : version.libraries) { + if (library.allowed() && library.getFile() != null && library.getFile().exists()) { + Element node = doc.createElement("orderEntry"); + node.setAttribute("type", "module-library"); + Element libraryElement = doc.createElement("library"); + Element classes = doc.createElement("CLASSES"); + Element javadoc = doc.createElement("JAVADOC"); + Element sources = doc.createElement("SOURCES"); + Element root = doc.createElement("root"); + root.setAttribute("url", "jar://" + library.getFile().getAbsolutePath() + "!/"); + classes.appendChild(root); + libraryElement.appendChild(classes); + libraryElement.appendChild(javadoc); + libraryElement.appendChild(sources); + node.appendChild(libraryElement); + component.appendChild(node); + } else if (!library.allowed()) { + this.getLogger().info(":" + library.getFile().getName() + " is not allowed on this os"); + } + } - Element node = doc.createElement("orderEntry"); - node.setAttribute("type", "module-library"); - Element libraryElement = doc.createElement("library"); - Element classes = doc.createElement("CLASSES"); - Element javadoc = doc.createElement("JAVADOC"); - Element sources = doc.createElement("SOURCES"); - Element root = doc.createElement("root"); - root.setAttribute("url", "jar://" + Constants.MINECRAFT_CLIENT_MAPPED_JAR.get(extension).getAbsolutePath() + "!/"); - classes.appendChild(root); - libraryElement.appendChild(classes); - libraryElement.appendChild(javadoc); - libraryElement.appendChild(sources); - node.appendChild(libraryElement); - component.appendChild(node); + Element node = doc.createElement("orderEntry"); + node.setAttribute("type", "module-library"); + Element libraryElement = doc.createElement("library"); + Element classes = doc.createElement("CLASSES"); + Element javadoc = doc.createElement("JAVADOC"); + Element sources = doc.createElement("SOURCES"); + Element root = doc.createElement("root"); + root.setAttribute("url", "jar://" + Constants.MINECRAFT_MAPPED_JAR.get(extension).getAbsolutePath() + "!/"); + classes.appendChild(root); + libraryElement.appendChild(classes); + libraryElement.appendChild(javadoc); + libraryElement.appendChild(sources); + node.appendChild(libraryElement); + component.appendChild(node); - TransformerFactory transformerFactory = TransformerFactory.newInstance(); - Transformer transformer = transformerFactory.newTransformer(); - DOMSource source = new DOMSource(doc); - StreamResult result = new StreamResult(file); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); - transformer.transform(source, result); + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(doc); + StreamResult result = new StreamResult(file); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); + transformer.transform(source, result); - file = new File(getProject().getName() + ".iws"); - docFactory = DocumentBuilderFactory.newInstance(); - docBuilder = docFactory.newDocumentBuilder(); - doc = docBuilder.parse(file); + file = new File(getProject().getName() + ".iws"); + docFactory = DocumentBuilderFactory.newInstance(); + docBuilder = docFactory.newDocumentBuilder(); + doc = docBuilder.parse(file); - NodeList list = doc.getElementsByTagName("component"); - Element runManager = null; - for (int i = 0; i < list.getLength(); i++) { - Element element = (Element) list.item(i); - if (element.getAttribute("name").equals("RunManager")) { - runManager = element; - break; - } - } + NodeList list = doc.getElementsByTagName("component"); + Element runManager = null; + for (int i = 0; i < list.getLength(); i++) { + Element element = (Element) list.item(i); + if (element.getAttribute("name").equals("RunManager")) { + runManager = element; + break; + } + } - if (runManager == null) { - this.getLogger().lifecycle(":failed to generate intellij run configurations"); - return; - } + if (runManager == null) { + this.getLogger().lifecycle(":failed to generate intellij run configurations"); + return; + } - IdeaRunConfig ideaClient = new IdeaRunConfig(); - ideaClient.mainClass = "net.minecraft.launchwrapper.Launch"; - ideaClient.projectName = getProject().getName(); - ideaClient.configName = "Minecraft Client"; - ideaClient.runDir = "file://$PROJECT_DIR$/" + extension.runDir; - ideaClient.vmArgs = "-Djava.library.path=../.gradle/minecraft/natives/ -Dfabric.development=true"; - ideaClient.programArgs = "--tweakClass net.fabricmc.base.launch.FabricClientTweaker --assetIndex " + version.assetIndex.id + " --assetsDir " + new File(Constants.CACHE_FILES, "assets").getAbsolutePath(); + IdeaRunConfig ideaClient = new IdeaRunConfig(); + ideaClient.mainClass = "net.minecraft.launchwrapper.Launch"; + ideaClient.projectName = getProject().getName(); + ideaClient.configName = "Minecraft Client"; + ideaClient.runDir = "file://$PROJECT_DIR$/" + extension.runDir; + ideaClient.vmArgs = "-Djava.library.path=../.gradle/minecraft/natives/ -Dfabric.development=true"; + ideaClient.programArgs = "--tweakClass net.fabricmc.base.launch.FabricClientTweaker --assetIndex " + version.assetIndex.id + " --assetsDir " + new File(Constants.CACHE_FILES, "assets").getAbsolutePath(); - runManager.appendChild(ideaClient.genRuns(runManager)); + runManager.appendChild(ideaClient.genRuns(runManager)); - transformerFactory = TransformerFactory.newInstance(); - transformer = transformerFactory.newTransformer(); - source = new DOMSource(doc); - result = new StreamResult(file); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); - transformer.transform(source, result); + transformerFactory = TransformerFactory.newInstance(); + transformer = transformerFactory.newTransformer(); + source = new DOMSource(doc); + result = new StreamResult(file); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); + transformer.transform(source, result); - File runDir = new File(Constants.WORKING_DIRECTORY, extension.runDir); - if (!runDir.exists()) { - runDir.mkdirs(); - } - } + File runDir = new File(Constants.WORKING_DIRECTORY, extension.runDir); + if (!runDir.exists()) { + runDir.mkdirs(); + } + } } diff --git a/src/main/java/net/fabricmc/loom/task/MapJarsTask.java b/src/main/java/net/fabricmc/loom/task/MapJarsTask.java index d9dd9e9..ddb4fa1 100644 --- a/src/main/java/net/fabricmc/loom/task/MapJarsTask.java +++ b/src/main/java/net/fabricmc/loom/task/MapJarsTask.java @@ -40,50 +40,46 @@ import java.util.jar.JarFile; public class MapJarsTask extends DefaultTask { - Deobfuscator deobfuscator; + Deobfuscator deobfuscator; - @TaskAction - public void mapJars() throws IOException, MappingParseException { - LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class); - if(Constants.MINECRAFT_CLIENT_MAPPED_JAR.get(extension).exists()){ - Constants.MINECRAFT_CLIENT_MAPPED_JAR.get(extension).delete(); - } - this.getLogger().lifecycle(":unpacking mappings"); - if(Constants.MAPPINGS_DIR.exists()){ - FileUtils.deleteDirectory(Constants.MAPPINGS_DIR); - } - ZipUtil.unpack(Constants.MAPPINGS_ZIP, Constants.MAPPINGS_DIR); + @TaskAction + public void mapJars() throws IOException, MappingParseException { + LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class); + this.getLogger().lifecycle(":unpacking mappings"); + if (Constants.MAPPINGS_DIR.exists()) { + FileUtils.deleteDirectory(Constants.MAPPINGS_DIR); + } + ZipUtil.unpack(Constants.MAPPINGS_ZIP, Constants.MAPPINGS_DIR); - this.getLogger().lifecycle(":remapping jar"); - deobfuscator = new Deobfuscator(new JarFile(Constants.MINECRAFT_CLIENT_JAR.get(extension))); - this.deobfuscator.setMappings(new MappingsEnigmaReader().read(new File(Constants.MAPPINGS_DIR, "pomf-master" + File.separator + "mappings"))); - this.deobfuscator.writeJar(Constants.MINECRAFT_CLIENT_MAPPED_JAR.get(extension), new ProgressListener()); + this.getLogger().lifecycle(":remapping jar"); + deobfuscator = new Deobfuscator(new JarFile(Constants.MINECRAFT_MERGED_JAR.get(extension))); + this.deobfuscator.setMappings(new MappingsEnigmaReader().read(new File(Constants.MAPPINGS_DIR, "pomf-master" + File.separator + "mappings"))); + this.deobfuscator.writeJar(Constants.MINECRAFT_MAPPED_JAR.get(extension), new ProgressListener()); - File tempAssests = new File(Constants.CACHE_FILES, "tempAssets"); + File tempAssests = new File(Constants.CACHE_FILES, "tempAssets"); - ZipUtil.unpack(Constants.MINECRAFT_CLIENT_JAR.get(extension), tempAssests, name -> { - if (name.startsWith("assets") || name.startsWith("log4j2.xml")) { - return name; - } else { - return null; - } - }); - ZipUtil.unpack(Constants.MINECRAFT_CLIENT_MAPPED_JAR.get(extension), tempAssests); + ZipUtil.unpack(Constants.MINECRAFT_CLIENT_JAR.get(extension), tempAssests, name -> { + if (name.startsWith("assets") || name.startsWith("log4j2.xml")) { + return name; + } else { + return null; + } + }); + ZipUtil.unpack(Constants.MINECRAFT_MAPPED_JAR.get(extension), tempAssests); - ZipUtil.pack(tempAssests, Constants.MINECRAFT_CLIENT_MAPPED_JAR.get(extension)); - } + ZipUtil.pack(tempAssests, Constants.MINECRAFT_MAPPED_JAR.get(extension)); + } - public static class ProgressListener implements Deobfuscator.ProgressListener { - @Override - public void init(int i, String s) { + public static class ProgressListener implements Deobfuscator.ProgressListener { + @Override + public void init(int i, String s) { - } + } - @Override - public void onProgress(int i, String s) { - - } - } + @Override + public void onProgress(int i, String s) { + } + } } diff --git a/src/main/java/net/fabricmc/loom/task/MergeJarsTask.java b/src/main/java/net/fabricmc/loom/task/MergeJarsTask.java new file mode 100644 index 0000000..6001058 --- /dev/null +++ b/src/main/java/net/fabricmc/loom/task/MergeJarsTask.java @@ -0,0 +1,59 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2016 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.task; + +import net.fabricmc.blendingjar.JarMerger; +import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.util.Constants; +import org.gradle.api.DefaultTask; +import org.gradle.api.tasks.TaskAction; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +public class MergeJarsTask extends DefaultTask { + + @TaskAction + public void mergeJars() throws IOException { + this.getLogger().lifecycle(":merging jars"); + LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class); + + FileInputStream client = new FileInputStream(Constants.MINECRAFT_CLIENT_JAR.get(extension)); + FileInputStream server = new FileInputStream(Constants.MINECRAFT_SERVER_JAR.get(extension)); + FileOutputStream merged = new FileOutputStream(Constants.MINECRAFT_MERGED_JAR.get(extension)); + + JarMerger jarMerger = new JarMerger(client, server, merged); + + jarMerger.merge(); + jarMerger.close(); + + client.close(); + server.close(); + merged.close(); + + } + +} diff --git a/src/main/java/net/fabricmc/loom/util/Checksum.java b/src/main/java/net/fabricmc/loom/util/Checksum.java index 9a6ab22..2cd718f 100644 --- a/src/main/java/net/fabricmc/loom/util/Checksum.java +++ b/src/main/java/net/fabricmc/loom/util/Checksum.java @@ -32,20 +32,20 @@ import java.io.File; import java.io.IOException; public class Checksum { - public static boolean equals(File file, String checksum) { - if (file == null) { - return false; - } - try { - HashCode hash = Files.hash(file, Hashing.sha1()); - StringBuilder builder = new StringBuilder(); - for (Byte hashBytes : hash.asBytes()) { - builder.append(Integer.toString((hashBytes & 0xFF) + 0x100, 16).substring(1)); - } - return builder.toString().equals(checksum); - } catch (IOException e) { - e.printStackTrace(); - } - return false; - } + public static boolean equals(File file, String checksum) { + if (file == null) { + return false; + } + try { + HashCode hash = Files.hash(file, Hashing.sha1()); + StringBuilder builder = new StringBuilder(); + for (Byte hashBytes : hash.asBytes()) { + builder.append(Integer.toString((hashBytes & 0xFF) + 0x100, 16).substring(1)); + } + return builder.toString().equals(checksum); + } catch (IOException e) { + e.printStackTrace(); + } + return false; + } } diff --git a/src/main/java/net/fabricmc/loom/util/Constants.java b/src/main/java/net/fabricmc/loom/util/Constants.java index c326ee7..2239c7e 100644 --- a/src/main/java/net/fabricmc/loom/util/Constants.java +++ b/src/main/java/net/fabricmc/loom/util/Constants.java @@ -24,8 +24,8 @@ package net.fabricmc.loom.util; -import net.fabricmc.loom.util.delayed.IDelayed; import net.fabricmc.loom.util.delayed.DelayedFile; +import net.fabricmc.loom.util.delayed.IDelayed; import java.io.File; import java.net.URL; @@ -34,40 +34,42 @@ import java.util.ArrayList; import java.util.List; public class Constants { - public static final File WORKING_DIRECTORY = new File("."); - public static final File CACHE_FILES = new File(WORKING_DIRECTORY, ".gradle/minecraft"); + public static final File WORKING_DIRECTORY = new File("."); + public static final File CACHE_FILES = new File(WORKING_DIRECTORY, ".gradle/minecraft"); - public static final IDelayed MINECRAFT_CLIENT_JAR = new DelayedFile(extension -> new File(CACHE_FILES, extension.version + "-client.jar")); - public static final IDelayed MINECRAFT_CLIENT_MAPPED_JAR = new DelayedFile(extension -> new File(CACHE_FILES, extension.version + "-client-mapped.jar")); + public static final IDelayed MINECRAFT_CLIENT_JAR = new DelayedFile(extension -> new File(CACHE_FILES, extension.version + "-client.jar")); + public static final IDelayed MINECRAFT_SERVER_JAR = new DelayedFile(extension -> new File(CACHE_FILES, extension.version + "-server.jar")); + public static final IDelayed MINECRAFT_MERGED_JAR = new DelayedFile(extension -> new File(CACHE_FILES, extension.version + "-merged.jar")); + public static final IDelayed MINECRAFT_MAPPED_JAR = new DelayedFile(extension -> new File(CACHE_FILES, extension.version + "-mapped.jar")); - public static final File MAPPINGS_ZIP = new File(CACHE_FILES, "mappings.zip"); - public static final File MAPPINGS_DIR = new File(CACHE_FILES, "mappings"); + public static final File MAPPINGS_ZIP = new File(CACHE_FILES, "mappings.zip"); + public static final File MAPPINGS_DIR = new File(CACHE_FILES, "mappings"); - public static final File MINECRAFT_LIBS = new File(CACHE_FILES, "libs"); - public static final File MINECRAFT_NATIVES = new File(CACHE_FILES, "natives"); - public static final IDelayed MINECRAFT_JSON = new DelayedFile(extension -> new File(CACHE_FILES, extension.version + "-info.json")); + public static final File MINECRAFT_LIBS = new File(CACHE_FILES, "libs"); + public static final File MINECRAFT_NATIVES = new File(CACHE_FILES, "natives"); + public static final IDelayed MINECRAFT_JSON = new DelayedFile(extension -> new File(CACHE_FILES, extension.version + "-info.json")); - public static final File MINECRAFT_ROOT = new File(WORKING_DIRECTORY, "minecraft"); - public static final IDelayed MAPPING_SRG = new DelayedFile(extension -> new File(WORKING_DIRECTORY, "mappings.srg")); + public static final File MINECRAFT_ROOT = new File(WORKING_DIRECTORY, "minecraft"); + public static final IDelayed MAPPING_SRG = new DelayedFile(extension -> new File(WORKING_DIRECTORY, "mappings.srg")); - public static final File VERSION_MANIFEST = new File(CACHE_FILES, "version_manifest.json"); + public static final File VERSION_MANIFEST = new File(CACHE_FILES, "version_manifest.json"); - public static final String LIBRARIES_BASE = "https://libraries.minecraft.net/"; - public static final String RESOURCES_BASE = "http://resources.download.minecraft.net/"; + public static final String LIBRARIES_BASE = "https://libraries.minecraft.net/"; + public static final String RESOURCES_BASE = "http://resources.download.minecraft.net/"; - public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"; + public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"; - public static final String CONFIG_NATIVES = "MC_NATIVES"; - public static final String CONFIG_MC_DEPENDENCIES = "MC_DEPENDENCIES"; - public static final String CONFIG_MC_DEPENDENCIES_CLIENT = "MC_DEPENDENCIES_CLIENT"; - public static final String SYSTEM_ARCH = System.getProperty("os.arch").equals("64") ? "64" : "32"; + public static final String CONFIG_NATIVES = "MC_NATIVES"; + public static final String CONFIG_MC_DEPENDENCIES = "MC_DEPENDENCIES"; + public static final String CONFIG_MC_DEPENDENCIES_CLIENT = "MC_DEPENDENCIES_CLIENT"; + public static final String SYSTEM_ARCH = System.getProperty("os.arch").equals("64") ? "64" : "32"; - public static List getClassPath() { - URL[] urls = ((URLClassLoader) Constants.class.getClassLoader()).getURLs(); - ArrayList list = new ArrayList<>(); - for (URL url : urls) { - list.add(url.getPath()); - } - return list; - } + public static List getClassPath() { + URL[] urls = ((URLClassLoader) Constants.class.getClassLoader()).getURLs(); + ArrayList list = new ArrayList<>(); + for (URL url : urls) { + list.add(url.getPath()); + } + return list; + } } diff --git a/src/main/java/net/fabricmc/loom/util/IdeaRunConfig.java b/src/main/java/net/fabricmc/loom/util/IdeaRunConfig.java index 681d43b..6f3bf0e 100644 --- a/src/main/java/net/fabricmc/loom/util/IdeaRunConfig.java +++ b/src/main/java/net/fabricmc/loom/util/IdeaRunConfig.java @@ -36,42 +36,42 @@ import java.io.IOException; import java.util.Map; public class IdeaRunConfig { - public String configName; - public String projectName; - public String mainClass; - public String runDir; - public String vmArgs; - public String programArgs; + public String configName; + public String projectName; + public String mainClass; + public String runDir; + public String vmArgs; + public String programArgs; - public Element genRuns(Element doc) throws IOException, ParserConfigurationException, TransformerException { - Element root = this.addXml(doc, "component", ImmutableMap.of("name", "ProjectRunConfigurationManager")); - root = addXml(root, "configuration", ImmutableMap.of("default", "false", "name", configName, "type", "Application", "factoryName", "Application")); + public Element genRuns(Element doc) throws IOException, ParserConfigurationException, TransformerException { + Element root = this.addXml(doc, "component", ImmutableMap.of("name", "ProjectRunConfigurationManager")); + root = addXml(root, "configuration", ImmutableMap.of("default", "false", "name", configName, "type", "Application", "factoryName", "Application")); - this.addXml(root, "module", ImmutableMap.of("name", projectName)); - this.addXml(root, "option", ImmutableMap.of("name", "MAIN_CLASS_NAME", "value", mainClass)); - this.addXml(root, "option", ImmutableMap.of("name", "WORKING_DIRECTORY", "value", runDir)); + this.addXml(root, "module", ImmutableMap.of("name", projectName)); + this.addXml(root, "option", ImmutableMap.of("name", "MAIN_CLASS_NAME", "value", mainClass)); + this.addXml(root, "option", ImmutableMap.of("name", "WORKING_DIRECTORY", "value", runDir)); - if (!Strings.isNullOrEmpty(vmArgs)) { - this.addXml(root, "option", ImmutableMap.of("name", "VM_PARAMETERS", "value", vmArgs)); - } + if (!Strings.isNullOrEmpty(vmArgs)) { + this.addXml(root, "option", ImmutableMap.of("name", "VM_PARAMETERS", "value", vmArgs)); + } - if (!Strings.isNullOrEmpty(programArgs)) { - this.addXml(root, "option", ImmutableMap.of("name", "PROGRAM_PARAMETERS", "value", programArgs)); - } - return root; - } + if (!Strings.isNullOrEmpty(programArgs)) { + this.addXml(root, "option", ImmutableMap.of("name", "PROGRAM_PARAMETERS", "value", programArgs)); + } + return root; + } - public Element addXml(Node parent, String name, Map values) { - Document doc = parent.getOwnerDocument(); - if (doc == null) { - doc = (Document) parent; - } + public Element addXml(Node parent, String name, Map values) { + Document doc = parent.getOwnerDocument(); + if (doc == null) { + doc = (Document) parent; + } - Element e = doc.createElement(name); - for (Map.Entry entry : values.entrySet()) { - e.setAttribute(entry.getKey(), entry.getValue()); - } - parent.appendChild(e); - return e; - } + Element e = doc.createElement(name); + for (Map.Entry entry : values.entrySet()) { + e.setAttribute(entry.getKey(), entry.getValue()); + } + parent.appendChild(e); + return e; + } } \ No newline at end of file diff --git a/src/main/java/net/fabricmc/loom/util/ManifestVersion.java b/src/main/java/net/fabricmc/loom/util/ManifestVersion.java index d18fcf2..9340802 100644 --- a/src/main/java/net/fabricmc/loom/util/ManifestVersion.java +++ b/src/main/java/net/fabricmc/loom/util/ManifestVersion.java @@ -28,9 +28,9 @@ import java.util.ArrayList; import java.util.List; public class ManifestVersion { - public List versions = new ArrayList<>(); + public List versions = new ArrayList<>(); - public static class Versions { - public String id, url; - } + public static class Versions { + public String id, url; + } } diff --git a/src/main/java/net/fabricmc/loom/util/OperatingSystem.java b/src/main/java/net/fabricmc/loom/util/OperatingSystem.java index 21692ed..4c5a6d7 100644 --- a/src/main/java/net/fabricmc/loom/util/OperatingSystem.java +++ b/src/main/java/net/fabricmc/loom/util/OperatingSystem.java @@ -25,26 +25,26 @@ package net.fabricmc.loom.util; public class OperatingSystem { - public static String getOS() { - String osName = System.getProperty("os.name").toLowerCase(); - if (osName.contains("win")) { - return "windows"; - } else if (osName.contains("mac")) { - return "osx"; - } else { - return "linux"; - } - } + public static String getOS() { + String osName = System.getProperty("os.name").toLowerCase(); + if (osName.contains("win")) { + return "windows"; + } else if (osName.contains("mac")) { + return "osx"; + } else { + return "linux"; + } + } - public static String getArch() { - if (is64Bit()) { - return "64"; - } else { - return "32"; - } - } + public static String getArch() { + if (is64Bit()) { + return "64"; + } else { + return "32"; + } + } - public static boolean is64Bit() { - return System.getProperty("sun.arch.data.model").contains("64"); - } + public static boolean is64Bit() { + return System.getProperty("sun.arch.data.model").contains("64"); + } } diff --git a/src/main/java/net/fabricmc/loom/util/Version.java b/src/main/java/net/fabricmc/loom/util/Version.java index 90d879d..8b0b9ea 100644 --- a/src/main/java/net/fabricmc/loom/util/Version.java +++ b/src/main/java/net/fabricmc/loom/util/Version.java @@ -31,129 +31,129 @@ import java.util.List; import java.util.Map; public class Version { - public List libraries; - public Map downloads; - public AssetIndex assetIndex; + public List libraries; + public Map downloads; + public AssetIndex assetIndex; - public class Downloads { - public String url; - public String sha1; - } + public class Downloads { + public String url; + public String sha1; + } - public class AssetIndex { - public String id; - public String sha1; - public String url; - } + public class AssetIndex { + public String id; + public String sha1; + public String url; + } - public class Library { - public String name; - public JsonObject natives; - public JsonObject downloads; - private Artifact artifact; - public Rule[] rules; + public class Library { + public String name; + public JsonObject natives; + public JsonObject downloads; + private Artifact artifact; + public Rule[] rules; - public String getURL() { - String path; - String[] parts = this.name.split(":", 3); - path = parts[0].replace(".", "/") + "/" + parts[1] + "/" + parts[2] + "/" + parts[1] + "-" + parts[2] + getClassifier() + ".jar"; - return Constants.LIBRARIES_BASE + path; - } + public String getURL() { + String path; + String[] parts = this.name.split(":", 3); + path = parts[0].replace(".", "/") + "/" + parts[1] + "/" + parts[2] + "/" + parts[1] + "-" + parts[2] + getClassifier() + ".jar"; + return Constants.LIBRARIES_BASE + path; + } - public File getFile() { - String[] parts = this.name.split(":", 3); - return new File(Constants.MINECRAFT_LIBS, parts[0].replace(".", File.separator) + File.separator + parts[1] + File.separator + parts[2] + File.separator + parts[1] + "-" + parts[2] + getClassifier() + ".jar"); - } + public File getFile() { + String[] parts = this.name.split(":", 3); + return new File(Constants.MINECRAFT_LIBS, parts[0].replace(".", File.separator) + File.separator + parts[1] + File.separator + parts[2] + File.separator + parts[1] + "-" + parts[2] + getClassifier() + ".jar"); + } - public String getSha1() { - if (this.downloads == null) { - return ""; - } else if (this.downloads.getAsJsonObject("artifact") == null) { - return ""; - } else if (this.downloads.getAsJsonObject("artifact").get("sha1") == null) { - return ""; - } else { - return this.downloads.getAsJsonObject("artifact").get("sha1").getAsString(); - } - } + public String getSha1() { + if (this.downloads == null) { + return ""; + } else if (this.downloads.getAsJsonObject("artifact") == null) { + return ""; + } else if (this.downloads.getAsJsonObject("artifact").get("sha1") == null) { + return ""; + } else { + return this.downloads.getAsJsonObject("artifact").get("sha1").getAsString(); + } + } - public String getClassifier() { - if (natives == null) { - return ""; - } else { - return "-" + natives.get(OperatingSystem.getOS().replace("${arch}", OperatingSystem.getArch())).getAsString().replace("\"", ""); - } - } + public String getClassifier() { + if (natives == null) { + return ""; + } else { + return "-" + natives.get(OperatingSystem.getOS().replace("${arch}", OperatingSystem.getArch())).getAsString().replace("\"", ""); + } + } - public boolean allowed() { - if (this.rules == null || this.rules.length <= 0) { - return true; - } + public boolean allowed() { + if (this.rules == null || this.rules.length <= 0) { + return true; + } - boolean success = false; - for (Rule rule : this.rules) { - if (rule.os != null && rule.os.name != null) { - if (rule.os.name.equalsIgnoreCase(OperatingSystem.getOS())) { - return rule.action.equalsIgnoreCase("allow"); - } - } else { - success = rule.action.equalsIgnoreCase("allow"); - } - } - return success; - } + boolean success = false; + for (Rule rule : this.rules) { + if (rule.os != null && rule.os.name != null) { + if (rule.os.name.equalsIgnoreCase(OperatingSystem.getOS())) { + return rule.action.equalsIgnoreCase("allow"); + } + } else { + success = rule.action.equalsIgnoreCase("allow"); + } + } + return success; + } - public String getArtifactName() { - if (artifact == null) { - artifact = new Artifact(name); - } - return artifact.getArtifact(natives == null ? artifact.getClassifier() : natives.get(OperatingSystem.getOS()).getAsString()); - } + public String getArtifactName() { + if (artifact == null) { + artifact = new Artifact(name); + } + return artifact.getArtifact(natives == null ? artifact.getClassifier() : natives.get(OperatingSystem.getOS()).getAsString()); + } - private class Artifact { - private final String domain, name, version, classifier, ext; + private class Artifact { + private final String domain, name, version, classifier, ext; - public Artifact(String name) { - String[] splitedArtifact = name.split(":"); - int idx = splitedArtifact[splitedArtifact.length - 1].indexOf('@'); - if (idx != -1) { - ext = splitedArtifact[splitedArtifact.length - 1].substring(idx + 1); - splitedArtifact[splitedArtifact.length - 1] = splitedArtifact[splitedArtifact.length - 1].substring(0, idx); - } else { - ext = "jar"; - } - this.domain = splitedArtifact[0]; - this.name = splitedArtifact[1]; - this.version = splitedArtifact[2]; - this.classifier = splitedArtifact.length > 3 ? splitedArtifact[3] : null; - } + public Artifact(String name) { + String[] splitedArtifact = name.split(":"); + int idx = splitedArtifact[splitedArtifact.length - 1].indexOf('@'); + if (idx != -1) { + ext = splitedArtifact[splitedArtifact.length - 1].substring(idx + 1); + splitedArtifact[splitedArtifact.length - 1] = splitedArtifact[splitedArtifact.length - 1].substring(0, idx); + } else { + ext = "jar"; + } + this.domain = splitedArtifact[0]; + this.name = splitedArtifact[1]; + this.version = splitedArtifact[2]; + this.classifier = splitedArtifact.length > 3 ? splitedArtifact[3] : null; + } - public String getArtifact(String classifier) { - String ret = domain + ":" + name + ":" + version; - if (classifier != null && classifier.indexOf('$') > -1) { - classifier = classifier.replace("${arch}", Constants.SYSTEM_ARCH); - } - if (classifier != null) { - ret += ":" + classifier; - } - if (!"jar".equals(ext)) { - ret += "@" + ext; - } - return ret; - } + public String getArtifact(String classifier) { + String ret = domain + ":" + name + ":" + version; + if (classifier != null && classifier.indexOf('$') > -1) { + classifier = classifier.replace("${arch}", Constants.SYSTEM_ARCH); + } + if (classifier != null) { + ret += ":" + classifier; + } + if (!"jar".equals(ext)) { + ret += "@" + ext; + } + return ret; + } - public String getClassifier() { - return classifier; - } - } - } + public String getClassifier() { + return classifier; + } + } + } - private class Rule { - public String action; - public OS os; + private class Rule { + public String action; + public OS os; - private class OS { - String name; - } - } + private class OS { + String name; + } + } } diff --git a/src/main/java/net/fabricmc/loom/util/assets/AssetIndex.java b/src/main/java/net/fabricmc/loom/util/assets/AssetIndex.java index 8e111cc..c3c5fb0 100644 --- a/src/main/java/net/fabricmc/loom/util/assets/AssetIndex.java +++ b/src/main/java/net/fabricmc/loom/util/assets/AssetIndex.java @@ -30,22 +30,22 @@ import java.util.Map; import java.util.Set; public class AssetIndex { - private final Map objects; - private boolean virtual; + private final Map objects; + private boolean virtual; - public AssetIndex() { - this.objects = new LinkedHashMap<>(); - } + public AssetIndex() { + this.objects = new LinkedHashMap<>(); + } - public Map getFileMap() { - return this.objects; - } + public Map getFileMap() { + return this.objects; + } - public Set getUniqueObjects() { - return new HashSet<>(this.objects.values()); - } + public Set getUniqueObjects() { + return new HashSet<>(this.objects.values()); + } - public boolean isVirtual() { - return this.virtual; - } + public boolean isVirtual() { + return this.virtual; + } } diff --git a/src/main/java/net/fabricmc/loom/util/assets/AssetObject.java b/src/main/java/net/fabricmc/loom/util/assets/AssetObject.java index ead0e3b..19af8a9 100644 --- a/src/main/java/net/fabricmc/loom/util/assets/AssetObject.java +++ b/src/main/java/net/fabricmc/loom/util/assets/AssetObject.java @@ -25,33 +25,33 @@ package net.fabricmc.loom.util.assets; public class AssetObject { - private String hash; - private long size; + private String hash; + private long size; - public String getHash() { - return this.hash; - } + public String getHash() { + return this.hash; + } - public long getSize() { - return this.size; - } + public long getSize() { + return this.size; + } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } else if ((o == null) || (getClass() != o.getClass())) { - return false; - } else { - AssetObject that = (AssetObject) o; - return this.size == that.size && this.hash.equals(that.hash); - } - } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } else if ((o == null) || (getClass() != o.getClass())) { + return false; + } else { + AssetObject that = (AssetObject) o; + return this.size == that.size && this.hash.equals(that.hash); + } + } - @Override - public int hashCode() { - int result = this.hash.hashCode(); - result = 31 * result + (int) (this.size ^ this.size >>> 32); - return result; - } + @Override + public int hashCode() { + int result = this.hash.hashCode(); + result = 31 * result + (int) (this.size ^ this.size >>> 32); + return result; + } } \ No newline at end of file diff --git a/src/main/java/net/fabricmc/loom/util/delayed/DelayedFile.java b/src/main/java/net/fabricmc/loom/util/delayed/DelayedFile.java index 20e144f..7932d56 100644 --- a/src/main/java/net/fabricmc/loom/util/delayed/DelayedFile.java +++ b/src/main/java/net/fabricmc/loom/util/delayed/DelayedFile.java @@ -30,18 +30,18 @@ import java.io.File; import java.util.function.Function; public class DelayedFile implements IDelayed { - private File file; - private Function function; + private File file; + private Function function; - public DelayedFile(Function function) { - this.function = function; - } + public DelayedFile(Function function) { + this.function = function; + } - @Override - public File get(LoomGradleExtension extension) { - if (this.file == null) { - this.file = this.function.apply(extension); - } - return this.file; - } + @Override + public File get(LoomGradleExtension extension) { + if (this.file == null) { + this.file = this.function.apply(extension); + } + return this.file; + } } diff --git a/src/main/java/net/fabricmc/loom/util/delayed/IDelayed.java b/src/main/java/net/fabricmc/loom/util/delayed/IDelayed.java index 97445ef..2f484a5 100644 --- a/src/main/java/net/fabricmc/loom/util/delayed/IDelayed.java +++ b/src/main/java/net/fabricmc/loom/util/delayed/IDelayed.java @@ -27,5 +27,5 @@ package net.fabricmc.loom.util.delayed; import net.fabricmc.loom.LoomGradleExtension; public interface IDelayed { - T get(LoomGradleExtension extension); + T get(LoomGradleExtension extension); } diff --git a/src/main/java/net/fabricmc/loom/util/progress/ProgressLogger.java b/src/main/java/net/fabricmc/loom/util/progress/ProgressLogger.java index 166e3ee..f03cfd1 100644 --- a/src/main/java/net/fabricmc/loom/util/progress/ProgressLogger.java +++ b/src/main/java/net/fabricmc/loom/util/progress/ProgressLogger.java @@ -33,198 +33,198 @@ import java.lang.reflect.Method; * Wrapper to ProgressLogger internal API */ public class ProgressLogger { - private final Object logger; - private final Method getDescription, setDescription, getShortDescription, setShortDescription, getLoggingHeader, setLoggingHeader, start, started, startedArg, progress, completed, completedArg; + private final Object logger; + private final Method getDescription, setDescription, getShortDescription, setShortDescription, getLoggingHeader, setLoggingHeader, start, started, startedArg, progress, completed, completedArg; - private ProgressLogger(Object logger) { - this.logger = logger; - this.getDescription = getMethod("getDescription"); - this.setDescription = getMethod("setDescription", String.class); - this.getShortDescription = getMethod("getShortDescription"); - this.setShortDescription = getMethod("setShortDescription", String.class); - this.getLoggingHeader = getMethod("getLoggingHeader"); - this.setLoggingHeader = getMethod("setLoggingHeader", String.class); - this.start = getMethod("start", String.class, String.class); - this.started = getMethod("started"); - this.startedArg = getMethod("started", String.class); - this.progress = getMethod("progress", String.class); - this.completed = getMethod("completed"); - this.completedArg = getMethod("completed", String.class); - } + private ProgressLogger(Object logger) { + this.logger = logger; + this.getDescription = getMethod("getDescription"); + this.setDescription = getMethod("setDescription", String.class); + this.getShortDescription = getMethod("getShortDescription"); + this.setShortDescription = getMethod("setShortDescription", String.class); + this.getLoggingHeader = getMethod("getLoggingHeader"); + this.setLoggingHeader = getMethod("setLoggingHeader", String.class); + this.start = getMethod("start", String.class, String.class); + this.started = getMethod("started"); + this.startedArg = getMethod("started", String.class); + this.progress = getMethod("progress", String.class); + this.completed = getMethod("completed"); + this.completedArg = getMethod("completed", String.class); + } - private static Class getFactoryClass() { - Class progressLoggerFactoryClass = null; - try { - //Gradle 2.14 and higher - progressLoggerFactoryClass = Class.forName("org.gradle.internal.logging.progress.ProgressLoggerFactory"); - } catch (ClassNotFoundException e) { - //prior to Gradle 2.14 - try { - progressLoggerFactoryClass = Class.forName("org.gradle.logging.ProgressLoggerFactory"); - } catch (ClassNotFoundException e1) { - // Unsupported Gradle version - } - } - return progressLoggerFactoryClass; - } + private static Class getFactoryClass() { + Class progressLoggerFactoryClass = null; + try { + //Gradle 2.14 and higher + progressLoggerFactoryClass = Class.forName("org.gradle.internal.logging.progress.ProgressLoggerFactory"); + } catch (ClassNotFoundException e) { + //prior to Gradle 2.14 + try { + progressLoggerFactoryClass = Class.forName("org.gradle.logging.ProgressLoggerFactory"); + } catch (ClassNotFoundException e1) { + // Unsupported Gradle version + } + } + return progressLoggerFactoryClass; + } - private Method getMethod(String methodName, Class... args) { - if (logger != null) { - try { - return logger.getClass().getMethod(methodName, args); - } catch (NoSuchMethodException ignored) { + private Method getMethod(String methodName, Class... args) { + if (logger != null) { + try { + return logger.getClass().getMethod(methodName, args); + } catch (NoSuchMethodException ignored) { - } - } - return null; - } + } + } + return null; + } - private Object invoke(Method method, Object... args) { - if (logger != null) { - try { - method.setAccessible(true); - return method.invoke(logger, args); - } catch (IllegalAccessException | InvocationTargetException ignored) { + private Object invoke(Method method, Object... args) { + if (logger != null) { + try { + method.setAccessible(true); + return method.invoke(logger, args); + } catch (IllegalAccessException | InvocationTargetException ignored) { - } - } - return null; - } + } + } + return null; + } - /** - * Get a Progress logger from the Gradle internal API - * - * @param project The project - * @param category The logger category - * @return In any case a progress logger - */ - public static ProgressLogger getProgressFactory(Project project, String category) { - try { - Method getServices = project.getClass().getMethod("getServices"); - Object serviceFactory = getServices.invoke(project); - Method get = serviceFactory.getClass().getMethod("get", Class.class); - Object progressLoggerFactory = get.invoke(serviceFactory, getFactoryClass()); - Method newOperation = progressLoggerFactory.getClass().getMethod("newOperation", String.class); - return new ProgressLogger(newOperation.invoke(progressLoggerFactory, category)); - } catch (Exception e) { - project.getLogger().error("Unable to get progress logger. Download progress will not be displayed."); - return new ProgressLogger(null); - } - } + /** + * Get a Progress logger from the Gradle internal API + * + * @param project The project + * @param category The logger category + * @return In any case a progress logger + */ + public static ProgressLogger getProgressFactory(Project project, String category) { + try { + Method getServices = project.getClass().getMethod("getServices"); + Object serviceFactory = getServices.invoke(project); + Method get = serviceFactory.getClass().getMethod("get", Class.class); + Object progressLoggerFactory = get.invoke(serviceFactory, getFactoryClass()); + Method newOperation = progressLoggerFactory.getClass().getMethod("newOperation", String.class); + return new ProgressLogger(newOperation.invoke(progressLoggerFactory, category)); + } catch (Exception e) { + project.getLogger().error("Unable to get progress logger. Download progress will not be displayed."); + return new ProgressLogger(null); + } + } - /** - * Returns the description of the operation. - * - * @return the description, must not be empty. - */ - public String getDescription() { - return (String) invoke(getDescription); - } + /** + * Returns the description of the operation. + * + * @return the description, must not be empty. + */ + public String getDescription() { + return (String) invoke(getDescription); + } - /** - *

Sets the description of the operation. This should be a full, stand-alone description of the operation. - *

- *

This must be called before {@link #started()}. - * - * @param description The description. - */ - public ProgressLogger setDescription(String description) { - invoke(setDescription, description); - return this; - } + /** + *

Sets the description of the operation. This should be a full, stand-alone description of the operation. + *

+ *

This must be called before {@link #started()}. + * + * @param description The description. + */ + public ProgressLogger setDescription(String description) { + invoke(setDescription, description); + return this; + } - /** - * Returns the short description of the operation. This is used in place of the full description when display space is limited. - * - * @return The short description, must not be empty. - */ - public String getShortDescription() { - return (String) invoke(getShortDescription); - } + /** + * Returns the short description of the operation. This is used in place of the full description when display space is limited. + * + * @return The short description, must not be empty. + */ + public String getShortDescription() { + return (String) invoke(getShortDescription); + } - /** - *

Sets the short description of the operation. This is used in place of the full description when display space is limited. - *

- *

This must be called before {@link #started()} - * - * @param description The short description. - */ - public ProgressLogger setShortDescription(String description) { - invoke(setShortDescription, description); - return this; - } + /** + *

Sets the short description of the operation. This is used in place of the full description when display space is limited. + *

+ *

This must be called before {@link #started()} + * + * @param description The short description. + */ + public ProgressLogger setShortDescription(String description) { + invoke(setShortDescription, description); + return this; + } - /** - *

Returns the logging header for the operation. This is logged before any other log messages for this operation are logged. It is usually - * also logged at the end of the operation, along with the final status message. Defaults to null. - *

- *

If not specified, no logging header is logged. - * - * @return The logging header, possibly empty. - */ - public String getLoggingHeader() { - return (String) invoke(getLoggingHeader); - } + /** + *

Returns the logging header for the operation. This is logged before any other log messages for this operation are logged. It is usually + * also logged at the end of the operation, along with the final status message. Defaults to null. + *

+ *

If not specified, no logging header is logged. + * + * @return The logging header, possibly empty. + */ + public String getLoggingHeader() { + return (String) invoke(getLoggingHeader); + } - /** - *

Sets the logging header for the operation. This is logged before any other log messages for this operation are logged. It is usually - * also logged at the end of the operation, along with the final status message. Defaults to null. - * - * @param header The header. May be empty or null. - */ - public ProgressLogger setLoggingHeader(String header) { - invoke(setLoggingHeader, header); - return this; - } + /** + *

Sets the logging header for the operation. This is logged before any other log messages for this operation are logged. It is usually + * also logged at the end of the operation, along with the final status message. Defaults to null. + * + * @param header The header. May be empty or null. + */ + public ProgressLogger setLoggingHeader(String header) { + invoke(setLoggingHeader, header); + return this; + } - /** - * Convenience method that sets descriptions and logs started() event. - * - * @return this logger instance - */ - public ProgressLogger start(String description, String shortDescription) { - invoke(start, description, shortDescription); - return this; - } + /** + * Convenience method that sets descriptions and logs started() event. + * + * @return this logger instance + */ + public ProgressLogger start(String description, String shortDescription) { + invoke(start, description, shortDescription); + return this; + } - /** - * Logs the start of the operation, with no initial status. - */ - public void started() { - invoke(started); - } + /** + * Logs the start of the operation, with no initial status. + */ + public void started() { + invoke(started); + } - /** - * Logs the start of the operation, with the given status. - * - * @param status The initial status message. Can be null or empty. - */ - public void started(String status) { - invoke(started, status); - } + /** + * Logs the start of the operation, with the given status. + * + * @param status The initial status message. Can be null or empty. + */ + public void started(String status) { + invoke(started, status); + } - /** - * Logs some progress, indicated by a new status. - * - * @param status The new status message. Can be null or empty. - */ - public void progress(String status) { - invoke(progress, status); - } + /** + * Logs some progress, indicated by a new status. + * + * @param status The new status message. Can be null or empty. + */ + public void progress(String status) { + invoke(progress, status); + } - /** - * Logs the completion of the operation, with no final status - */ - public void completed() { - invoke(completed); - } + /** + * Logs the completion of the operation, with no final status + */ + public void completed() { + invoke(completed); + } - /** - * Logs the completion of the operation, with a final status. This is generally logged along with the description. - * - * @param status The final status message. Can be null or empty. - */ - public void completed(String status) { - invoke(completed, status); - } + /** + * Logs the completion of the operation, with a final status. This is generally logged along with the description. + * + * @param status The final status message. Can be null or empty. + */ + public void completed(String status) { + invoke(completed, status); + } }