Skip to content

Commit 5025eb1

Browse files
committed
Adjust ProxyServerHook for beta server
1 parent 1a361a3 commit 5025eb1

File tree

1 file changed

+20
-49
lines changed

1 file changed

+20
-49
lines changed

src/launcher/org/gotti/wurmunlimited/modloader/ProxyServerHook.java

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
import java.lang.reflect.InvocationHandler;
44
import java.lang.reflect.Method;
55

6+
import javassist.CannotCompileException;
67
import javassist.ClassPool;
78
import javassist.CtClass;
89
import javassist.CtMethod;
910
import javassist.CtPrimitiveType;
1011
import javassist.NotFoundException;
11-
import javassist.bytecode.BadBytecode;
12-
import javassist.bytecode.Bytecode;
13-
import javassist.bytecode.CodeAttribute;
14-
import javassist.bytecode.CodeIterator;
15-
import javassist.bytecode.ConstPool;
1612
import javassist.bytecode.Descriptor;
17-
import javassist.bytecode.LocalVariableAttribute;
18-
import javassist.bytecode.MethodInfo;
13+
import javassist.expr.ExprEditor;
14+
import javassist.expr.FieldAccess;
1915

2016
import org.gotti.wurmunlimited.modloader.classhooks.HookException;
2117
import org.gotti.wurmunlimited.modloader.classhooks.HookManager;
@@ -89,56 +85,31 @@ private void registerOnMessageHook() {
8985
CtClass ctCommunicator = classPool.get("com.wurmonline.server.creatures.Communicator");
9086

9187
CtClass[] paramTypes = {
92-
CtPrimitiveType.intType,
9388
classPool.get("java.nio.ByteBuffer")
9489
};
9590

96-
String descriptor = Descriptor.ofMethod(CtClass.booleanType, new CtClass[] {
97-
classPool.get("com.wurmonline.server.creatures.Communicator"),
98-
classPool.get(String.class.getName())
99-
});
100-
101-
CtMethod method = ctCommunicator.getMethod("reallyHandle", Descriptor.ofMethod(CtPrimitiveType.voidType, paramTypes));
102-
MethodInfo methodInfo = method.getMethodInfo();
103-
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
104-
ConstPool constPool = methodInfo.getConstPool();
105-
106-
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
107-
int messageIndex = -1;
108-
for (int i = 0; i < attr.tableLength(); i++) {
109-
if ("message".equals(attr.variableName(i))) {
110-
messageIndex = attr.index(i);
111-
}
91+
// com.wurmonline.server.creatures.Communicator.reallyHandle_CMD_MESSAGE(ByteBuffer)
92+
CtMethod method;
93+
try {
94+
method = ctCommunicator.getMethod("reallyHandle_CMD_MESSAGE", Descriptor.ofMethod(CtPrimitiveType.voidType, paramTypes));
95+
} catch (NotFoundException e) {
96+
// Backward compatible
97+
method = ctCommunicator.getMethod("reallyHandle", Descriptor.ofMethod(CtPrimitiveType.voidType, paramTypes));
11298
}
11399

114-
if (messageIndex == -1) {
115-
throw new HookException("Message variable can not be resolved");
116-
}
117-
118-
CodeIterator codeIterator = codeAttribute.iterator();
119-
int lastOp = 0;
120-
while (codeIterator.hasNext()) {
121-
int pos = codeIterator.next();
122-
int op = codeIterator.byteAt(pos);
123-
if (op == CodeIterator.PUTSTATIC) {
124-
int indexByte1 = codeIterator.byteAt(pos + 1);
125-
int indexByte2 = codeIterator.byteAt(pos + 2);
126-
int constPoolIndex = indexByte1 << 8 | indexByte2;
127-
if ("commandMessage".equals(constPool.getFieldrefName(constPoolIndex)) && lastOp == CodeIterator.ALOAD) {
128-
Bytecode bytecode = new Bytecode(constPool);
129-
bytecode.add(Bytecode.ALOAD_0);
130-
bytecode.addAload(codeIterator.byteAt(pos - 1));
131-
bytecode.addInvokestatic(classPool.get(this.getClass().getName()), "communicatorMessageHook", descriptor);
132-
bytecode.add(Bytecode.IFEQ, 0, 4, Bytecode.RETURN);
133-
codeIterator.insertAt(pos + 3, bytecode.get());
134-
break;
100+
method.instrument(new ExprEditor() {
101+
@Override
102+
public void edit(FieldAccess f) throws CannotCompileException {
103+
if (f.isWriter() && f.getClassName().equals("com.wurmonline.server.creatures.Communicator") && f.getFieldName().equals("commandMessage")) {
104+
StringBuffer code = new StringBuffer();
105+
code.append("$proceed($$);\n");
106+
code.append(String.format("if (%s#communicatorMessageHook(this, $1)) { return; };\n", ProxyServerHook.class.getName()));
107+
f.replace(code.toString());
135108
}
136109
}
137-
lastOp = op;
138-
}
110+
});
139111

140-
methodInfo.rebuildStackMap(classPool);
141-
} catch (NotFoundException | BadBytecode e) {
112+
} catch (NotFoundException | CannotCompileException e) {
142113
throw new HookException(e);
143114
}
144115
}

0 commit comments

Comments
 (0)