Skip to content

Commit

Permalink
Better Synchronization Between On-Disk Editing and In-Memory Editing
Browse files Browse the repository at this point in the history
Probably introduced a bug or two. Compare issues with the previous implementation as that was tested slightly more.
  • Loading branch information
Konloch committed Sep 30, 2024
1 parent d343bce commit 8b84ae7
Showing 1 changed file with 78 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package the.bytecode.club.bytecodeviewer.plugin;

import com.google.common.io.Files;
import com.konloch.taskmanager.Task;
import com.konloch.taskmanager.TaskRunnable;
import me.konloch.kontainer.io.DiskReader;
import me.konloch.kontainer.io.DiskWriter;
import org.apache.commons.compress.utils.FileNameUtils;
Expand All @@ -36,9 +38,12 @@
import the.bytecode.club.bytecodeviewer.util.SyntaxLanguage;

import javax.swing.*;
import javax.swing.text.DefaultCaret;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -66,13 +71,15 @@ public PluginWriter(PluginTemplate template) throws IOException
{
this.content = template.getContents();
this.pluginName = "Template." + template.getExtension();

buildGUI();
}

public PluginWriter(String content, String pluginName)
{
this.content = content;
this.pluginName = pluginName;

buildGUI();
}

Expand All @@ -86,9 +93,13 @@ public void buildGUI()
area.setOnCtrlS(this::save);
area.setText(content);
area.setCaretPosition(0);
DefaultCaret caret = (DefaultCaret)area.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
SyntaxLanguage.setLanguage(area, pluginName);
content = null;

lastModifiedPluginWriterPane = System.currentTimeMillis();

area.addKeyListener(new KeyAdapter()
{
@Override
Expand All @@ -98,6 +109,19 @@ public void keyTyped(KeyEvent e)
}
});

//TODO this could be replaced with a file watch service
// I'll probably come back and fix this in the future, but if anyone needs to replace it:
// - https://github.com/Konloch/GitWatch4J/ has a base you can use

//every 1 second, read the file timestamps and if the file has changed throw trigger an update
BytecodeViewer.getTaskManager().delayLoop(1_000, task ->
{
if(!area.isValid())
task.stop();
else
updateUIFromDiskChanges(null);
});

JButton run = new JButton("Run");

JMenuBar menuBar = new JMenuBar();
Expand Down Expand Up @@ -182,42 +206,8 @@ public void runPlugin()

try
{
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
}
//update the UI from disk changes / write to disk if plugin writer input has been modified
updateUIFromDiskChanges(tempFile);

//run plugin from that location
PluginManager.runPlugin(tempFile);
Expand Down Expand Up @@ -286,6 +276,58 @@ public void setSourceFile(File file)
menuSaveAs.updateUI();
menuSave.updateUI();
savePath = file;

setPluginName(file.getName());
}

public synchronized void updateUIFromDiskChanges(File tempFile)
{
try
{
//opened a plugin from (Plugins>Open Plugin or Plugins>Recent Plugins)
if (savePath != null)
{
if(savePath.lastModified() <= lastModifiedPluginWriterPane)
{
if(tempFile != null) //when user clicks 'Run' instead of running every second
{
//original save path should be overwritten
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
{
//update content from latest disk data
content = DiskReader.loadAsString(savePath.getAbsolutePath());

//update plugin writer UI on disk update
SwingUtilities.invokeLater(() ->
{
try
{
area.setText(content);
}
catch (Exception e)
{
e.printStackTrace();
}
});

lastModifiedPluginWriterPane = System.currentTimeMillis();

if(tempFile != null)
Files.write(content.getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location
}
}
else if(tempFile != null)//temp plugin editing (Plugins>New Java Plugin>Run)
{
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

0 comments on commit 8b84ae7

Please sign in to comment.