import com.jpexs.decompiler.flash.SWF; import com.jpexs.decompiler.flash.abc.ABC; import com.jpexs.decompiler.flash.abc.avm2.LocalDataArea; import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction; import com.jpexs.decompiler.flash.abc.types.MethodBody; import com.jpexs.decompiler.flash.ecma.Null; import java.io.*; import java.nio.file.*; import java.util.*; /** Executes inserted bytes with FFDec arithmetic/branches and stubbed display/property access. * This is a fragment test, not a substitute for Android AIR runtime testing. */ public final class TestBattleFragment { private static String property(ABC abc, AVM2Instruction ins) { return abc.constants.getString(abc.constants.getMultiname(ins.operands[0]).name_index); } @SuppressWarnings("unchecked") private static void execute(ABC abc, MethodBody body, long start, long end, Map registers, Map banner) throws Exception { var code = body.getCode().code; Map positions = new HashMap<>(); for (int i = 0; i < code.size(); i++) positions.put(code.get(i).getAddress(), i); LocalDataArea area = new LocalDataArea(); area.localRegisters.putAll(registers); Object stage = new Object(); Map driver = Map.of("stage", stage); int index = positions.get(start), steps = 0; while (code.get(index).getAddress() < end) { if (++steps > 2000) throw new AssertionError("Fragment did not terminate"); var ins = code.get(index); String opcode = ins.definition.instructionName; area.jump = null; switch (opcode) { case "getlex" -> { String name = property(abc, ins); area.operandStack.push(switch (name) { case "Math" -> Math.class; case "StageDriver" -> driver; default -> throw new AssertionError("Unexpected global " + name); }); } case "getproperty" -> { Object object = area.operandStack.pop(); area.operandStack.push(((Map) object).get(property(abc, ins))); } case "setproperty" -> { Object value = area.operandStack.pop(); Map object = (Map) area.operandStack.pop(); object.put(property(abc, ins), value); } case "callproperty" -> { List args = new ArrayList<>(); for (int i = 0; i < ins.operands[1]; i++) args.add(0, area.operandStack.pop()); Object receiver = area.operandStack.pop(); String name = property(abc, ins); if (receiver == Math.class && name.equals("min")) { area.operandStack.push(Math.min(((Number) args.get(0)).doubleValue(), ((Number) args.get(1)).doubleValue())); } else if (receiver == stage && name.equals("getChildByName")) { area.operandStack.push(banner == null ? Null.INSTANCE : banner); } else throw new AssertionError("Unexpected call " + name); } case "coerce" -> { /* Display stub is already represented by its map. */ } default -> { if (!ins.definition.execute(area, abc.constants, ins)) throw new AssertionError("Unsupported instruction " + opcode); } } index = area.jump == null ? index + 1 : positions.get(ins.getTargetAddress()); } if (code.get(index).getAddress() != end || !area.operandStack.empty()) throw new AssertionError("Fragment exit/stack mismatch"); registers.putAll(area.localRegisters); } private static void damageCases(ABC abc, MethodBody body) throws Exception { long end = -1; var code = body.getCode().code; for (int i = 0; i < code.size() - 1; i++) { var a = code.get(i); var b = code.get(i + 1); if (a.getAddress() > 0x280 && a.definition.instructionName.equals("getlex") && property(abc, a).equals("monVo") && b.definition.instructionName.equals("debugline") && b.operands[0] == 977) { end = a.getAddress(); break; } } if (end < 0) throw new AssertionError("Damage insertion boundaries missing"); int[][] cases = { {1, 2, 123, 1230}, {1, 2, 0, 0}, {1, 2, 4999, 49990}, {1, 2, 5000, 50000}, {1, 2, 9000, 50000}, {1, 2, Integer.MAX_VALUE, 50000}, {2, 1, 123, 123}, {1, 1, 123, 123}, {2, 2, 123, 123}, {0, 2, 123, 123} }; for (boolean visible : new boolean[]{true, false}) for (int[] c : cases) { Map locals = new HashMap<>(); locals.put(0, Map.of("team", (long)c[1])); locals.put(2, c[0] == 0 ? Null.INSTANCE : Map.of("team", (long)c[0])); locals.put(14, (long)c[2]); Map banner = visible ? new HashMap<>() : null; execute(abc, body, 0x280, end, locals, banner); int actual = ((Number)locals.get(14)).intValue(); if (actual != c[3]) throw new AssertionError("Wrong damage " + Arrays.toString(c) + " actual=" + actual); if (visible && c[0] == 1 && c[1] == 2 && !banner.get("text").toString().contains(" -> " + c[3])) throw new AssertionError("Hit diagnostics missing"); } System.out.println("PASS 20 damage fragment cases: player/enemy/null, cap, integer overflow, banner present/absent"); } private static void statsCase(ABC abc, MethodBody body) throws Exception { var code = body.getCode().code; long end = -1; for (int i = 0; i < code.size() - 1; i++) { var a = code.get(i); var b = code.get(i + 1); if (a.getAddress() > 0xd4 && a.definition.instructionName.equals("getlocal") && a.operands[0] == 5 && b.definition.instructionName.equals("debugline") && b.operands[0] == 202) { end = a.getAddress(); break; } } if (end < 0) throw new AssertionError("Stat insertion boundaries missing"); String[] names = {"atk", "def", "dex", "hp", "hpMax", "atkPoiDmg", "sunFire", "moonHeal", "deadBoom", "thorns", "crit", "selfCure", "atkVim", "atkStun", "defStun", "atkPoi", "skillCooldown"}; for (boolean visible : new boolean[]{true, false}) { Map mon = new HashMap<>(); for (int i = 0; i < names.length; i++) mon.put(names[i], i < 10 ? (Object)Long.valueOf(100 + i) : Double.valueOf(0.1)); Map before = new HashMap<>(mon); Map locals = new HashMap<>(); locals.put(5, mon); Map banner = visible ? new HashMap<>() : null; execute(abc, body, 0xd4, end, locals, banner); for (String name : names) { double expected = ((Number)before.get(name)).doubleValue() * 2; if (Math.abs(((Number)mon.get(name)).doubleValue() - expected) > 0.000001) throw new AssertionError("Wrong stat " + name); } if (visible && !banner.get("text").toString().contains("ENTRY ATK=200")) throw new AssertionError("Entry diagnostics missing"); } System.out.println("PASS 34 stat fragment assertions including HP/max HP and fractional modifiers"); } public static void main(String[] args) throws Exception { boolean stats = args[1].equals("stats"); try (var input = new BufferedInputStream(Files.newInputStream(Path.of(args[0])))) { SWF swf = new SWF(input, false); String target = stats ? "net.pixvi.myr.center.TeamCenter" : "net.pixvi.myr.battle.view.BattleMon"; String name = stats ? "getBattleTeamVo" : "beHurt"; for (var pack : swf.getAS3Packs()) if (pack.getClassPath().toString().equals(target)) { for (var body : pack.abc.bodies) if (name.equals(pack.abc.constants.getString(pack.abc.method_info.get(body.method_info).name_index))) { if (stats) statsCase(pack.abc, body); else damageCases(pack.abc, body); return; } } } throw new AssertionError("Target method missing"); } }