diff --git a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ABCStreamTest.java b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ABCStreamTest.java index 3857af61f..f8e50109f 100644 --- a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ABCStreamTest.java +++ b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ABCStreamTest.java @@ -1,18 +1,19 @@ /* * Copyright (C) 2010-2015 JPEXS, All rights reserved. - * + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash; import com.jpexs.decompiler.flash.abc.ABCInputStream; diff --git a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/DirectEditingTest.java b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/DirectEditingTest.java new file mode 100644 index 000000000..626687f86 --- /dev/null +++ b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/DirectEditingTest.java @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2010-2015 JPEXS, All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. + */ +package com.jpexs.decompiler.flash; + +import com.jpexs.decompiler.flash.abc.ABC; +import com.jpexs.decompiler.flash.abc.ScriptPack; +import com.jpexs.decompiler.flash.abc.avm2.parser.AVM2ParseException; +import com.jpexs.decompiler.flash.action.Action; +import com.jpexs.decompiler.flash.action.parser.ActionParseException; +import com.jpexs.decompiler.flash.action.parser.script.ActionScriptParser; +import com.jpexs.decompiler.flash.configuration.Configuration; +import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode; +import com.jpexs.decompiler.flash.helpers.CodeFormatting; +import com.jpexs.decompiler.flash.helpers.HighlightedTextWriter; +import com.jpexs.decompiler.flash.tags.ABCContainerTag; +import com.jpexs.decompiler.flash.tags.base.ASMSource; +import com.jpexs.decompiler.graph.CompilationException; +import com.jpexs.decompiler.graph.TranslateException; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import static org.testng.Assert.fail; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * + * @author JPEXS + */ +public class DirectEditingTest extends FileTestBase { + + @BeforeClass + public void init() { + Configuration.autoDeobfuscate.set(false); + Configuration.debugCopy.set(false); + } + + public static final String TESTDATADIR = "testdata/directediting"; + + @Test(dataProvider = "provideFiles") + public void testDirectEditing(String fileName) throws IOException, InterruptedException, AVM2ParseException, CompilationException { + try { + SWF swf = new SWF(new BufferedInputStream(new FileInputStream(TESTDATADIR + File.separator + fileName)), false); + if (swf.isAS3()) { + boolean dotest = false; + List allAbcs = new ArrayList<>(); + for (ABCContainerTag ct : swf.getAbcList()) { + allAbcs.add(ct.getABC()); + } + for (ABC abc : allAbcs) { + for (int s = 0; s < abc.script_info.size(); s++) { + String startAfter = null; + HighlightedTextWriter htw = new HighlightedTextWriter(new CodeFormatting(), false); + ScriptPack en = abc.script_info.get(s).getPacks(abc, s, null).get(0); + String classPathString = en.getClassPath().toString(); + if (startAfter == null || classPathString.equals(startAfter)) { + dotest = true; + } + if (!dotest) { + System.out.println("Skipped:" + classPathString); + continue; + } + + System.out.println("Recompiling:" + classPathString + "..."); + en.toSource(htw, abc.script_info.get(s).traits.traits, ScriptExportMode.AS, false); + String original = htw.toString(); + com.jpexs.decompiler.flash.abc.avm2.parser.script.ActionScriptParser.compile(original, abc, allAbcs, false, en.getClassPath().className + ".as", abc.instance_info.size()); + + //remove last compiled script: + abc.script_info.remove(abc.script_info.size() - 1); + } + } + } else { + Map asms = swf.getASMs(false); + + for (ASMSource asm : asms.values()) { + try { + HighlightedTextWriter writer = new HighlightedTextWriter(new CodeFormatting(), false); + Action.actionsToSource(asm, asm.getActions(), asm.toString()/*FIXME?*/, writer); + String as = writer.toString(); + as = asm.removePrefixAndSuffix(as); + ActionScriptParser par = new ActionScriptParser(swf.version); + try { + asm.setActions(par.actionsFromString(as)); + } catch (ActionParseException | CompilationException ex) { + fail("Unable to parse: " + asm.getSwf().getShortFileName() + "/" + asm.toString()); + } + writer = new HighlightedTextWriter(new CodeFormatting(), false); + Action.actionsToSource(asm, asm.getActions(), asm.toString()/*FIXME?*/, writer); + String as2 = writer.toString(); + as2 = asm.removePrefixAndSuffix(as2); + try { + asm.setActions(par.actionsFromString(as2)); + } catch (ActionParseException | CompilationException ex) { + fail("Unable to parse: " + asm.getSwf().getShortFileName() + "/" + asm.toString()); + } + writer = new HighlightedTextWriter(new CodeFormatting(), false); + Action.actionsToSource(asm, asm.getActions(), asm.toString()/*FIXME?*/, writer); + String as3 = writer.toString(); + as3 = asm.removePrefixAndSuffix(as3); + if (!as3.equals(as2)) { + fail("ActionScript is different: " + asm.getSwf().getShortFileName() + "/" + asm.toString()); + } + } catch (InterruptedException | IOException | OutOfMemoryError | TranslateException | StackOverflowError ex) { + } + } + } + } catch (Exception ex) { + fail("Exception during decompilation: " + fileName + " " + ex.getMessage()); + } + } + + @Override + public String getTestDataDir() { + return TESTDATADIR; + } +} diff --git a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ExportTest.java b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ExportTest.java index 5e975779a..228d8fbca 100644 --- a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ExportTest.java +++ b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ExportTest.java @@ -20,34 +20,35 @@ import com.jpexs.decompiler.flash.configuration.Configuration; import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode; import java.io.File; import java.io.FileInputStream; -import java.io.FilenameFilter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; import static org.testng.Assert.fail; +import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; -import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /** * * @author JPEXS */ -public class ExportTest { +public class ExportTest extends FileTestBase { @BeforeClass public void init() { - //Main.initLogging(false); + Configuration.autoDeobfuscate.set(false); + Configuration.debugCopy.set(false); } public static final String TESTDATADIR = "testdata/decompile"; + public static Handler loggerHandler; + @BeforeClass public void addLogger() { - Configuration.autoDeobfuscate.set(true); Logger logger = Logger.getLogger(""); - logger.addHandler(new Handler() { + loggerHandler = new Handler() { @Override public void publish(LogRecord record) { if (record.getLevel() == Level.SEVERE) { @@ -62,44 +63,33 @@ public class ExportTest { @Override public void close() throws SecurityException { } - }); + }; + + logger.addHandler(loggerHandler); } - @DataProvider(name = "swfFiles") - public Object[][] createData() { - File dir = new File(TESTDATADIR); - File[] files = new File[0]; - if (dir.exists()) { - files = dir.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.toLowerCase().endsWith(".swf"); - } - }); + @AfterClass + public void removeLogger() { + if (loggerHandler != null) { + Logger logger = Logger.getLogger(""); + logger.removeHandler(loggerHandler); } - Object[][] ret = new Object[files.length + 2][1]; - ret[0][0] = new File(TESTDATADIR + File.separator + "../as2/as2.swf"); - ret[1][0] = new File(TESTDATADIR + File.separator + "../as3/as3.swf"); - for (int f = 0; f < files.length; f++) { - ret[f + 2][0] = files[f]; - } - return ret; } - @Test(dataProvider = "swfFiles") - public void testDecompileAS(File f) { - testDecompile(f, ScriptExportMode.AS); + @Test(dataProvider = "provideFiles") + public void testDecompileAS(String fileName) { + testDecompile(fileName, ScriptExportMode.AS); } - @Test(dataProvider = "swfFiles") - public void testDecompilePcode(File f) { - testDecompile(f, ScriptExportMode.PCODE); + @Test(dataProvider = "provideFiles") + public void testDecompilePcode(String fileName) { + testDecompile(fileName, ScriptExportMode.PCODE); } - public void testDecompile(File f, ScriptExportMode exportMode) { + public void testDecompile(String fileName, ScriptExportMode exportMode) { try { - SWF swf = new SWF(new FileInputStream(f), false); - Configuration.debugCopy.set(true); + File f = new File(fileName); + SWF swf = new SWF(new FileInputStream(TESTDATADIR + File.separator + fileName), false); String folderName = exportMode == ScriptExportMode.AS ? "output" : "outputp"; File fdir = new File(TESTDATADIR + File.separator + folderName + File.separator + f.getName()); fdir.mkdirs(); @@ -117,7 +107,12 @@ public class ExportTest { }, fdir.getAbsolutePath(), exportMode, false, null); } catch (Exception ex) { - fail(); + fail("Exception during decompilation: " + fileName + " " + ex.getMessage()); } } + + @Override + public String getTestDataDir() { + return TESTDATADIR; + } } diff --git a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/FileTestBase.java b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/FileTestBase.java new file mode 100644 index 000000000..8ee0c0820 --- /dev/null +++ b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/FileTestBase.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2010-2015 JPEXS, All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. + */ +package com.jpexs.decompiler.flash; + +import java.io.File; +import java.io.FilenameFilter; +import org.testng.annotations.DataProvider; + +/** + * + * @author JPEXS + */ +public abstract class FileTestBase { + + protected abstract String getTestDataDir(); + + @DataProvider(name = "provideFiles") + public Object[][] provideFiles() { + File dir = new File(getTestDataDir()); + File[] files = new File[0]; + if (dir.exists()) { + files = dir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.toLowerCase().endsWith(".swf"); + } + }); + } + Object[][] ret = new Object[files.length + 2][1]; + ret[0][0] = "../as2/as2.swf"; + ret[1][0] = "../as3/as3.swf"; + for (int f = 0; f < files.length; f++) { + ret[f + 2][0] = files[f].getName(); + } + return ret; + } +} diff --git a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/RecompileTest.java b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/RecompileTest.java index adc9b3f9c..df8ec530f 100644 --- a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/RecompileTest.java +++ b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/RecompileTest.java @@ -16,144 +16,52 @@ */ package com.jpexs.decompiler.flash; -import com.jpexs.decompiler.flash.abc.ABC; -import com.jpexs.decompiler.flash.abc.ClassPath; import com.jpexs.decompiler.flash.abc.NotSameException; -import com.jpexs.decompiler.flash.abc.ScriptPack; -import com.jpexs.decompiler.flash.abc.avm2.parser.AVM2ParseException; -import com.jpexs.decompiler.flash.action.Action; -import com.jpexs.decompiler.flash.action.parser.ActionParseException; -import com.jpexs.decompiler.flash.action.parser.script.ActionScriptParser; import com.jpexs.decompiler.flash.configuration.Configuration; -import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode; -import com.jpexs.decompiler.flash.helpers.CodeFormatting; -import com.jpexs.decompiler.flash.helpers.HighlightedTextWriter; -import com.jpexs.decompiler.flash.helpers.collections.MyEntry; -import com.jpexs.decompiler.flash.tags.ABCContainerTag; import com.jpexs.decompiler.flash.tags.Tag; import com.jpexs.decompiler.flash.tags.TagStub; -import com.jpexs.decompiler.flash.tags.base.ASMSource; -import com.jpexs.decompiler.graph.CompilationException; -import com.jpexs.decompiler.graph.TranslateException; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FilenameFilter; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; import static org.testng.Assert.fail; import org.testng.annotations.BeforeClass; -import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /** * * @author JPEXS */ -public class RecompileTest { +public class RecompileTest extends FileTestBase { @BeforeClass public void init() { - //Main.initLogging(false); + Configuration.autoDeobfuscate.set(false); } public static final String TESTDATADIR = "testdata/recompile"; @Test(dataProvider = "provideFiles") - public void testRecompile(String filename) { + public void testRecompile(String fileName) { try { - try (FileInputStream fis = new FileInputStream(TESTDATADIR + File.separator + filename)) { + try (FileInputStream fis = new FileInputStream(TESTDATADIR + File.separator + fileName)) { Configuration.debugCopy.set(true); SWF swf = new SWF(new BufferedInputStream(fis), false); swf.saveTo(new ByteArrayOutputStream()); } - } catch (IOException | InterruptedException ex) { - fail(); } catch (NotSameException ex) { - fail("File is different after recompiling: " + filename); + fail("File is different after recompiling: " + fileName); + } catch (IOException | InterruptedException ex) { + fail("Exception during decompilation: " + fileName + " " + ex.getMessage()); } } @Test(dataProvider = "provideFiles") - public void testDirectEditing(String filename) throws IOException, InterruptedException, AVM2ParseException, CompilationException { - Configuration.autoDeobfuscate.set(false); + public void testTagEditing(String fileName) throws IOException, InterruptedException { try { - SWF swf = new SWF(new BufferedInputStream(new FileInputStream(TESTDATADIR + File.separator + filename)), false); - if (swf.isAS3()) { - boolean dotest = false; - List allAbcs = new ArrayList<>(); - for (ABCContainerTag ct : swf.getAbcList()) { - allAbcs.add(ct.getABC()); - } - for (ABC abc : allAbcs) { - for (int s = 0; s < abc.script_info.size(); s++) { - String startAfter = null; - HighlightedTextWriter htw = new HighlightedTextWriter(new CodeFormatting(), false); - MyEntry en = abc.script_info.get(s).getPacks(abc, s).get(0); - if (startAfter == null || en.getKey().toString().equals(startAfter)) { - dotest = true; - } - if (!dotest) { - System.out.println("Skipped:" + en.getKey().toString()); - continue; - } - - System.out.println("Recompiling:" + en.getKey().toString() + "..."); - en.getValue().toSource(htw, abc.script_info.get(s).traits.traits, ScriptExportMode.AS, false); - String original = htw.toString(); - com.jpexs.decompiler.flash.abc.avm2.parser.script.ActionScriptParser.compile(original, abc, allAbcs, false, en.getKey().className + ".as", abc.instance_info.size()); - //remove last compiled script: - abc.script_info.remove(abc.script_info.size() - 1); - } - } - } else { - Map asms = swf.getASMs(false); - - for (ASMSource asm : asms.values()) { - try { - HighlightedTextWriter writer = new HighlightedTextWriter(new CodeFormatting(), false); - Action.actionsToSource(asm, asm.getActions(), asm.toString()/*FIXME?*/, writer); - String as = writer.toString(); - as = asm.removePrefixAndSuffix(as); - ActionScriptParser par = new ActionScriptParser(swf.version); - try { - asm.setActions(par.actionsFromString(as)); - } catch (ActionParseException | CompilationException ex) { - fail("Unable to parse: " + asm.getSwf().getShortFileName() + "/" + asm.toString()); - } - writer = new HighlightedTextWriter(new CodeFormatting(), false); - Action.actionsToSource(asm, asm.getActions(), asm.toString()/*FIXME?*/, writer); - String as2 = writer.toString(); - as2 = asm.removePrefixAndSuffix(as2); - try { - asm.setActions(par.actionsFromString(as2)); - } catch (ActionParseException | CompilationException ex) { - fail("Unable to parse: " + asm.getSwf().getShortFileName() + "/" + asm.toString()); - } - writer = new HighlightedTextWriter(new CodeFormatting(), false); - Action.actionsToSource(asm, asm.getActions(), asm.toString()/*FIXME?*/, writer); - String as3 = writer.toString(); - as3 = asm.removePrefixAndSuffix(as3); - if (!as3.equals(as2)) { - fail("ActionScript is different: " + asm.getSwf().getShortFileName() + "/" + asm.toString()); - } - } catch (InterruptedException | IOException | OutOfMemoryError | TranslateException | StackOverflowError ex) { - } - } - } - } catch (Exception ex) { - System.out.println("FAIL"); - throw ex; - } - } - - @Test(dataProvider = "provideFiles") - public void testTagEditing(String filename) throws IOException, InterruptedException { - try { - SWF swf = new SWF(new BufferedInputStream(new FileInputStream(TESTDATADIR + File.separator + filename)), false); + Configuration.debugCopy.set(false); + SWF swf = new SWF(new BufferedInputStream(new FileInputStream(TESTDATADIR + File.separator + fileName)), false); for (Tag tag : swf.tags) { if (!(tag instanceof TagStub)) { Tag tag2 = tag.cloneTag(); @@ -163,28 +71,12 @@ public class RecompileTest { } } } catch (Exception ex) { - // ignore + fail("Exception during decompilation: " + fileName + " " + ex.getMessage()); } } - @DataProvider(name = "provideFiles") - public Object[][] provideFiles() { - File dir = new File(TESTDATADIR); - File[] files = new File[0]; - if (dir.exists()) { - files = dir.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.toLowerCase().endsWith(".swf"); - } - }); - } - Object[][] ret = new Object[files.length + 2][1]; - ret[0][0] = "../as2/as2.swf"; - ret[1][0] = "../as3/as3.swf"; - for (int f = 0; f < files.length; f++) { - ret[f + 2][0] = files[f].getName(); - } - return ret; + @Override + public String getTestDataDir() { + return TESTDATADIR; } } diff --git a/src/com/jpexs/decompiler/flash/gui/MainFrameRibbonMenu.java b/src/com/jpexs/decompiler/flash/gui/MainFrameRibbonMenu.java index 39c1dc673..2554e85ab 100644 --- a/src/com/jpexs/decompiler/flash/gui/MainFrameRibbonMenu.java +++ b/src/com/jpexs/decompiler/flash/gui/MainFrameRibbonMenu.java @@ -266,7 +266,7 @@ public class MainFrameRibbonMenu extends MainFrameMenu implements ActionListener ribbon.addTask(createSettingsRibbonTask(externalFlashPlayerUnavailable)); ribbon.addTask(createHelpRibbonTask()); - if (Configuration.showDebugMenu.get()) { + if (Configuration.showDebugMenu.get() || Configuration.debugMode.get()) { ribbon.addTask(createDebugRibbonTask()); } @@ -726,10 +726,10 @@ public class MainFrameRibbonMenu extends MainFrameMenu implements ActionListener memoryInformationCommandButton.addActionListener(e -> { String architecture = System.getProperty("sun.arch.data.model"); Runtime runtime = Runtime.getRuntime(); - String info = "Architecture: " + architecture + Helper.newLine + - "Max: " + (runtime.maxMemory() / 1024 / 1024)+ "MB" + Helper.newLine + - "Used: " + (runtime.totalMemory() / 1024 / 1024)+ "MB" + Helper.newLine + - "Free: " + (runtime.freeMemory() / 1024 / 1024)+ "MB"; + String info = "Architecture: " + architecture + Helper.newLine + + "Max: " + (runtime.maxMemory() / 1024 / 1024) + "MB" + Helper.newLine + + "Used: " + (runtime.totalMemory() / 1024 / 1024) + "MB" + Helper.newLine + + "Free: " + (runtime.freeMemory() / 1024 / 1024) + "MB"; View.showMessageDialog(null, info); SWF swf = mainFrame.getPanel().getCurrentSwf(); if (swf != null) { diff --git a/src/com/jpexs/decompiler/flash/gui/PreviewPanel.java b/src/com/jpexs/decompiler/flash/gui/PreviewPanel.java index 572b8e762..eaabc8869 100644 --- a/src/com/jpexs/decompiler/flash/gui/PreviewPanel.java +++ b/src/com/jpexs/decompiler/flash/gui/PreviewPanel.java @@ -948,7 +948,7 @@ public class PreviewPanel extends JSplitPane implements ActionListener { new ShowFrameTag(swf).writeTag(sos2); } - }//not showframe + } // not showframe new EndTag(swf).writeTag(sos2); data = baos.toByteArray();