-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
build.gradle
264 lines (231 loc) · 10.9 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.ow2.asm:asm:9.7'
classpath 'org.ow2.asm:asm-tree:9.7'
}
}
plugins {
id 'java'
id 'idea'
id 'eclipse'
id 'maven-publish'
id 'net.minecraftforge.licenser' version '1.0.1'
id 'net.minecraftforge.gradleutils' version '[2.3,2.4)'
}
group = 'net.minecraftforge'
archivesBaseName = 'srg2source'
version = gradleutils.tagOffsetVersion
println('Version: ' + version)
java {
toolchain.languageVersion = JavaLanguageVersion.of(17)
withSourcesJar()
}
configurations {
shadow
implementation.extendsFrom shadow
}
repositories {
//mavenLocal()
maven gradleutils.forgeMaven
mavenCentral()
}
dependencies {
implementation(libs.securemodules)
implementation(libs.nulls)
shadow(libs.bundles.asm)
shadow(libs.jopt.simple) // easy CLI parsing
shadow(libs.srgutils) //Because tons of projects all parsing SRG files is annoying
shadow(libs.bundles.eclipse) // necessary eclipse AST stuff
implementation(libs.ml) // We need to patch eclipse to allow sources from more then raw files
testImplementation(libs.junit.api)
testImplementation(libs.jimfs)
testRuntimeOnly(libs.bundles.junit.runtime)
}
changelog {
from '8.0'
}
tasks.named('jar', Jar).configure {
exclude 'data/**'
manifest {
attributes([
'Automatic-Module-Name': 'net.minecraftforge.srg2source',
'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',
'Implementation-Version': project.version
] as LinkedHashMap, 'net/minecraftforge/srg2source/')
}
}
tasks.register('shadowJarPatched', Jar).configure {
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 {
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")
}
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')
license gradleutils.pom.Licenses.LGPLv2_1
developers {
developer gradleutils.pom.Developers.LexManos
}
}
}
repositories {
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
}