-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PreLaunch: Add ModifyVariable generator
- Loading branch information
Showing
5 changed files
with
159 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/kotlin/com/chattriggers/ctjs/launch/generation/ModifyVariableGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.chattriggers.ctjs.launch.generation | ||
|
||
import com.chattriggers.ctjs.launch.Local | ||
import com.chattriggers.ctjs.launch.ModifyVariable | ||
import com.chattriggers.ctjs.utils.descriptorString | ||
import org.objectweb.asm.tree.MethodNode | ||
import org.spongepowered.asm.mixin.injection.ModifyVariable as SPModifyVariable | ||
|
||
internal class ModifyVariableGenerator( | ||
ctx: GenerationContext, | ||
id: Int, | ||
private var modifyVariable: ModifyVariable, | ||
) : InjectorGenerator(ctx, id) { | ||
override val type = "modifyVariable" | ||
|
||
override fun getInjectionSignature(): InjectionSignature { | ||
val (mappedMethod, method) = ctx.findMethod(modifyVariable.method) | ||
|
||
// Construct a temporary local so we can call Utils.getParameterFromLocal | ||
val tempLocal = Local( | ||
modifyVariable.print, | ||
modifyVariable.parameterName, | ||
modifyVariable.index, | ||
modifyVariable.ordinal, | ||
modifyVariable.type, | ||
mutable = false, | ||
) | ||
|
||
val parameter = Utils.getParameterFromLocal(tempLocal, mappedMethod, name = "ModifyVariable") | ||
|
||
// Update our ModifyVariable annotation since we may have changed the index | ||
modifyVariable = modifyVariable.copy( | ||
index = parameter.local?.index ?: modifyVariable.index, | ||
) | ||
|
||
return InjectionSignature( | ||
mappedMethod, | ||
listOf(parameter.copy(local = null)), | ||
parameter.descriptor, | ||
method.isStatic, | ||
) | ||
} | ||
|
||
override fun attachAnnotation(node: MethodNode, signature: InjectionSignature) { | ||
node.visitAnnotation(SPModifyVariable::class.descriptorString(), true).apply { | ||
visit("method", listOf(signature.targetMethod.toFullDescriptor())) | ||
visit("at", Utils.createAtAnnotation(modifyVariable.at)) | ||
if (modifyVariable.slice != null) | ||
visit("slice", Utils.createSliceAnnotation(modifyVariable.slice!!)) | ||
if (modifyVariable.print != null) | ||
visit("print", modifyVariable.print) | ||
if (modifyVariable.ordinal != null) | ||
visit("ordinal", modifyVariable.ordinal) | ||
if (modifyVariable.index != null) | ||
visit("index", modifyVariable.index) | ||
if (modifyVariable.remap != null) | ||
visit("remap", modifyVariable.remap) | ||
if (modifyVariable.require != null) | ||
visit("require", modifyVariable.require) | ||
if (modifyVariable.expect != null) | ||
visit("expect", modifyVariable.expect) | ||
if (modifyVariable.allow != null) | ||
visit("allow", modifyVariable.allow) | ||
if (modifyVariable.constraints != null) | ||
visit("constraints", modifyVariable.constraints) | ||
visitEnd() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters