Skip to content

Commit

Permalink
Use mixins to construct the TestFunctionInfo class
Browse files Browse the repository at this point in the history
There's some funky things going on here, but thankfully they're limited
to test code.
  • Loading branch information
SquidDev committed Jan 16, 2021
1 parent 0be030c commit 23bf33c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 8 deletions.
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ buildscript {
name = "forge"
url = "https://files.minecraftforge.net/maven"
}
maven {
name = "mixin"
url = "https://dist.creeper.host/Sponge/maven"
}
}
dependencies {
classpath 'com.google.code.gson:gson:2.8.1'
classpath 'net.minecraftforge.gradle:ForgeGradle:3.0.190'
classpath 'net.sf.proguard:proguard-gradle:6.1.0beta2'
classpath 'org.ajoberstar.grgit:grgit-gradle:3.0.0'
classpath 'org.spongepowered:mixingradle:0.7-SNAPSHOT'
}
}

Expand All @@ -25,6 +30,7 @@ plugins {
}

apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'org.spongepowered.mixin'
apply plugin: 'org.ajoberstar.grgit'
apply plugin: 'maven-publish'
apply plugin: 'maven'
Expand Down Expand Up @@ -79,6 +85,10 @@ minecraft {
testServer {
workingDirectory project.file('test-files/server')
parent runs.server
properties 'mixin.env.disableRefMap': 'true'

arg "-mixin.config=cctest.mixin.json"
arg "--nogui"

mods {
cctest {
Expand All @@ -93,6 +103,10 @@ minecraft {
accessTransformer file('src/main/resources/META-INF/accesstransformer.cfg')
}

mixin {
add sourceSets.test, "cctest.refmap.json"
}

sourceSets {
main.resources {
srcDir 'src/generated/resources'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.ingame.mixin;

import net.minecraft.test.TestFunctionInfo;
import net.minecraft.test.TestTrackerHolder;
import net.minecraft.util.Rotation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.function.Consumer;

/**
* Mixin to replace final fields and some getters with non-final versions.
*
* Due to (I assume) the magic of proguard, some getters are replaced with constant
* implementations. Thus we need to replace them with a sensible version.
*/
@Mixin( TestFunctionInfo.class )
public class MixinTestFunctionInfo
{
@Shadow
@Mutable
private String batchName;

@Shadow
@Mutable
private String testName;

@Shadow
@Mutable
private String structureName;

@Shadow
@Mutable
private boolean required;

@Shadow
@Mutable
private Consumer<TestTrackerHolder> function;

@Shadow
@Mutable
private int maxTicks;

@Shadow
@Mutable
private long setupTicks;

@Shadow
@Mutable
private Rotation rotation;

@Overwrite
public int getMaxTicks()
{
return this.maxTicks;
}

@Overwrite
public long getSetupTicks()
{
return setupTicks;
}

@Overwrite
public boolean isRequired()
{
return required;
}
}
8 changes: 0 additions & 8 deletions src/test/java/dan200/computercraft/ingame/mod/TestLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
Expand Down Expand Up @@ -110,13 +109,6 @@ private static TestFunctionInfo createTestFunction(
private static void setFinalField( TestFunctionInfo func, String name, Object value ) throws ReflectiveOperationException
{
Field field = TestFunctionInfo.class.getDeclaredField( name );
if( (field.getModifiers() & Modifier.FINAL) != 0 )
{
Field modifiers = Field.class.getDeclaredField( "modifiers" );
modifiers.setAccessible( true );
modifiers.set( field, field.getModifiers() & ~Modifier.FINAL );
}

field.setAccessible( true );
field.set( func, value );
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/cctest.mixin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"required": true,
"package": "dan200.computercraft.ingame.mixin",
"compatibilityLevel": "JAVA_8",
"refmap": "cctest.refmap.json",
"mixins": [
"MixinTestFunctionInfo"
],
"minVersion": "0.8"
}

0 comments on commit 23bf33c

Please sign in to comment.