mirror of
https://github.com/AppleCommander/AppleCommander.git
synced 2026-11-25 21:27:36 +00:00
Adding custom links, which enabled Disassembly code/data capabilities.
This commit is contained in:
1 parent
622590c760
commit
4bfae2ce35
2 files changed
+60
-27
No files matched your search
+36
-10
@@ -32,14 +32,12 @@ import org.applecommander.javafx.content.FileContentWindow;
|
||||
import org.applecommander.transfer.ProdosAttributes;
|
||||
import org.applecommander.util.DataBuffer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
public class DisassemblyView extends NavigableTextView {
|
||||
private final InstructionSet instructionSet;
|
||||
private final ToggleButton codeButton;
|
||||
private final List<Integer> entryPoints = new ArrayList<>();
|
||||
|
||||
public DisassemblyView(FileContentWindow parent) {
|
||||
this.instructionSet = switch (parent.getFileEntry().getContentType()) {
|
||||
@@ -68,18 +66,38 @@ public class DisassemblyView extends NavigableTextView {
|
||||
return Optional.of(codeButton);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCustomLink(int value) {
|
||||
if (entryPoints.contains(value)) {
|
||||
// The integer remove method removes by index value instead of the object value.
|
||||
entryPoints.remove((Object)value);
|
||||
}
|
||||
else {
|
||||
entryPoints.add(value);
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
public void build(TextBuilder builder) {
|
||||
if (codeButton == null) {
|
||||
// We get called early as everything is constructed; ignore the call.
|
||||
return;
|
||||
}
|
||||
ProdosAttributes attributes = parent.getFileEntry().getProdosAttributes();
|
||||
final int startAddress = attributes.auxType();
|
||||
final DataBuffer dataFork = parent.getFileEntry().getDataFork();
|
||||
final int endAddress = startAddress + dataFork.limit();
|
||||
Map<Integer,String> labels = new HashMap<>();
|
||||
List<Instruction> instructions = Disassembler.with(dataFork.asBytes())
|
||||
Disassembler.Builder disassembler = Disassembler.with(dataFork.asBytes())
|
||||
.startingAddress(startAddress)
|
||||
.use(instructionSet)
|
||||
.decode(labels);
|
||||
.use(instructionSet);
|
||||
final boolean detectionMode = codeButton.isSelected();
|
||||
if (detectionMode) {
|
||||
if (entryPoints.isEmpty()) entryPoints.add(startAddress);
|
||||
entryPoints.forEach(disassembler::entryPoint);
|
||||
}
|
||||
List<Instruction> instructions = disassembler.decode(labels);
|
||||
int bytesPerLine = instructionSet.defaults().bytesPerInstruction();
|
||||
boolean entryPoints = this.codeButton != null && this.codeButton.isSelected();
|
||||
for (Instruction inst : instructions) {
|
||||
builder.addNavigableTarget(inst.address(), "%04X", inst.address());
|
||||
builder.addText("- ");
|
||||
@@ -88,8 +106,8 @@ public class DisassemblyView extends NavigableTextView {
|
||||
if (i >= code.length) {
|
||||
builder.addText(" ");
|
||||
}
|
||||
else if (entryPoints) {
|
||||
builder.addLink("%02X", false, Byte.toUnsignedInt(code[i]));
|
||||
else if (detectionMode) {
|
||||
builder.addCustomLink(inst.address()+i, "%02X", Byte.toUnsignedInt(code[i]));
|
||||
builder.addText(" ");
|
||||
}
|
||||
else {
|
||||
@@ -97,6 +115,14 @@ public class DisassemblyView extends NavigableTextView {
|
||||
}
|
||||
}
|
||||
builder.addText(" %-10.10s ", labels.getOrDefault(inst.address(), ""));
|
||||
if (detectionMode) {
|
||||
if (entryPoints.contains(inst.address())) {
|
||||
builder.addCustomLink(inst.address(), "--> ");
|
||||
}
|
||||
else {
|
||||
builder.addText(" ");
|
||||
}
|
||||
}
|
||||
builder.addText("%-5s ", inst.mnemonic());
|
||||
// We may need to pad out 30 spaces, so track where we started.
|
||||
int startChar = builder.length();
|
||||
|
||||
+24
-17
@@ -48,6 +48,7 @@ public abstract class NavigableTextView extends StackPane implements FileContent
|
||||
private final List<Range> highlightedRanges = new ArrayList<>();
|
||||
private final Map<Range,Integer> navigableLinks = new HashMap<>();
|
||||
private final Map<Integer,Range> navigableTargets = new HashMap<>();
|
||||
private final Map<Range,Integer> customLinks = new HashMap<>();
|
||||
private Font font;
|
||||
|
||||
public NavigableTextView(FileContentWindow parent) {
|
||||
@@ -104,11 +105,17 @@ public abstract class NavigableTextView extends StackPane implements FileContent
|
||||
FxUtils.centerNodeInScrollPane(parent.getScrollPane(), path);
|
||||
}
|
||||
});
|
||||
Optional<Range> custom = customLinks.keySet().stream().filter(r -> r.in(index)).findFirst();
|
||||
custom.ifPresent(range -> handleCustomLink(customLinks.get(range)));
|
||||
});
|
||||
this.linkColor = Color.CYAN.deriveColor(1.0, 1.0, 1.0, 0.35);
|
||||
refresh();
|
||||
}
|
||||
|
||||
public void handleCustomLink(int value) {
|
||||
// do nothing in default implementation
|
||||
}
|
||||
|
||||
public void setFont(double size) {
|
||||
this.font = Font.font("monospaced", size);
|
||||
if (this.textFlow != null) {
|
||||
@@ -129,18 +136,13 @@ public abstract class NavigableTextView extends StackPane implements FileContent
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Node> getAdditionalToolbarButtons() {
|
||||
return FileContentView.super.getAdditionalToolbarButtons();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copy() {
|
||||
Clipboard clipboard = Clipboard.getSystemClipboard();
|
||||
ClipboardContent content = new ClipboardContent();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (var node : getChildren()) {
|
||||
for (var node : textFlow.getChildren()) {
|
||||
if (node instanceof Text text) {
|
||||
sb.append(text.getText());
|
||||
}
|
||||
@@ -166,6 +168,8 @@ public abstract class NavigableTextView extends StackPane implements FileContent
|
||||
navigableLinks.putAll(builder.getNavigableLinks());
|
||||
navigableTargets.clear();
|
||||
navigableTargets.putAll(builder.getNavigableTargets());
|
||||
customLinks.clear();
|
||||
customLinks.putAll(builder.getCustomLinks());
|
||||
recalculateHighlightedRanges();
|
||||
}
|
||||
|
||||
@@ -197,6 +201,7 @@ public abstract class NavigableTextView extends StackPane implements FileContent
|
||||
private final List<Range> highlightedRanges = new ArrayList<>();
|
||||
private final Map<Range,Integer> navigableLinks = new HashMap<>();
|
||||
private final Map<Integer,Range> navigableTargets = new HashMap<>();
|
||||
private final Map<Range,Integer> customLinks = new HashMap<>();
|
||||
|
||||
public String getText() {
|
||||
return sb.toString();
|
||||
@@ -213,13 +218,21 @@ public abstract class NavigableTextView extends StackPane implements FileContent
|
||||
public Map<Integer,Range> getNavigableTargets() {
|
||||
return navigableTargets;
|
||||
}
|
||||
public Map<Range, Integer> getCustomLinks() {
|
||||
return customLinks;
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return sb.length();
|
||||
}
|
||||
/// Add text into the resulting document.
|
||||
public void addText(String fmt, Object... args) {
|
||||
sb.append(String.format(fmt, args));
|
||||
if (args.length == 0) {
|
||||
sb.append(fmt);
|
||||
}
|
||||
else {
|
||||
sb.append(String.format(fmt, args));
|
||||
}
|
||||
}
|
||||
private Range addTextAsRange(String fmt, Object... args) {
|
||||
int firstChar = sb.length();
|
||||
@@ -244,17 +257,11 @@ public abstract class NavigableTextView extends StackPane implements FileContent
|
||||
clickableRanges.add(range);
|
||||
highlightedRanges.add(range);
|
||||
}
|
||||
/// Add a link - TODO. Left over, needs to allow custom linking.
|
||||
public void addLink(String fmt, boolean highlighted, Object... args) {
|
||||
int firstChar = sb.length();
|
||||
addText(fmt, args);
|
||||
int lastChar = sb.length();
|
||||
// Range assumes it is _inclusive_.
|
||||
Range range = new Range(firstChar, lastChar-1);
|
||||
/// Add a custom link.
|
||||
public void addCustomLink(int target, String fmt, Object... args) {
|
||||
Range range = addTextAsRange(fmt, args);
|
||||
clickableRanges.add(range);
|
||||
if (highlighted) {
|
||||
highlightedRanges.add(range);
|
||||
}
|
||||
customLinks.put(range, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user