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:
Konloch 2024-09-29 20:24:17 -06:00
parent c29f0fbb21
commit 805dac8e36

AI 샘플 코드 생성 중입니다

Loading...

View File

@ -37,6 +37,8 @@ import the.bytecode.club.bytecodeviewer.util.SyntaxLanguage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@ -58,6 +60,7 @@ public class PluginWriter extends JFrame
private String content;
private String pluginName;
private File savePath;
private long lastModifiedPluginWriterPane = 0;
public PluginWriter(PluginTemplate template) throws IOException
{
@ -86,6 +89,15 @@ public class PluginWriter extends JFrame
SyntaxLanguage.setLanguage(area, pluginName);
content = null;
area.addKeyListener(new KeyAdapter()
{
@Override
public void keyTyped(KeyEvent e)
{
lastModifiedPluginWriterPane = System.currentTimeMillis();
}
});
JButton run = new JButton("Run");
JMenuBar menuBar = new JMenuBar();
@ -170,11 +182,42 @@ public class PluginWriter extends JFrame
try
{
//write to temporary file location
if(savePath != null)
Files.copy(savePath, tempFile);
else
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile);
if(savePath != null) //opened a plugin from (Plugins>Open Plugin or Plugins>Recent Plugins)
{
//original save path should be overwritten
if(savePath.lastModified() <= lastModifiedPluginWriterPane)
{
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
PluginManager.runPlugin(tempFile);
@ -232,6 +275,7 @@ public class PluginWriter extends JFrame
DiskWriter.replaceFile(savePath.getAbsolutePath(), area.getText(), false);
addRecentPlugin(savePath);
}, "Plugin Editor Save");
exportThread.start();
}