Rewrite unused fields
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package net.runelite.deob.deobfuscators;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import net.runelite.asm.ClassFile;
|
||||
import net.runelite.asm.ClassGroup;
|
||||
@@ -10,59 +12,46 @@ import net.runelite.asm.Method;
|
||||
import net.runelite.asm.attributes.Code;
|
||||
import net.runelite.asm.attributes.code.Instruction;
|
||||
import net.runelite.asm.attributes.code.instruction.types.FieldInstruction;
|
||||
import net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction;
|
||||
import net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction;
|
||||
|
||||
public class UnusedFields implements Deobfuscator
|
||||
{
|
||||
private static boolean isUnused(ClassGroup group, Field field)
|
||||
private final Set<Field> used = new HashSet<>();
|
||||
|
||||
private void checkForFieldUsage(ClassGroup group)
|
||||
{
|
||||
int get = 0, set = 0;
|
||||
for (ClassFile cf : group.getClasses())
|
||||
for (Method m : cf.getMethods().getMethods())
|
||||
{
|
||||
Code code = m.getCode();
|
||||
if (code == null)
|
||||
continue;
|
||||
|
||||
|
||||
for (Instruction ins : code.getInstructions().getInstructions())
|
||||
{
|
||||
if (ins instanceof FieldInstruction)
|
||||
{
|
||||
FieldInstruction fi = (FieldInstruction) ins;
|
||||
|
||||
if (fi.getMyField() != field)
|
||||
continue;
|
||||
|
||||
if (ins instanceof GetFieldInstruction)
|
||||
++get;
|
||||
if (ins instanceof SetFieldInstruction)
|
||||
++set;
|
||||
used.add(fi.getMyField());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// for only checking 'get' wed need a way to remove field initialization in constructors/class initializers
|
||||
return get + set == 0;
|
||||
// if (get == 0)
|
||||
// return true;
|
||||
//
|
||||
// return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ClassGroup group)
|
||||
{
|
||||
checkForFieldUsage(group);
|
||||
|
||||
int count = 0;
|
||||
for (ClassFile cf : group.getClasses())
|
||||
for (Field f : new ArrayList<>(cf.getFields().getFields()))
|
||||
{
|
||||
if (isUnused(group, f))
|
||||
if (!used.contains(f))
|
||||
{
|
||||
cf.getFields().getFields().remove(f);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Removed " + count + " unused fields");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user