#845 Import exported AS1/2 (DefineButton2&DefineSprite) button

This commit is contained in:
honfika@gmail.com
2015-04-26 21:06:54 +02:00
parent d28d72c096
commit 92712a7233
15 changed files with 321 additions and 153 deletions

View File

@@ -65,6 +65,7 @@ import com.jpexs.decompiler.flash.exporters.commonshape.Matrix;
import com.jpexs.decompiler.flash.exporters.commonshape.SVGExporter;
import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode;
import com.jpexs.decompiler.flash.exporters.script.AS2ScriptExporter;
import com.jpexs.decompiler.flash.exporters.script.AS3ScriptExporter;
import com.jpexs.decompiler.flash.exporters.settings.ScriptExportSettings;
import com.jpexs.decompiler.flash.helpers.HighlightedText;
import com.jpexs.decompiler.flash.helpers.HighlightedTextWriter;
@@ -130,7 +131,6 @@ import com.jpexs.decompiler.graph.TranslateStack;
import com.jpexs.decompiler.graph.model.LocalData;
import com.jpexs.helpers.ByteArrayRange;
import com.jpexs.helpers.Cache;
import com.jpexs.helpers.CancellableWorker;
import com.jpexs.helpers.Helper;
import com.jpexs.helpers.NulStream;
import com.jpexs.helpers.ProgressListener;
@@ -161,14 +161,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.DeflaterOutputStream;
@@ -1204,136 +1196,6 @@ public final class SWF implements SWFContainerItem, Timelined {
return displayRect;
}
private class ExportPackTask implements Callable<File> {
ScriptPack pack;
String directory;
ScriptExportSettings exportSettings;
ClassPath path;
AtomicInteger index;
int count;
boolean parallel;
AbortRetryIgnoreHandler handler;
long startTime;
long stopTime;
EventListener eventListener;
public ExportPackTask(AbortRetryIgnoreHandler handler, AtomicInteger index, int count, ClassPath path, ScriptPack pack, String directory, ScriptExportSettings exportSettings, boolean parallel, EventListener evl) {
this.pack = pack;
this.directory = directory;
this.exportSettings = exportSettings;
this.path = path;
this.index = index;
this.count = count;
this.parallel = parallel;
this.handler = handler;
this.eventListener = evl;
}
@Override
public File call() throws Exception {
RunnableIOExResult<File> rio = new RunnableIOExResult<File>() {
@Override
public void run() throws IOException {
startTime = System.currentTimeMillis();
this.result = pack.export(directory, exportSettings, parallel);
stopTime = System.currentTimeMillis();
}
};
int currentIndex = index.getAndIncrement();
if (eventListener != null) {
synchronized (ABC.class) {
eventListener.handleExportingEvent("script", currentIndex, count, path);
}
}
new RetryTask(rio, handler).run();
if (eventListener != null) {
synchronized (ABC.class) {
long time = stopTime - startTime;
eventListener.handleExportedEvent("script", currentIndex, count, path + ", " + Helper.formatTimeSec(time));
}
}
return rio.result;
}
}
private List<File> exportActionScript2(AbortRetryIgnoreHandler handler, String outdir, ScriptExportSettings exportSettings, boolean parallel, EventListener evl) throws IOException {
List<File> ret = new ArrayList<>();
if (!outdir.endsWith(File.separator)) {
outdir += File.separator;
}
ret.addAll(new AS2ScriptExporter().exportAS2ScriptsTimeout(handler, outdir, getASMs(true), exportSettings, evl));
return ret;
}
private List<File> exportActionScript3(final AbortRetryIgnoreHandler handler, final String outdir, final ScriptExportSettings exportSettings, final boolean parallel, final EventListener evl) {
final AtomicInteger cnt = new AtomicInteger(1);
final List<File> ret = new ArrayList<>();
final List<ScriptPack> packs = getAS3Packs();
if (!parallel || packs.size() < 2) {
try {
CancellableWorker.call(new Callable<Void>() {
@Override
public Void call() throws Exception {
for (ScriptPack item : packs) {
ExportPackTask task = new ExportPackTask(handler, cnt, packs.size(), item.getClassPath(), item, outdir, exportSettings, parallel, evl);
ret.add(task.call());
}
return null;
}
}, Configuration.exportTimeout.get(), TimeUnit.SECONDS);
} catch (TimeoutException ex) {
logger.log(Level.SEVERE, Helper.formatTimeToText(Configuration.exportTimeout.get()) + " ActionScript export limit reached", ex);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Error during ABC export", ex);
}
} else {
ExecutorService executor = Executors.newFixedThreadPool(Configuration.getParallelThreadCount());
List<Future<File>> futureResults = new ArrayList<>();
for (ScriptPack item : packs) {
Future<File> future = executor.submit(new ExportPackTask(handler, cnt, packs.size(), item.getClassPath(), item, outdir, exportSettings, parallel, evl));
futureResults.add(future);
}
try {
executor.shutdown();
if (!executor.awaitTermination(Configuration.exportTimeout.get(), TimeUnit.SECONDS)) {
logger.log(Level.SEVERE, Helper.formatTimeToText(Configuration.exportTimeout.get()) + " ActionScript export limit reached");
}
} catch (InterruptedException ex) {
} finally {
executor.shutdownNow();
}
for (int f = 0; f < futureResults.size(); f++) {
try {
if (futureResults.get(f).isDone()) {
ret.add(futureResults.get(f).get());
}
} catch (InterruptedException ex) {
} catch (ExecutionException ex) {
logger.log(Level.SEVERE, "Error during ABC export", ex);
}
}
}
return ret;
}
public EventListener getExportEventListener() {
EventListener evl = new EventListener() {
@Override
@@ -1363,9 +1225,9 @@ public final class SWF implements SWFContainerItem, Timelined {
List<File> ret = new ArrayList<>();
if (isAS3()) {
ret.addAll(exportActionScript3(handler, outdir, exportSettings, parallel, evl));
ret.addAll(new AS3ScriptExporter().exportActionScript3(this, handler, outdir, exportSettings, parallel, evl));
} else {
ret.addAll(exportActionScript2(handler, outdir, exportSettings, parallel, evl));
ret.addAll(new AS2ScriptExporter().exportAS2ScriptsTimeout(handler, outdir, getASMs(true), exportSettings, evl));
}
return ret;
}

View File

@@ -43,8 +43,6 @@ import java.util.List;
*/
public class TextExporter {
public static final String TEXT_EXPORT_FOLDER = "texts";
public static final String TEXT_EXPORT_FILENAME_FORMATTED = "textsformatted.txt";
public static final String TEXT_EXPORT_FILENAME_PLAIN = "textsplain.txt";

View File

@@ -18,6 +18,7 @@ package com.jpexs.decompiler.flash.exporters.script;
import com.jpexs.decompiler.flash.AbortRetryIgnoreHandler;
import com.jpexs.decompiler.flash.EventListener;
import com.jpexs.decompiler.flash.SWF;
import com.jpexs.decompiler.flash.action.Action;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode;
@@ -51,6 +52,10 @@ public class AS2ScriptExporter {
private static final Logger logger = Logger.getLogger(AS2ScriptExporter.class.getName());
public List<File> exportActionScript2(SWF swf, AbortRetryIgnoreHandler handler, String outdir, ScriptExportSettings exportSettings, boolean parallel, EventListener evl) throws IOException {
return exportAS2ScriptsTimeout(handler, outdir, swf.getASMs(true), exportSettings, evl);
}
public List<File> exportAS2ScriptsTimeout(final AbortRetryIgnoreHandler handler, final String outdir, final Map<String, ASMSource> asms, final ScriptExportSettings exportSettings, final EventListener evl) throws IOException {
try {
List<File> result = CancellableWorker.call(new Callable<List<File>>() {

View File

@@ -0,0 +1,104 @@
/*
* 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.exporters.script;
import com.jpexs.decompiler.flash.AbortRetryIgnoreHandler;
import com.jpexs.decompiler.flash.EventListener;
import com.jpexs.decompiler.flash.SWF;
import com.jpexs.decompiler.flash.abc.ScriptPack;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.exporters.settings.ScriptExportSettings;
import com.jpexs.helpers.CancellableWorker;
import com.jpexs.helpers.Helper;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author JPEXS
*/
public class AS3ScriptExporter {
private static final Logger logger = Logger.getLogger(AS3ScriptExporter.class.getName());
public List<File> exportActionScript3(SWF swf, final AbortRetryIgnoreHandler handler, final String outdir, final ScriptExportSettings exportSettings, final boolean parallel, final EventListener evl) {
final AtomicInteger cnt = new AtomicInteger(1);
final List<File> ret = new ArrayList<>();
final List<ScriptPack> packs = swf.getAS3Packs();
if (!parallel || packs.size() < 2) {
try {
CancellableWorker.call(new Callable<Void>() {
@Override
public Void call() throws Exception {
for (ScriptPack item : packs) {
ExportPackTask task = new ExportPackTask(handler, cnt, packs.size(), item.getClassPath(), item, outdir, exportSettings, parallel, evl);
ret.add(task.call());
}
return null;
}
}, Configuration.exportTimeout.get(), TimeUnit.SECONDS);
} catch (TimeoutException ex) {
logger.log(Level.SEVERE, Helper.formatTimeToText(Configuration.exportTimeout.get()) + " ActionScript export limit reached", ex);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Error during ABC export", ex);
}
} else {
ExecutorService executor = Executors.newFixedThreadPool(Configuration.getParallelThreadCount());
List<Future<File>> futureResults = new ArrayList<>();
for (ScriptPack item : packs) {
Future<File> future = executor.submit(new ExportPackTask(handler, cnt, packs.size(), item.getClassPath(), item, outdir, exportSettings, parallel, evl));
futureResults.add(future);
}
try {
executor.shutdown();
if (!executor.awaitTermination(Configuration.exportTimeout.get(), TimeUnit.SECONDS)) {
logger.log(Level.SEVERE, Helper.formatTimeToText(Configuration.exportTimeout.get()) + " ActionScript export limit reached");
}
} catch (InterruptedException ex) {
} finally {
executor.shutdownNow();
}
for (int f = 0; f < futureResults.size(); f++) {
try {
if (futureResults.get(f).isDone()) {
ret.add(futureResults.get(f).get());
}
} catch (InterruptedException ex) {
} catch (ExecutionException ex) {
logger.log(Level.SEVERE, "Error during ABC export", ex);
}
}
}
return ret;
}
}

View File

@@ -0,0 +1,98 @@
/*
* 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.exporters.script;
import com.jpexs.decompiler.flash.AbortRetryIgnoreHandler;
import com.jpexs.decompiler.flash.EventListener;
import com.jpexs.decompiler.flash.RetryTask;
import com.jpexs.decompiler.flash.RunnableIOExResult;
import com.jpexs.decompiler.flash.abc.ABC;
import com.jpexs.decompiler.flash.abc.ClassPath;
import com.jpexs.decompiler.flash.abc.ScriptPack;
import com.jpexs.decompiler.flash.exporters.settings.ScriptExportSettings;
import com.jpexs.helpers.Helper;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
* @author JPEXS
*/
public class ExportPackTask implements Callable<File> {
ScriptPack pack;
String directory;
ScriptExportSettings exportSettings;
ClassPath path;
AtomicInteger index;
int count;
boolean parallel;
AbortRetryIgnoreHandler handler;
long startTime;
long stopTime;
EventListener eventListener;
public ExportPackTask(AbortRetryIgnoreHandler handler, AtomicInteger index, int count, ClassPath path, ScriptPack pack, String directory, ScriptExportSettings exportSettings, boolean parallel, EventListener evl) {
this.pack = pack;
this.directory = directory;
this.exportSettings = exportSettings;
this.path = path;
this.index = index;
this.count = count;
this.parallel = parallel;
this.handler = handler;
this.eventListener = evl;
}
@Override
public File call() throws Exception {
RunnableIOExResult<File> rio = new RunnableIOExResult<File>() {
@Override
public void run() throws IOException {
startTime = System.currentTimeMillis();
this.result = pack.export(directory, exportSettings, parallel);
stopTime = System.currentTimeMillis();
}
};
int currentIndex = index.getAndIncrement();
if (eventListener != null) {
synchronized (ABC.class) {
eventListener.handleExportingEvent("script", currentIndex, count, path);
}
}
new RetryTask(rio, handler).run();
if (eventListener != null) {
synchronized (ABC.class) {
long time = stopTime - startTime;
eventListener.handleExportedEvent("script", currentIndex, count, path + ", " + Helper.formatTimeSec(time));
}
}
return rio.result;
}
}

View File

@@ -25,6 +25,8 @@ import com.jpexs.decompiler.flash.helpers.FileTextWriter;
*/
public class ScriptExportSettings {
public static final String EXPORT_FOLDER_NAME = "scripts";
public ScriptExportMode mode;
public boolean singleFile;

View File

@@ -24,6 +24,8 @@ import com.jpexs.decompiler.flash.exporters.modes.TextExportMode;
*/
public class TextExportSettings {
public static final String EXPORT_FOLDER_NAME = "texts";
public TextExportMode mode;
public boolean singleFile;

View File

@@ -0,0 +1,61 @@
/*
* 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.importers;
import com.jpexs.decompiler.flash.action.parser.ActionParseException;
import com.jpexs.decompiler.flash.tags.base.ASMSource;
import com.jpexs.decompiler.graph.CompilationException;
import com.jpexs.helpers.Helper;
import com.jpexs.helpers.Path;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author JPEXS
*/
public class ScriptImporter {
private static final Logger logger = Logger.getLogger(ScriptImporter.class.getName());
public void importScripts(String scriptsFolder, Map<String, ASMSource> asms) {
for (String key : asms.keySet()) {
String fileName = Path.combine(scriptsFolder, key) + ".as";
if (new File(fileName).exists()) {
ASMSource src = asms.get(key);
String as = Helper.readTextFile(fileName);
com.jpexs.decompiler.flash.action.parser.script.ActionScriptParser par = new com.jpexs.decompiler.flash.action.parser.script.ActionScriptParser(src.getSwf().version);
try {
src.setActions(par.actionsFromString(as));
} catch (ActionParseException ex) {
logger.log(Level.SEVERE, "%error% on line %line%, file: %file%".replace("%error%", ex.text).replace("%line%", Long.toString(ex.line)).replace("%file%", fileName), ex);
} catch (CompilationException ex) {
logger.log(Level.SEVERE, "%error% on line %line%, file: %file%".replace("%error%", ex.text).replace("%line%", Long.toString(ex.line)).replace("%file%", fileName), ex);
} catch (IOException ex) {
logger.log(Level.SEVERE, "error during script import, file: %file%".replace("%file%", fileName), ex);
}
src.setModified();
}
}
}
}

View File

@@ -18,7 +18,7 @@ package com.jpexs.decompiler.flash.importers;
import com.jpexs.decompiler.flash.SWF;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.exporters.TextExporter;
import com.jpexs.decompiler.flash.exporters.settings.TextExportSettings;
import com.jpexs.decompiler.flash.tags.Tag;
import com.jpexs.decompiler.flash.tags.base.MissingCharacterHandler;
import com.jpexs.decompiler.flash.tags.base.TextImportErrorHandler;
@@ -111,7 +111,7 @@ public class TextImporter {
}
public void importTextsMultipleFiles(String folder, SWF swf) {
File textsFolder = new File(Path.combine(folder, TextExporter.TEXT_EXPORT_FOLDER));
File textsFolder = new File(Path.combine(folder, TextExportSettings.EXPORT_FOLDER_NAME));
String[] files = textsFolder.list(new FilenameFilter() {
private final Pattern pat = Pattern.compile("\\d+\\.txt", Pattern.CASE_INSENSITIVE);