diff --git a/trunk/src/com/jpexs/asdec/EventListener.java b/trunk/src/com/jpexs/asdec/EventListener.java new file mode 100644 index 000000000..87ae29e60 --- /dev/null +++ b/trunk/src/com/jpexs/asdec/EventListener.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2010-2012 JPEXS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.jpexs.asdec; + +/** + * + * @author Jindra + */ +public interface EventListener { + public void handleEvent(String event,Object data); +} diff --git a/trunk/src/com/jpexs/asdec/Main.java b/trunk/src/com/jpexs/asdec/Main.java index 43c23618c..9890bc826 100644 --- a/trunk/src/com/jpexs/asdec/Main.java +++ b/trunk/src/com/jpexs/asdec/Main.java @@ -171,6 +171,13 @@ public class Main { FileInputStream fis = new FileInputStream(file); InputStream bis = new BufferedInputStream(fis); SWF locswf = new SWF(bis); + locswf.addEventListener(new EventListener() { + public void handleEvent(String event, Object data) { + if (event.equals("export")) { + startWork((String) data); + } + } + }); return locswf; } @@ -179,72 +186,6 @@ public class Main { swf.saveTo(new FileOutputStream(outfile)); } - public static boolean exportSWF(String inFile, String outdir, boolean isPcode) throws Exception { - SWF swf = parseSWF(inFile); - boolean asV3Found = false; - for (Tag t : swf.tags) { - if (t instanceof DoABCTag) { - ((DoABCTag) t).abc.export(outdir, isPcode); - asV3Found = true; - } - } - if (!asV3Found) { - List list2 = new ArrayList(); - list2.addAll(swf.tags); - return exportNode(TagNode.createTagList(list2), outdir, isPcode); - } - return asV3Found; - } - - public static boolean exportNode(List nodeList, String outdir, boolean isPcode) { - File dir = new File(outdir); - if (!dir.exists()) { - dir.mkdirs(); - } - List existingNames = new ArrayList(); - for (TagNode node : nodeList) { - String name = ""; - if (node.tag instanceof TagName) { - name = ((TagName) node.tag).getName(); - } else { - name = node.tag.toString(); - } - int i = 1; - String baseName = name; - while (existingNames.contains(name)) { - i++; - name = baseName + "_" + i; - } - existingNames.add(name); - if (node.subItems.isEmpty()) { - if (node.tag instanceof ASMSource) { - try { - String f = outdir + File.separatorChar + name + ".as"; - Main.startWork("Exporting " + f + " ..."); - String ret = ""; - if (isPcode) { - ret = ((ASMSource) node.tag).getASMSource(10); //TODO:Ensure correct version here - } else { - List as = ((ASMSource) node.tag).getActions(10);//TODO:Ensure correct version here - com.jpexs.asdec.action.Action.setActionsAddresses(as, 0, 10);//TODO:Ensure correct version here - ret = (Highlighting.stripHilights(com.jpexs.asdec.action.Action.actionsToSource(as, 10))); //TODO:Ensure correct version here - } - - - FileOutputStream fos = new FileOutputStream(f); - fos.write(ret.getBytes()); - fos.close(); - } catch (Exception ex) { - } - } - } else { - exportNode(node.subItems, outdir + File.separatorChar + name, isPcode); - } - - } - return true; - } - private static class OpenFileWorker extends SwingWorker { @Override @@ -542,7 +483,15 @@ public class Main { boolean exportOK = true; try { printHeader(); - exportOK = exportSWF(inFile.getAbsolutePath(), outDir.getAbsolutePath(), exportFormat.equals("pcode")); + SWF exfile = new SWF(new FileInputStream(inFile)); + exfile.addEventListener(new EventListener() { + public void handleEvent(String event, Object data) { + if (event.equals("export")) { + System.out.println((String) data); + } + } + }); + exportOK = exfile.exportActionScript(outDir.getAbsolutePath(), exportFormat.equals("pcode")); } catch (Exception ex) { exportOK = false; System.err.print("FAIL: Exporting Failed on Exception - "); diff --git a/trunk/src/com/jpexs/asdec/SWF.java b/trunk/src/com/jpexs/asdec/SWF.java index 89cde239e..b4d63e920 100644 --- a/trunk/src/com/jpexs/asdec/SWF.java +++ b/trunk/src/com/jpexs/asdec/SWF.java @@ -17,10 +17,16 @@ package com.jpexs.asdec; import SevenZip.Compression.LZMA.Encoder; +import com.jpexs.asdec.action.TagNode; +import com.jpexs.asdec.helpers.Highlighting; +import com.jpexs.asdec.tags.ASMSource; +import com.jpexs.asdec.tags.DoABCTag; import com.jpexs.asdec.tags.Tag; +import com.jpexs.asdec.tags.TagName; import com.jpexs.asdec.types.RECT; import java.io.*; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.zip.DeflaterOutputStream; import java.util.zip.InflaterInputStream; @@ -277,4 +283,95 @@ public class SWF { } return true; } + + + public boolean exportActionScript(String outdir, boolean isPcode) throws Exception { + boolean asV3Found = false; + final EventListener evl=new EventListener(){ + + public void handleEvent(String event, Object data) { + if(event.equals("export")){ + informListeners(event, data); + } + } + + }; + for (Tag t : tags) { + if (t instanceof DoABCTag) { + ((DoABCTag) t).abc.addEventListener(evl); + ((DoABCTag) t).abc.export(outdir, isPcode); + asV3Found = true; + } + } + if (!asV3Found) { + List list2 = new ArrayList(); + list2.addAll(tags); + return exportNode(TagNode.createTagList(list2), outdir, isPcode); + } + return asV3Found; + } + + private boolean exportNode(List nodeList, String outdir, boolean isPcode) { + File dir = new File(outdir); + if (!dir.exists()) { + dir.mkdirs(); + } + List existingNames = new ArrayList(); + for (TagNode node : nodeList) { + String name = ""; + if (node.tag instanceof TagName) { + name = ((TagName) node.tag).getName(); + } else { + name = node.tag.toString(); + } + int i = 1; + String baseName = name; + while (existingNames.contains(name)) { + i++; + name = baseName + "_" + i; + } + existingNames.add(name); + if (node.subItems.isEmpty()) { + if (node.tag instanceof ASMSource) { + try { + String f = outdir + File.separatorChar + name + ".as"; + informListeners("export", "Exporting " + f + " ..."); + String ret = ""; + if (isPcode) { + ret = ((ASMSource) node.tag).getASMSource(10); //TODO:Ensure correct version here + } else { + List as = ((ASMSource) node.tag).getActions(10);//TODO:Ensure correct version here + com.jpexs.asdec.action.Action.setActionsAddresses(as, 0, 10);//TODO:Ensure correct version here + ret = (Highlighting.stripHilights(com.jpexs.asdec.action.Action.actionsToSource(as, 10))); //TODO:Ensure correct version here + } + + + FileOutputStream fos = new FileOutputStream(f); + fos.write(ret.getBytes()); + fos.close(); + } catch (Exception ex) { + } + } + } else { + exportNode(node.subItems, outdir + File.separatorChar + name, isPcode); + } + + } + return true; + } + + + protected HashSet listeners=new HashSet(); + + public void addEventListener(EventListener listener){ + listeners.add(listener); + } + public void removeEventListener(EventListener listener){ + listeners.remove(listener); + } + protected void informListeners(String event,Object data){ + for(EventListener listener:listeners){ + listener.handleEvent(event, data); + } + } } diff --git a/trunk/src/com/jpexs/asdec/abc/ABC.java b/trunk/src/com/jpexs/asdec/abc/ABC.java index 3b46f4cf8..9baaf6da4 100644 --- a/trunk/src/com/jpexs/asdec/abc/ABC.java +++ b/trunk/src/com/jpexs/asdec/abc/ABC.java @@ -16,6 +16,7 @@ */ package com.jpexs.asdec.abc; +import com.jpexs.asdec.EventListener; import com.jpexs.asdec.Main; import com.jpexs.asdec.abc.avm2.AVM2Code; import com.jpexs.asdec.abc.avm2.ConstantPool; @@ -34,6 +35,7 @@ import com.jpexs.asdec.helpers.Helper; import com.jpexs.asdec.helpers.Highlighting; import java.io.*; import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Stack; @@ -56,6 +58,22 @@ public class ABC { public static final int MINORwithDECIMAL = 17; public static final boolean AUTOINIT_STATIC_VARIABLES = false; + protected HashSet listeners=new HashSet(); + + public void addEventListener(EventListener listener){ + listeners.add(listener); + } + public void removeEventListener(EventListener listener){ + listeners.remove(listener); + } + protected void informListeners(String event,Object data){ + for(EventListener listener:listeners){ + listener.handleEvent(event, data); + } + } + + + public int deobfuscateIdentifiers() { int ret = 0; for (int i = 1; i < constants.constant_multiname.length; i++) { @@ -721,7 +739,7 @@ public class ABC { if ((packageName != null) && (!packageName.equals(""))) { fullName = packageName + "." + fullName; } - Main.startWork("Exporting " + (i + 1) + "/" + instance_info.length + " " + fullName + " ..."); + informListeners("export", "Exporting " + (i + 1) + "/" + instance_info.length + " " + fullName + " ..."); File outDir = new File(directory + File.separatorChar + packageName.replace('.', File.separatorChar)); if (!outDir.exists()) { outDir.mkdirs(); diff --git a/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java b/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java index ef0e200cb..82929816d 100644 --- a/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java +++ b/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java @@ -19,6 +19,7 @@ package com.jpexs.asdec.abc.gui; import com.jpexs.asdec.Configuration; import com.jpexs.asdec.Main; import com.jpexs.asdec.abc.ABC; +import com.jpexs.asdec.EventListener; import com.jpexs.asdec.abc.gui.tablemodels.*; import com.jpexs.asdec.gui.LoadingPanel; import com.jpexs.asdec.gui.View; @@ -417,11 +418,9 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener { (new Thread() { @Override public void run() { - try { - for (DoABCTag tag : list) { - tag.abc.export(selFile, isPcode); - } - } catch (IOException ignored) { + try { + Main.swf.exportActionScript(selFile, isPcode); + } catch (Exception ignored) { JOptionPane.showMessageDialog(null, "Cannot write to the file"); } Main.stopWork(); diff --git a/trunk/src/com/jpexs/asdec/action/gui/MainFrame.java b/trunk/src/com/jpexs/asdec/action/gui/MainFrame.java index a41489c1c..bddc9b0e4 100644 --- a/trunk/src/com/jpexs/asdec/action/gui/MainFrame.java +++ b/trunk/src/com/jpexs/asdec/action/gui/MainFrame.java @@ -335,9 +335,7 @@ public class MainFrame extends JFrame implements TreeSelectionListener, ActionLi @Override public void run() { try { - List list2 = new ArrayList(); - list2.addAll(list); - Main.exportNode(TagNode.createTagList(list2), selFile, isPcode); + Main.swf.exportActionScript(selFile, isPcode); } catch (Exception ignored) { JOptionPane.showMessageDialog(null, "Cannot write to the file"); }