From 2ef4e759c0a3c3330ce9c339d7d0440630c0134f Mon Sep 17 00:00:00 2001 From: "honfika@gmail.com" Date: Thu, 18 Jun 2015 08:35:15 +0200 Subject: [PATCH] windows+mac "exe" export with adobe projector --- .../src/com/jpexs/decompiler/flash/SWF.java | 10 ++- .../flash/configuration/Configuration.java | 54 +++++++++++---- .../flash/exporters/modes/ExeExportMode.java | 3 +- .../src/com/jpexs/helpers/Helper.java | 13 ++++ src/com/jpexs/decompiler/flash/gui/Main.java | 65 +++++++++++++------ 5 files changed, 108 insertions(+), 37 deletions(-) diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWF.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWF.java index 18da4c582..42176573f 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWF.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWF.java @@ -795,14 +795,18 @@ public final class SWF implements SWFContainerItem, Timelined { } os.write(data); } else if (compression == SWFCompression.ZLIB) { - try (DeflaterOutputStream dos = new DeflaterOutputStream(os)) { - Helper.copyStream(is, dos, Long.MAX_VALUE); + DeflaterOutputStream dos = new DeflaterOutputStream(os); + try { + Helper.copyStream(is, dos); + } finally { + dos.finish(); } } else { - Helper.copyStream(is, os, Long.MAX_VALUE); + Helper.copyStream(is, os); } } + @Override public boolean isModified() { for (Tag tag : tags) { if (tag.isModified()) { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java index a8fb6111d..861368ca3 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java @@ -879,26 +879,56 @@ public class Configuration { } public static File getPlayerSWC() { - String a = getLatestPlayerGlobalUrl(); - String b = getLatestProjectorUrlWin(); - String c = getLatestProjectorUrlMac(); - String d = getLatestProjectorUrlLinux(); - File libsdir = getFlashLibPath(); - if (libsdir != null && libsdir.exists()) { - File[] libs = libsdir.listFiles(new FilenameFilter() { + File libsDir = getFlashLibPath(); + if (libsDir != null && libsDir.exists()) { + File[] libs = libsDir.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.toLowerCase().startsWith("playerglobal"); } }); - List libnames = new ArrayList<>(); + List libNames = new ArrayList<>(); for (File f : libs) { - libnames.add(f.getName()); + libNames.add(f.getName()); } - Collections.sort(libnames); - if (!libnames.isEmpty()) { - return new File(libsdir.getAbsolutePath() + File.separator + libnames.get(libnames.size() - 1)); + Collections.sort(libNames); + if (!libNames.isEmpty()) { + return new File(libsDir.getAbsolutePath() + File.separator + libNames.get(libNames.size() - 1)); + } else { + return null; + } + } + + return null; + } + + public static File getProjectorFile(ExeExportMode exportMode) { + File projectoDir = getProjectorPath(); + if (projectoDir != null && projectoDir.exists()) { + File[] projectors = projectoDir.listFiles(new FilenameFilter() { + + @Override + public boolean accept(File dir, String name) { + switch (exportMode) { + case PROJECTOR_WIN: + return name.toLowerCase().endsWith(".exe"); + case PROJECTOR_MAC: + return name.toLowerCase().endsWith(".dmg"); + case PROJECTOR_LINUX: + return name.toLowerCase().endsWith(".gz"); + } + + return false; + } + }); + List projectorNames = new ArrayList<>(); + for (File f : projectors) { + projectorNames.add(f.getName()); + } + Collections.sort(projectorNames); + if (!projectorNames.isEmpty()) { + return new File(projectoDir.getAbsolutePath() + File.separator + projectorNames.get(projectorNames.size() - 1)); } else { return null; } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/modes/ExeExportMode.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/modes/ExeExportMode.java index 6288c5a23..187d9699f 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/modes/ExeExportMode.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/modes/ExeExportMode.java @@ -22,6 +22,5 @@ package com.jpexs.decompiler.flash.exporters.modes; */ public enum ExeExportMode { - WRAPPER, PROJECTOR_WIN, PROJECTOR_MAC/*, PROJECTOR_LINUX*/ - + WRAPPER, PROJECTOR_WIN, PROJECTOR_MAC, PROJECTOR_LINUX } diff --git a/libsrc/ffdec_lib/src/com/jpexs/helpers/Helper.java b/libsrc/ffdec_lib/src/com/jpexs/helpers/Helper.java index 4da3030ec..d5eeea231 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/helpers/Helper.java +++ b/libsrc/ffdec_lib/src/com/jpexs/helpers/Helper.java @@ -655,6 +655,19 @@ public class Helper { return baos.toByteArray(); } + public static void copyStream(InputStream is, OutputStream os) { + try { + final int bufSize = 4096; + byte[] buf = new byte[bufSize]; + int cnt = 0; + while ((cnt = is.read(buf)) > 0) { + os.write(buf, 0, cnt); + } + } catch (IOException ex) { + // ignore + } + } + public static void copyStream(InputStream is, OutputStream os, long maxLength) { try { final int bufSize = 4096; diff --git a/src/com/jpexs/decompiler/flash/gui/Main.java b/src/com/jpexs/decompiler/flash/gui/Main.java index 385c7c438..a3542e8af 100644 --- a/src/com/jpexs/decompiler/flash/gui/Main.java +++ b/src/com/jpexs/decompiler/flash/gui/Main.java @@ -67,7 +67,6 @@ import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStream; import java.lang.reflect.Field; import java.net.InetSocketAddress; import java.net.Proxy; @@ -392,37 +391,63 @@ public class Main { } File outfileF = new File(outfile); File tmpFile = new File(outfile + ".tmp"); - try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(tmpFile))) { + try (FileOutputStream fos = new FileOutputStream(tmpFile); + BufferedOutputStream bos = new BufferedOutputStream(fos)) { if (mode == SaveFileMode.EXE) { switch (exeExportMode) { case WRAPPER: InputStream exeStream = View.class.getClassLoader().getResourceAsStream("com/jpexs/helpers/resource/Swf2Exe.bin"); - byte[] buffer = new byte[4096]; - int bytesRead; - while ((bytesRead = exeStream.read(buffer)) != -1) { - fos.write(buffer, 0, bytesRead); - } + Helper.copyStream(exeStream, bos); int width = swf.displayRect.Xmax - swf.displayRect.Xmin; int height = swf.displayRect.Ymax - swf.displayRect.Ymin; - fos.write(width & 0xff); - fos.write((width >> 8) & 0xff); - fos.write((width >> 16) & 0xff); - fos.write((width >> 24) & 0xff); - fos.write(height & 0xff); - fos.write((height >> 8) & 0xff); - fos.write((height >> 16) & 0xff); - fos.write((height >> 24) & 0xff); - fos.write(Configuration.saveAsExeScaleMode.get()); + bos.write(width & 0xff); + bos.write((width >> 8) & 0xff); + bos.write((width >> 16) & 0xff); + bos.write((width >> 24) & 0xff); + bos.write(height & 0xff); + bos.write((height >> 8) & 0xff); + bos.write((height >> 16) & 0xff); + bos.write((height >> 24) & 0xff); + bos.write(Configuration.saveAsExeScaleMode.get()); break; case PROJECTOR_WIN: - // todo - break; case PROJECTOR_MAC: - // todo + case PROJECTOR_LINUX: + File projectorFile = Configuration.getProjectorFile(exeExportMode); + if (projectorFile == null) { + String message = "Projector not found, please place it to " + Configuration.getProjectorPath(); + logger.log(Level.SEVERE, message); + throw new IOException(message); + } + Helper.copyStream(new FileInputStream(projectorFile), bos); + bos.flush(); break; } } - swf.saveTo(fos); + + long pos = fos.getChannel().position(); + swf.saveTo(bos); + + if (mode == SaveFileMode.EXE) { + switch (exeExportMode) { + case PROJECTOR_WIN: + case PROJECTOR_MAC: + case PROJECTOR_LINUX: + bos.flush(); + int swfSize = (int) (fos.getChannel().position() - pos); + + // write magic number + bos.write(0x56); + bos.write(0x34); + bos.write(0x12); + bos.write(0xfa); + + bos.write(swfSize & 0xff); + bos.write((swfSize >> 8) & 0xff); + bos.write((swfSize >> 16) & 0xff); + bos.write((swfSize >> 24) & 0xff); + } + } } if (tmpFile.exists()) { if (tmpFile.length() > 0) {