Better Plugin Writing
Resolves #394 You can now write plugins from the Plugin Writer in BCV, or from disk. BCV will read the last edit dates on both and use the latest version. As a bonus BCV will self-update the pane to keep the BCV plugin writer synced with your local file. Local files are also overwritten when the plugin writer is used to edit the plugin, then ran.
This commit is contained in:
parent
c29f0fbb21
commit
805dac8e36
|
@ -37,6 +37,8 @@ import the.bytecode.club.bytecodeviewer.util.SyntaxLanguage;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.event.KeyAdapter;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
@ -58,6 +60,7 @@ public class PluginWriter extends JFrame
|
||||||
private String content;
|
private String content;
|
||||||
private String pluginName;
|
private String pluginName;
|
||||||
private File savePath;
|
private File savePath;
|
||||||
|
private long lastModifiedPluginWriterPane = 0;
|
||||||
|
|
||||||
public PluginWriter(PluginTemplate template) throws IOException
|
public PluginWriter(PluginTemplate template) throws IOException
|
||||||
{
|
{
|
||||||
|
@ -86,6 +89,15 @@ public class PluginWriter extends JFrame
|
||||||
SyntaxLanguage.setLanguage(area, pluginName);
|
SyntaxLanguage.setLanguage(area, pluginName);
|
||||||
content = null;
|
content = null;
|
||||||
|
|
||||||
|
area.addKeyListener(new KeyAdapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void keyTyped(KeyEvent e)
|
||||||
|
{
|
||||||
|
lastModifiedPluginWriterPane = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
JButton run = new JButton("Run");
|
JButton run = new JButton("Run");
|
||||||
|
|
||||||
JMenuBar menuBar = new JMenuBar();
|
JMenuBar menuBar = new JMenuBar();
|
||||||
|
@ -170,11 +182,42 @@ public class PluginWriter extends JFrame
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//write to temporary file location
|
if(savePath != null) //opened a plugin from (Plugins>Open Plugin or Plugins>Recent Plugins)
|
||||||
if(savePath != null)
|
{
|
||||||
Files.copy(savePath, tempFile);
|
//original save path should be overwritten
|
||||||
else
|
if(savePath.lastModified() <= lastModifiedPluginWriterPane)
|
||||||
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile);
|
{
|
||||||
|
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), savePath); //overwrite original plugin location with new data
|
||||||
|
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Files.copy(savePath, tempFile); //write to temporary file location
|
||||||
|
|
||||||
|
//update content from latest disk data
|
||||||
|
content = DiskReader.loadAsString(savePath.getAbsolutePath());
|
||||||
|
|
||||||
|
//update plugin writer UI on disk update
|
||||||
|
SwingUtilities.invokeLater(()->
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int caretPosition = area.getCaretPosition();
|
||||||
|
|
||||||
|
area.setText(content);
|
||||||
|
area.setCaretPosition(caretPosition);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else //temp plugin editing (Plugins>New Java Plugin>Run)
|
||||||
|
{
|
||||||
|
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location
|
||||||
|
}
|
||||||
|
|
||||||
//run plugin from that location
|
//run plugin from that location
|
||||||
PluginManager.runPlugin(tempFile);
|
PluginManager.runPlugin(tempFile);
|
||||||
|
@ -232,6 +275,7 @@ public class PluginWriter extends JFrame
|
||||||
DiskWriter.replaceFile(savePath.getAbsolutePath(), area.getText(), false);
|
DiskWriter.replaceFile(savePath.getAbsolutePath(), area.getText(), false);
|
||||||
addRecentPlugin(savePath);
|
addRecentPlugin(savePath);
|
||||||
}, "Plugin Editor Save");
|
}, "Plugin Editor Save");
|
||||||
|
|
||||||
exportThread.start();
|
exportThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user