Debugger function moved to separated class, other small fixes

This commit is contained in:
honfika@gmail.com
2015-03-09 23:18:02 +01:00
parent d1c6c51c88
commit c98facb399
8 changed files with 224 additions and 197 deletions

View File

@@ -23,21 +23,12 @@ import com.jpexs.decompiler.flash.SWFBundle;
import com.jpexs.decompiler.flash.SWFSourceInfo;
import com.jpexs.decompiler.flash.SearchMode;
import com.jpexs.decompiler.flash.Version;
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.abc.avm2.AVM2Code;
import com.jpexs.decompiler.flash.abc.types.Multiname;
import com.jpexs.decompiler.flash.abc.types.Namespace;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.console.CommandLineArgumentParser;
import com.jpexs.decompiler.flash.console.ContextMenuTools;
import com.jpexs.decompiler.flash.gui.debugger.Debugger;
import com.jpexs.decompiler.flash.gui.proxy.ProxyFrame;
import com.jpexs.decompiler.flash.helpers.SWFDecompilerPlugin;
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.base.FontTag;
import com.jpexs.decompiler.flash.treeitems.SWFList;
import com.jpexs.helpers.Cache;
@@ -84,7 +75,6 @@ import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Random;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
@@ -114,8 +104,6 @@ public class Main {
public static LoadingDialog loadingDialog;
public static ModeFrame modeFrame;
private static boolean working = false;
private static TrayIcon trayIcon;
@@ -134,44 +122,10 @@ public class Main {
private static final Logger logger = Logger.getLogger(Main.class.getName());
private static Debugger debugger;
public static DebugLogDialog debugDialog;
public static boolean shouldCloseWhenClosingLoadingDialog;
public static final String DEBUGGER_PACKAGE = "com.jpexs.decompiler.flash.debugger";
private static ABCContainerTag getDebuggerABCTag(SWF swf) {
for (ABCContainerTag ac : swf.getAbcList()) {
ABC a = ac.getABC();
for (MyEntry<ClassPath, ScriptPack> m : a.getScriptPacks()) {
if (isDebuggerClass(m.getKey().packageStr, null)) {
return ac;
}
}
}
return null;
}
private static String getDebuggerPackage(SWF swf) {
ABCContainerTag ac = getDebuggerABCTag(swf);
if (ac == null) {
return null;
}
ABC a = ac.getABC();
for (MyEntry<ClassPath, ScriptPack> m : a.getScriptPacks()) {
if (isDebuggerClass(m.getKey().packageStr, null)) {
return m.getKey().packageStr;
}
}
return null;
}
public static boolean hasDebugger(SWF swf) {
return getDebuggerABCTag(swf) != null;
}
public static void ensureMainFrame() {
if (mainFrame == null) {
synchronized (Main.class) {
@@ -1184,15 +1138,6 @@ public class Main {
public static void exit() {
Configuration.saveConfig();
/*if (proxyFrame != null && Main.proxyFrame.isVisible()) {
return;
}
if (loadFromMemoryFrame != null && Main.loadFromMemoryFrame.isVisible()) {
return;
}
if (loadFromCacheFrame != null && loadFromCacheFrame.isVisible()) {
return;
}*/
if (mainFrame != null && mainFrame.getPanel() != null) {
mainFrame.getPanel().unloadFlashPlayer();
}
@@ -1207,135 +1152,6 @@ public class Main {
(new AdvancedSettingsDialog()).setVisible(true);
}
private static boolean isDebuggerClass(String tested, String cls) {
if (tested == null) {
return false;
}
if (cls == null) {
cls = "";
} else {
cls = "\\." + Pattern.quote(cls);
}
return tested.matches(Pattern.quote(DEBUGGER_PACKAGE) + "(\\.pkg[a-f0-9]+)?" + cls);
}
private static String byteArrayToHex(byte[] a) {
StringBuilder sb = new StringBuilder(a.length * 2);
for (byte b : a) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
public static void replaceTraceCalls(String fname) {
SWF swf = getMainFrame().getPanel().getCurrentSwf();
if (hasDebugger(swf)) {
String debuggerPkg = getDebuggerPackage(swf);
//change trace to fname
for (ABCContainerTag ct : swf.getAbcList()) {
ABC a = ct.getABC();
for (int i = 1; i < a.constants.constant_multiname.size(); i++) {
Multiname m = a.constants.constant_multiname.get(i);
if ("trace".equals(m.getNameWithNamespace(a.constants, true))) {
m.namespace_index = a.constants.getNamespaceId(new Namespace(Namespace.KIND_PACKAGE, a.constants.getStringId(debuggerPkg, true)), 0, true);
m.name_index = a.constants.getStringId(fname, true);
((Tag) ct).setModified(true);
}
}
}
}
}
public static void switchDebugger() {
int port = Configuration.debuggerPort.get();
SWF swf = getMainFrame().getPanel().getCurrentSwf();
ABCContainerTag found = getDebuggerABCTag(swf);
if (found != null) {
swf.tags.remove((Tag) found);
swf.getAbcList().remove(found);
//Change all debugger calls to normal trace
for (ABCContainerTag ct : swf.getAbcList()) {
ABC a = ct.getABC();
for (int i = 1; i < a.constants.constant_multiname.size(); i++) {
Multiname m = a.constants.constant_multiname.get(i);
if (isDebuggerClass(m.getNameWithNamespace(a.constants, true), "debugTrace")
|| isDebuggerClass(m.getNameWithNamespace(a.constants, true), "debugAlert")
|| isDebuggerClass(m.getNameWithNamespace(a.constants, true), "debugSocket")
|| isDebuggerClass(m.getNameWithNamespace(a.constants, true), "debugConsole")) {
m.name_index = a.constants.getStringId("trace", true);
m.namespace_index = a.constants.getNamespaceId(new Namespace(Namespace.KIND_PACKAGE, a.constants.getStringId("", true)), 0, true);
((Tag) ct).setModified(true);
}
}
}
} else {
Random rnd = new Random();
byte rb[] = new byte[16];
rnd.nextBytes(rb);
String rhex = byteArrayToHex(rb);
try {
//load debug swf
SWF debugSWF = new SWF(Main.class.getClassLoader().getResourceAsStream("com/jpexs/decompiler/flash/gui/debugger/debug.swf"), false);
ABCContainerTag firstAbc = swf.getAbcList().get(0);
String newdebuggerpkg = DEBUGGER_PACKAGE;
if (Configuration.randomDebuggerPackage.get()) {
newdebuggerpkg += ".pkg" + rhex;
}
//add debug ABC tags to main SWF
for (ABCContainerTag ds : debugSWF.getAbcList()) {
ABC a = ds.getABC();
//Append random hex to Debugger package name
for (int i = 1; i < a.constants.constant_namespace.size(); i++) {
if (a.constants.constant_namespace.get(i).hasName(DEBUGGER_PACKAGE, a.constants)) {
a.constants.constant_namespace.get(i).name_index = a.constants.getStringId(newdebuggerpkg, true);
}
}
//Set debugger port to actually set port
for (int i = 0; i < a.constants.constant_int.size(); i++) {
if (a.constants.constant_int.get(i) == 123456L) {
a.constants.constant_int.set(i, (long) port);
}
}
//Add to target SWF
((Tag) ds).setSwf(swf);
swf.tags.add(swf.tags.indexOf(firstAbc), (Tag) ds);
swf.getAbcList().add(swf.getAbcList().indexOf(firstAbc), ds);
((Tag) ds).setModified(true);
}
} catch (Exception ex) {
logger.log(Level.SEVERE, "Error while attaching debugger", ex);
//ignore
}
}
initDebugger();
}
private static void initDebugger() {
if (debugger == null) {
synchronized (Main.class) {
if (debugger == null) {
Debugger dbg = new Debugger(Configuration.debuggerPort.get());
dbg.start();
debugger = dbg;
}
}
}
}
public static void debuggerShowLog() {
initDebugger();
if (debugDialog == null) {
debugDialog = new DebugLogDialog(debugger);
}
debugDialog.setVisible(true);
}
public static void autoCheckForUpdates() {
if (Configuration.checkForUpdatesAuto.get()) {
Calendar lastUpdatesCheckDate = Configuration.lastUpdatesCheckDate.get();