#1240 JPEXS does not use multiple threads for decompilation on Text Search: AS2 is also multi thread now, work status improvements during search

This commit is contained in:
honfika@gmail.com
2016-12-24 21:06:23 +01:00
parent 5f2ffd0089
commit c3fc9785e3
9 changed files with 2419 additions and 2328 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -26,6 +26,7 @@ import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode;
import com.jpexs.decompiler.flash.helpers.HighlightedText;
import com.jpexs.decompiler.flash.helpers.HighlightedTextWriter;
import com.jpexs.decompiler.flash.tags.base.ASMSource;
import com.jpexs.helpers.ImmediateFuture;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@@ -51,10 +52,13 @@ public class DecompilerPool {
}
public Future<HighlightedText> submitTask(ASMSource src, ActionList actions, ScriptDecompiledListener<HighlightedText> listener) {
Future<HighlightedText> f = executor.submit(new Callable<HighlightedText>() {
Callable<HighlightedText> callable = new Callable<HighlightedText>() {
@Override
public HighlightedText call() throws Exception {
Thread.sleep(10000);
if (listener != null) {
listener.onStart();
}
HighlightedTextWriter writer = new HighlightedTextWriter(Configuration.getCodeFormatting(), true);
writer.startFunction("!script");
src.getActionScriptSource(writer, actions);
@@ -72,22 +76,25 @@ public class DecompilerPool {
return result;
}
});
};
return f;
return submit(callable);
}
public Future<HighlightedText> submitTask(ScriptPack pack, ScriptDecompiledListener<HighlightedText> listener) {
Future<HighlightedText> f = executor.submit(new Callable<HighlightedText>() {
Callable<HighlightedText> callable = new Callable<HighlightedText>() {
@Override
public HighlightedText call() throws Exception {
if (listener != null) {
listener.onStart();
}
int scriptIndex = pack.scriptIndex;
ScriptInfo script = null;
if (scriptIndex > -1) {
script = pack.abc.script_info.get(scriptIndex);
}
boolean parallel = Configuration.parallelSpeedUp.get();
Thread.sleep(10000);
HighlightedTextWriter writer = new HighlightedTextWriter(Configuration.getCodeFormatting(), true);
pack.toSource(writer, script == null ? null : script.traits.traits, new ConvertData(), ScriptExportMode.AS, parallel);
@@ -103,9 +110,30 @@ public class DecompilerPool {
return result;
}
});
};
return f;
return submit(callable);
}
private Future<HighlightedText> submit(Callable<HighlightedText> callable) {
boolean parallel = Configuration.parallelSpeedUp.get();
if (parallel) {
Future<HighlightedText> f = executor.submit(callable);
return f;
} else {
boolean cancelled = false;
Throwable throwable = null;
HighlightedText result = null;
try {
result = callable.call();
} catch (InterruptedException ex) {
cancelled = true;
} catch (Exception ex) {
throwable = ex;
}
return new ImmediateFuture<>(result, throwable, cancelled);
}
}
public String getStat() {

View File

@@ -2958,8 +2958,7 @@ public final class SWF implements SWFContainerItem, Timelined {
timelined.setModified(true);
timelined.resetTimeline();
} else // timeline should be always the swf here
{
if (removeDependencies) {
if (removeDependencies) {
removeTagWithDependenciesFromTimeline(tag, timelined.getTimeline());
timelined.setModified(true);
} else {
@@ -2968,7 +2967,6 @@ public final class SWF implements SWFContainerItem, Timelined {
timelined.setModified(true);
}
}
}
}
@Override

View File

@@ -20,8 +20,9 @@ package com.jpexs.decompiler.flash.cache;
*
* @author JPEXS
*/
@FunctionalInterface
public interface ScriptDecompiledListener<T> {
public void onStart();
public void onComplete(T result);
}

View File

@@ -1,20 +1,22 @@
/*
* Copyright (C) 2010-2016 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.helpers;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@@ -29,8 +31,20 @@ public class ImmediateFuture<V> implements Future<V> {
private final V value;
private final Throwable throwable;
private final boolean cancelled;
public ImmediateFuture(V value) {
this.value = value;
throwable = null;
cancelled = false;
}
public ImmediateFuture(V value, Throwable throwable, boolean cancelled) {
this.value = value;
this.throwable = throwable;
this.cancelled = cancelled;
}
@Override
@@ -40,7 +54,7 @@ public class ImmediateFuture<V> implements Future<V> {
@Override
public boolean isCancelled() {
public boolean isCancelled() {
return cancelled;
}
@Override
@@ -50,11 +64,19 @@ public class ImmediateFuture<V> implements Future<V> {
@Override
public V get() throws InterruptedException, ExecutionException {
if (cancelled) {
throw new CancellationException();
}
if (throwable != null) {
throw new ExecutionException(throwable);
}
return value;
}
@Override
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return get();
}
}

View File

@@ -360,6 +360,7 @@ public class AdvancedSettingsDialog extends AppDialog {
String locName = locNames.get(name);
try {
field.setAccessible(true);
ConfigurationItem item = (ConfigurationItem) field.get(null);
ParameterizedType listType = (ParameterizedType) field.getGenericType();
@@ -580,6 +581,7 @@ public class AdvancedSettingsDialog extends AppDialog {
Field field = fields.get(name);
ConfigurationItem item = null;
try {
field.setAccessible(true);
item = (ConfigurationItem) field.get(null);
} catch (IllegalArgumentException | IllegalAccessException ex) {
// Reflection exceptions. This should never happen

View File

@@ -851,24 +851,26 @@ public final class MainPanel extends JPanel implements TreeSelectionListener, Se
taskThread.interrupt();
}
Thread t = new Thread() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
DecompilerPool d = swf.getDecompilerPool();
statusPanel.setStatus(swf.getFileTitle() + " " + d.getStat());
if (Configuration._debugMode.get()) {
Thread t = new Thread() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
DecompilerPool d = swf.getDecompilerPool();
statusPanel.setStatus(swf.getFileTitle() + " " + d.getStat());
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
break;
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
break;
}
}
}
}
};
};
t.start();
taskThread = t;
t.start();
taskThread = t;
}
}
private void updateUi() {

View File

@@ -44,6 +44,7 @@ import com.jpexs.decompiler.flash.action.parser.ActionParseException;
import com.jpexs.decompiler.flash.action.parser.script.ActionScriptLexer;
import com.jpexs.decompiler.flash.action.parser.script.ParsedSymbol;
import com.jpexs.decompiler.flash.action.parser.script.SymbolType;
import com.jpexs.decompiler.flash.cache.ScriptDecompiledListener;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.ecma.EcmaScript;
import com.jpexs.decompiler.flash.gui.AppDialog;
@@ -101,6 +102,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -208,8 +210,11 @@ public class ABCPanel extends JPanel implements ItemListener, SearchListener<ABC
final Pattern pat = regexp
? Pattern.compile(txt, ignoreCase ? (Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE) : 0)
: Pattern.compile(Pattern.quote(txt), ignoreCase ? (Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE) : 0);
int pos = 0;
List<Future<HighlightedText>> futures = new ArrayList<>();
String workText = AppStrings.translate("work.searching");
String decAdd = ", " + AppStrings.translate("work.decompiling");
try {
loop:
for (final ScriptPack pack : allpacks) {
@@ -229,22 +234,33 @@ public class ABCPanel extends JPanel implements ItemListener, SearchListener<ABC
}
}
String workText = AppStrings.translate("work.searching");
String decAdd = "";
if (!SWF.isCached(pack)) {
decAdd = ", " + AppStrings.translate("work.decompiling");
}
int fpos = pos;
Future<HighlightedText> text = SWF.getCachedFuture(pack, new ScriptDecompiledListener<HighlightedText>() {
@Override
public void onStart() {
Main.startWork(workText + " \"" + txt + "\"" + decAdd + " - (" + fpos + "/" + allpacks.size() + ") " + pack.getClassPath().toString() + "... ", worker);
}
Main.startWork(workText + " \"" + txt + "\"" + decAdd + " - (" + pos + "/" + allpacks.size() + ") " + pack.getClassPath().toString() + "... ", worker);
Future<HighlightedText> text = SWF.getCachedFuture(pack, (HighlightedText result) -> {
if (pat.matcher(result.text).find()) {
ABCPanelSearchResult searchResult = new ABCPanelSearchResult(pack);
found.add(searchResult);
@Override
public void onComplete(HighlightedText result) {
Main.startWork(workText + " \"" + txt + "\" - (" + fpos + "/" + allpacks.size() + ") " + pack.getClassPath().toString() + "... ", worker);
if (pat.matcher(result.text).find()) {
ABCPanelSearchResult searchResult = new ABCPanelSearchResult(pack);
found.add(searchResult);
}
}
});
futures.add(text);
}
for (Future<HighlightedText> future : futures) {
try {
future.get();
} catch (ExecutionException ex) {
Logger.getLogger(ABCPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (InterruptedException ex) {
for (Future<HighlightedText> future : futures) {
future.cancel(true);

View File

@@ -30,6 +30,7 @@ import com.jpexs.decompiler.flash.action.parser.script.ParsedSymbol;
import com.jpexs.decompiler.flash.action.parser.script.SymbolType;
import com.jpexs.decompiler.flash.action.swf4.ActionPush;
import com.jpexs.decompiler.flash.action.swf4.ConstantIndex;
import com.jpexs.decompiler.flash.cache.ScriptDecompiledListener;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode;
import com.jpexs.decompiler.flash.gui.AppStrings;
@@ -69,6 +70,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
@@ -256,30 +258,48 @@ public class ActionPanel extends JPanel implements SearchListener<ActionSearchRe
} else {
pat = Pattern.compile(Pattern.quote(txt), ignoreCase ? (Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE) : 0);
}
int pos = 0;
for (Entry<String, ASMSource> item : asms.entrySet()) {
pos++;
String workText = AppStrings.translate("work.searching");
String decAdd = "";
ASMSource asm = item.getValue();
if (!pcode && !SWF.isCached(asm)) {
decAdd = ", " + AppStrings.translate("work.decompiling");
}
Main.startWork(workText + " \"" + txt + "\"" + decAdd + " - (" + pos + "/" + asms.size() + ") " + item.getKey() + "... ", worker);
try {
int pos = 0;
List<Future<HighlightedText>> futures = new ArrayList<>();
String workText = AppStrings.translate("work.searching");
String decAdd = ", " + AppStrings.translate("work.decompiling");
try {
for (Entry<String, ASMSource> item : asms.entrySet()) {
pos++;
ASMSource asm = item.getValue();
if (pcode) {
Main.startWork(workText + " \"" + txt + "\" - (" + pos + "/" + asms.size() + ") " + item.getKey() + "... ", worker);
HighlightedTextWriter writer = new HighlightedTextWriter(Configuration.getCodeFormatting(), true);
asm.getASMSource(ScriptExportMode.PCODE, writer, null);
String text = writer.toString();
if (pat.matcher(text).find()) {
found.add(new ActionSearchResult(asm, pcode, item.getKey()));
}
} else if (pat.matcher(SWF.getCached(asm, null).text).find()) {
found.add(new ActionSearchResult(asm, pcode, item.getKey()));
} else {
int fpos = pos;
Future<HighlightedText> text = SWF.getCachedFuture(asm, null, new ScriptDecompiledListener<HighlightedText>() {
@Override
public void onStart() {
Main.startWork(workText + " \"" + txt + "\"" + decAdd + " - (" + fpos + "/" + asms.size() + ") " + item.getKey() + "... ", worker);
}
@Override
public void onComplete(HighlightedText result) {
Main.startWork(workText + " \"" + txt + "\"" + decAdd + " - (" + fpos + "/" + asms.size() + ") " + item.getKey() + "... ", worker);
if (pat.matcher(result.text).find()) {
ActionSearchResult searchResult = new ActionSearchResult(asm, pcode, item.getKey());
found.add(searchResult);
}
}
});
futures.add(text);
}
} catch (InterruptedException ex) {
break;
}
} catch (InterruptedException ex) {
for (Future<HighlightedText> future : futures) {
future.cancel(true);
}
}