Skip to content

Instantly share code, notes, and snippets.

@Caellian
Last active October 9, 2019 12:35
Show Gist options
  • Select an option

  • Save Caellian/63b95a1e94efa7a1d9c320a22f93fe59 to your computer and use it in GitHub Desktop.

Select an option

Save Caellian/63b95a1e94efa7a1d9c320a22f93fe59 to your computer and use it in GitHub Desktop.
Minecraft Project gradle files.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "io.github.java-diff-utils:java-diff-utils:4.0"
}
}
import com.github.difflib.algorithm.DiffException
import com.github.difflib.patch.Patch
import com.github.difflib.patch.PatchFailedException
import java.io.*
import java.util.*
List<String> fileToLines(file) throws FileNotFoundException, IOException {
List<String> lines = new ArrayList<>();
String line = "";
BufferedReader breader;
try {
breader = new BufferedReader(new FileReader(project.file(file)));
while ((line = breader.readLine()) != null) {
lines.add(line);
}
} finally {
in.close();
}
return lines;
}
void genPatches(from, to, out) {
def fromFiles = fileTree(project.file(from)).filter { it.isFile() }.files.name
def toFiles = fileTree(project.file(from)).filter { it.isFile() }.files.name
}
ext.test = {
println("Test");
}
import org.gradle.internal.os.OperatingSystem
import groovy.json.JsonSlurper
import java.net.URL
import java.nio.channels.Channels
import java.nio.channels.ReadableByteChannel
import java.nio.file.*
String getMCDir() {
if (!mcVersionsPath.isEmpty()) {
return mcVersionsPath
}
switch (OperatingSystem.current()) {
case OperatingSystem.LINUX:
case OperatingSystem.WINDOWS:
return "${System.getProperty("user.home")}${File.separator}.minecraft${File.separator}"
break
case OperatingSystem.MAC_OS:
return "${System.getProperty("user.home")}${File.separator}Library${File.separator}Application Support${File.separator}minecraft${File.separator}"
break
}
}
String getVersionsDir() {
return "${getMCDir()}versions${File.separator}"
}
String getSelectedVersionDir() {
return getVersionsDir() + "${mcVersion}${File.separator}"
}
String getJarPath() {
return getSelectedVersionDir() + "${mcVersion}.jar"
}
String getMetaPath() {
return getSelectedVersionDir() + "${mcVersion}.json"
}
String getClassifier() {
switch (OperatingSystem.current()) {
case OperatingSystem.LINUX:
return 'natives-linux'
break
case OperatingSystem.WINDOWS:
return 'natives-windows'
break
case OperatingSystem.MAC_OS:
return 'natives-macos'
break
}
}
String getOSName() {
switch (OperatingSystem.current()) {
case OperatingSystem.LINUX:
return 'linux'
break
case OperatingSystem.WINDOWS:
return 'windows'
break
case OperatingSystem.MAC_OS:
return 'osx'
break
}
}
// Cache version meta to reduce file I/O
class VersionMeta {
static Object data = null
}
Object getVersionMeta() {
if (VersionMeta.data == null) {
VersionMeta.data = new JsonSlurper().parseText(new File(getMetaPath()).getText('UTF-8'))
}
return VersionMeta.data
}
Boolean isLibAllowed(lib) {
if (lib.rules == null) return true
boolean defaultAllow = false
for (rule in lib.rules) {
if (rule.action == 'allow' && rule.os == null) {
defaultAllow = true
continue
}
if (rule.action == 'disallow' && rule.os.name == getOSName()) {
return false
}
if (rule.action == 'allow' && rule.os.name == getOSName()) {
return true
}
}
return defaultAllow
}
void download(url, file) {
ReadableByteChannel rbc = Channels.newChannel(new URL(url).openStream())
File parent = file.getParentFile()
if (!parent.exists()) {
parent.mkdirs()
}
FileOutputStream fos = new FileOutputStream(file)
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE)
fos.close()
rbc.close()
}
ext.downloadLibs = { dest ->
if (dest.exists()) {
println 'Deleting old libraries.'
dest.deleteDir()
}
dest.mkdirs()
def downloaded = []
for(lib in getVersionMeta().libraries.findAll{isLibAllowed(it)}) {
if (!(lib.name in downloaded)) {
println "Downloading ${lib.name}..."
download(lib.downloads.artifact.url, new File(dest, lib.downloads.artifact.path))
downloaded.add(lib.name)
}
if (lib.downloads.classifiers != null) {
Object javadoc = lib.downloads.classifiers.javadoc
if (javadoc != null) {
download(javadoc.url, new File(dest, javadoc.path))
}
Object sources = lib.downloads.classifiers.sources
if (sources != null) {
download(sources.url, new File(dest, sources.path))
}
Object osSpecific = lib.downloads.classifiers[getClassifier()]
if (osSpecific != null) {
download(osSpecific.url, new File(dest, osSpecific.path))
}
}
}
println 'Done.'
}
String getAssetIndexPath() {
return "${getMCDir()}assets${File.separator}indexes${File.separator}${getVersionMeta().assets}.json"
}
String getAssetObjectDir() {
return "${getMCDir()}assets${File.separator}objects${File.separator}"
}
// Cache asset hash map to reduce file I/O
class AssetMap {
static Object data = null
}
def getAssetMap() {
if (AssetMap.data == null) {
AssetMap.data = new JsonSlurper().parseText(new File(getAssetIndexPath()).getText('UTF-8')).objects.collectEntries { k, v -> [(v.hash): k]}
}
return AssetMap.data
}
ext.prepareAssets = { dest ->
for (group in new File(getAssetObjectDir()).listFiles()) {
for (obj in group.listFiles()) {
String hash = obj.getName()
String destName = getAssetMap()[hash]
println "Copying ${destName}..."
File parent = new File(dest, destName).getParentFile()
if (!parent.exists()) {
parent.mkdirs()
}
File destination = new File(dest, getAssetMap()[hash])
if (destination.exists()) {
destination.delete()
}
Files.copy(obj.toPath(), destination.toPath())
}
}
}
ext.downloadJar = { type, dest ->
File jarFile = new File(dest, "${getVersionMeta().id}-${type}.jar")
if (jarFile.exists()) {
println "Minecraft ${type} jar already downloaded."
} else {
println "Downloading Minecraft ${type} jar..."
String downloadUrl = getVersionMeta().downloads[type].url
download(downloadUrl, jarFile)
println "Done."
}
}
ext.downloadMappings = { type, dest ->
File mappingsFile = new File(dest, "${getVersionMeta().id}-${type}.mappings")
if (mappingsFile.exists()) {
println "Minecraft ${type} mappings already downloaded."
} else {
println "Downloading Minecraft ${type} mappings..."
println getVersionMeta().downloads
String downloadUrl = getVersionMeta().downloads["${type}_mappings"].url
download(downloadUrl, )
println "Done."
}
}
ext.decompile = { type, workingDir ->
File dir = project.file("${workingDir}/decompiled/")
if (!dir.exists()) {
mkdir(dir)
}
if (project.file("${workingDir}/decompiled/${mcVersion}-${type}.jar").exists()) {
println 'Jar already decompiled.'
println "To decompile source jar again delete: ${project.file("${workingDir}/decompiled/${mcVersion}-${type}.jar")}"
} else {
println 'Decompiling with fernflower...'
def argList = [ project.file('../gradle/fernflower.jar') ]
if (fernflowerArgs != '') argList.addAll(fernflowerArgs.split(' '))
argList.add("${workingDir}/${mcVersion}-${type}.jar")
argList.add("${workingDir}/decompiled/")
javaexec {
main = '-jar'
args argList
}
}
if (!project.file("${workingDir}/decompiled/${mcVersion}-${type}.jar").exists()) {
println 'Something went wrong, source jar doesn\'t exist.'
} else {
if (project.file("${workingDir}/decompiled/files").exists() && project.file("${workingDir}/decompiled/files").list().length != 0) {
println 'Source jar already unzipped.'
println "To unzip it again delete: ${project.file("${workingDir}/decompiled/files")}"
} else {
mkdir(project.file("${workingDir}/decompiled/files"))
println 'Unzipping source jar...'
unzip(project.file("${workingDir}/decompiled/${mcVersion}-${type}.jar"), project.file("${workingDir}/decompiled/files"))
println 'Done.'
}
}
}
import java.util.zip.ZipFile
import java.io.*
import java.util.zip.*
ext.unzip = { file, dest ->
def zis = new ZipInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024]
def entry = zis.getNextEntry();
while (entry != null) {
File newFile = new File(dest, entry.getName())
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs()
}
def fos = new FileOutputStream(newFile)
int len = 0
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close()
entry = zis.getNextEntry()
}
zis.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment