Skip to content

Commit

Permalink
Code cleanup: remove unnecessary casts and explicit vararg calls
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenraven committed Jan 25, 2023
1 parent 15b20bf commit adb4297
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 196 deletions.
24 changes: 12 additions & 12 deletions src/main/java/ru/fewizz/idextender/asm/AsmUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static boolean transformInlinedSizeMethod(
boolean found = false;
boolean foundOnce = false;
final int i = 0;
final ListIterator<AbstractInsnNode> it = (ListIterator<AbstractInsnNode>) method.instructions.iterator();
final ListIterator<AbstractInsnNode> it = method.instructions.iterator();
while (it.hasNext()) {
final AbstractInsnNode insn = it.next();
if (insn.getOpcode() == 3 && oldValue == 0) {
Expand Down Expand Up @@ -122,23 +122,23 @@ public static boolean transformInlinedSizeMethod(
if (found) {
foundOnce = true;
if (newValue == 0) {
it.set((AbstractInsnNode) new InsnNode(3));
it.set(new InsnNode(3));
} else if (newValue == 1) {
it.set((AbstractInsnNode) new InsnNode(4));
it.set(new InsnNode(4));
} else if (newValue == 2) {
it.set((AbstractInsnNode) new InsnNode(5));
it.set(new InsnNode(5));
} else if (newValue == 3) {
it.set((AbstractInsnNode) new InsnNode(6));
it.set(new InsnNode(6));
} else if (newValue == 4) {
it.set((AbstractInsnNode) new InsnNode(7));
it.set(new InsnNode(7));
} else if (newValue == 5) {
it.set((AbstractInsnNode) new InsnNode(8));
it.set(new InsnNode(8));
} else if (newValue >= -128 && newValue <= 127) {
it.set((AbstractInsnNode) new IntInsnNode(16, newValue));
it.set(new IntInsnNode(16, newValue));
} else if (newValue >= -32768 && newValue <= 32767) {
it.set((AbstractInsnNode) new IntInsnNode(17, newValue));
it.set(new IntInsnNode(17, newValue));
} else {
it.set((AbstractInsnNode) new LdcInsnNode((Object) newValue));
it.set(new LdcInsnNode(newValue));
}
found = false;
}
Expand All @@ -151,8 +151,8 @@ public static boolean transformInlinedSizeMethod(

public static void dump(final InsnList list) {
final Textifier textifier = new Textifier();
final TraceMethodVisitor visitor = new TraceMethodVisitor((Printer) textifier);
list.accept((MethodVisitor) visitor);
final TraceMethodVisitor visitor = new TraceMethodVisitor(textifier);
list.accept(visitor);
final PrintWriter writer = new PrintWriter(System.out);
textifier.print(writer);
writer.flush();
Expand Down
57 changes: 23 additions & 34 deletions src/main/java/ru/fewizz/idextender/asm/ClassEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,51 @@
import ru.fewizz.idextender.asm.transformer.*;

public enum ClassEdit {
SelfHooks((IClassNodeTransformer) new SelfHooks(), new String[] {"ru.fewizz.idextender.Hooks"}),
VanillaBlockFire((IClassNodeTransformer) new VanillaBlockFire(), new String[] {"net.minecraft.block.BlockFire"}),
VanillaStatList((IClassNodeTransformer) new VanillaStatList(), new String[] {"net.minecraft.stats.StatList"}),
SelfHooks(new SelfHooks(), new String[] {"ru.fewizz.idextender.Hooks"}),
VanillaBlockFire(new VanillaBlockFire(), new String[] {"net.minecraft.block.BlockFire"}),
VanillaStatList(new VanillaStatList(), new String[] {"net.minecraft.stats.StatList"}),
VanillaAnvilChunkLoader(
(IClassNodeTransformer) new VanillaAnvilChunkLoader(),
new String[] {"net.minecraft.world.chunk.storage.AnvilChunkLoader"}),
VanillaChunk((IClassNodeTransformer) new VanillaChunk(), new String[] {"net.minecraft.world.chunk.Chunk"}),
new VanillaAnvilChunkLoader(), new String[] {"net.minecraft.world.chunk.storage.AnvilChunkLoader"}),
VanillaChunk(new VanillaChunk(), new String[] {"net.minecraft.world.chunk.Chunk"}),
VanillaExtendedBlockStorage(
(IClassNodeTransformer) new VanillaExtendedBlockStorage(),
new String[] {"net.minecraft.world.chunk.storage.ExtendedBlockStorage"}),
VanillaDataWatcher(
(IClassNodeTransformer) new VanillaDataWatcher(), new String[] {"net.minecraft.entity.DataWatcher"}),
new VanillaExtendedBlockStorage(), new String[] {"net.minecraft.world.chunk.storage.ExtendedBlockStorage"}),
VanillaDataWatcher(new VanillaDataWatcher(), new String[] {"net.minecraft.entity.DataWatcher"}),
VanillaNetHandlerPlayClient(
(IClassNodeTransformer) new VanillaNetHandlerPlayClient(),
new String[] {"net.minecraft.client.network.NetHandlerPlayClient"}),
new VanillaNetHandlerPlayClient(), new String[] {"net.minecraft.client.network.NetHandlerPlayClient"}),
VanillaS21PacketChunkData(
(IClassNodeTransformer) new VanillaS21PacketChunkData(),
new String[] {"net.minecraft.network.play.server.S21PacketChunkData"}),
new VanillaS21PacketChunkData(), new String[] {"net.minecraft.network.play.server.S21PacketChunkData"}),
VanillaS22PacketMultiBlockChange(
(IClassNodeTransformer) new VanillaS22PacketMultiBlockChange(),
new VanillaS22PacketMultiBlockChange(),
new String[] {"net.minecraft.network.play.server.S22PacketMultiBlockChange"}),
VanillaS24PacketBlockActivation(
(IClassNodeTransformer) new VanillaS24PacketBlockActivation(),
new VanillaS24PacketBlockActivation(),
new String[] {"net.minecraft.network.play.server.S24PacketBlockAction"}),
VanillaS26PacketMapChunkBulk(
(IClassNodeTransformer) new VanillaS26PacketMapChunkBulk(),
new VanillaS26PacketMapChunkBulk(),
new String[] {"net.minecraft.network.play.server.S26PacketMapChunkBulk"}),
VanillaRenderGlobal(
(IClassNodeTransformer) new VanillaRenderGlobal(),
new String[] {"net.minecraft.client.renderer.RenderGlobal"}),
VanillaRenderGlobal(new VanillaRenderGlobal(), new String[] {"net.minecraft.client.renderer.RenderGlobal"}),
VanillaPlayerControllerMP(
(IClassNodeTransformer) new VanillaPlayerControllerMP(),
new String[] {"net.minecraft.client.multiplayer.PlayerControllerMP"}),
new VanillaPlayerControllerMP(), new String[] {"net.minecraft.client.multiplayer.PlayerControllerMP"}),
VanillaItemInWorldManager(
(IClassNodeTransformer) new VanillaItemInWorldManager(),
new String[] {"net.minecraft.server.management.ItemInWorldManager"}),
VanillaWorld((IClassNodeTransformer) new VanillaWorld(), new String[] {"net.minecraft.world.World"}),
FmlRegistry((IClassNodeTransformer) new FmlRegistry(), new String[] {
new VanillaItemInWorldManager(), new String[] {"net.minecraft.server.management.ItemInWorldManager"}),
VanillaWorld(new VanillaWorld(), new String[] {"net.minecraft.world.World"}),
FmlRegistry(new FmlRegistry(), new String[] {
"cpw.mods.fml.common.registry.GameData", "cpw.mods.fml.common.registry.FMLControlledNamespacedRegistry"
}),
CofhBlockHelper((IClassNodeTransformer) new CofhBlockHelper(), new String[] {"cofh.lib.util.helpers.BlockHelper"}),
CofhBlockHelper(new CofhBlockHelper(), new String[] {"cofh.lib.util.helpers.BlockHelper"}),
UndergroundBiomesOreUBifier(
(IClassNodeTransformer) new UndergroundBiomesOreUBifier(),
new String[] {"exterminatorJeff.undergroundBiomes.worldGen.OreUBifier"}),
new UndergroundBiomesOreUBifier(), new String[] {"exterminatorJeff.undergroundBiomes.worldGen.OreUBifier"}),
UndergroundBiomesBiomeUndergroundDecorator(
(IClassNodeTransformer) new UndergroundBiomesBiomeUndergroundDecorator(),
new UndergroundBiomesBiomeUndergroundDecorator(),
new String[] {"exterminatorJeff.undergroundBiomes.worldGen.BiomeUndergroundDecorator"}),
MFQM((IClassNodeTransformer) new MFQM(), new String[] {"MoreFunQuicksandMod.main.MFQM"}),
WorldEditBaseBlock(
(IClassNodeTransformer) new WorldEditBaseBlock(), new String[] {"com.sk89q.worldedit.blocks.BaseBlock"});
MFQM(new MFQM(), new String[] {"MoreFunQuicksandMod.main.MFQM"}),
WorldEditBaseBlock(new WorldEditBaseBlock(), new String[] {"com.sk89q.worldedit.blocks.BaseBlock"});

private static final Map<String, ClassEdit> editMap;
private final IClassNodeTransformer transformer;
private final String[] classNames;

private ClassEdit(final IClassNodeTransformer transformer, final String[] classNames) {
ClassEdit(final IClassNodeTransformer transformer, final String[] classNames) {
this.transformer = transformer;
this.classNames = classNames;
}
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/ru/fewizz/idextender/asm/IETransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,32 @@ public byte[] transform(final String name, final String transformedName, final b
if (edit == null) {
return bytes;
}
IETransformer.logger.debug("Patching {} with {}...", new Object[] {transformedName, edit.getName()});
IETransformer.logger.debug("Patching {} with {}...", transformedName, edit.getName());
final ClassNode cn = new ClassNode(Opcodes.ASM5);
final ClassReader reader = new ClassReader(bytes);
final int readFlags = 0;
reader.accept((ClassVisitor) cn, 0);
reader.accept(cn, 0);
try {
edit.getTransformer().transform(cn, IETransformer.isObfuscated);
} catch (AsmTransformException t) {
IETransformer.logger.error(
"Error transforming {} with {}: {}",
new Object[] {transformedName, edit.getName(), t.getMessage()});
"Error transforming {} with {}: {}", transformedName, edit.getName(), t.getMessage());
throw t;
} catch (Throwable t2) {
IETransformer.logger.error(
"Error transforming {} with {}: {}",
new Object[] {transformedName, edit.getName(), t2.getMessage()});
"Error transforming {} with {}: {}", transformedName, edit.getName(), t2.getMessage());
throw new RuntimeException(t2);
}
final ClassWriter writer = new ClassWriter(0);
try {
final ClassVisitor check = (ClassVisitor) new CheckClassAdapter((ClassVisitor) writer);
final ClassVisitor check = new CheckClassAdapter(writer);
cn.accept(check);
} catch (Throwable t3) {
IETransformer.logger.error(
"Error verifying {} transformed with {}: {}",
new Object[] {transformedName, edit.getName(), t3.getMessage()});
"Error verifying {} transformed with {}: {}", transformedName, edit.getName(), t3.getMessage());
throw new RuntimeException(t3);
}
IETransformer.logger.debug("Patched {} successfully.", new Object[] {edit.getName()});
IETransformer.logger.debug("Patched {} successfully.", edit.getName());
return writer.toByteArray();
}

Expand Down
74 changes: 24 additions & 50 deletions src/main/java/ru/fewizz/idextender/asm/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,38 @@

public enum Name {
hooks("ru/fewizz/idextender/Hooks"),
hooks_create16BArray(Name.hooks, "create16BArray", (String) null, (String) null, "()[S"),
hooks_create16BArray(Name.hooks, "create16BArray", null, null, "()[S"),
hooks_getBlockData(
Name.hooks,
"getBlockData",
(String) null,
(String) null,
"(Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;)[B"),
Name.hooks, "getBlockData", null, null, "(Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;)[B"),
hooks_setBlockData(
Name.hooks,
"setBlockData",
(String) null,
(String) null,
"(Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;[BI)V"),
Name.hooks, "setBlockData", null, null, "(Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;[BI)V"),
hooks_writeChunkToNbt(
Name.hooks,
"writeChunkToNbt",
(String) null,
(String) null,
null,
null,
"(Lnet/minecraft/nbt/NBTTagCompound;Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;)V"),
hooks_readChunkFromNbt(
Name.hooks,
"readChunkFromNbt",
(String) null,
(String) null,
null,
null,
"(Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;Lnet/minecraft/nbt/NBTTagCompound;)V"),
hooks_getBlockId(
Name.hooks,
"getBlockId",
(String) null,
(String) null,
"(Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;III)I"),
Name.hooks, "getBlockId", null, null, "(Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;III)I"),
hooks_getBlockById(
Name.hooks,
"getBlock",
(String) null,
(String) null,
null,
null,
"(Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;III)Lnet/minecraft/block/Block;"),
hooks_setBlockId(
Name.hooks,
"setBlockId",
(String) null,
(String) null,
"(Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;IIII)V"),
Name.hooks, "setBlockId", null, null, "(Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;IIII)V"),
hooks_getIdFromBlockWithCheck(
Name.hooks,
"getIdFromBlockWithCheck",
(String) null,
(String) null,
null,
null,
"(Lnet/minecraft/block/Block;Lnet/minecraft/block/Block;)I"),
acl("net/minecraft/world/chunk/storage/AnvilChunkLoader", "aqk"),
block("net/minecraft/block/Block", "aji"),
Expand All @@ -75,7 +59,7 @@ public enum Name {
entityPlayer("net/minecraft/entity/player/EntityPlayer", "yz"),
ebs_getBlock(
Name.extendedBlockStorage, "getBlockByExtId", "a", "func_150819_a", "(III)Lnet/minecraft/block/Block;"),
ebs_setBlock(Name.extendedBlockStorage, "func_150818_a", "a", (String) null, "(IIILnet/minecraft/block/Block;)V"),
ebs_setBlock(Name.extendedBlockStorage, "func_150818_a", "a", null, "(IIILnet/minecraft/block/Block;)V"),
ebs_getBlockLSBArray(Name.extendedBlockStorage, "getBlockLSBArray", "g", "func_76658_g", "()[B"),
ebs_getBlockMSBArray(
Name.extendedBlockStorage,
Expand Down Expand Up @@ -138,36 +122,26 @@ public enum Name {
"a",
"func_147287_a",
"(Lnet/minecraft/network/play/server/S22PacketMultiBlockChange;)V"),
s22_init_server(Name.s22, "<init>", (String) null, (String) null, "(I[SLnet/minecraft/world/chunk/Chunk;)V"),
s21_undefined1(Name.s21, "func_149275_c", "c", (String) null, "()I"),
s22_init_server(Name.s22, "<init>", null, null, "(I[SLnet/minecraft/world/chunk/Chunk;)V"),
s21_undefined1(Name.s21, "func_149275_c", "c", null, "()I"),
s21_undefined2(
Name.s21,
"func_149269_a",
"a",
(String) null,
null,
"(Lnet/minecraft/world/chunk/Chunk;ZI)Lnet/minecraft/network/play/server/S21PacketChunkData$Extracted;"),
renderGlobal_playAuxSFX(
Name.renderGlobal, "playAuxSFX", "a", "func_72706_a", "(Lnet/minecraft/entity/player/EntityPlayer;IIIII)V"),
playerControllerMP_onPlayerDestroyBlock(
Name.playerControllerMP, "onPlayerDestroyBlock", "a", "func_78751_a", "(IIII)Z"),
itemInWorldManager_tryHarvestBlock(Name.itemInWorldManager, "tryHarvestBlock", "b", "func_73084_b", "(III)Z"),
world_breakBlock(Name.world, "func_147480_a", "a", (String) null, "(IIIZ)Z"),
world_breakBlock(Name.world, "func_147480_a", "a", null, "(IIIZ)Z"),
ub_bud("exterminatorJeff/undergroundBiomes/worldGen/BiomeUndergroundDecorator"),
ub_bud_replaceChunkOres_world(
Name.ub_bud, "replaceChunkOres", (String) null, (String) null, "(IILnet/minecraft/world/World;)V"),
ub_bud_replaceChunkOres_world(Name.ub_bud, "replaceChunkOres", null, null, "(IILnet/minecraft/world/World;)V"),
ub_bud_replaceChunkOres_iChunkProvider(
Name.ub_bud,
"replaceChunkOres",
(String) null,
(String) null,
"(Lnet/minecraft/world/chunk/IChunkProvider;II)V"),
Name.ub_bud, "replaceChunkOres", null, null, "(Lnet/minecraft/world/chunk/IChunkProvider;II)V"),
MFQM("MoreFunQuicksandMod/main/MFQM"),
MFQM_preInit(
Name.MFQM,
"preInit",
(String) null,
(String) null,
"(Lcpw/mods/fml/common/event/FMLPreInitializationEvent;)V");
MFQM_preInit(Name.MFQM, "preInit", null, null, "(Lcpw/mods/fml/common/event/FMLPreInitializationEvent;)V");

public final Name clazz;
public final String deobf;
Expand All @@ -176,19 +150,19 @@ public enum Name {
public final String desc;
public String obfDesc;

private Name(final String deobf) {
Name(final String deobf) {
this(deobf, deobf);
}

private Name(final String deobf, final String obf) {
Name(final String deobf, final String obf) {
this.clazz = null;
this.deobf = deobf;
this.obf = obf;
this.srg = deobf;
this.desc = null;
}

private Name(final Name clazz, final String deobf, final String obf, final String srg, final String desc) {
Name(final Name clazz, final String deobf, final String obf, final String srg, final String desc) {
this.clazz = clazz;
this.deobf = deobf;
this.obf = ((obf != null) ? obf : deobf);
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/ru/fewizz/idextender/asm/transformer/SelfHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ public void transform(final ClassNode cn, final boolean obfuscated) {
private void transformGet(final ClassNode cn, final MethodNode method) {
final InsnList code = method.instructions;
code.clear();
code.add((AbstractInsnNode) new VarInsnNode(25, 0));
code.add((AbstractInsnNode)
code.add(new VarInsnNode(25, 0));
code.add(
new FieldInsnNode(180, Type.getArgumentTypes(method.desc)[0].getInternalName(), "block16BArray", "[S"));
code.add((AbstractInsnNode) new InsnNode(176));
code.add(new InsnNode(176));
method.localVariables = null;
method.maxStack = 1;
}

private void transformSetBlockRefCount(final ClassNode cn, final MethodNode method, final boolean isObf) {
final InsnList code = method.instructions;
code.clear();
code.add((AbstractInsnNode) new VarInsnNode(25, 0));
code.add((AbstractInsnNode) new VarInsnNode(21, 1));
code.add((AbstractInsnNode) Name.ebs_blockRefCount.virtualSet(isObf));
code.add((AbstractInsnNode) new InsnNode(177));
code.add(new VarInsnNode(25, 0));
code.add(new VarInsnNode(21, 1));
code.add(Name.ebs_blockRefCount.virtualSet(isObf));
code.add(new InsnNode(177));
method.localVariables = null;
method.maxStack = 2;
}

private void transformSetTickRefCount(final ClassNode cn, final MethodNode method, final boolean isObf) {
final InsnList code = method.instructions;
code.clear();
code.add((AbstractInsnNode) new VarInsnNode(25, 0));
code.add((AbstractInsnNode) new VarInsnNode(21, 1));
code.add((AbstractInsnNode) Name.ebs_tickRefCount.virtualSet(isObf));
code.add((AbstractInsnNode) new InsnNode(177));
code.add(new VarInsnNode(25, 0));
code.add(new VarInsnNode(21, 1));
code.add(Name.ebs_tickRefCount.virtualSet(isObf));
code.add(new InsnNode(177));
method.localVariables = null;
method.maxStack = 2;
}
Expand Down
Loading

0 comments on commit adb4297

Please sign in to comment.