Skip to content

Commit

Permalink
hell
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Jun 7, 2024
1 parent 7575635 commit 0698c98
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 58 deletions.
191 changes: 191 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import net.fabricmc.loom.api.LoomGradleExtensionAPI
import net.fabricmc.loom.task.RemapJarTask
import org.gradle.configurationcache.extensions.capitalized
import org.objectweb.asm.*
import org.objectweb.asm.tree.AnnotationNode
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.tree.MethodNode
import java.io.ByteArrayOutputStream
import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.jar.JarOutputStream
import java.util.zip.Deflater

plugins {
java
Expand Down Expand Up @@ -128,6 +156,9 @@ subprojects {
injectAccessWidener = true
dependsOn(shadowJar)
archiveClassifier = null
doLast {
transformJar(project.path, outputs.files.singleFile)
}
}

val common: Configuration by configurations.creating
Expand Down Expand Up @@ -191,6 +222,166 @@ subprojects {
}
}

fun transformJar(projectPath: String, jar: File) {
val contents = linkedMapOf<String, ByteArray>()
JarFile(jar).use {
it.entries().asIterator().forEach { entry ->
if (!entry.isDirectory) {
contents[entry.name] = it.getInputStream(entry).readAllBytes()
}
}
}

jar.delete()

JarOutputStream(jar.outputStream()).use { out ->
out.setLevel(Deflater.BEST_COMPRESSION)
contents.forEach { var (name, data) = it
if(name.startsWith("architectury_inject_${project.name}_common"))
return@forEach

if (name.endsWith(".json") || name.endsWith(".mcmeta")) {
data = (JsonOutput.toJson(JsonSlurper().parse(data)).toByteArray())
} else if (name.endsWith(".class")) {
data = transformClass(projectPath, data)
}

out.putNextEntry(JarEntry(name))
out.write(data)
out.closeEntry()
}
out.finish()
out.close()
}
}

@Suppress("LocalVariableName")
fun transformClass(projectPath: String, bytes: ByteArray): ByteArray {
val node = ClassNode()
ClassReader(bytes).accept(node, 0)

if (node.invisibleAnnotations != null ) {
// Cache the field, so we don't CME the list during the remove
val annotationNodes = node.invisibleAnnotations.toList()
for (annotationNode in annotationNodes) {
if (annotationNode.desc.equals("Ldev/ithundxr/createnumismatics/annotation/asm/CCForgeImpl;")) {
// Remove
node.invisibleAnnotations.remove(annotationNode)

if (projectPath == ":forge") {
// Add the interface that's needed
node.interfaces.add("net/minecraftforge/common/capabilities/ICapabilityProvider")

// getCapability method
run {
val mv = node.visitMethod(
Opcodes.ACC_PUBLIC,
"getCapability",
"(Lnet/minecraftforge/common/capabilities/Capability;Lnet/minecraft/core/Direction;)Lnet/minecraftforge/common/util/LazyOptional;",
null,
null
)
mv.visitCode()

val L1 = Label()

// L0
mv.visitLabel(Label())
mv.visitVarInsn(Opcodes.ALOAD, 0)
mv.visitFieldInsn(
Opcodes.GETFIELD,
node.name,
"computerBehaviour",
"Lcom/simibubi/create/compat/computercraft/AbstractComputerBehaviour;"
)
mv.visitVarInsn(Opcodes.ALOAD, 1)
mv.visitMethodInsn(
Opcodes.INVOKEVIRTUAL,
"com/simibubi/create/compat/computercraft/AbstractComputerBehaviour",
"isPeripheralCap",
"(Lnet/minecraftforge/common/capabilities/Capability;)Z",
false
)
mv.visitJumpInsn(Opcodes.IFEQ, L1)

// L2
mv.visitLabel(Label())
mv.visitVarInsn(Opcodes.ALOAD, 0)
mv.visitFieldInsn(
Opcodes.GETFIELD,
node.name,
"computerBehaviour",
"Lcom/simibubi/create/compat/computercraft/AbstractComputerBehaviour;"
)
mv.visitMethodInsn(
Opcodes.INVOKEVIRTUAL,
"com/simibubi/create/compat/computercraft/AbstractComputerBehaviour",
"getPeripheralCapability",
"()Lnet/minecraftforge/common/util/LazyOptional;",
false
)
mv.visitInsn(Opcodes.ARETURN)

// L1
mv.visitLabel(L1)
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null)
mv.visitVarInsn(Opcodes.ALOAD, 0)
mv.visitVarInsn(Opcodes.ALOAD, 1)
mv.visitVarInsn(Opcodes.ALOAD, 2)
mv.visitMethodInsn(
Opcodes.INVOKESPECIAL,
node.name,
"getCapability",
"(Lnet/minecraftforge/common/capabilities/Capability;Lnet/minecraft/core/Direction;)Lnet/minecraftforge/common/util/LazyOptional;",
false
)
mv.visitInsn(Opcodes.ARETURN)

mv.visitEnd()
}

// invalidateCaps method
run {
val mv = node.visitMethod(Opcodes.ACC_PUBLIC, "invalidateCaps", "()V", null, null)
mv.visitCode()

// L0
val L0 = Label()
mv.visitLabel(L0)
mv.visitVarInsn(Opcodes.ALOAD, 0)
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, node.name, "invalidateCaps", "()V", false)

// L1
mv.visitLabel(Label())
mv.visitVarInsn(Opcodes.ALOAD, 0)
mv.visitFieldInsn(
Opcodes.GETFIELD,
node.name,
"computerBehaviour",
"Lcom/simibubi/create/compat/computercraft/AbstractComputerBehaviour;"
)
mv.visitMethodInsn(
Opcodes.INVOKEVIRTUAL,
"com/simibubi/create/compat/computercraft/AbstractComputerBehaviour",
"removePeripheral",
"()V",
false
)

// L2
mv.visitLabel(Label())
mv.visitInsn(Opcodes.RETURN)

mv.visitEnd()
}
}
}
}
}

return ClassWriter(0).also { node.accept(it) }.toByteArray()
}

fun calculateGitHash(): String {
val stdout = ByteArrayOutputStream()
exec {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.annotation.asm;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

/**
* Horrible hack to make it so self mixins aren't needed, lovely ASM, look away.
*/
@Target(ElementType.TYPE)
public @interface CCForgeImpl {}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.simibubi.create.foundation.utility.Components;
import com.simibubi.create.foundation.utility.Couple;
import com.simibubi.create.foundation.utility.Lang;
import dev.ithundxr.createnumismatics.annotation.asm.CCForgeImpl;
import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy;
import dev.ithundxr.createnumismatics.content.backend.Coin;
import dev.ithundxr.createnumismatics.content.backend.behaviours.SliderStylePriceBehaviour;
Expand All @@ -46,6 +47,7 @@

import java.util.List;

@CCForgeImpl
public class BrassDepositorBlockEntity extends AbstractDepositorBlockEntity implements MenuProvider {

private SliderStylePriceBehaviour price;
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion forge/src/main/resources/numismatics.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
],
"mixins": [
"ServerGamePacketListenerImplMixin",
"self.BrassDepositorBlockEntityCapabilities",
"self.ComputerBehaviourCapabilities",
"self.VendorBlockEntityCapabilities"
],
Expand Down

0 comments on commit 0698c98

Please sign in to comment.