Started Replace Strings Cleanup

This commit is contained in:
Konloch 2021-07-06 13:38:45 -07:00
parent dd4bec1446
commit 5509a659fb

View file

@ -34,16 +34,16 @@ import the.bytecode.club.bytecodeviewer.api.PluginConsole;
* @author Konloch * @author Konloch
*/ */
public class ReplaceStrings extends Plugin { public class ReplaceStrings extends Plugin
{
PluginConsole frame = new PluginConsole("Replace Strings"); PluginConsole frame = new PluginConsole("Replace Strings");
String originalLDC; String originalLDC;
String newLDC; String newLDC;
String className; String className;
boolean contains; boolean contains;
public ReplaceStrings(String originalLDC, String newLDC, String className, public ReplaceStrings(String originalLDC, String newLDC, String className, boolean contains)
boolean contains) { {
this.originalLDC = originalLDC; this.originalLDC = originalLDC;
this.newLDC = newLDC; this.newLDC = newLDC;
this.className = className; this.className = className;
@ -51,103 +51,112 @@ public class ReplaceStrings extends Plugin {
} }
@Override @Override
public void execute(ArrayList<ClassNode> classNodeList) { public void execute(ArrayList<ClassNode> classNodeList)
if (!className.equals("*")) { {
for (ClassNode classNode : classNodeList) { if (!className.equals("*"))
{
for (ClassNode classNode : classNodeList)
if (classNode.name.equals(className)) if (classNode.name.equals(className))
scanClassNode(classNode); scanClassNode(classNode);
} }
} else { else
for (ClassNode classNode : classNodeList) { {
for (ClassNode classNode : classNodeList)
scanClassNode(classNode); scanClassNode(classNode);
} }
}
frame.setVisible(true); frame.setVisible(true);
} }
public void scanClassNode(ClassNode classNode) { public void scanClassNode(ClassNode classNode)
for (Object o : classNode.fields.toArray()) { {
for (Object o : classNode.fields.toArray())
{
FieldNode f = (FieldNode) o; FieldNode f = (FieldNode) o;
Object v = f.value; Object v = f.value;
if (v instanceof String) { if (v instanceof String)
{
String s = (String) v; String s = (String) v;
if (contains) { if (contains)
{
if (s.contains(originalLDC)) if (s.contains(originalLDC))
f.value = ((String) f.value).replaceAll(originalLDC, f.value = ((String) f.value).replaceAll(originalLDC, newLDC);
newLDC); }
} else { else
{
if (s.equals(originalLDC)) if (s.equals(originalLDC))
f.value = newLDC; f.value = newLDC;
} }
} }
if (v instanceof String[]) {
for (int i = 0; i < ((String[]) v).length; i++) { if (v instanceof String[])
{
for (int i = 0; i < ((String[]) v).length; i++)
{
String s = ((String[]) v)[i]; String s = ((String[]) v)[i];
if (contains) { if (contains)
if (s.contains(originalLDC)) { {
f.value = ((String[]) f.value)[i].replaceAll( if (s.contains(originalLDC))
originalLDC, newLDC); {
f.value = ((String[]) f.value)[i].replaceAll(originalLDC, newLDC);
String ugh = s.replaceAll("\\n", "\\\\n") String ugh = s.replaceAll("\\n", "\\\\n")
.replaceAll("\\r", "\\\\r"); .replaceAll("\\r", "\\\\r");
frame.appendText(classNode.name + "." + f.name + "" frame.appendText(classNode.name + "." + f.name + ""
+ f.desc + " -> \"" + ugh + f.desc + " -> \"" + ugh + "\" replaced with \""
+ "\" replaced with \""
+ s.replaceAll(originalLDC, newLDC) + "\""); + s.replaceAll(originalLDC, newLDC) + "\"");
} }
} else { }
if (s.equals(originalLDC)) { else
{
if (s.equals(originalLDC))
{
((String[]) f.value)[i] = newLDC; ((String[]) f.value)[i] = newLDC;
String ugh = s.replaceAll("\\n", "\\\\n") String ugh = s.replaceAll("\\n", "\\\\n")
.replaceAll("\\r", "\\\\r"); .replaceAll("\\r", "\\\\r");
frame.appendText(classNode.name + "." + f.name + "" frame.appendText(classNode.name + "." + f.name + ""
+ f.desc + " -> \"" + ugh + f.desc + " -> \"" + ugh + "\" replaced with \"" + newLDC + "\"");
+ "\" replaced with \"" + newLDC + "\"");
} }
} }
} }
} }
} }
for (Object o : classNode.methods.toArray()) { for (Object o : classNode.methods.toArray())
{
MethodNode m = (MethodNode) o; MethodNode m = (MethodNode) o;
InsnList iList = m.instructions; InsnList iList = m.instructions;
for (AbstractInsnNode a : iList.toArray()) { for (AbstractInsnNode a : iList.toArray())
if (a instanceof LdcInsnNode) { {
if (((LdcInsnNode) a).cst instanceof String) { if (a instanceof LdcInsnNode)
{
if (((LdcInsnNode) a).cst instanceof String)
{
final String s = (String) ((LdcInsnNode) a).cst; final String s = (String) ((LdcInsnNode) a).cst;
if (contains) { if (contains)
if (s.contains(originalLDC)) { {
if (s.contains(originalLDC))
{
((LdcInsnNode) a).cst = ((String) ((LdcInsnNode) a).cst) ((LdcInsnNode) a).cst = ((String) ((LdcInsnNode) a).cst)
.replaceAll(originalLDC, newLDC); .replaceAll(originalLDC, newLDC);
String ugh = s.replaceAll("\\n", "\\\\n") String ugh = s.replaceAll("\\n", "\\\\n")
.replaceAll("\\r", "\\\\r"); .replaceAll("\\r", "\\\\r");
frame.appendText(classNode.name frame.appendText(classNode.name + "." + m.name + "" + m.desc
+ "." + " -> \"" + ugh + "\" replaced with \""
+ m.name
+ ""
+ m.desc
+ " -> \""
+ ugh
+ "\" replaced with \""
+ s.replaceAll(originalLDC, newLDC) + s.replaceAll(originalLDC, newLDC)
.replaceAll("\\n", "\\\\n") .replaceAll("\\n", "\\\\n")
.replaceAll("\\r", "\\\\r") .replaceAll("\\r", "\\\\r")
+ "\""); + "\"");
} }
} else { }
if (s.equals(originalLDC)) { else
{
if (s.equals(originalLDC))
{
((LdcInsnNode) a).cst = newLDC; ((LdcInsnNode) a).cst = newLDC;
String ugh = s.replaceAll("\\n", "\\\\n") String ugh = s.replaceAll("\\n", "\\\\n")
.replaceAll("\\r", "\\\\r"); .replaceAll("\\r", "\\\\r");
frame.appendText(classNode.name frame.appendText(classNode.name + "." + m.name + "" + m.desc
+ "." + " -> \"" + ugh + "\" replaced with \""
+ m.name
+ ""
+ m.desc
+ " -> \""
+ ugh
+ "\" replaced with \""
+ newLDC.replaceAll("\\n", "\\\\n") + newLDC.replaceAll("\\n", "\\\\n")
.replaceAll("\\r", "\\\\r") .replaceAll("\\r", "\\\\r")
+ "\""); + "\"");