import com.jpexs.decompiler.flash.SWF; import com.jpexs.decompiler.flash.abc.ABC; import com.jpexs.decompiler.flash.abc.ScriptPack; import com.jpexs.decompiler.flash.abc.avm2.AVM2Code; import com.jpexs.decompiler.flash.abc.avm2.AVM2ConstantPool; import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction; import com.jpexs.decompiler.flash.abc.avm2.instructions.localregs.GetLocal0Ins; import com.jpexs.decompiler.flash.abc.avm2.instructions.localregs.SetLocal2Ins; import com.jpexs.decompiler.flash.abc.avm2.instructions.localregs.SetLocalIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.other.GetLexIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.other.NopIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.stack.PushStringIns; import com.jpexs.decompiler.flash.abc.avm2.parser.pcode.ASM3Parser; import com.jpexs.decompiler.flash.abc.avm2.parser.pcode.MissingSymbolHandler; import com.jpexs.decompiler.flash.abc.types.MethodBody; import com.jpexs.decompiler.flash.abc.types.MethodInfo; import com.jpexs.decompiler.flash.abc.types.traits.Trait; import com.jpexs.decompiler.flash.abc.types.traits.TraitMethodGetterSetter; import com.jpexs.decompiler.flash.tags.Tag; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.StringReader; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; /** Applies a single method-body patch and retains its original signature. */ public final class PatchBattleDamage { private static final MissingSymbolHandler ALLOW_NEW_CONSTANTS = new MissingSymbolHandler() { public boolean missingString(String value) { return true; } public boolean missingInt(long value) { return true; } public boolean missingUInt(long value) { return true; } public boolean missingDouble(double value) { return true; } public boolean missingFloat(float value) { return true; } public boolean missingFloat4(com.jpexs.decompiler.flash.abc.types.Float4 value) { return true; } public boolean missingDecimal(macromedia.asc.util.Decimal128 value) { return true; } }; public static void main(String[] args) throws Exception { if (args.length != 4) { throw new IllegalArgumentException("Usage: PatchBattleDamage input.app body.pcode output.app --damage|--stats|--loader"); } Path input = Path.of(args[0]); Path output = Path.of(args[2]); String pcode = Files.readString(Path.of(args[1])); try (BufferedInputStream stream = new BufferedInputStream(Files.newInputStream(input))) { SWF swf = new SWF(stream, false); String mode = args[3]; boolean patchLoader = "--loader".equals(mode); for (ScriptPack pack : swf.getAS3Packs()) { String scriptName = pack.getClassPath().toString(); if (patchLoader) { if (!"net.pixvi.myrloader.MYRLoaderStarter".equals(scriptName)) { continue; } ABC abc = pack.abc; int bodyIndex = abc.findMethodBodyByName( "net.pixvi.myrloader.MYRLoaderStarter", "fixComplete"); if (bodyIndex < 0) { throw new IllegalStateException("fixComplete body not found"); } MethodBody body = abc.bodies.get(bodyIndex); AVM2ConstantPool constants = abc.constants; List patchSites = new java.util.ArrayList<>(); for (int i = 0; i < body.getCode().code.size(); i++) { var code = body.getCode().code; if (!(code.get(i).definition instanceof PushStringIns)) { continue; } int stringIndex = code.get(i).operands[0]; if (!"MYRMain.app".equals(constants.getString(stringIndex))) { continue; } if (i < 2 || i + 3 >= code.size() || !(code.get(i - 1).definition instanceof GetLexIns) || (!(code.get(i + 3).definition instanceof SetLocal2Ins) && !(code.get(i + 3).definition instanceof SetLocalIns))) { continue; } patchSites.add(i); } if (patchSites.size() != 1) { throw new IllegalStateException("Expected one updated-main fallback, got " + patchSites.size()); } int patchSite = patchSites.get(0); var code = body.getCode().code; int stringIndex = code.get(patchSite).operands[0]; code.set(patchSite - 2, new AVM2Instruction(code.get(patchSite - 2).getAddress(), new PushStringIns(), new int[]{stringIndex})); for (int j = patchSite - 1; j <= patchSite + 2; j++) { code.set(j, new AVM2Instruction(code.get(j).getAddress(), new NopIns(), new int[0])); } body.setCode(body.getCode()); ((Tag) abc.parentTag).setModified(true); try (BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(output))) { swf.saveTo(out); } System.out.println("Patched loader to use packaged MYRMain.app; original signature retained"); return; } String methodName; int expectedBody; int expectedParameters; if ("--stats".equals(mode)) { if (!"net.pixvi.myr.center.TeamCenter".equals(scriptName)) { continue; } methodName = "getBattleTeamVo"; expectedBody = 11; expectedParameters = 1; } else if ("--damage".equals(mode) && "net.pixvi.myr.battle.view.BattleMon".equals(scriptName)) { methodName = "beHurt"; expectedBody = 32; expectedParameters = 13; } else { continue; } ABC abc = pack.abc; int bodyIndex = expectedBody; if (bodyIndex < 0) { throw new IllegalStateException("A verified body index is required for " + scriptName); } MethodBody body = abc.bodies.get(bodyIndex); List traits = abc.getMethodIndexing().findMethodTraits(pack, bodyIndex); if (traits.size() != 1) { throw new IllegalStateException("Expected one " + methodName + " trait, got " + traits.size()); } int methodInfoIndex = ((TraitMethodGetterSetter) traits.get(0)).method_info; MethodInfo signature = abc.method_info.get(methodInfoIndex); if (signature.param_types.length != expectedParameters) { throw new IllegalStateException("Unexpected " + methodName + " parameter count: " + signature.param_types.length + ", method_info=" + methodInfoIndex + ", body.method_info=" + body.method_info); } // The body-only parser resets MethodInfo before reading the body. int[] paramTypes = signature.param_types.clone(); int returnType = signature.ret_type; int nameIndex = signature.name_index; int flags = signature.flags; var optional = signature.optional.clone(); int[] paramNames = signature.paramNames.clone(); // Passing the real trait makes the parser replace only the body. AVM2Code code = ASM3Parser.parse(abc, new StringReader(pcode), traits.get(0), ALLOW_NEW_CONSTANTS, body, signature); signature.param_types = paramTypes; signature.ret_type = returnType; signature.name_index = nameIndex; signature.flags = flags; signature.optional = optional; signature.paramNames = paramNames; body.setCode(code); ((Tag) abc.parentTag).setModified(true); try (BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(output))) { swf.saveTo(out); } System.out.println("Patched " + methodName + " body " + bodyIndex + "; retained " + signature.param_types.length + " parameters"); return; } } throw new IllegalStateException("Requested script not found"); } }