|
| 1 | +// Import selected Overlays from a specified folder |
| 2 | +//@author ThirstyWraith |
| 3 | +//@author acemon33 |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.io.File; |
| 8 | +import java.io.ByteArrayInputStream; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.awt.*; |
| 11 | +import javax.swing.*; |
| 12 | +import javax.swing.event.DocumentEvent; |
| 13 | +import javax.swing.event.DocumentListener; |
| 14 | + |
| 15 | +import ghidra.app.script.GhidraScript; |
| 16 | +import ghidra.util.Msg; |
| 17 | +import ghidra.program.model.address.Address; |
| 18 | +import ghidra.program.model.mem.*; |
| 19 | + |
| 20 | + |
| 21 | +public class OverlaysImporter extends GhidraScript { |
| 22 | + |
| 23 | + @Override |
| 24 | + protected void run() throws Exception { |
| 25 | + File selectedFolder = askDirectory("Select a Folder", "Choose a folder contained Overlays:"); |
| 26 | + if (selectedFolder != null && selectedFolder.isDirectory()) { |
| 27 | + List<JCheckBox> checkBoxes = createFileCheckBoxes(selectedFolder); |
| 28 | + |
| 29 | + JPanel panel = createPanel(checkBoxes); |
| 30 | + |
| 31 | + JButton okButton = new JButton("OK"); |
| 32 | + okButton.setEnabled(false); |
| 33 | + |
| 34 | + JTextField addressField = setAddressFieldValidation(panel, okButton); |
| 35 | + |
| 36 | + showDialog(panel, okButton, addressField, selectedFolder, checkBoxes); |
| 37 | + |
| 38 | + println("finish"); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private List<JCheckBox> createFileCheckBoxes(File selectedFolder) { |
| 43 | + File[] files = selectedFolder.listFiles(); |
| 44 | + List<JCheckBox> checkBoxes = new ArrayList<>(); |
| 45 | + for (File file : files) { |
| 46 | + if (file.isFile()) { |
| 47 | + JCheckBox checkBox = new JCheckBox(file.getName()); |
| 48 | + checkBoxes.add(checkBox); |
| 49 | + } |
| 50 | + } |
| 51 | + return checkBoxes; |
| 52 | + } |
| 53 | + |
| 54 | + private JPanel createPanel(List<JCheckBox> checkBoxes) { |
| 55 | + JPanel panel = new JPanel(new BorderLayout()); |
| 56 | + |
| 57 | + JCheckBox selectAllCheckBox = new JCheckBox("Select All"); |
| 58 | + selectAllCheckBox.addItemListener(e -> { |
| 59 | + boolean isSelected = selectAllCheckBox.isSelected(); |
| 60 | + for (JCheckBox checkBox : checkBoxes) { |
| 61 | + checkBox.setSelected(isSelected); |
| 62 | + } |
| 63 | + }); |
| 64 | + panel.add(selectAllCheckBox, BorderLayout.NORTH); |
| 65 | + |
| 66 | + JPanel checkBoxPanel = new JPanel(new GridLayout(0, 1)); |
| 67 | + for (JCheckBox checkBox : checkBoxes) { |
| 68 | + checkBoxPanel.add(checkBox); |
| 69 | + } |
| 70 | + JScrollPane scrollPane = new JScrollPane(checkBoxPanel); |
| 71 | + panel.add(scrollPane, BorderLayout.CENTER); |
| 72 | + |
| 73 | + JPanel addressPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 74 | + JLabel addressLabel = new JLabel("Enter Address:"); |
| 75 | + JTextField addressField = new JTextField(20); |
| 76 | + addressPanel.add(addressLabel); |
| 77 | + addressPanel.add(addressField); |
| 78 | + panel.add(addressPanel, BorderLayout.SOUTH); |
| 79 | + |
| 80 | + return panel; |
| 81 | + } |
| 82 | + |
| 83 | + private JTextField setAddressFieldValidation(JPanel panel, JButton okButton) { |
| 84 | + JTextField addressField = (JTextField) ((JPanel) panel.getComponent(2)).getComponent(1); |
| 85 | + addressField.getDocument().addDocumentListener(new DocumentListener() { |
| 86 | + @Override |
| 87 | + public void insertUpdate(DocumentEvent e) { |
| 88 | + validateAddress(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void removeUpdate(DocumentEvent e) { |
| 93 | + validateAddress(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void changedUpdate(DocumentEvent e) { |
| 98 | + validateAddress(); |
| 99 | + } |
| 100 | + |
| 101 | + private void validateAddress() { |
| 102 | + String addressText = addressField.getText().trim(); |
| 103 | + try { |
| 104 | + Address address = currentProgram.getAddressFactory().getAddress(addressText); |
| 105 | + okButton.setEnabled(address != null && currentProgram.getMemory().contains(address)); |
| 106 | + } catch (Exception ex) { |
| 107 | + okButton.setEnabled(false); |
| 108 | + } |
| 109 | + } |
| 110 | + }); |
| 111 | + return addressField; |
| 112 | + } |
| 113 | + |
| 114 | + private void showDialog(JPanel panel, JButton okButton, JTextField addressField, File selectedFolder, List<JCheckBox> checkBoxes) { |
| 115 | + Object[] options = { okButton, "Cancel" }; |
| 116 | + JOptionPane optionPane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[1]); |
| 117 | + JDialog dialog = optionPane.createDialog("Select Files and Enter Address"); |
| 118 | + okButton.addActionListener(e -> { |
| 119 | + try { |
| 120 | + Address bassAddress = currentProgram.getAddressFactory().getAddress(addressField.getText().trim()); |
| 121 | + Memory memory = currentProgram.getMemory(); |
| 122 | + |
| 123 | + for (JCheckBox checkBox : checkBoxes) { |
| 124 | + if (checkBox.isSelected()) { |
| 125 | + File file = new File(selectedFolder.getAbsolutePath() + "/" + checkBox.getText()); |
| 126 | + String blockName = getBlockName(file.getName()); |
| 127 | + |
| 128 | + FileInputStream fileInputStream = new FileInputStream(file); |
| 129 | + byte[] fileData = new byte[(int) file.length()]; |
| 130 | + fileInputStream.read(fileData); |
| 131 | + fileInputStream.close(); |
| 132 | + |
| 133 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData); |
| 134 | + MemoryBlock block = memory.createInitializedBlock(blockName, bassAddress, inputStream, file.length(), monitor, true); |
| 135 | + block.setRead(true); |
| 136 | + block.setWrite(true); |
| 137 | + block.setExecute(true); |
| 138 | + println(String.format("Added %s as overlay block at %s", blockName, bassAddress)); |
| 139 | + } |
| 140 | + } |
| 141 | + } catch (Exception ex) { |
| 142 | + println("Invalid address format: " + ex.getMessage()); |
| 143 | + } |
| 144 | + dialog.dispose(); |
| 145 | + }); |
| 146 | + dialog.setVisible(true); |
| 147 | + } |
| 148 | + |
| 149 | + private String getBlockName(String fileName) { |
| 150 | + int lastDotIndex = fileName.lastIndexOf('.'); |
| 151 | + return lastDotIndex != -1 ? fileName.substring(0, lastDotIndex) : fileName; |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments