Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
mattco98 committed Jun 16, 2023
1 parent 652f3ac commit c79bb96
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 126 deletions.
23 changes: 5 additions & 18 deletions src/main/kotlin/com/chattriggers/ctjs/launch/Mappings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ object Mappings {

methods.getOrPut(method.unmappedName, ::mutableListOf).add(MappedMethod(
name = Mapping.fromMapped(method),
parameters = method.parameters.map {
parameters = method.parameters.sortedBy { it.localVariableIndex }.mapIndexed { index, param ->
MappedParameter(
Mapping(it.unmappedName, it.mappedName),
Mapping(param.unmappedName, param.mappedName),
Mapping(
unmappedType.getArgumentByLvtIndex(it.localVariableIndex).internalName,
mappedType.getArgumentByLvtIndex(it.localVariableIndex).internalName,
unmappedType.argumentTypes[index].descriptor,
mappedType.argumentTypes[index].descriptor,
),
it.localVariableIndex,
param.localVariableIndex,
)
},
returnType = Mapping(unmappedType.returnType.descriptor, mappedType.returnType.descriptor)
Expand Down Expand Up @@ -147,17 +147,4 @@ object Mappings {
drop(1).dropLast(1)
} else this).replace('.', '/')
}

fun Type.getArgumentByLvtIndex(index: Int): Type {
require(sort == Type.METHOD)

var currentIndex = 1
for (argumentType in argumentTypes) {
if (index == currentIndex)
return argumentType
currentIndex += argumentType.size
}

error("Invalid LVT index $index for method descriptor $this")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.chattriggers.ctjs.launch
import java.lang.invoke.MethodHandle
import java.lang.invoke.SwitchPoint

data class MixinCallback(val id: Int, val injector: IInjector) {
data class MixinCallback(internal val id: Int, internal val injector: IInjector) {
internal var method: Any? = null
internal var handle: MethodHandle? = null
internal var invalidator = SwitchPoint()
Expand Down
15 changes: 1 addition & 14 deletions src/main/kotlin/com/chattriggers/ctjs/launch/annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class At(
val by: Int?,
val args: List<String>?,
val target: String?,
val desc: Desc?,
val ordinal: Int?,
val opcode: Int?,
val remap: Boolean?,
Expand All @@ -40,7 +39,6 @@ class At(
if (by != other.by) return false
if (!args.listEq(other.args)) return false
if (target != other.target) return false
if (desc != other.desc) return false
if (ordinal != other.ordinal) return false
if (opcode != other.opcode) return false
return remap == other.remap
Expand All @@ -54,7 +52,6 @@ class At(
result = 31 * result + (by ?: 0)
result = 31 * result + args.listHash()
result = 31 * result + (target?.hashCode() ?: 0)
result = 31 * result + (desc?.hashCode() ?: 0)
result = 31 * result + (ordinal ?: 0)
result = 31 * result + (opcode ?: 0)
result = 31 * result + (remap?.hashCode() ?: 0)
Expand All @@ -69,16 +66,6 @@ class At(
}
}

data class Desc(
val value: String,
val id: String?,
val owner: Class<*>?,
val ret: Class<*>?,
val args: List<Class<*>>?,
val min: Int?,
val max: Int?,
)

data class Slice(
val id: String?,
val from: At?,
Expand Down Expand Up @@ -150,9 +137,9 @@ class ModifyArg(
val method: String,
val slice: Slice?,
val at: At,
val index: Int?,
val captureAllParams: Boolean?,
val locals: List<Local>?,
val index: Int?,
val remap: Boolean?,
val require: Int?,
val expect: Int?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.chattriggers.ctjs.launch.generation

import com.chattriggers.ctjs.launch.Inject
import com.chattriggers.ctjs.launch.Mappings
import gg.essential.elementa.state.map
import org.objectweb.asm.Type
import org.objectweb.asm.tree.MethodNode
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo
Expand All @@ -21,16 +19,16 @@ class InjectGenerator(
val (mappedMethod, method) = ctx.findMethod(name, possibleParameters)
val parameters = mutableListOf<Parameter>()

inject.locals?.forEach {
parameters.add(Parameter(Utils.getLocalType(it, mappedMethod), it))
}

if (mappedMethod.returnType.value == "V") {
parameters.add(Parameter(Type.getType(CallbackInfo::class.java), null))
} else {
parameters.add(Parameter(Type.getType(CallbackInfoReturnable::class.java), null))
}

inject.locals?.forEach {
parameters.add(Utils.getParameterFromLocal(it, mappedMethod))
}

return InjectionSignature(
name,
parameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ abstract class InjectorGenerator(protected val ctx: GenerationContext, val id: I
val local = parameters[i].local ?: continue
node.visitParameterAnnotation(i, com.llamalad7.mixinextras.sugar.Local::class.descriptorString(), false).apply {
local.print?.let { visit("print", it) }
local.ordinal?.let { visit("ordinal", it) }
local.index?.let { visit("index", it) }
// local.argsOnly?.let { visit("argsOnly", it) }
local.ordinal?.let { visit("ordinal", it) }

if (local.parameterName != null)
visit("argsOnly", true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ModifyArgGenerator(
}

modifyArg.locals?.forEach {
parameters.add(Parameter(Utils.getLocalType(it, mappedMethod), it))
parameters.add(Utils.getParameterFromLocal(it, mappedMethod))
}

return InjectionSignature(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ModifyArgsGenerator(
val parameters = mutableListOf<Parameter>()
parameters.add(Parameter(Type.getType(Args::class.java), null))
modifyArgs.locals?.forEach {
parameters.add(Parameter(Utils.getLocalType(it, mappedMethod), null))
parameters.add(Utils.getParameterFromLocal(it, mappedMethod))
}

return InjectionSignature(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.chattriggers.ctjs.launch.generation

import com.chattriggers.ctjs.launch.ModifyExpressionValue
import com.chattriggers.ctjs.utils.descriptorString
import org.objectweb.asm.Type
import org.objectweb.asm.tree.MethodNode
import com.llamalad7.mixinextras.injector.ModifyExpressionValue as SPModifyExpressionValue

Expand All @@ -26,7 +25,7 @@ class ModifyExpressionValueGenerator(
}

val parameters = listOf(Parameter(exprType, null)) + modifyExpressionValue.locals?.map {
Parameter(Utils.getLocalType(it, mappedMethod), null)
Utils.getParameterFromLocal(it, mappedMethod)
}.orEmpty()

return InjectionSignature(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.chattriggers.ctjs.launch.generation

import com.chattriggers.ctjs.launch.Mappings
import com.chattriggers.ctjs.launch.ModifyReceiver
import com.chattriggers.ctjs.utils.descriptorString
import org.objectweb.asm.tree.MethodNode
Expand Down Expand Up @@ -32,7 +31,7 @@ class ModifyReceiverGenerator(

val params = listOf(Parameter(owner, null)) +
extraParams.map { Parameter(it, null) } +
modifyReceiver.locals?.map { Parameter(Utils.getLocalType(it, mappedMethod), null) }.orEmpty()
modifyReceiver.locals?.map { Utils.getParameterFromLocal(it, mappedMethod) }.orEmpty()

return InjectionSignature(
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RedirectGenerator(
}

redirect.locals?.forEach {
parameters.add(Parameter(Utils.getLocalType(it, mappedMethod), it))
parameters.add(Utils.getParameterFromLocal(it, mappedMethod))
}

return InjectionSignature(
Expand Down
34 changes: 10 additions & 24 deletions src/main/kotlin/com/chattriggers/ctjs/launch/generation/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ internal object Utils {
visit("by", at.by)
if (at.args != null)
visit("args", at.args)
if (at.desc != null)
visit("desc", createDescAnnotation(at.desc))
if (at.ordinal != null)
visit("ordinal", at.ordinal)
if (at.opcode != null)
Expand Down Expand Up @@ -58,25 +56,6 @@ internal object Utils {
}
}

fun createDescAnnotation(desc: Desc): AnnotationNode {
return AnnotationNode(org.spongepowered.asm.mixin.injection.Desc::class.java.descriptorString()).apply {
if (desc.id != null)
visit("id", desc.id)
if (desc.owner != null)
visit("owner", Type.getType(desc.owner))
visit("value", desc.value)
if (desc.ret != null)
visit("ret", Type.getType(desc.ret))
if (desc.args != null)
visit("args", desc.args)
if (desc.min != null)
visit("min", desc.min)
if (desc.max != null)
visit("max", desc.max)
visitEnd()
}
}

fun createSliceAnnotation(slice: Slice): AnnotationNode {
return AnnotationNode(org.spongepowered.asm.mixin.injection.Slice::class.java.descriptorString()).apply {
if (slice.id != null)
Expand Down Expand Up @@ -266,8 +245,10 @@ internal object Utils {
error("Unable to find method $unmappedName in class ${mappedClass.name.original}")
}

fun getLocalType(local: Local, method: Mappings.MappedMethod): Type {
return when {
fun getParameterFromLocal(local: Local, method: Mappings.MappedMethod): InjectorGenerator.Parameter {
var modifiedLocal = local

val type = when {
local.print == true -> {
// The type doesn't matter, it won't actually be applied
"I"
Expand All @@ -276,8 +257,11 @@ internal object Utils {
require(local.type == null && local.index == null && local.ordinal == null) {
"Local that specifies parameterName cannot specify type, index, or ordinal"
}
method.parameters.find { p -> p.name.original == local.parameterName }?.type?.value

val parameter = method.parameters.find { p -> p.name.original == local.parameterName }
?: error("Could not find parameter \"${local.parameterName}\" in method ${method.name.original}")
modifiedLocal = local.copy(index = parameter.lvtIndex)
parameter.type.value
}
local.type != null -> {
if (local.index != null) {
Expand All @@ -294,6 +278,8 @@ internal object Utils {
else -> error("Local must specify one of the following options: \"print\"; \"parameterName\"; " +
"\"type\" and either \"ordinal\" or \"index\"")
}.let(Type::getType)

return InjectorGenerator.Parameter(type, modifiedLocal)
}

sealed class AtTarget(val name: String)
Expand Down
Loading

0 comments on commit c79bb96

Please sign in to comment.