-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmixinLoadingHacks.gradle
120 lines (105 loc) · 5.49 KB
/
mixinLoadingHacks.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
import static java.util.Collections.singletonMap
// intellij is wrong, this is required
import org.gradle.api.artifacts.transform.TransformParameters
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.nio.file.attribute.BasicFileAttributes
// This is required because normally, FML on 1.12.2 doesn't load tweaker plugins as mods
// Anything that uses Mixin on 1.12.2 is actually a tweaker plugin that loads mixin
// to be able to actually load a mod from the same jar, Mixin FML agent then injects that jar into FML mod loading\
// but this breaks in dev on FG3+, because mixin FML agent is looking for the string "deobfedDeps" in the jar name
// this artifact transform just adds the string Mixin FML agent is looking for into the jar name
def artifactType = Attribute.of('artifactType', String)
def renamedForMixinFmlAgent = Attribute.of('renamedForMixinFmlAgent', Boolean)
def hasCubicChunksBuild = gradle.includedBuilds.any { it.name == "CubicChunks" || it.name == "1.12" }
dependencies {
attributesSchema {
attribute(renamedForMixinFmlAgent)
}
artifactTypes.named("jar") {
attributes.attribute(renamedForMixinFmlAgent, false)
}
}
configurations.configureEach {
if (canBeResolved) {
attributes.attribute(renamedForMixinFmlAgent, true)
}
}
dependencies {
registerTransform(RenameForMixinFmlAgentTransform) {
from.attribute(renamedForMixinFmlAgent, false).attribute(artifactType, "jar")
to.attribute(renamedForMixinFmlAgent, true).attribute(artifactType, "jar")
parameters {
transformCubicChunksJar = !hasCubicChunksBuild
}
}
}
abstract class RenameForMixinFmlAgentTransform implements TransformAction<Parameters> {
interface Parameters extends TransformParameters {
@Input
Property<Boolean> getTransformCubicChunksJar()
}
@InputArtifact
abstract Provider<FileSystemLocation> getInputArtifact()
@Override
void transform(TransformOutputs outputs) {
def input = inputArtifact.get().asFile
if (input.name.contains('CubicChunks')) {
if (!input.exists()) {
// For some reason, for this specific project (what is special here?), when intellij is importing it
// an exception in artifact transforms actually makes it fail to build the model
// An exception normally always happens in this situation (transforming a composite build dependency substitution)
// but for CubicWorldGen specifically it causes an error in intellij
// as a workaround, return here to not throw an exception, this seems to make intellij happy
return;
}
// skip transforming CC when it's a substituted build - this exists only for running in IDE
// and in this case the actual project will be used when we don't transform
if (!parameters.transformCubicChunksJar.get()) {
outputs.file(inputArtifact)
return;
}
def renamedJar = outputs.file('hackForMixinFMLAgent_deobfedDeps_' + input.name)
Files.copy(input.toPath(), renamedJar.toPath())
} else if (input.name.contains('223896')) {
File modifiedJar = outputs.file('hackForMixinFMLAgent_deobfedDeps_MalisisCoreNoMixin_' + input.name)
try (FileSystem inFs = FileSystems.newFileSystem(input.toPath(), getClass().classLoader)
FileSystem outFs = createJarAndOpenFileSystem(modifiedJar.toPath())) {
copyFilteredMalisisCoreContents(inFs.getPath("/"), outFs.getPath("/"))
}
} else {
outputs.file(inputArtifact)
}
}
// remove Mixin from MalisisCore jar - this breaks because MalisisCore is above other sources of Mixin on the classpath
static void copyFilteredMalisisCoreContents(java.nio.file.Path from, java.nio.file.Path to) throws IOException {
final fromAbsolute = from.toAbsolutePath();
Files.walkFileTree(from, new java.nio.file.FileVisitor<java.nio.file.Path>() {
@Override FileVisitResult preVisitDirectory(java.nio.file.Path dir, BasicFileAttributes attrs) throws IOException {
if (dir.toString().startsWith('/org/spongepowered')) {
return FileVisitResult.SKIP_SUBTREE
}
Files.createDirectories(to.resolve(fromAbsolute.relativize(dir).toString()))
return FileVisitResult.CONTINUE;
}
@Override FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, to.resolve(fromAbsolute.relativize(file).toString()), StandardCopyOption.REPLACE_EXISTING)
return FileVisitResult.CONTINUE;
}
@Override FileVisitResult visitFileFailed(java.nio.file.Path file, IOException exc) {
return FileVisitResult.CONTINUE;
}
@Override FileVisitResult postVisitDirectory(java.nio.file.Path dir, IOException exc) {
return FileVisitResult.CONTINUE;
}
});
}
static FileSystem createJarAndOpenFileSystem(java.nio.file.Path jarPath) throws IOException {
URI uri = jarPath.toUri();
URI jarUri = new URI("jar:" + uri.getScheme(), uri.getPath(), null);
return FileSystems.newFileSystem(jarUri, singletonMap("create", "true"));
}
}