Rename things

This commit is contained in:
modmuss50
2016-08-16 19:19:05 +01:00
parent 451adbeeb6
commit d358f461fe
23 changed files with 76 additions and 79 deletions

View File

@@ -0,0 +1,118 @@
package net.fabric.loom;
import net.fabric.loom.util.Constants;
import com.google.common.collect.ImmutableMap;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
import org.gradle.plugins.ide.idea.model.IdeaModel;
public class AbstractPlugin implements Plugin<Project> {
protected Project project;
@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"));
project.getExtensions().create("minecraft", LoomGradleExtension.class);
// 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);
// 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();
}
/**
* 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 extends Task> T makeTask(String name, Class<T> 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 extends Task> T makeTask(Project target, String name, Class<T> 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);
});
}
/**
* 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));
// 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");
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)));
Javadoc javadoc = (Javadoc) project.getTasks().getByName(JavaPlugin.JAVADOC_TASK_NAME);
javadoc.setClasspath(main.getOutput().plus(main.getCompileClasspath()));
}
}

View File

@@ -0,0 +1,6 @@
package net.fabric.loom;
public class LoomGradleExtension {
public String version;
public String runDir = "run";
}

View File

@@ -0,0 +1,22 @@
package net.fabric.loom;
import net.fabric.loom.task.DownloadTask;
import net.fabric.loom.task.ExtractNativesTask;
import net.fabric.loom.task.GenIdeaProjectTask;
import net.fabric.loom.task.MapJarsTask;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
public class LoomGradlePlugin extends AbstractPlugin {
@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("extractNatives", ExtractNativesTask.class).dependsOn("download");
makeTask("genIdeaRuns", GenIdeaProjectTask.class).dependsOn("cleanIdea").dependsOn("idea").dependsOn("extractNatives");
}
}

View File

@@ -0,0 +1,132 @@
package net.fabric.loom.task;
import net.fabric.loom.util.Constants;
import net.fabric.loom.util.progress.ProgressLogger;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
/**
* Generic Download class compatible with ProgressLogger
*/
public class DownloadNewTask extends DefaultTask {
@Input
private Object output;
@OutputFile
private String url;
private String taskName;
@TaskAction
public void download() throws IOException {
File outputFile = getProject().file(getOutput());
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
getLogger().info("Downloading " + getURL());
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");
}
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 void setOutput(Object output) {
this.output = output;
}
public String getURL() {
return url;
}
public void setURL(String url) {
this.url = url;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public String getTaskName() {
return taskName;
}
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;
}
public void close() throws IOException {
rbc.close();
}
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;
}
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));
}
}
}
}

View File

@@ -0,0 +1,124 @@
package net.fabric.loom.task;
import net.fabric.loom.util.Constants;
import net.fabric.loom.util.Version;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.FileUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.tasks.TaskAction;
import net.fabric.loom.LoomGradleExtension;
import net.fabric.loom.util.Checksum;
import net.fabric.loom.util.ManifestVersion;
import net.fabric.loom.util.assets.AssetIndex;
import net.fabric.loom.util.assets.AssetObject;
import net.fabric.loom.util.progress.ProgressLogger;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
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);
if (!Constants.MINECRAFT_JSON.get(extension).exists()) {
this.getLogger().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<ManifestVersion.Versions> 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 {
this.getLogger().info(":failed downloading minecraft json");
throw new RuntimeException("Failed downloading Minecraft json");
}
}
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.MAPPINGS_ZIP.exists()) {
Constants.MAPPINGS_ZIP.delete();
}
this.getLogger().lifecycle(":downloading mappings");
FileUtils.copyURLToFile(new URL("https://github.com/FabricMC/pomf/archive/master.zip"), Constants.MAPPINGS_ZIP);
DependencyHandler dependencyHandler = getProject().getDependencies();
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());
}
}
}
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()));
}
// Force add LaunchWrapper
dependencyHandler.add(Constants.CONFIG_MC_DEPENDENCIES, "net.minecraft:launchwrapper:1.11");
Version.AssetIndex assetIndex = version.assetIndex;
File assets = new File(Constants.CACHE_FILES, "assets");
if (!assets.exists()) {
assets.mkdirs();
}
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);
}
ProgressLogger progressLogger = ProgressLogger.getProgressFactory(getProject(), getClass().getName());
progressLogger.start("Downloading assets...", "assets");
AssetIndex index = new Gson().fromJson(new FileReader(assetsInfo), AssetIndex.class);
Map<String, AssetObject> parent = index.getFileMap();
final int totalSize = parent.size();
int position = 0;
this.getLogger().lifecycle(":downloading assets...");
for (Map.Entry<String, AssetObject> 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();
}
}
}

