Skip to content
This repository was archived by the owner on Jan 3, 2020. It is now read-only.

Commit 9ce359b

Browse files
committed
AccessTransformer & TabComplete
1 parent 46094fc commit 9ce359b

File tree

12 files changed

+299
-205
lines changed

12 files changed

+299
-205
lines changed

src/main/java/org/devinprogress/YAIF/Bridges/CommonBridge.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public IActionBridge.ActionFeedback onEnter(JTextField txt) { //send msg
4545

4646
@Override
4747
public IActionBridge.ActionFeedback onEsc(JTextField txt) {
48-
return IActionBridge.ActionFeedback.Quit;
48+
return IActionBridge.ActionFeedback.Nothing;
4949
}
5050

5151
@Override
@@ -85,6 +85,11 @@ public ActionFeedback onBackspace(JTextField txt) {
8585
return null;
8686
}
8787

88+
@Override
89+
public void onTabComplete(JTextField txt) {
90+
91+
}
92+
8893
@Override
8994
public boolean sameAs(GuiScreen screen, GuiTextField txtField) {
9095
return scr==screen;

src/main/java/org/devinprogress/YAIF/Bridges/EditSignBridge.java

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,17 @@ public class EditSignBridge implements IActionBridge {
1818
private InputFieldWrapper wrapper;
1919
private int currentLine=0;
2020
private TileEntitySign sign;
21-
private Field currentLineField=null;
2221
public EditSignBridge(GuiEditSign gui,InputFieldWrapper w){
2322
this.gui=gui;
2423
this.wrapper=w;
2524
currentLine=0;
2625
w.setTextNoEvent("");
27-
//TODO: USE AccessTransformer
28-
for(Field f:gui.getClass().getDeclaredFields())
29-
if(f.getType().equals(TileEntitySign.class)){
30-
try {
31-
f.setAccessible(true);
32-
sign = (TileEntitySign)f.get(gui);
33-
break;
34-
}catch(Exception e){
35-
e.printStackTrace();
36-
}
37-
}
38-
39-
//TODO: modification needed of updated
40-
try {
41-
currentLineField = gui.getClass().getDeclaredField(YetAnotherInputFix.ObfuscatedEnv ? "field_146851_h" : "editLine");
42-
currentLineField.setAccessible(true);
43-
}catch(Exception e){
44-
e.printStackTrace();
45-
}
26+
sign=gui.tileSign;
4627
}
4728
@Override
4829
public ActionFeedback onEnter(JTextField txt) {
4930
currentLine=currentLine+1&3;
50-
try{
51-
currentLineField.set(gui,currentLine);
52-
}catch(Exception e){
53-
e.printStackTrace();
54-
}
31+
gui.editLine=currentLine;
5532
wrapper.setTextNoEvent(sign.signText[currentLine]);
5633
return null;
5734
}
@@ -79,23 +56,15 @@ public ActionFeedback onTab(JTextField txt) {
7956
@Override
8057
public ActionFeedback onUp(JTextField txt) {
8158
currentLine=currentLine-1&3;
82-
try{
83-
currentLineField.set(gui,currentLine);
84-
}catch(Exception e){
85-
e.printStackTrace();
86-
}
59+
gui.editLine=currentLine;
8760
wrapper.setTextNoEvent(sign.signText[currentLine]);
8861
return null;
8962
}
9063

9164
@Override
9265
public ActionFeedback onDown(JTextField txt) {
9366
currentLine=currentLine+1&3;
94-
try{
95-
currentLineField.set(gui,currentLine);
96-
}catch(Exception e){
97-
e.printStackTrace();
98-
}
67+
gui.editLine=currentLine;
9968
wrapper.setTextNoEvent(sign.signText[currentLine]);
10069
return null;
10170
}
@@ -108,6 +77,11 @@ public ActionFeedback onBackspace(JTextField txt) {
10877
return onChange(txt);
10978
}
11079

80+
@Override
81+
public void onTabComplete(JTextField txt) {
82+
83+
}
84+
11185
@Override
11286
public boolean sameAs(GuiScreen screen, GuiTextField txtField) {
11387
return screen==gui;

src/main/java/org/devinprogress/YAIF/Bridges/GuiChatBridge.java

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,24 @@ public class GuiChatBridge implements IActionBridge {
2121
private InputFieldWrapper wrapper=null;
2222

2323
private boolean isCmd=false;
24-
private static Method keyTypedMethod=null;
24+
//private static Method keyTypedMethod=null;
2525

2626
public GuiChatBridge(GuiTextField textField,GuiChat screen,InputFieldWrapper wrapper){
2727
this.screen=screen;
2828
txt=textField;
2929
this.wrapper=wrapper;
3030
wrapper.DoActions(ActionFeedback.SetText,txt.getText());
3131

32-
//TODO: use AccessTransformer instead of reflection
33-
if(keyTypedMethod==null){
34-
for(Method m:screen.getClass().getDeclaredMethods()){
35-
if(m.getParameterTypes().length==2&&m.getReturnType()==void.class&&m.getParameterTypes()[0]==char.class&&m.getParameterTypes()[1]==int.class){
36-
//The Method Desc "(CI)V" seem to be unique
37-
keyTypedMethod=m;
38-
keyTypedMethod.setAccessible(true);
39-
}
40-
}
41-
}
42-
43-
for(Field f:screen.getClass().getDeclaredFields()){
44-
if(f.getType().equals(String.class)){
45-
String def="";
46-
try {
47-
f.setAccessible(true);
48-
def=(String)(f.get(screen));
49-
}catch(Exception e){
50-
e.printStackTrace();
51-
}
52-
if(def.equals("/")){
53-
isCmd=true;
54-
break;
55-
}
56-
}
57-
}
32+
if (screen.defaultInputFieldText.equals("/"))
33+
isCmd=true;
34+
else
35+
isCmd=false;
5836
}
5937

6038
@Override
6139
public ActionFeedback onEnter(JTextField txt) { //send
6240
this.txt.setText(txt.getText());
63-
try {
64-
keyTypedMethod.invoke(screen, '\n', 28);//Magic Numbers can be found at http://minecraft.gamepedia.com/Key_Codes
65-
}catch(Exception e){
66-
e.printStackTrace();
67-
}
41+
screen.keyTyped('\n', 28);//Magic Numbers can be found at http://minecraft.gamepedia.com/Key_Codes
6842
Keyboard.enableRepeatEvents(false);
6943
FMLClientHandler.instance().getClient().ingameGUI.getChatGUI().resetScroll();
7044

@@ -100,29 +74,25 @@ public void run() {
10074
@Override
10175
public ActionFeedback onTab(JTextField txt) {
10276
//You have to listen to S3APacketTabComplete to get the compliance result.
103-
YetAnotherInputFix.logger.info("Tab Completion not finished yet.");
77+
//YetAnotherInputFix.logger.info("Tab Completion not finished yet.");
78+
int cursorPos=txt.getCaretPosition();
79+
this.txt.setCursorPosition(cursorPos);
80+
screen.keyTyped('\t',15);
81+
wrapper.setTextNoEvent(this.txt.getText());
10482
//TODO: Finish it.
10583
return null;//return null == return Nothing
10684
}
10785

10886
@Override
10987
public ActionFeedback onUp(JTextField txt) {
110-
try {
111-
keyTypedMethod.invoke(screen, ' ', 200);
112-
}catch(Exception e){
113-
e.printStackTrace();
114-
}
88+
screen.keyTyped( ' ', 200);
11589
wrapper.setTextNoEvent(this.txt.getText());
11690
return null;
11791
}
11892

11993
@Override
12094
public ActionFeedback onDown(JTextField txt) {
121-
try {
122-
keyTypedMethod.invoke(screen, ' ', 208);
123-
}catch(Exception e){
124-
e.printStackTrace();
125-
}
95+
screen.keyTyped(' ', 208);
12696
wrapper.setTextNoEvent(this.txt.getText());
12797
return null;
12898
}
@@ -135,6 +105,11 @@ public ActionFeedback onBackspace(JTextField txt) {
135105
return onChange(txt);
136106
}
137107

108+
@Override
109+
public void onTabComplete(JTextField txt) {
110+
wrapper.setTextNoEvent(this.txt.getText());
111+
}
112+
138113
@Override
139114
public boolean sameAs(GuiScreen screen, GuiTextField txtField) {
140115
return this.screen==screen && txtField==txt;

src/main/java/org/devinprogress/YAIF/Bridges/IActionBridge.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ enum ActionFeedback{
2525
public ActionFeedback onUp(final JTextField txt);
2626
public ActionFeedback onDown(final JTextField txt);
2727
public ActionFeedback onBackspace(final JTextField txt);
28+
public void onTabComplete(final JTextField txt);
2829
public boolean sameAs(GuiScreen screen,GuiTextField txtField);
2930
}

src/main/java/org/devinprogress/YAIF/InputFieldWrapper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public void windowClosing(WindowEvent e) {
7878
frame.validate();
7979
}
8080

81+
public void onTabComplete(){
82+
if(bridge!=null)
83+
bridge.onTabComplete(txtField);
84+
}
85+
8186
private void bindKeys(){
8287
//Should be Called Only Once
8388
//Be careful about txtField.setText(). It will trigger here and further trigger the bridges.
@@ -217,8 +222,8 @@ private IActionBridge getBridge(){//Remember to add cases here if new Bridges ad
217222
}
218223
else if(YetAnotherInputFix.currentGuiScreen instanceof GuiEditSign)
219224
return new EditSignBridge((GuiEditSign)YetAnotherInputFix.currentGuiScreen,this);
220-
//else return null;
221-
else return new CommonBridge(YetAnotherInputFix.currentGuiScreen, this);
225+
else return null;
226+
//else return new CommonBridge(YetAnotherInputFix.currentGuiScreen, this);
222227
}
223228

224229
public void setTextNoEvent(final String str){
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package org.devinprogress.YAIF.Transformer;
2+
3+
import org.devinprogress.YAIF.YetAnotherInputFix;
4+
import org.objectweb.asm.ClassReader;
5+
import org.objectweb.asm.ClassWriter;
6+
import org.objectweb.asm.Opcodes;
7+
import org.objectweb.asm.tree.*;
8+
9+
import java.lang.reflect.Method;
10+
import java.util.*;
11+
12+
/**
13+
* Created by recursiveg on 14-10-21.
14+
*/
15+
public class ASMHelper {
16+
private Object obj;
17+
//private Map<String,List<String[]>> map;
18+
//public static Boolean DEOBF_ENV;
19+
//Map<DeobfuscatedClassName,[DeobfMethodName,ObfedMethodName,Description,ProcessMethod]>
20+
private List<MethodRecord> map=null;
21+
private Set<String> classMap=null;
22+
public ASMHelper(Object o){
23+
obj=o;
24+
map=new ArrayList<MethodRecord>();
25+
classMap=new HashSet<String>();
26+
}
27+
28+
public void add(String className,String methodName,String methodNameDeobf,String Descripton,String DescriptionDeobf,String targetTransformer){
29+
map.add(new MethodRecord(
30+
className,
31+
methodName,
32+
methodNameDeobf,
33+
Descripton,
34+
DescriptionDeobf,
35+
targetTransformer
36+
));
37+
classMap.add(className);
38+
}
39+
40+
public byte[] transform(String className,byte[] bytes){
41+
42+
if(!classMap.contains(className))return bytes;
43+
//System.out.println("Examing Class:"+className);
44+
ClassReader cr=new ClassReader(bytes);
45+
ClassNode cn=new ClassNode();
46+
cr.accept(cn, 0);
47+
48+
for(MethodNode mn:cn.methods){
49+
//System.out.println(String.format("Examing Method: %s%s",mn.name,mn.desc));
50+
for(MethodRecord r:map){
51+
r.preProcess(!YetAnotherInputFix.ObfuscatedEnv,obj);
52+
if(mn.name.equals(r.MethodName)&&mn.desc.equals(r.Desc)&&className.equals(r.ClassName)){
53+
try{
54+
//System.out.println(String.format("Invoking Method: %s%s",mn.name,mn.desc));
55+
r.ProcessMethod.invoke(obj,mn);
56+
}catch(Exception e){
57+
e.printStackTrace();
58+
return bytes;
59+
}
60+
break;
61+
}
62+
}
63+
}
64+
65+
ClassWriter cw=new ClassWriter(0);
66+
cn.accept(cw);
67+
return cw.toByteArray();
68+
}
69+
70+
public static AbstractInsnNode getNthInsnNode(MethodNode mn,int opcode,int N){
71+
AbstractInsnNode n=mn.instructions.getFirst();
72+
int count=0;
73+
while(n!=null){
74+
if(n.getOpcode()==opcode){
75+
count++;
76+
if(count==N)
77+
break;
78+
}
79+
n=n.getNext();
80+
}
81+
return n;
82+
}
83+
84+
public static AbstractInsnNode getALOAD(MethodNode mn,int index,int val){
85+
AbstractInsnNode n=mn.instructions.getFirst();
86+
int count=0;
87+
while(n!=null){
88+
if(n.getOpcode()==Opcodes.ALOAD&&((VarInsnNode)n).var==val){
89+
count++;
90+
if(count==index)
91+
break;
92+
}
93+
n=n.getNext();
94+
}
95+
return n;
96+
}
97+
98+
public static void InsertInvokeStaticBefore(MethodNode mn,AbstractInsnNode n,String targetClass,String targetMethod,String desc){
99+
mn.instructions.insertBefore(n,new MethodInsnNode(Opcodes.INVOKESTATIC,
100+
targetClass.replace('.','/'),targetMethod,desc));
101+
}
102+
103+
public static void InsertInvokeStaticAfter(MethodNode mn,AbstractInsnNode n,String targetClass,String targetMethod,String desc){
104+
mn.instructions.insert(n, new MethodInsnNode(Opcodes.INVOKESTATIC,
105+
targetClass.replace('.', '/'), targetMethod, desc));
106+
}
107+
108+
public class MethodRecord{
109+
public String ClassName;
110+
public String MethodName;
111+
public String MethodNameDeobf;
112+
public String Desc;
113+
public String DescDeobf;
114+
public String ProcessMethodName;
115+
public Method ProcessMethod;
116+
private boolean flag=false;
117+
public MethodRecord(String a,String b,String c,String d,String e,String f){
118+
ClassName=a;
119+
MethodName=b;
120+
MethodNameDeobf=c;
121+
Desc=d;
122+
DescDeobf=e;
123+
ProcessMethodName=f;
124+
}
125+
126+
public void preProcess(boolean Deobf,Object o){
127+
if(flag)return;
128+
129+
if(Deobf){
130+
MethodName=MethodNameDeobf;
131+
Desc=DescDeobf;
132+
}
133+
try{
134+
ProcessMethod=o.getClass().getDeclaredMethod(ProcessMethodName,MethodNode.class);
135+
}catch(Exception e){
136+
e.printStackTrace();
137+
}
138+
flag=true;
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)