AS3 - method body parameters editable

This commit is contained in:
Jindra Petk
2011-07-10 19:38:27 +02:00
parent 27df4647a0
commit fdbd5b2ef8
5 changed files with 178 additions and 34 deletions

View File

@@ -61,20 +61,19 @@ public class ASMSourceEditorPane extends LineMarkedEditorPane {
JOptionPane.showMessageDialog(this, "Returned object:"+o.toString());
}
public void save(ConstantPool constants) {
public boolean save(ConstantPool constants) {
try {
AVM2Code acode = ASM3Parser.parse(new ByteArrayInputStream(getText().getBytes()), constants, new DialogMissingSymbolHandler(),abc.bodies[bodyIndex]);
acode.getBytes(abc.bodies[bodyIndex].codeBytes);
abc.bodies[bodyIndex].code = acode;
Main.abcMainFrame.decompiledTextArea.reloadClass();
Main.abcMainFrame.decompiledTextArea.gotoLastTrait();
abc.bodies[bodyIndex].code = acode;
} catch (IOException ex) {
} catch (ParseException ex) {
JOptionPane.showMessageDialog(this, (ex.text + " on line " + ex.line));
selectLine((int) ex.line);
return;
return false;
}
JOptionPane.showMessageDialog(this, ("Code Saved"));
return true;
}
public void verify(ConstantPool constants, ABC abc) {

View File

@@ -38,6 +38,17 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements MouseL
private int classIndex;
public int lastTraitIndex = 0;
public void setNoTrait()
{
Main.abcMainFrame.sourceTextArea.setText("");
Main.abcMainFrame.methodBodyPanel.loadFromBody(null);
Main.abcMainFrame.methodBodyLabel.setText("Uneditable trait");
Main.abcMainFrame.methodBodyPanel.setVisible(false);
Main.abcMainFrame.sourceTextArea.setVisible(false);
Main.abcMainFrame.buttonsPanel.setVisible(false);
Main.abcMainFrame.sourceTextArea.bodyIndex=-1;
}
public void caretUpdate(CaretEvent e) {
getCaret().setVisible(true);
int pos = getCaretPosition();
@@ -45,10 +56,13 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements MouseL
if ((pos >= th.startPos) && (pos < th.startPos + th.len)) {
int bi = abc.findBodyIndex(abc.findMethodIdByTraitId(classIndex, (int) th.offset));
if (bi == -1) {
Main.abcMainFrame.sourceTextArea.setText("");
if ((bi == -1)||(bi==0)) {
setNoTrait();
break;
}
Main.abcMainFrame.methodBodyPanel.setVisible(true);
Main.abcMainFrame.sourceTextArea.setVisible(true);
Main.abcMainFrame.buttonsPanel.setVisible(true);
lastTraitIndex = (int) th.offset;
if (Main.abcMainFrame.sourceTextArea.bodyIndex != bi) {
/*try {
@@ -57,6 +71,8 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements MouseL
Logger.getLogger(DecompiledEditorPane.class.getName()).log(Level.SEVERE, null, ex);
}*/
Main.abcMainFrame.sourceTextArea.setBodyIndex(bi, abc);
Main.abcMainFrame.methodBodyPanel.loadFromBody(abc.bodies[bi]);
Main.abcMainFrame.methodBodyLabel.setText("Method body #"+bi);
}
for (Highlighting h : highlights) {
if ((pos >= h.startPos) && (pos < h.startPos + h.len)) {
@@ -68,8 +84,10 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements MouseL
break;
}
}
}
return;
}
}
setNoTrait();
}
private class BufferedClass {
@@ -91,22 +109,19 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements MouseL
}
public void gotoTrait(int traitId) {
if(traitId==-1)
{
setCaretPosition(0);
return;
}
for (Highlighting th : traitHighlights) {
if (th.offset == traitId) {
setCaretPosition(th.startPos + th.len - 1);
setCaretPosition(th.startPos);
break;
return;
}
}
int mi = abc.findMethodIdByTraitId(classIndex, traitId);
int bi = abc.findBodyIndex(mi);
if (bi == -1) {
Main.abcMainFrame.sourceTextArea.setText("");
return;
}
if (Main.abcMainFrame.sourceTextArea.bodyIndex != bi) {
Main.abcMainFrame.sourceTextArea.setBodyIndex(bi, abc);
}
setCaretPosition(0);
}
public DecompiledEditorPane() {
@@ -142,6 +157,7 @@ public class DecompiledEditorPane extends LineMarkedEditorPane implements MouseL
bufferedClasses.remove(classIndex);
}
setClassIndex(classIndex, abc);
setNoTrait();
}
public void setABC(ABC abc) {

View File

@@ -61,6 +61,10 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener {
public JLabel statusLabel = new JLabel("");
public JLabel asmLabel = new JLabel("P-code source (editable)");
public JLabel decLabel = new JLabel("ActionScript source");
public MethodBodyPanel methodBodyPanel;
public JLabel methodBodyLabel;
public JPanel detailPanel;
public JPanel buttonsPanel;
public void setStatus(String s) {
if (s.equals("")) {
@@ -166,7 +170,7 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener {
DefaultSyntaxKit.initKit();
this.list = list;
setSize(800, 600);
setSize(1024, 600);
this.abc = list.get(listIndex).abc;
getContentPane().setLayout(new BorderLayout());
sourceTextArea = new ASMSourceEditorPane();
@@ -175,8 +179,8 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener {
rightPanel.setLayout(new BorderLayout());
rightPanel.add(new JScrollPane(sourceTextArea), BorderLayout.CENTER);
sourceTextArea.setContentType("text/flasm3");
JPanel buttonsPan = new JPanel();
buttonsPan.setLayout(new FlowLayout());
buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout());
JButton verifyButton = new JButton("Verify");
verifyButton.setActionCommand("VERIFYBODY");
verifyButton.addActionListener(this);
@@ -194,20 +198,35 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener {
execButton.addActionListener(this);
//buttonsPan.add(graphButton);
buttonsPan.add(saveButton);
buttonsPanel.add(saveButton);
// buttonsPan.add(execButton);
rightPanel.add(buttonsPan, BorderLayout.SOUTH);
rightPanel.add(buttonsPanel, BorderLayout.SOUTH);
decompiledTextArea = new DecompiledEditorPane();
decompiledScrollPane = new JScrollPane(decompiledTextArea);
JPanel panA = new JPanel();
panA.setLayout(new BorderLayout());
panA.add(rightPanel, BorderLayout.CENTER);
panA.add(asmLabel, BorderLayout.NORTH);
detailPanel=new JPanel();
detailPanel.setLayout(new BorderLayout());
methodBodyPanel=new MethodBodyPanel();
JPanel methodBodyPanel1=new JPanel();
methodBodyPanel1.setLayout(new BorderLayout());
methodBodyPanel1.add(methodBodyPanel,BorderLayout.CENTER);
asmLabel.setHorizontalAlignment(SwingConstants.CENTER);
asmLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
methodBodyLabel=new JLabel("Method body");
methodBodyLabel.setHorizontalAlignment(SwingConstants.CENTER);
methodBodyLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
methodBodyPanel1.add(methodBodyLabel,BorderLayout.NORTH);
detailPanel.add(methodBodyPanel1,BorderLayout.NORTH);
JPanel pcodePanel = new JPanel();
pcodePanel.setLayout(new BorderLayout());
pcodePanel.add(rightPanel, BorderLayout.CENTER);
//pcodePanel.add(asmLabel, BorderLayout.NORTH);
detailPanel.add(pcodePanel,BorderLayout.CENTER);
//asmLabel.setHorizontalAlignment(SwingConstants.CENTER);
//asmLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
JPanel panB = new JPanel();
panB.setLayout(new BorderLayout());
panB.add(decompiledScrollPane, BorderLayout.CENTER);
@@ -215,9 +234,9 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener {
decLabel.setHorizontalAlignment(SwingConstants.CENTER);
decLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
panB, panA);
panB, detailPanel);
decompiledTextArea.setContentType("text/actionscript");
JPanel pan2 = new JPanel();
pan2.setLayout(new BorderLayout());
pan2.add((abcComboBox = new JComboBox(new ABCComboBoxModel(list))), BorderLayout.NORTH);
@@ -364,7 +383,13 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener {
sourceTextArea.verify(abc.constants, abc);
}
if (e.getActionCommand().equals("SAVEBODY")) {
sourceTextArea.save(abc.constants);
if(sourceTextArea.save(abc.constants))
{
methodBodyPanel.save();
int lasttrait=decompiledTextArea.lastTraitIndex;
Main.abcMainFrame.decompiledTextArea.reloadClass();
Main.abcMainFrame.decompiledTextArea.gotoTrait(lasttrait);
}
}
if (e.getActionCommand().equals("SAVE")) {
try {

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2011 Jindra
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.jpexs.asdec.abc.gui;
import com.jpexs.asdec.abc.types.MethodBody;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
/**
*
* @author Jindra
*/
public class MethodBodyPanel extends JPanel {
public JLabel maxStackLabel = new JLabel("Max stack:");
public JFormattedTextField maxStackField = new JFormattedTextField(NumberFormat.getNumberInstance());
public JLabel localCountLabel = new JLabel("Local registers count:");
public JFormattedTextField localCountField = new JFormattedTextField(NumberFormat.getNumberInstance());
public JLabel initScopeDepthLabel = new JLabel("Minimum scope depth:");
public JFormattedTextField initScopeDepthField = new JFormattedTextField(NumberFormat.getNumberInstance());
public JLabel maxScopeDepthLabel = new JLabel("Maximum scope depth:");
public JFormattedTextField maxScopeDepthField = new JFormattedTextField(NumberFormat.getNumberInstance());
public MethodBody body;
public MethodBodyPanel() {
setLayout(new GridLayout(4,1));
maxStackField.setPreferredSize(new Dimension(75, 20));
JPanel pan1=new JPanel();
pan1.setLayout(new FlowLayout());
pan1.add(maxStackLabel);
pan1.add(maxStackField);
add(pan1);
localCountField.setPreferredSize(new Dimension(75, 20));
JPanel pan2=new JPanel();
pan2.setLayout(new FlowLayout());
pan2.add(localCountLabel);
pan2.add(localCountField);
add(pan2);
initScopeDepthField.setPreferredSize(new Dimension(75, 20));
JPanel pan3=new JPanel();
pan3.setLayout(new FlowLayout());
pan3.add(initScopeDepthLabel);
pan3.add(initScopeDepthField);
add(pan3);
maxScopeDepthField.setPreferredSize(new Dimension(75, 20));
JPanel pan4=new JPanel();
pan4.setLayout(new FlowLayout());
pan4.add(maxScopeDepthLabel);
pan4.add(maxScopeDepthField);
add(pan4);
setPreferredSize(new Dimension(150,150));
}
public void loadFromBody(MethodBody body)
{
this.body=body;
if(body==null){
maxStackField.setText("0");
localCountField.setText("0");
initScopeDepthField.setText("0");
maxScopeDepthField.setText("0");
return;
}
maxStackField.setText(""+body.max_stack);
localCountField.setText(""+body.max_regs);
initScopeDepthField.setText(""+body.init_scope_depth);
maxScopeDepthField.setText(""+body.max_scope_depth);
}
public void save(){
if(body!=null)
{
body.max_stack=Integer.parseInt(maxStackField.getText());
body.max_regs=Integer.parseInt(localCountField.getText());
body.init_scope_depth=Integer.parseInt(initScopeDepthField.getText());
body.max_scope_depth=Integer.parseInt(maxScopeDepthField.getText());
}
}
}

View File

@@ -51,8 +51,7 @@ public class TraitsList extends JList implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
int index = getSelectedIndex();
if (index == -1)
return;
Main.abcMainFrame.decompiledTextArea.gotoTrait(index);
}