Split source preprocessor into separate gradle plugin
This commit is contained in:
@@ -20,6 +20,7 @@ buildscript {
|
||||
name = "sonatype"
|
||||
url = "https://oss.sonatype.org/content/repositories/snapshots/"
|
||||
}
|
||||
maven { url 'https://jitpack.io' }
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@@ -41,13 +42,20 @@ buildscript {
|
||||
mcVersion >= 10710 ? '1.2-SNAPSHOT' :
|
||||
'invalid'
|
||||
)
|
||||
classpath 'com.github.replaymod:preprocessor:3654e32'
|
||||
}
|
||||
}
|
||||
|
||||
import com.replaymod.gradle.preprocess.PreprocessTask
|
||||
|
||||
def mcVersionStr = "${(int)(mcVersion/10000)}.${(int)(mcVersion/100)%100}" + (mcVersion%100==0 ? '' : ".${mcVersion%100}")
|
||||
|
||||
def FG3 = mcVersion >= 11300
|
||||
|
||||
def jGuiVersion = project.name
|
||||
if (['1.10.2', '1.11', '1.11.2'].contains(jGuiVersion)) jGuiVersion = '1.9.4'
|
||||
if (['1.12.1', '1.12.2'].contains(jGuiVersion)) jGuiVersion = '1.12'
|
||||
|
||||
if (mcVersion >= 10800) {
|
||||
apply plugin: 'com.github.johnrengelman.shadow'
|
||||
|
||||
@@ -70,6 +78,8 @@ if (mcVersion >= 10800) {
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'com.replaymod.preprocess'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
targetCompatibility = 1.8
|
||||
|
||||
@@ -204,9 +214,6 @@ dependencies {
|
||||
if (studioVersion == '1.8.9') studioVersion = '1.8'
|
||||
shadow "com.github.ReplayMod.ReplayStudio:$studioVersion:04a929c", shadeExclusions
|
||||
|
||||
def jGuiVersion = project.name
|
||||
if (['1.10.2', '1.11', '1.11.2'].contains(jGuiVersion)) jGuiVersion = '1.9.4'
|
||||
if (['1.12.1', '1.12.2'].contains(jGuiVersion)) jGuiVersion = '1.12'
|
||||
shadowReobf(project(":jGui:$jGuiVersion")){
|
||||
transitive = false // FG 1.2 puts all MC deps into the compile configuration and we don't want to shade those
|
||||
exclude group: 'org.projectlombok', module: 'lombok' // runtime only for @SneakyThrows which isn't used
|
||||
@@ -243,42 +250,15 @@ if (mcVersion <= 10710) {
|
||||
}
|
||||
|
||||
if (project.name != 'core') {
|
||||
apply from: '../preprocessor.gradle'
|
||||
|
||||
def preprocessedSrc = 'build/preprocessed/src'
|
||||
def preprocessedRes = 'build/preprocessed/res'
|
||||
def preprocessedAT = 'build/preprocessed/at.cfg'
|
||||
def originalSrc = '../../src/main/java'
|
||||
def originalRes = '../../src/main/resources'
|
||||
def originalAT = originalRes + '/META-INF/replaymod_at.cfg'
|
||||
def vars = [MC: mcVersion as int]
|
||||
|
||||
sourceSets {
|
||||
main.java.srcDir preprocessedSrc
|
||||
main.resources.srcDir preprocessedRes
|
||||
}
|
||||
|
||||
task preprocessJava {
|
||||
inputs.dir(originalSrc)
|
||||
outputs.dir(preprocessedSrc)
|
||||
doLast {
|
||||
project.convertTree(vars, originalSrc, preprocessedSrc)
|
||||
}
|
||||
}
|
||||
|
||||
task preprocessResources {
|
||||
inputs.dir(originalRes)
|
||||
outputs.dir(preprocessedRes)
|
||||
doLast {
|
||||
project.convertTree(vars, originalRes, preprocessedRes)
|
||||
}
|
||||
}
|
||||
|
||||
compileJava.dependsOn preprocessJava
|
||||
processResources.dependsOn preprocessResources
|
||||
if (FG3) {
|
||||
// FG3 reads the AT file right after project evaluation, so we need to convert it right now
|
||||
project.convertTree(vars, originalAT, preprocessedAT)
|
||||
PreprocessTask.convertFile(PreprocessTask.CFG_KEYWORDS, vars, file(originalAT), file(preprocessedAT))
|
||||
minecraft.accessTransformers << file(preprocessedAT)
|
||||
} else {
|
||||
(mcVersion >= 10800 ? [deobfMcMCP, deobfMcSRG] : [deobfuscateJar, deobfBinJar]).each { task ->
|
||||
@@ -286,14 +266,14 @@ if (project.name != 'core') {
|
||||
}
|
||||
minecraft.accessTransformer preprocessedRes + '/META-INF/replaymod_at.cfg'
|
||||
}
|
||||
|
||||
project(":jGui:$jGuiVersion").afterEvaluate { jGui ->
|
||||
project.tasks.setCoreVersion.dependsOn jGui.tasks.setCoreVersion
|
||||
}
|
||||
} else {
|
||||
if (FG3) {
|
||||
minecraft.accessTransformers << file('../../src/main/resources/META-INF/replaymod_at.cfg')
|
||||
}
|
||||
sourceSets {
|
||||
main.java.srcDirs = ['../../src/main/java']
|
||||
main.resources.srcDirs = ['../../src/main/resources']
|
||||
}
|
||||
}
|
||||
sourceSets {
|
||||
main {
|
||||
|
||||
@@ -1,171 +0,0 @@
|
||||
static def evalVar(vars, String var) {
|
||||
if (var.number) {
|
||||
return var as int
|
||||
} else {
|
||||
return vars[var]
|
||||
}
|
||||
}
|
||||
|
||||
import java.util.regex.Pattern
|
||||
|
||||
static def evalExpr(vars, String expr) {
|
||||
expr = expr.trim()
|
||||
|
||||
def parts = expr.split(/\|\|/)
|
||||
if (parts.length > 1) {
|
||||
return parts.any { evalExpr(vars, it) }
|
||||
}
|
||||
parts = expr.split(/&&/)
|
||||
if (parts.length > 1) {
|
||||
return !parts.any { !evalExpr(vars, it) }
|
||||
}
|
||||
|
||||
def matcher = Pattern.compile(/(.+)(<=|>=|<|>)(.+)/).matcher(expr)
|
||||
if (matcher.matches()) {
|
||||
def lhs = evalVar(vars, matcher.group(1))
|
||||
def rhs = evalVar(vars, matcher.group(3))
|
||||
switch (matcher.group(2)) {
|
||||
case '>=': return lhs >= rhs
|
||||
case '<=': return lhs <= rhs
|
||||
case '>': return lhs > rhs
|
||||
case '<': return lhs < rhs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static def getIndent(String str) {
|
||||
return str.takeWhile {it == ' '}.length()
|
||||
}
|
||||
|
||||
class ParserException extends RuntimeException {
|
||||
ParserException(String str) {
|
||||
super(str)
|
||||
}
|
||||
}
|
||||
|
||||
static def convertSource(Map<String, String> kws, Map<String, Integer> vars, List<String> lines, String fileName) {
|
||||
def ifStack = []
|
||||
List<Integer> indentStack = []
|
||||
def active = true
|
||||
def n = 0
|
||||
lines = lines.collect { line ->
|
||||
n++
|
||||
def trimmed = line.trim()
|
||||
if (trimmed.startsWith(kws.if)) {
|
||||
def result = evalExpr(vars, trimmed.substring(kws.if.length()))
|
||||
ifStack.push(result)
|
||||
if (result != null) {
|
||||
indentStack.push(getIndent(line))
|
||||
active &= result
|
||||
}
|
||||
} else if (trimmed.startsWith(kws.else)) {
|
||||
if (ifStack.isEmpty()) {
|
||||
throw new ParserException("Unexpected else in line $n of $fileName")
|
||||
}
|
||||
def head = ifStack.pop()
|
||||
if (head != null) {
|
||||
head = !head
|
||||
indentStack.pop()
|
||||
indentStack.push(getIndent(line))
|
||||
}
|
||||
ifStack.push(head)
|
||||
active = true;
|
||||
ifStack.each {
|
||||
if (it != null) {
|
||||
active &= it
|
||||
}
|
||||
}
|
||||
} else if (trimmed.startsWith(kws.ifdef)) {
|
||||
def result = vars.containsKey(trimmed.substring(kws.ifdef.length()))
|
||||
ifStack.push(result)
|
||||
indentStack.push(getIndent(line))
|
||||
active &= result
|
||||
} else if (trimmed.startsWith(kws.endif)) {
|
||||
if (ifStack.isEmpty()) {
|
||||
throw new ParserException("Unexpected endif in line $n of $fileName")
|
||||
}
|
||||
def head = ifStack.pop()
|
||||
if (head != null) {
|
||||
indentStack.pop()
|
||||
active = true;
|
||||
ifStack.each {
|
||||
if (it != null) {
|
||||
active &= it
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (active) {
|
||||
if (trimmed.startsWith(kws.eval)) {
|
||||
line = line.replaceFirst(Pattern.quote(kws.eval) + ' ?', '')
|
||||
if (line.trim().isEmpty()) {
|
||||
line = ''
|
||||
}
|
||||
}
|
||||
} else {
|
||||
def currIndent = indentStack.last()
|
||||
if (trimmed.isEmpty()) {
|
||||
line = ' ' * currIndent + kws.eval
|
||||
} else if (!trimmed.startsWith(kws.eval)) {
|
||||
def actualIndent = getIndent(line)
|
||||
if (currIndent <= actualIndent) {
|
||||
line = ' ' * currIndent + kws.eval + ' ' + line.substring(currIndent)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
line
|
||||
}
|
||||
if (!ifStack.isEmpty()) {
|
||||
throw new ParserException("Missing endif in $fileName")
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
static def convertFile(Map<String, String> kws, Map<String, Integer> vars, File inFile, File outFile) {
|
||||
def string = new String(inFile.readBytes(), StandardCharsets.UTF_8)
|
||||
def lines = string.readLines()
|
||||
try {
|
||||
lines = convertSource(kws, vars, lines, inFile.path)
|
||||
} catch (e) {
|
||||
if (e instanceof ParserException) {
|
||||
throw e
|
||||
}
|
||||
throw new RuntimeException("Failed to convert file " + inFile, e)
|
||||
}
|
||||
outFile.parentFile.mkdirs()
|
||||
if (string.endsWith('\n')) {
|
||||
outFile.write(lines.collect { it + '\n' }.join(''))
|
||||
} else {
|
||||
outFile.write(lines.join('\n'))
|
||||
}
|
||||
}
|
||||
|
||||
project.ext.convertTree = { Map<String, Integer> vars, String inName, String outName=inName ->
|
||||
def defaultKws = [if: '//#if ', ifdef: '//#ifdef ', else: '//#else', endif: '//#endif', eval: '//$$']
|
||||
def cfgKws = [if: '##if ', ifdef: '##ifdef ', else: '##else', endif: '##endif', eval: '#$$']
|
||||
def extensions = [
|
||||
'.java': defaultKws,
|
||||
'.gradle': defaultKws,
|
||||
'.json': defaultKws,
|
||||
'.mcmeta': defaultKws,
|
||||
'.cfg': cfgKws
|
||||
]
|
||||
def inPath = project.file(inName).toPath()
|
||||
def outPath = project.file(outName).toPath()
|
||||
def inPlace = inPath.toAbsolutePath().equals(outPath.toAbsolutePath())
|
||||
project.fileTree(inName).forEach { file ->
|
||||
def outFile = outPath.resolve(inPath.relativize(file.toPath())).toFile()
|
||||
def kws = extensions.find { ext, _ -> file.name.endsWith(ext) }
|
||||
if (kws) {
|
||||
convertFile(kws.value, vars, file, outFile)
|
||||
} else if (!inPlace) {
|
||||
copy {
|
||||
from file
|
||||
into outFile.parentFile
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user