tag editor: limit values according to basetype

This commit is contained in:
Honfika
2014-02-08 20:58:08 +01:00
parent a57344e99f
commit 2e3893313d
37 changed files with 275 additions and 42 deletions

View File

@@ -709,6 +709,7 @@ public class Action implements GraphSourceItem {
* @param actions List of actions
* @param version SWF version
* @param path
* @param writer
* @throws java.lang.InterruptedException
*/
public static void actionsToSource(ASMSource asm, final List<Action> actions, final int version, final String path, GraphTextWriter writer) throws InterruptedException {

View File

@@ -85,6 +85,7 @@ public class ActionListReader {
* @param path
* @return List of actions
* @throws IOException
* @throws java.lang.InterruptedException
* @throws java.util.concurrent.TimeoutException
*/
public static List<Action> readActionListTimeout(final List<DisassemblyListener> listeners, final long containerSWFOffset, final MemoryInputStream mis, final int version, final int ip, final int endIp, final String path) throws IOException, InterruptedException, TimeoutException {
@@ -528,6 +529,7 @@ public class ActionListReader {
*
* @param actions
* @param index
* @param removeWhenLast
* @return
*/
public static boolean removeAction(List<Action> actions, int index, int version, boolean removeWhenLast) {

View File

@@ -122,6 +122,7 @@ public class CommandLineArgumentParser {
/**
* Parses the console arguments
*
* @param arguments
* @return path to the file which should be opened or null
* @throws java.io.IOException
*/

View File

@@ -38,6 +38,7 @@ public class FontPreviewDialog extends AppDialog {
/**
* Creates new form FontPreviewDialog
* @param font
* @param modal
*/
public FontPreviewDialog(java.awt.Frame parent, boolean modal, Font font) {
super();

View File

@@ -23,7 +23,6 @@ import com.jpexs.decompiler.flash.gui.generictageditors.StringEditor;
import com.jpexs.decompiler.flash.gui.helpers.SpringUtilities;
import com.jpexs.decompiler.flash.helpers.GraphTextWriter;
import com.jpexs.decompiler.flash.tags.Tag;
import com.jpexs.decompiler.flash.types.BasicType;
import com.jpexs.decompiler.flash.types.RECT;
import com.jpexs.decompiler.flash.types.RGB;
import com.jpexs.decompiler.flash.types.ZONEDATA;
@@ -31,8 +30,10 @@ import com.jpexs.decompiler.flash.types.ZONERECORD;
import com.jpexs.decompiler.flash.types.annotations.SWFType;
import java.awt.BorderLayout;
import java.awt.Component;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
@@ -75,6 +76,7 @@ public class GenericTagPanel extends JPanel {
public void clear() {
tag = null;
genericTagPropertiesEditPanel.removeAll();
genericTagPropertiesEditPanel.setSize(0, 0);
}
public void setEditMode(boolean edit) {
@@ -94,7 +96,20 @@ public class GenericTagPanel extends JPanel {
for (Field field : fields) {
try {
field.setAccessible(true);
sb.append(field.getName()).append(": ").append(field.get(tag));
Object value = field.get(tag);
sb.append(field.getName()).append(": ");
if (field.getType().isArray()) {
sb.append("[");
for (int i = 0; i < Array.getLength(value); i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(Array.get(value, i));
}
sb.append("]");
} else {
sb.append(value);
}
sb.append(GraphTextWriter.NEW_LINE);
} catch (IllegalArgumentException | IllegalAccessException ex) {
Logger.getLogger(GenericTagPanel.class.getName()).log(Level.SEVERE, null, ex);
@@ -106,11 +121,12 @@ public class GenericTagPanel extends JPanel {
public void generateEditControls(Tag tag) {
genericTagPropertiesEditPanel.removeAll();
genericTagPropertiesEditPanel.setSize(0, 0);
this.tag = tag;
int propCount = generateEditControlsRecursive(tag, "");
//Lay out the panel.
SpringUtilities.makeCompactGrid(genericTagPropertiesEditPanel,
propCount, 2, //rows, cols
propCount, 3, //rows, cols
6, 6, //initX, initY
6, 6); //xPad, yPad
repaint();
@@ -127,18 +143,23 @@ public class GenericTagPanel extends JPanel {
field.setAccessible(true);
String name = parent + field.getName();
Object value = field.get(obj);
if (value instanceof Iterable) {
int i = 0;
for (Object obj1 : (Iterable) value) {
propCount += generateEditControlsRecursive(obj1, name + "[" + i++ + "]");
if (List.class.isAssignableFrom(field.getType())) {
if (value != null) {
int i = 0;
for (Object obj1 : (Iterable) value) {
propCount += addEditor(name + "[" + i + "]", obj, field, i, obj1.getClass(), obj1);
i++;
}
}
} else if (value instanceof Object[]) {
Object[] objArr = (Object[]) value;
for (int i = 0; i < objArr.length; i++) {
propCount += generateEditControlsRecursive(objArr[i], name + "[" + i + "]");
} else if (field.getType().isArray()) {
if (value != null) {
for (int i = 0; i < Array.getLength(value); i++) {
Object item = Array.get(value, i);
propCount += addEditor(name + "[" + i + "]", obj, field, i, item.getClass(), item);
}
}
} else {
propCount += addEditor(name, obj, field);
propCount += addEditor(name, obj, field, 0, field.getType(), value);
}
} catch (IllegalArgumentException | IllegalAccessException ex) {
Logger.getLogger(GenericTagPanel.class.getName()).log(Level.SEVERE, null, ex);
@@ -147,29 +168,27 @@ public class GenericTagPanel extends JPanel {
return propCount;
}
private int addEditor(String name, Object obj, Field field) throws IllegalArgumentException, IllegalAccessException {
private int addEditor(String name, Object obj, Field field, int index, Class<?> type, Object value) throws IllegalArgumentException, IllegalAccessException {
Component editor;
Class<?> type = field.getType();
SWFType swfType = field.getAnnotation(SWFType.class);
BasicType basicType = swfType == null ? BasicType.NONE : swfType.value();
if (type.equals(int.class) || type.equals(Integer.class) ||
type.equals(short.class) || type.equals(Short.class) ||
type.equals(long.class) || type.equals(Long.class) ||
type.equals(double.class) || type.equals(Double.class) ||
type.equals(float.class) || type.equals(Float.class)) {
editor = new NumberEditor(obj, field, basicType);
editor = new NumberEditor(obj, field, index, type, swfType);
} else if (type.equals(boolean.class) || type.equals(Boolean.class)) {
editor = new BooleanEditor(obj, field);
editor = new BooleanEditor(obj, field, index, type);
} else if (type.equals(String.class)) {
editor = new StringEditor(obj, field);
editor = new StringEditor(obj, field, index, type);
} else if (type.equals(RECT.class)
|| type.equals(RGB.class)
|| type.equals(ZONERECORD.class)
|| type.equals(ZONEDATA.class)) {
// todo: add other swf releated classes
return generateEditControlsRecursive(field.get(obj), field.getName() + ".");
return generateEditControlsRecursive(value, field.getName() + ".");
} else {
JTextArea textArea = new JTextArea(field.get(obj).toString());
JTextArea textArea = new JTextArea(value.toString());
textArea.setLineWrap(true);
textArea.setEditable(false);
editor = textArea;
@@ -179,9 +198,32 @@ public class GenericTagPanel extends JPanel {
genericTagPropertiesEditPanel.add(label);
label.setLabelFor(editor);
genericTagPropertiesEditPanel.add(editor);
JLabel typeLabel = new JLabel(swfTypeToString(swfType), JLabel.TRAILING);
genericTagPropertiesEditPanel.add(typeLabel);
return 1;
}
public String swfTypeToString(SWFType swfType) {
if (swfType == null) {
return null;
}
String result = swfType.value().toString();
if (swfType.count() > 0) {
result += "[" + swfType.count();
if (swfType.countAdd() > 0) {
result += " + " + swfType.countAdd();
}
result += "]";
} else if (!swfType.countField().equals("")) {
result += "[" + swfType.count();
if (swfType.countAdd() > 0) {
result += " + " + swfType.countAdd();
}
result += "]";
}
return result;
}
public void save() {
for (Object component : genericTagPropertiesEditPanel.getComponents()) {
if (component instanceof GenericTagEditor) {
@@ -191,4 +233,8 @@ public class GenericTagPanel extends JPanel {
tag.setModified(true);
setTagText(tag);
}
public Tag getTag() {
return tag;
}
}

View File

@@ -2141,7 +2141,10 @@ public final class MainPanel extends JPanel implements ActionListener, TreeSelec
break;
case ACTION_SAVE_GENERIC_TAG: {
genericTagPanel.save();
tagTree.repaint();
refreshTree();
TagTreeModel ttm = (TagTreeModel) tagTree.getModel();
TreePath tp = ttm.getTagPath(genericTagPanel.getTag());
tagTree.setSelectionPath(tp);
genericEditButton.setVisible(true);
genericSaveButton.setVisible(false);
genericCancelButton.setVisible(false);

View File

@@ -28,13 +28,17 @@ public class BooleanEditor extends JCheckBox implements GenericTagEditor {
private final Object obj;
private final Field field;
private final int index;
private final Class<?> type;
public BooleanEditor(Object obj, Field field) {
public BooleanEditor(Object obj, Field field, int index, Class<?> type) {
setBackground(Color.white);
this.obj = obj;
this.field = field;
this.index = index;
this.type = type;
try {
setSelected((boolean) field.get(obj));
setSelected((boolean) ReflectionTools.getValue(obj, field, index));
} catch (IllegalArgumentException | IllegalAccessException ex) {
// ignore
}
@@ -43,7 +47,7 @@ public class BooleanEditor extends JCheckBox implements GenericTagEditor {
@Override
public void save() {
try {
field.set(obj, isSelected());
ReflectionTools.setValue(obj, field, index, isSelected());
} catch (IllegalArgumentException | IllegalAccessException ex) {
// ignore
}

View File

@@ -17,30 +17,37 @@
package com.jpexs.decompiler.flash.gui.generictageditors;
import com.jpexs.decompiler.flash.types.BasicType;
import com.jpexs.decompiler.flash.types.annotations.SWFType;
import java.awt.Color;
import java.lang.reflect.Field;
import javax.swing.JTextArea;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
/**
*
* @author JPEXS
*/
public class NumberEditor extends JTextArea implements GenericTagEditor {
public class NumberEditor extends JSpinner implements GenericTagEditor {
private final Object obj;
private final Field field;
private final BasicType basicType;
private final int index;
private final Class<?> type;
private final SWFType swfType;
public NumberEditor(Object obj, Field field, BasicType basicType) {
public NumberEditor(Object obj, Field field, int index, Class<?> type, SWFType swfType) {
setBackground(Color.white);
setSize(100, getSize().height);
setMaximumSize(getSize());
setWrapStyleWord(true);
this.obj = obj;
this.field = field;
this.basicType = basicType;
this.index = index;
this.type = type;
this.swfType = swfType;
try {
setText(field.get(obj).toString());
Object value = ReflectionTools.getValue(obj, field, index);
setModel(getModel(swfType, value));
} catch (IllegalArgumentException | IllegalAccessException ex) {
// ignore
}
@@ -49,20 +56,102 @@ public class NumberEditor extends JTextArea implements GenericTagEditor {
@Override
public void save() {
try {
Class<?> type = field.getType();
Object value = null;
if (type.equals(int.class) || type.equals(Integer.class)) {
field.set(obj, Integer.parseInt(getText()));
value = Integer.parseInt(getValue().toString());
} else if (type.equals(short.class) || type.equals(Short.class)) {
field.set(obj, Short.parseShort(getText()));
value = Short.parseShort(getValue().toString());
} else if (type.equals(long.class) || type.equals(Long.class)) {
field.set(obj, Long.parseLong(getText()));
value = Long.parseLong(getValue().toString());
} else if (type.equals(double.class) || type.equals(Double.class)) {
field.set(obj, Double.parseDouble(getText()));
value = Double.parseDouble(getValue().toString());
} else if (type.equals(float.class) || type.equals(Float.class)) {
field.set(obj, Float.parseFloat(getText()));
value = Float.parseFloat(getValue().toString());
}
if (value != null) {
ReflectionTools.setValue(obj, field, index, value);
}
} catch (IllegalArgumentException | IllegalAccessException ex) {
// ignore
}
}
private SpinnerModel getModel(SWFType swfType, Object value) {
SpinnerNumberModel m = null;
BasicType basicType = swfType == null ? BasicType.NONE : swfType.value();
switch (basicType) {
case UI8:
m = new SpinnerNumberModel(toInt(value), 0, 0xff, 1);
break;
case UI16:
m = new SpinnerNumberModel(toInt(value), 0, 0xffff, 1);
break;
case UB: {
long max = 1;
if (swfType.count() > 0) {
max <<= (swfType.count());
}
m = new SpinnerNumberModel((Number) toLong(value), 0L, (long) max - 1, 1L);
}
break;
case UI32:
case EncodedU32:
case NONE:
m = new SpinnerNumberModel((Number) toLong(value), 0L, 0xffffffffL, 1L);
break;
case SI8:
m = new SpinnerNumberModel(toInt(value), -0x80, 0x7f, 1);
break;
case SI16:
m = new SpinnerNumberModel(toInt(value), -0x8000, 0x7fff, 1);
break;
case SB: {
long max = 1;
if (swfType.count() > 0) {
max <<= (swfType.count() - 1);
}
m = new SpinnerNumberModel((Number) toLong(value), (long) (-max), (long) max - 1, 1L);
}
break;
case SI32:
m = new SpinnerNumberModel(toDouble(value), -0x80000000, 0x7fffffff, 1);
break;
case FB:
case FLOAT:
case FLOAT16:
case FIXED:
case FIXED8:
m = new SpinnerNumberModel(toDouble(value), -0x80000000, 0x7fffffff, 0.01);
break;
}
return m;
}
private double toDouble(Object value) {
if (value instanceof Float) {
return (double) (Float) value;
}
if (value instanceof Double) {
return (double) (Double) value;
}
return 0;
}
private int toInt(Object value) {
if (value instanceof Short) {
return (int) (Short) value;
}
if (value instanceof Integer) {
return (int) (Integer) value;
}
return 0;
}
private long toLong(Object value) {
if (value instanceof Long) {
return (long) (Long) value;
}
return 0;
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (C) 2010-2014 JPEXS
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
package com.jpexs.decompiler.flash.gui.generictageditors;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.List;
/**
*
* @author JPEXS
*/
public class ReflectionTools {
public static Object getValue(Object obj, Field field, int index) throws IllegalArgumentException, IllegalAccessException {
Object value = field.get(obj);
if (List.class.isAssignableFrom(field.getType())) {
return ((List) value).get(index);
}
if (field.getType().isArray()) {
return Array.get(value, index);
}
return value;
}
@SuppressWarnings("unchecked")
public static void setValue(Object obj, Field field, int index, Object newValue) throws IllegalArgumentException, IllegalAccessException {
Object value = field.get(obj);
if (List.class.isAssignableFrom(field.getType())) {
((List) value).set(index, newValue);
}
else if (field.getType().isArray()) {
Array.set(value, index, newValue);
}
else {
field.set(obj, newValue);
}
}
}

View File

@@ -26,16 +26,20 @@ import javax.swing.JTextArea;
*/
public class StringEditor extends JTextArea implements GenericTagEditor {
private final Object tag;
private final Object obj;
private final Field field;
private final int index;
private final Class<?> type;
public StringEditor(Object obj, Field field) {
public StringEditor(Object obj, Field field, int index, Class<?> type) {
setBackground(Color.white);
setLineWrap(true);
this.tag = obj;
this.obj = obj;
this.field = field;
this.index = index;
this.type = type;
try {
setText((String) field.get(obj));
setText((String) ReflectionTools.getValue(obj, field, index));
} catch (IllegalArgumentException | IllegalAccessException ex) {
// ignore
}
@@ -44,7 +48,7 @@ public class StringEditor extends JTextArea implements GenericTagEditor {
@Override
public void save() {
try {
field.set(tag, getText());
ReflectionTools.setValue(obj, field, index, getText());
} catch (IllegalArgumentException | IllegalAccessException ex) {
// ignore
}

View File

@@ -61,6 +61,7 @@ public class DefineButtonCxformTag extends Tag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -101,6 +101,7 @@ public class DefineButtonSoundTag extends CharacterTag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -143,6 +143,7 @@ public class DefineButtonTag extends CharacterTag implements ASMSource, BoundedT
* Converts actions to ASM source
*
* @param version SWF version
* @param actions
* @return ASM source
* @throws java.lang.InterruptedException
*/

View File

@@ -90,6 +90,7 @@ public class DefineFontInfo2Tag extends Tag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -76,6 +76,7 @@ public class DefineSceneAndFrameLabelDataTag extends Tag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -92,6 +92,7 @@ public class DefineSoundTag extends CharacterTag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -196,6 +196,7 @@ public class DefineSpriteTag extends CharacterTag implements Container, BoundedT
* @param parallel
* @param skipUnusualTags
* @throws IOException
* @throws java.lang.InterruptedException
*/
public DefineSpriteTag(SWF swf, byte[] data, int version, int level, long pos, boolean parallel, boolean skipUnusualTags) throws IOException, InterruptedException {
super(swf, ID, "DefineSprite", data, pos);

View File

@@ -102,6 +102,7 @@ public class DefineVideoStreamTag extends CharacterTag implements BoundedTag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -75,6 +75,7 @@ public class DoActionTag extends Tag implements ASMSource {
* Converts actions to ASM source
*
* @param version SWF version
* @param actions
* @return ASM source
* @throws java.lang.InterruptedException
*/

View File

@@ -105,6 +105,7 @@ public class DoInitActionTag extends CharacterIdTag implements ASMSource {
* Converts actions to ASM source
*
* @param version SWF version
* @param actions
* @return ASM source
* @throws java.lang.InterruptedException
*/

View File

@@ -66,6 +66,7 @@ public class EnableDebugger2Tag extends Tag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -60,6 +60,7 @@ public class EnableDebuggerTag extends Tag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -52,6 +52,7 @@ public class ExportAssetsTag extends Tag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -52,6 +52,7 @@ public class ImportAssets2Tag extends Tag implements ImportTag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -43,6 +43,7 @@ public class ImportAssetsTag extends Tag implements ImportTag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -60,6 +60,7 @@ public class ProtectTag extends Tag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -69,6 +69,7 @@ public class RemoveObjectTag extends CharacterIdTag implements RemoveTag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -117,6 +117,7 @@ public class SoundStreamHeadTag extends CharacterTag implements SoundStreamHeadT
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -57,6 +57,7 @@ public class StartSound2Tag extends Tag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -61,6 +61,7 @@ public class StartSoundTag extends Tag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -63,6 +63,7 @@ public class VideoFrameTag extends Tag {
/**
* Constructor
*
* @param swf
* @param data Data bytes
* @param version SWF version
* @param pos

View File

@@ -35,6 +35,7 @@ public interface ASMSource extends TreeItem {
*
* @param version SWF version
* @param exportMode PCode or hex?
* @param actions
* @return ASM source
* @throws java.lang.InterruptedException
*/

View File

@@ -159,6 +159,7 @@ public class BUTTONCONDACTION implements ASMSource, Exportable, ContainerItem {
* Converts actions to ASM source
*
* @param version SWF version
* @param actions
* @return ASM source
* @throws java.lang.InterruptedException
*/

View File

@@ -165,6 +165,7 @@ public class CLIPACTIONRECORD implements ASMSource, Exportable, ContainerItem {
* Converts actions to ASM source
*
* @param version SWF version
* @param actions
* @return ASM source
* @throws java.lang.InterruptedException
*/

View File

@@ -2155,6 +2155,7 @@ public class Graph {
* Converts list of TreeItems to string
*
* @param tree List of TreeItem
* @param writer
* @param localData
* @return String
* @throws java.lang.InterruptedException

View File

@@ -787,6 +787,7 @@ public interface WinNT extends WinError, WinDef, WinBase, BaseTSD {
/**
* Override to the appropriate object for INVALID_HANDLE_VALUE.
* @param context
* @return
*/
@Override

View File

@@ -195,6 +195,7 @@ public interface WinUser extends StdCallLibrary, WinDef {
/**
* Return whether to continue enumeration.
* @param data
* @return
*/
boolean callback(HWND hWnd, Pointer data);