Skip to content

Commit

Permalink
Forge: Implements REP protection #63
Browse files Browse the repository at this point in the history
  • Loading branch information
joserobjr committed Sep 14, 2016
1 parent 8d94a9b commit 029a875
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public String[] getASMTransformerClass()
ModEnv.rayTraceResultClass = "net.minecraft.util.math.RayTraceResult";

return new String[]{
"br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore.JammerPartTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore.WirelessBoltTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.EntityREPTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.JammerPartTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.WirelessBoltTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.immersiveintegration.TileItemRobinTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.zettaindustries.QuarryFixerBlockTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.zettaindustries.BlockSulfurTransformer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public String[] getASMTransformerClass()
ModEnv.seven = true;

return new String[]{
"br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore.JammerPartTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore.WirelessBoltTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.EntityREPTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.JammerPartTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.WirelessBoltTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.immersiveintegration.TileItemRobinTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.zettaindustries.QuarryFixerBlockTransformer",
"br.com.gamemods.minecity.forge.base.core.transformer.mod.zettaindustries.BlockSulfurTransformer",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe;

import br.com.gamemods.minecity.api.CollectionUtil;
import br.com.gamemods.minecity.forge.base.core.MethodPatcher;
import br.com.gamemods.minecity.forge.base.core.ModEnv;
import br.com.gamemods.minecity.forge.base.core.Referenced;
import br.com.gamemods.minecity.forge.base.core.transformer.InsertSetterGetterTransformer;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.*;

import java.util.Comparator;

import static org.objectweb.asm.Opcodes.*;

@Referenced("br.com.gamemods.minecity.forge.mc_1_7_10.core.MineCitySevenCoreMod")
@Referenced("br.com.gamemods.minecity.forge.mc_1_10_2.core.MineCityFrostCoreMod")
@MethodPatcher
public class EntityREPTransformer extends InsertSetterGetterTransformer
{
public EntityREPTransformer()
{
super("codechicken.wirelessredstone.addons.EntityREP",
"br.com.gamemods.minecity.forge.base.accessors.entity.projectile.ProjectileShooter",
"mineCityShooter",
"br/com/gamemods/minecity/forge/base/protection/wrcbe/IEntityREP",
"setMineCityShooter", "getMineCityShooter"
);
}

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass)
{
if(!"codechicken.wirelessredstone.addons.EntityREP".equals(transformedName))
return basicClass;

basicClass = super.transform(name, transformedName, basicClass);

ClassNode node = new ClassNode();
ClassReader reader = new ClassReader(basicClass);
reader.accept(node, 0);

String bolt = "br/com/gamemods/minecity/forge/base/protection/wrcbe/IWirelessBolt";

for(MethodNode method : node.methods)
{
if(method.name.equals("detonate"))
{
CollectionUtil.stream(method.instructions.iterator())
.filter(ins-> ins.getOpcode() == INVOKESPECIAL).map(MethodInsnNode.class::cast)
.filter(ins-> ins.name.equals("<init>"))
.filter(ins-> ins.owner.equals("codechicken/wirelessredstone/core/WirelessBolt"))
.map(ins-> method.instructions.indexOf(ins))
.sorted(Comparator.reverseOrder()).mapToInt(Integer::intValue)
.forEachOrdered(index -> {
InsnList list = new InsnList();
list = new InsnList();
list.add(new VarInsnNode(ALOAD, 0));
list.add(new MethodInsnNode(INVOKEINTERFACE,
bolt, "createdFromProjectile", "(Lbr/com/gamemods/minecity/forge/base/accessors/entity/projectile/EntityProjectile;)L"+bolt+";",
true
));
list.add(new TypeInsnNode(CHECKCAST, "codechicken/wirelessredstone/core/WirelessBolt"));
method.instructions.insert(method.instructions.get(index), list);
});
break;
}
}

MethodNode method = new MethodNode(ACC_PUBLIC, "getShooterEntity", "()Lbr/com/gamemods/minecity/forge/base/accessors/entity/base/IEntityLivingBase;", null, null);
method.visitCode();
method.visitVarInsn(ALOAD, 0);
method.visitFieldInsn(GETFIELD, transformedName.replace('.','/'), "shootingEntity", "Lnet/minecraft/entity/EntityLivingBase;");
method.visitInsn(ARETURN);
method.visitEnd();
node.methods.add(method);


ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
node.accept(writer);
basicClass = ModEnv.saveClass(transformedName, writer.toByteArray());
return basicClass;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore;
package br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe;

import br.com.gamemods.minecity.api.CollectionUtil;
import br.com.gamemods.minecity.forge.base.core.MethodPatcher;
Expand Down Expand Up @@ -50,20 +50,6 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)
));
list.add(new TypeInsnNode(CHECKCAST, "codechicken/wirelessredstone/core/WirelessBolt"));
method.instructions.insert(method.instructions.get(index), list);

