Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 49 additions & 179 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath libs.bundles.asm
}
}

plugins {
id 'java'
id 'idea'
id 'eclipse'
id 'maven-publish'
id 'net.minecraftforge.licenser' version '1.1.0'
id 'net.minecraftforge.gradleutils' version '[2.3,2.4)'
id 'com.github.ben-manes.versions' version '0.52.0'
alias libs.plugins.licenser
alias libs.plugins.gradleutils
alias libs.plugins.versions
}

final projectDisplayName = 'Srg2Source'
final projectVendor = 'Forge Development LLC'
final projectArtifactId = base.archivesName = 'srg2source'
description = 'Srg2Source library for ForgeGradle'
group = 'net.minecraftforge'
archivesBaseName = 'srg2source'
version = gradleutils.tagOffsetVersion
version = gitversion.tagOffset

java {
toolchain.languageVersion = JavaLanguageVersion.of(17)

withSourcesJar()
}

Expand All @@ -31,12 +26,6 @@ configurations {
implementation.extendsFrom shadow
}

repositories {
//mavenLocal()
maven gradleutils.forgeMaven
mavenCentral()
}

dependencies {
implementation(libs.securemodules)
implementation(libs.nulls)
Expand All @@ -55,167 +44,85 @@ changelog {
from '8.0'
}

tasks.named('jar', Jar).configure {
license {
header = rootProject.file('LICENSE-header.txt')
include 'net/minecraftforge/'
}

tasks.register('patchJDT', PatchJDTClasses) {
targets.add(PatchJDTClasses.COMPILATION_UNIT_RESOLVER)
targets.add(PatchJDTClasses.RANGE_EXTRACTOR)
libraries.from(unpatchedJar.archiveFile)
libraries.from(configurations.shadow.filter { !it.isDirectory() })
output = project.layout.buildDirectory.file("patch_jdt.jar")
}

tasks.named('jar', Jar) {
exclude 'data/**'
manifest {
attributes([
'Automatic-Module-Name': 'net.minecraftforge.srg2source',
'Main-Class': 'net.minecraftforge.srg2source.ConsoleTool'
'Main-Class' : 'net.minecraftforge.srg2source.ConsoleTool'
] as LinkedHashMap)
attributes([
'Specification-Title': 'Srg2Source',
'Specification-Vendor': 'Forge Development LLC',
'Specification-Version': gradleutils.gitInfo.tag,
'Implementation-Title': 'Srg2Source',
'Implementation-Vendor': 'Forge Development LLC',
'Specification-Title' : projectDisplayName,
'Specification-Vendor' : projectVendor,
'Specification-Version' : gitversion.info.tag,
'Implementation-Title' : projectDisplayName,
'Implementation-Vendor' : projectVendor,
'Implementation-Version': project.version
] as LinkedHashMap, 'net/minecraftforge/srg2source/')
}
}

tasks.register('shadowJarPatched', Jar).configure {
tasks.register('shadowJarPatched', Jar) {
dependsOn patchJDT
archiveClassifier = 'fatjar'
manifest = jar.manifest

from(zipTree(patchJDT.output))
from(sourceSets.main.output) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

from { configurations.shadow.collect { it.isDirectory() ? it : zipTree(it) } } {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'module-info.class')
}

license {
header = file('LICENSE-header.txt')
include 'net/minecraftforge/'
}

import java.util.zip.*
import org.objectweb.asm.*
import org.objectweb.asm.tree.*

//TODO: Eclipse complains about unused messages. Find a way to make it shut up.
abstract class PatchJDTClasses extends DefaultTask {
static def COMPILATION_UNIT_RESOLVER = 'org/eclipse/jdt/core/dom/CompilationUnitResolver'
static def RANGE_EXTRACTOR = 'net/minecraftforge/srg2source/extract/RangeExtractor'
static def RESOLVE_METHOD = 'resolve([Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;Lorg/eclipse/jdt/core/dom/FileASTRequestor;ILjava/util/Map;I)V'
static def GET_CONTENTS = 'org/eclipse/jdt/internal/compiler/util/Util.getFileCharContent(Ljava/io/File;Ljava/lang/String;)[C'
static def HOOK_DESC_RESOLVE = '(Ljava/lang/String;Ljava/lang/String;)[C'

@Input abstract SetProperty<String> getTargets()
@InputFiles @Classpath abstract ConfigurableFileCollection getLibraries()
@OutputFile abstract RegularFileProperty getOutput()

@TaskAction
void patchClass() {
def toProcess = targets.get().collect()
new ZipOutputStream(new FileOutputStream(output.get().getAsFile())).withCloseable{ zout ->
libraries.getFiles().stream().filter{ !it.isDirectory() }.each { lib ->
new ZipFile(lib).withCloseable { zin ->
def remove = []
toProcess.each{ target ->
def entry = zin.getEntry(target+'.class')
if (entry == null)
return

def node = new ClassNode()
def reader = new ClassReader(zin.getInputStream(entry))
reader.accept(node, 0)

//CompilationUnitResolver allows batch compiling, the problem is it is hardcoded to read the contents from a File.
//So we patch this call to redirect to us, so we can get the contents from our InputSupplier
if (COMPILATION_UNIT_RESOLVER.equals(target)) {
logger.lifecycle('Transforming: ' + target + ' From: ' + lib)
def resolve = node.methods.find{ RESOLVE_METHOD.equals(it.name + it.desc) }
if (resolve == null)
throw new RuntimeException('Failed to patch ' + target + ': Could not find method ' + RESOLVE_METHOD)
for (int x = 0; x < resolve.instructions.size(); x++) {
def insn = resolve.instructions.get(x)
if (insn.type == AbstractInsnNode.METHOD_INSN) {
if (GET_CONTENTS.equals(insn.owner + '.' + insn.name + insn.desc)) {
if (
resolve.instructions.get(x - 5).opcode == Opcodes.NEW &&
resolve.instructions.get(x - 4).opcode == Opcodes.DUP &&
resolve.instructions.get(x - 3).opcode == Opcodes.ALOAD &&
resolve.instructions.get(x - 2).opcode == Opcodes.INVOKESPECIAL &&
resolve.instructions.get(x - 1).opcode == Opcodes.ALOAD
) {
resolve.instructions.set(resolve.instructions.get(x - 5), new InsnNode(Opcodes.NOP)); // NEW File
resolve.instructions.set(resolve.instructions.get(x - 4), new InsnNode(Opcodes.NOP)); // DUP
resolve.instructions.set(resolve.instructions.get(x - 2), new InsnNode(Opcodes.NOP)); // INVOKESTATIC <init>
insn.owner = RANGE_EXTRACTOR
insn.desc = HOOK_DESC_RESOLVE
//logger.lifecycle('Patched ' + node.name)
remove.add(target)
} else {
throw new IllegalStateException('Found Util.getFileCharContents call, with unexpected context')
}
}
}
}
} else if (RANGE_EXTRACTOR.equals(target)) {
logger.lifecycle('Tansforming: ' + target + ' From: ' + lib)
def marker = node.methods.find{ 'hasBeenASMPatched()Z'.equals(it.name + it.desc) }
if (marker == null)
throw new RuntimeException('Failed to patch ' + target + ': Could not find method hasBeenASMPatched()Z')
marker.instructions.clear()
marker.instructions.add(new InsnNode(Opcodes.ICONST_1))
marker.instructions.add(new InsnNode(Opcodes.IRETURN))
//logger.lifecycle('Patched: ' + node.name)
remove.add(target)
}

def writer = new ClassWriter(0)
node.accept(writer)

def nentry = new ZipEntry(entry.name)
nentry.time = 0
zout.putNextEntry(nentry)
zout.write(writer.toByteArray())
zout.closeEntry()
}
toProcess.removeAll(remove)
}
}
if (!toProcess.isEmpty())
throw new IllegalStateException('Patching class failed: ' + toProcess)
}
}
}

tasks.register('unpatchedJar', Jar).configure {
tasks.register('unpatchedJar', Jar) {
from sourceSets.main.output
archiveClassifier = 'unpatched'
}

tasks.register('patchJDT', PatchJDTClasses).configure {
targets.add(PatchJDTClasses.COMPILATION_UNIT_RESOLVER)
targets.add(PatchJDTClasses.RANGE_EXTRACTOR)
libraries.from(unpatchedJar.archiveFile)
libraries.from(configurations.shadow.filter{ !it.isDirectory() })
output = project.layout.buildDirectory.file("patch_jdt.jar")
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation
options.warnings = false // Shutup deprecated for removal warnings
}

publishing {
publications.register('mavenJava', MavenPublication) {
artifact jar
artifact shadowJarPatched
artifact sourcesJar
pom {
name = 'Srg2Source'
description = 'Srg2Source library for ForgeGradle'
url = 'https://github.com/MinecraftForge/Srg2Source'

gradleutils.pom.setGitHubDetails(pom, 'Srg2Source')
// TODO [Srg2Source] Enable this. Previous builds have the project.name as the artifact ID
// For now it is disabled until we want to lowercase the artifact ID
//artifactId = projectArtifactId

pom { pom ->
name = projectDisplayName
description = project.description

gradleutils.pom.setGitHubDetails pom

license gradleutils.pom.Licenses.LGPLv2_1
licenses {
license gradleutils.pom.licenses.LGPLv2_1
}

developers {
developer gradleutils.pom.Developers.LexManos
developer gradleutils.pom.developers.LexManos
}
}
}
Expand All @@ -224,40 +131,3 @@ publishing {
maven gradleutils.publishingForgeMaven
}
}
// We need to write the manifest to the binary file so we have properly versioned packaged at dev time.
tasks.register('writeManifest') {
doLast {
if (plugins.findPlugin('net.minecraftforge.gradle.patcher')) // Forge project
universalJar.manifest.writeTo(rootProject.file('src/main/resources/META-INF/MANIFEST.MF'))
else
jar.manifest.writeTo(project.file('src/main/resources/META-INF/MANIFEST.MF'))
}
}

tasks.register('generateResources') {
dependsOn('writeManifest')
}

// Make sure out manifests get written before compiling the code, IDEA calls this task if you tell it to use the gradle build.
tasks.withType(JavaCompile).configureEach {
dependsOn 'generateResources'
dependsOn 'processResources' // Needed because we merge the output of this with the output of the compile task. And gradle detects downstream tasks using the output without a hard dep
options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation
options.warnings = false // Shutup deprecated for removal warnings
}

// Merge the resources and classes into the same directory. We'll need to split them at runtime because
// Minecraft and Forge are in the same sourceSet as they are inter dependent.. for now..
sourceSets.each {
def dir = layout.buildDirectory.dir("classes/java/$it.name")
it.output.resourcesDir = dir
it.java.destinationDirectory = dir
}

eclipse {
// Run everytime eclipse builds the code
//autoBuildTasks writeManifest
// Run when importing the project
synchronizationTasks generateResources, writeManifest
}

15 changes: 15 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id 'java-gradle-plugin'
id 'groovy'
alias libs.plugins.licenser
}

license {
header = rootProject.file('../LICENSE-header.txt')
newLine = false
exclude '**/*.properties'
}

dependencies {
implementation libs.bundles.asm
}
14 changes: 14 additions & 0 deletions buildSrc/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
dependencyResolutionManagement {
repositories {
mavenCentral()
}

versionCatalogs.register('libs') {
plugin 'licenser', 'net.minecraftforge.licenser' version '1.2.0'

version('asm', '9.7.1')
library('asm', 'org.ow2.asm', 'asm' ).versionRef('asm')
library('asm-tree', 'org.ow2.asm', 'asm-tree').versionRef('asm')
bundle('asm', ['asm', 'asm-tree'])
}
}
Loading