背景:我正在注入“我的世界”启动器来获取applet (我已经做到了),但现在我希望通过我的类加载器加载“我的世界”的文件。我找到了GameUpdater.java (我的游戏更新程序,也是客户端applet的分派器)的方法,在这个方法下有一个叫做"createApplet“的方法。
GameUpdater.java:
public Applet createApplet() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class localClass = classLoader.loadClass("net.minecraft.client.MinecraftApplet");
return (Applet)localClass.newInstance();
} 好吧,非常简单,用您自己的静态加载方法替换classLoader.loadClass。所以,我尝试了,在我的类加载器中,这是我的转换代码:
for(Method method : generator.getMethods()) {
if(method.getName().equals("createApplet")) {
ConstantPoolGen cpg = generator.getConstantPool();
MethodGen methodGen = new MethodGen(method, generator.getClassName(), cpg);
Instruction instruction = null;
InstructionList instructionList = methodGen.getInstructionList();
InstructionHandle[] instructionHandles = instructionList.getInstructionHandles();
for(int i = 0; i < instructionHandles.length; i++) {
//System.out.println(instructionHandles[i].getInstruction()); //debug
if(instructionHandles[i].getInstruction() instanceof LDC) {
instruction = instructionHandles[i].getInstruction();
InstructionFactory instructionFactory = new InstructionFactory(generator, cpg);
InvokeInstruction classLoaderCall =
instructionFactory.createInvoke(
"MinecraftLauncher", "loadClass", Type.CLASS, new Type[]{Type.STRING},Constants.INVOKESTATIC);
instructionList.insert(instruction, classLoaderCall);
methodGen.setInstructionList(instructionList);
instructionList.setPositions();
methodGen.setMaxStack();
methodGen.setMaxLocals();
methodGen.removeLineNumbers();
generator.replaceMethod(method, methodGen.getMethod());
generator.getJavaClass().dump("gameupdater.class");
}
}
}然而,我却跌倒在地。下面是更新后的gameupdater.class (正如您在上面看到的,我转储它)
public Applet createApplet() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class localClass = MinecraftLauncher.loadClass(classLoader).loadClass("net.minecraft.client.MinecraftApplet");
return (Applet)localClass.newInstance();
}下面是GameUpdater中createApplet方法的字节码的图片

现在,我没有其他的想法来做这件事。如果有人能给我指出正确的方向,那就太棒了!同时,我将继续尝试,并阅读bcel文档。
如果你有任何关于更多代码等的问题,请告诉我。
发布于 2013-01-06 13:59:21
解决了。诀窍是在添加新的OPCODE (静态方法替换load函数)之后删除InvokerVirtual (从指令表中删除OPCODE)。
示例
instructionList.insert(instruction, classLoaderCall);
instructionList.delete(instruction);https://stackoverflow.com/questions/14178980
复制相似问题