View File

@@ -0,0 +1,20 @@
package net.fabric.loom.task;
import net.fabric.loom.util.Constants;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
import org.zeroturnaround.zip.ZipUtil;
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);
}
}
}
}

View File

@@ -0,0 +1,167 @@
package net.fabric.loom.task;
import net.fabric.loom.LoomGradleExtension;
import net.fabric.loom.util.Constants;
import net.fabric.loom.util.IdeaRunConfig;
import net.fabric.loom.util.Version;
import com.google.gson.Gson;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
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);
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;
}
}
if (component == null) {
this.getLogger().lifecycle(":failed to generate intellij run configurations");
return;
}
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);
}
}
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);
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();
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");
}
}
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);
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);
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;
}
IdeaRunConfig ideaClient = new IdeaRunConfig();
ideaClient.mainClass = "net.fabricmc.base.launch.FabricClientMain ";
ideaClient.projectName = getProject().getName();
ideaClient.configName = "Minecraft Client";
ideaClient.runDir = "file://$PROJECT_DIR$/" + extension.runDir;
ideaClient.arguments = "--assetIndex " + version.assetIndex.id + " --assetsDir " + new File(Constants.CACHE_FILES, "assets").getAbsolutePath();
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);
File runDir = new File(Constants.WORKING_DIRECTORY, extension.runDir);
if (!runDir.exists()) {
runDir.mkdirs();
}
}
}

View File

@@ -0,0 +1,65 @@
package net.fabric.loom.task;
import cuchaz.enigma.Deobfuscator;
import cuchaz.enigma.mapping.MappingsEnigmaReader;
import cuchaz.enigma.throwables.MappingParseException;
import net.fabric.loom.LoomGradleExtension;
import net.fabric.loom.util.Constants;
import org.apache.commons.io.FileUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
import org.zeroturnaround.zip.ZipUtil;
import java.io.File;
import java.io.IOException;
import java.util.jar.JarFile;
public class MapJarsTask extends DefaultTask {
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);
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());
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.pack(tempAssests, Constants.MINECRAFT_CLIENT_MAPPED_JAR.get(extension));
}
public static class ProgressListener implements Deobfuscator.ProgressListener {
@Override
public void init(int i, String s) {
}
@Override
public void onProgress(int i, String s) {
}
}
}

View File

@@ -0,0 +1,27 @@
package net.fabric.loom.util;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.common.io.Files;
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;
}
}

View File

@@ -0,0 +1,49 @@
package net.fabric.loom.util;
import net.fabric.loom.util.delayed.IDelayed;
import net.fabric.loom.util.delayed.DelayedFile;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
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 IDelayed<File> MINECRAFT_CLIENT_JAR = new DelayedFile(extension -> new File(CACHE_FILES, extension.version + "-client.jar"));
public static final IDelayed<File> MINECRAFT_CLIENT_MAPPED_JAR = new DelayedFile(extension -> new File(CACHE_FILES, extension.version + "-client-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 MINECRAFT_LIBS = new File(CACHE_FILES, "libs");
public static final File MINECRAFT_NATIVES = new File(CACHE_FILES, "natives");
public static final IDelayed<File> 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<File> 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 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 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<String> getClassPath() {
URL[] urls = ((URLClassLoader) Constants.class.getClassLoader()).getURLs();
ArrayList<String> list = new ArrayList<>();
for (URL url : urls) {
list.add(url.getPath());
}
return list;
}
}

View File

@@ -0,0 +1,48 @@
package net.fabric.loom.util;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
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 arguments;
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));
if (!Strings.isNullOrEmpty(arguments)) {
this.addXml(root, "option", ImmutableMap.of("name", "PROGRAM_PARAMETERS", "value", arguments));
}
return root;
}
public Element addXml(Node parent, String name, Map<String, String> values) {
Document doc = parent.getOwnerDocument();
if (doc == null) {
doc = (Document) parent;
}
Element e = doc.createElement(name);
for (Map.Entry<String, String> entry : values.entrySet()) {
e.setAttribute(entry.getKey(), entry.getValue());
}
parent.appendChild(e);
return e;
}
}

