Skip to content

Commit

Permalink
fix RTM: SwitchTypeErrorをワールド全体に発信してた問題
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Mar 3, 2020
1 parent f6d4986 commit 7c29afa
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/anatawa12/fixRtm/Hocks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ package com.anatawa12.fixRtm
import io.netty.buffer.ByteBuf
import io.netty.buffer.Unpooled
import jp.ngt.ngtlib.event.TickProcessEntry
import jp.ngt.ngtlib.io.NGTLog
import jp.ngt.ngtlib.renderer.model.ModelFormatException
import jp.ngt.rtm.modelpack.ResourceType
import jp.ngt.rtm.modelpack.modelset.ResourceSet
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.util.text.TextComponentTranslation
import java.util.zip.Inflater

object ExModelPackManager {
Expand Down Expand Up @@ -92,3 +95,20 @@ fun <K, V>MovingSoundMaker_loadSoundJson_nullCheck(map: Map<K, V>?, domain: Stri
throw ModelFormatException("sound.json for $domain is invalid.")
return map
}


var BlockMarker_onMarkerActivated_player: EntityPlayer? = null
fun BlockMarker_onMarkerActivated(player: EntityPlayer) {
if (!player.world.isRemote)
BlockMarker_onMarkerActivated_player = player
}

fun sendSwitchTypeError(message: String, vararg objects: Any?) {
val player = BlockMarker_onMarkerActivated_player
if (player == null) {
Exception("fixRTM Bug!!! BlockMarker_onMarkerActivated_player is not init-ed").printStackTrace()
NGTLog.sendChatMessageToAll(message, objects)
return
}
player.sendMessage(TextComponentTranslation(message, *objects))
}
1 change: 1 addition & 0 deletions src/main/java/com/anatawa12/fixRtm/asm/FixRtmCorePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class FixRtmCorePlugin : IFMLLoadingPlugin {
, "com.anatawa12.fixRtm.asm.DummyModelSetTransform"
, "com.anatawa12.fixRtm.asm.ModelPackConstructThreadTransformer"
, "com.anatawa12.fixRtm.asm.NPEInMovingSoundMakerTransformer"
, "com.anatawa12.fixRtm.asm.SwitchTypeErrorWillShownToAllTransformer"
//, "com.anatawa12.fixRtm.asm.ConstructDummisTransformer"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.anatawa12.fixRtm.asm

import net.minecraft.launchwrapper.IClassTransformer
import org.objectweb.asm.*
import org.objectweb.asm.Opcodes.*
import org.objectweb.asm.Type.BOOLEAN_TYPE
import org.objectweb.asm.Type.INT_TYPE

@Suppress("PropertyName", "unused")
class SwitchTypeErrorWillShownToAllTransformer : IClassTransformer {
val RailMaker = Type.getObjectType("jp/ngt/rtm/rail/util/RailMaker")!!
val SwitchType = Type.getObjectType("jp/ngt/rtm/rail/util/SwitchType")!!
val BlockMarker = Type.getObjectType("jp/ngt/rtm/rail/BlockMarker")!!
val NGTLog = Type.getObjectType("jp/ngt/ngtlib/io/NGTLog")!!

val World = Type.getObjectType("net/minecraft/world/World")!!
val EntityPlayer = Type.getObjectType("net/minecraft/entity/player/EntityPlayer")!!

val HocksKt = Type.getObjectType("com/anatawa12/fixRtm/HocksKt")!!

val String = Type.getObjectType("java/lang/String")!!
val Object = Type.getObjectType("java/lang/Object")!!

// NGTLog
val sendChatMessageToAll = "sendChatMessageToAll"
val sendChatMessageToAll_desc = "($String[$Object)V"

// RailMaker
val getSwitch = "getSwitch"
val getSwitch_desc = "()$SwitchType"

// BlockMarker
val onMarkerActivated = "onMarkerActivated"
val onMarkerActivated_desc = "($World$INT_TYPE$INT_TYPE$INT_TYPE$EntityPlayer$BOOLEAN_TYPE)$BOOLEAN_TYPE"

// HocksKt
val BlockMarker_onMarkerActivated = "BlockMarker_onMarkerActivated"
val BlockMarker_onMarkerActivated_desc = "($EntityPlayer)V"

val sendSwitchTypeError = "sendSwitchTypeError"
val sendSwitchTypeError_desc = "($String[$Object)V"

override fun transform(name: String, transformedName: String, basicClass: ByteArray?): ByteArray? {
if (basicClass == null) return basicClass
if (transformedName == RailMaker.className) {
val cr = ClassReader(basicClass)
val cw = ClassWriter(0)
val ca = object : ClassVisitor(ASM5, cw) {
override fun visitMethod(access: Int, name: String?, desc: String?, signature: String?, exceptions: Array<out String>?): MethodVisitor {
if (name == getSwitch && desc == getSwitch_desc) {
val mv1 = super.visitMethod(access, name, desc, signature, exceptions)
return object : MethodVisitor(ASM5, mv1) {
override fun visitMethodInsn(opcode: Int, owner: String?, name: String?, desc: String?, itf: Boolean) {
@Suppress("SimplifyBooleanWithConstants")
if (opcode == INVOKESTATIC &&
owner == NGTLog.internalName &&
name == sendChatMessageToAll &&
desc == sendChatMessageToAll_desc &&
itf == false) {
super.visitMethodInsn(INVOKESTATIC, HocksKt.internalName, sendSwitchTypeError, sendSwitchTypeError_desc, false)
} else {
super.visitMethodInsn(opcode, owner, name, desc, itf)
}
}
}
}
return super.visitMethod(access, name, desc, signature, exceptions)
}
}
cr.accept(ca, 0)
return cw.toByteArray()
} else if (transformedName == BlockMarker.className) {
val cr = ClassReader(basicClass)
val cw = ClassWriter(0)
val ca = object : ClassVisitor(ASM5, cw) {
override fun visitMethod(access: Int, name: String?, desc: String?, signature: String?, exceptions: Array<out String>?): MethodVisitor {
if (name == onMarkerActivated && desc == onMarkerActivated_desc) {
val mv1 = super.visitMethod(access, name, desc, signature, exceptions)
return object : MethodVisitor(ASM5, mv1) {
override fun visitCode() {
super.visitCode()
visitVarInsn(ALOAD, 5)
visitMethodInsn(INVOKESTATIC, HocksKt.internalName, BlockMarker_onMarkerActivated, BlockMarker_onMarkerActivated_desc, false)
}
}
}
return super.visitMethod(access, name, desc, signature, exceptions)
}
}
cr.accept(ca, 0)
return cw.toByteArray()
}
return basicClass
}
}

/*
jp.ngt.rtm.modelpack.state.ResourceState.getResourceSet
*/

0 comments on commit 7c29afa

Please sign in to comment.