背景:我正在注入Minecraft Launcher以获取小程序(已完成),但是现在我希望通过类加载器加载Minecraft的文件。我找到了GameUpdater.java(Minecraft的gameupdater,也是客户端小程序的调度程序)的方法,在该方法下有一个称为“ 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文档。

如果您对更多代码等有任何疑问,请告诉我。

最佳答案

解决了。诀窍是在添加新的InvokerVirtual(用静态方法替换加载函数)后删除InvokerVirtual(从指令列表中删除OPCODE)。



instructionList.insert(instruction, classLoaderCall);
instructionList.delete(instruction);

07-24 20:56