View File

@@ -0,0 +1,12 @@
package net.fabric.loom.util;
import java.util.ArrayList;
import java.util.List;
public class ManifestVersion {
public List<Versions> versions = new ArrayList<>();
public static class Versions {
public String id, url;
}
}

View File

@@ -0,0 +1,26 @@
package net.fabric.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 getArch() {
if (is64Bit()) {
return "64";
} else {
return "32";
}
}
public static boolean is64Bit() {
return System.getProperty("sun.arch.data.model").contains("64");
}
}

View File

@@ -0,0 +1,135 @@
package net.fabric.loom.util;
import com.google.gson.JsonObject;
import java.io.File;
import java.util.List;
import java.util.Map;
public class Version {
public List<Library> libraries;
public Map<String, Downloads> downloads;
public AssetIndex assetIndex;
public class Downloads {
public String url;
public String sha1;
}
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 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 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 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;
}
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;
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 getClassifier() {
return classifier;
}
}
}
private class Rule {
public String action;
public OS os;
private class OS {
String name;
}
}
}

View File

@@ -0,0 +1,27 @@
package net.fabric.loom.util.assets;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class AssetIndex {
private final Map<String, AssetObject> objects;
private boolean virtual;
public AssetIndex() {
this.objects = new LinkedHashMap<>();
}
public Map<String, AssetObject> getFileMap() {
return this.objects;
}
public Set<AssetObject> getUniqueObjects() {
return new HashSet<>(this.objects.values());
}
public boolean isVirtual() {
return this.virtual;
}
}

View File

@@ -0,0 +1,33 @@
package net.fabric.loom.util.assets;
public class AssetObject {
private String hash;
private long size;
public String getHash() {
return this.hash;
}
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 int hashCode() {
int result = this.hash.hashCode();
result = 31 * result + (int) (this.size ^ this.size >>> 32);
return result;
}
}

View File

@@ -0,0 +1,23 @@
package net.fabric.loom.util.delayed;
import net.fabric.loom.LoomGradleExtension;
import java.io.File;
import java.util.function.Function;
public class DelayedFile implements IDelayed<File> {
private File file;
private Function<LoomGradleExtension, File> function;
public DelayedFile(Function<LoomGradleExtension, File> function) {
this.function = function;
}
@Override
public File get(LoomGradleExtension extension) {
if (this.file == null) {
this.file = this.function.apply(extension);
}
return this.file;
}
}

View File

@@ -0,0 +1,7 @@
package net.fabric.loom.util.delayed;
import net.fabric.loom.LoomGradleExtension;
public interface IDelayed<T> {
T get(LoomGradleExtension extension);
}

View File

@@ -0,0 +1,206 @@
package net.fabric.loom.util.progress;
import org.gradle.api.Project;
import java.lang.reflect.InvocationTargetException;
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 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 Method getMethod(String methodName, Class<?>... args) {
if (logger != null) {
try {
return logger.getClass().getMethod(methodName, args);
} catch (NoSuchMethodException ignored) {
}
}
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) {
}
}
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);
}
}
/**
* Returns the description of the operation.
*
* @return the description, must not be empty.
*/
public String getDescription() {
return (String) invoke(getDescription);
}
/**
* <p>Sets the description of the operation. This should be a full, stand-alone description of the operation.
* <p>
* <p>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);
}
/**
* <p>Sets the short description of the operation. This is used in place of the full description when display space is limited.
* <p>
* <p>This must be called before {@link #started()}
*
* @param description The short description.
*/
public ProgressLogger setShortDescription(String description) {
invoke(setShortDescription, description);
return this;
}
/**
* <p>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.
* <p>
* <p>If not specified, no logging header is logged.
*
* @return The logging header, possibly empty.
*/
public String getLoggingHeader() {
return (String) invoke(getLoggingHeader);
}
/**
* <p>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;
}
/**
* 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 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 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);
}
}