/*
VarInsnNode var = (VarInsnNode) method.instructions.get(index + 1);
InsnList list = new InsnList();
list = new InsnList();
list.add(new VarInsnNode(ALOAD, var.var));
list.add(new TypeInsnNode(CHECKCAST, bolt));
list.add(new VarInsnNode(ALOAD, 0));
list.add(new MethodInsnNode(INVOKEINTERFACE,
bolt, "createdFromPart", "(L"+jammerPart+";)V",
false
));
method.instructions.insert(var, list);
*/
});

if(method.name.equals("jamTile"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore;
package br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe;

import br.com.gamemods.minecity.api.CollectionUtil;
import br.com.gamemods.minecity.forge.base.core.MethodPatcher;
Expand All @@ -16,11 +16,12 @@

import static org.objectweb.asm.Opcodes.*;

@Referenced("br.com.gamemods.minecity.forge.mc_1_7_10.core.MineCitySevenCoreMod")
@Referenced("br.com.gamemods.minecity.forge.mc_1_10_2.core.MineCityFrostCoreMod")
@Referenced
@MethodPatcher
public class WirelessBoltTransformer extends InsertSetterGetterTransformer
{
@Referenced("br.com.gamemods.minecity.forge.mc_1_7_10.core.MineCitySevenCoreMod")
@Referenced("br.com.gamemods.minecity.forge.mc_1_10_2.core.MineCityFrostCoreMod")
public WirelessBoltTransformer()
{
super("codechicken.wirelessredstone.core.WirelessBolt",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package br.com.gamemods.minecity.forge.base.protection.wrcbe;

import br.com.gamemods.minecity.forge.base.MineCityForge;
import br.com.gamemods.minecity.forge.base.accessors.entity.base.IEntityLivingBase;
import br.com.gamemods.minecity.forge.base.accessors.entity.projectile.EntityProjectile;
import br.com.gamemods.minecity.forge.base.accessors.entity.projectile.ProjectileShooter;
import br.com.gamemods.minecity.forge.base.core.Referenced;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.EntityREPTransformer;

@Referenced(at = EntityREPTransformer.class)
public interface IEntityREP extends EntityProjectile
{
@Referenced(at = EntityREPTransformer.class)
IEntityLivingBase getShooterEntity();

@Override
default void detectShooter(MineCityForge mod)
{
IEntityLivingBase entity = getShooterEntity();
if(entity != null)
setShooter(new ProjectileShooter(getEntityPos(mod), entity));
else
setShooter(new ProjectileShooter(getEntityPos(mod)));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.com.gamemods.minecity.forge.base.protection.wrcbe;

import br.com.gamemods.minecity.forge.base.core.Referenced;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore.JammerPartTransformer;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.JammerPartTransformer;
import br.com.gamemods.minecity.forge.base.protection.forgemultipart.ITMultiPart;

@Referenced(at = JammerPartTransformer.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package br.com.gamemods.minecity.forge.base.protection.wrcbe;

import br.com.gamemods.minecity.forge.base.accessors.entity.projectile.EntityProjectile;
import br.com.gamemods.minecity.forge.base.accessors.entity.projectile.Projectile;
import br.com.gamemods.minecity.forge.base.accessors.entity.projectile.ProjectileShooter;
import br.com.gamemods.minecity.forge.base.core.ModEnv;
import br.com.gamemods.minecity.forge.base.core.Referenced;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore.JammerPartTransformer;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore.WirelessBoltTransformer;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.EntityREPTransformer;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.JammerPartTransformer;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.WirelessBoltTransformer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -19,6 +21,17 @@ default IWirelessBolt createdFromPart(IJammerPart part)
return this;
}

@Referenced(at = EntityREPTransformer.class)
default IWirelessBolt createdFromProjectile(EntityProjectile projectile)
{
ProjectileShooter shooter = projectile.getShooter();
if(shooter == null)
shooter = new ProjectileShooter(projectile.getEntityPos(ModEnv.entityProtections.mod), projectile);

setShooter(shooter);
return this;
}

@SuppressWarnings("deprecation")
@Nullable
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import br.com.gamemods.minecity.forge.base.accessors.entity.projectile.ProjectileShooter;
import br.com.gamemods.minecity.forge.base.core.ModEnv;
import br.com.gamemods.minecity.forge.base.core.Referenced;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore.JammerPartTransformer;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbecore.WirelessBoltTransformer;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.JammerPartTransformer;
import br.com.gamemods.minecity.forge.base.core.transformer.mod.wrcbe.WirelessBoltTransformer;
import br.com.gamemods.minecity.forge.base.protection.ShooterDamageSource;
import br.com.gamemods.minecity.structure.ClaimedChunk;
import net.minecraft.entity.Entity;
Expand Down

0 comments on commit 029a875

Please sign in to comment.