diff options
| author | 2015-05-21 23:30:00 +0100 | |
|---|---|---|
| committer | 2015-05-21 23:30:00 +0100 | |
| commit | e3f452250e51b7271f3989c7dfd12e4422934942 (patch) | |
| tree | 5aa482f9a6e21eb318a3e23e7d8274d77c73faf6 /src/cuchaz/enigma/bytecode/LocalVariableRenamer.java | |
| download | enigma-fork-e3f452250e51b7271f3989c7dfd12e4422934942.tar.gz enigma-fork-e3f452250e51b7271f3989c7dfd12e4422934942.tar.xz enigma-fork-e3f452250e51b7271f3989c7dfd12e4422934942.zip | |
Support Gradle alongside SSJB
This makes builds faster, simpler and better automated but still keeps
Cuchaz happy. :)
Diffstat (limited to 'src/cuchaz/enigma/bytecode/LocalVariableRenamer.java')
| -rw-r--r-- | src/cuchaz/enigma/bytecode/LocalVariableRenamer.java | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/bytecode/LocalVariableRenamer.java b/src/cuchaz/enigma/bytecode/LocalVariableRenamer.java new file mode 100644 index 0000000..ae0455f --- /dev/null +++ b/src/cuchaz/enigma/bytecode/LocalVariableRenamer.java | |||
| @@ -0,0 +1,123 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.bytecode; | ||
| 12 | |||
| 13 | import javassist.CtBehavior; | ||
| 14 | import javassist.CtClass; | ||
| 15 | import javassist.bytecode.ByteArray; | ||
| 16 | import javassist.bytecode.CodeAttribute; | ||
| 17 | import javassist.bytecode.ConstPool; | ||
| 18 | import javassist.bytecode.LocalVariableAttribute; | ||
| 19 | import javassist.bytecode.LocalVariableTypeAttribute; | ||
| 20 | import cuchaz.enigma.mapping.ArgumentEntry; | ||
| 21 | import cuchaz.enigma.mapping.BehaviorEntry; | ||
| 22 | import cuchaz.enigma.mapping.EntryFactory; | ||
| 23 | import cuchaz.enigma.mapping.Translator; | ||
| 24 | |||
| 25 | |||
| 26 | public class LocalVariableRenamer { | ||
| 27 | |||
| 28 | private Translator m_translator; | ||
| 29 | |||
| 30 | public LocalVariableRenamer(Translator translator) { | ||
| 31 | m_translator = translator; | ||
| 32 | } | ||
| 33 | |||
| 34 | public void rename(CtClass c) { | ||
| 35 | for (CtBehavior behavior : c.getDeclaredBehaviors()) { | ||
| 36 | |||
| 37 | // if there's a local variable table, just rename everything to v1, v2, v3, ... for now | ||
| 38 | CodeAttribute codeAttribute = behavior.getMethodInfo().getCodeAttribute(); | ||
| 39 | if (codeAttribute == null) { | ||
| 40 | continue; | ||
| 41 | } | ||
| 42 | |||
| 43 | BehaviorEntry behaviorEntry = EntryFactory.getBehaviorEntry(behavior); | ||
| 44 | ConstPool constants = c.getClassFile().getConstPool(); | ||
| 45 | |||
| 46 | LocalVariableAttribute table = (LocalVariableAttribute)codeAttribute.getAttribute(LocalVariableAttribute.tag); | ||
| 47 | if (table != null) { | ||
| 48 | renameLVT(behaviorEntry, constants, table); | ||
| 49 | } | ||
| 50 | |||
| 51 | LocalVariableTypeAttribute typeTable = (LocalVariableTypeAttribute)codeAttribute.getAttribute(LocalVariableAttribute.typeTag); | ||
| 52 | if (typeTable != null) { | ||
| 53 | renameLVTT(typeTable, table); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | // DEBUG | ||
| 59 | @SuppressWarnings("unused") | ||
| 60 | private void dumpTable(LocalVariableAttribute table) { | ||
| 61 | for (int i=0; i<table.tableLength(); i++) { | ||
| 62 | System.out.println(String.format("\t%d (%d): %s %s", | ||
| 63 | i, table.index(i), table.variableName(i), table.descriptor(i) | ||
| 64 | )); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | private void renameLVT(BehaviorEntry behaviorEntry, ConstPool constants, LocalVariableAttribute table) { | ||
| 69 | |||
| 70 | // skip empty tables | ||
| 71 | if (table.tableLength() <= 0) { | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | // where do we start counting variables? | ||
| 76 | int starti = 0; | ||
| 77 | if (table.variableName(0).equals("this")) { | ||
| 78 | // skip the "this" variable | ||
| 79 | starti = 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | // rename method arguments first | ||
| 83 | int numArgs = 0; | ||
| 84 | if (behaviorEntry.getSignature() != null) { | ||
| 85 | numArgs = behaviorEntry.getSignature().getArgumentTypes().size(); | ||
| 86 | for (int i=starti; i<starti + numArgs && i<table.tableLength(); i++) { | ||
| 87 | int argi = i - starti; | ||
| 88 | String argName = m_translator.translate(new ArgumentEntry(behaviorEntry, argi, "")); | ||
| 89 | if (argName == null) { | ||
| 90 | argName = "a" + (argi + 1); | ||
| 91 | } | ||
| 92 | renameVariable(table, i, constants.addUtf8Info(argName)); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | // then rename the rest of the args, if any | ||
| 97 | for (int i=starti + numArgs; i<table.tableLength(); i++) { | ||
| 98 | int firstIndex = table.index(starti + numArgs); | ||
| 99 | renameVariable(table, i, constants.addUtf8Info("v" + (table.index(i) - firstIndex + 1))); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | private void renameLVTT(LocalVariableTypeAttribute typeTable, LocalVariableAttribute table) { | ||
| 104 | // rename args to the same names as in the LVT | ||
| 105 | for (int i=0; i<typeTable.tableLength(); i++) { | ||
| 106 | renameVariable(typeTable, i, getNameIndex(table, typeTable.index(i))); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | private void renameVariable(LocalVariableAttribute table, int i, int stringId) { | ||
| 111 | // based off of LocalVariableAttribute.nameIndex() | ||
| 112 | ByteArray.write16bit(stringId, table.get(), i*10 + 6); | ||
| 113 | } | ||
| 114 | |||
| 115 | private int getNameIndex(LocalVariableAttribute table, int index) { | ||
| 116 | for (int i=0; i<table.tableLength(); i++) { | ||
| 117 | if (table.index(i) == index) { | ||
| 118 | return table.nameIndex(i); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | return 0; | ||
| 122 | } | ||
| 123 | } | ||