forked from Konloch/bytecode-viewer
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPluginWriter.java
More file actions
344 lines (294 loc) · 11.9 KB
/
PluginWriter.java
File metadata and controls
344 lines (294 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Konloch - Konloch.com / BytecodeViewer.com *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
package the.bytecode.club.bytecodeviewer.plugin;
import com.google.common.io.Files;
import com.konloch.disklib.DiskReader;
import com.konloch.disklib.DiskWriter;
import org.apache.commons.io.FilenameUtils;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.gui.components.FileChooser;
import the.bytecode.club.bytecodeviewer.gui.components.SearchableRSyntaxTextArea;
import the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer.ComponentViewer;
import the.bytecode.club.bytecodeviewer.resources.IconResources;
import the.bytecode.club.bytecodeviewer.translation.TranslatedComponents;
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJMenu;
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJMenuItem;
import the.bytecode.club.bytecodeviewer.util.DialogUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
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;
import static the.bytecode.club.bytecodeviewer.Constants.FS;
import static the.bytecode.club.bytecodeviewer.Constants.TEMP_DIRECTORY;
import static the.bytecode.club.bytecodeviewer.Settings.addRecentPlugin;
/**
* @author Konloch
* @since 7/1/2021
*/
public class PluginWriter extends JFrame
{
private SearchableRSyntaxTextArea area;
private JMenuItem menuSaveAs;
private JMenuItem menuSave;
private String content;
private String pluginName;
private File savePath;
private long lastModifiedPluginWriterPane = 0;
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();
}
public void buildGUI()
{
setTitle("Editing BCV Plugin: " + pluginName);
setIconImages(IconResources.iconList);
setSize(new Dimension(542, 316));
area = (SearchableRSyntaxTextArea) Configuration.rstaTheme.apply(new SearchableRSyntaxTextArea());
area.setOnCtrlS(this::save);
area.setText(content);
area.setCaretPosition(0);
SyntaxLanguage.setLanguage(area, pluginName);
content = null;
lastModifiedPluginWriterPane = System.currentTimeMillis();
area.addKeyListener(new KeyAdapter()
{
@Override
public void keyTyped(KeyEvent e)
{
lastModifiedPluginWriterPane = System.currentTimeMillis();
}
});
//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();
JMenu menu = new TranslatedJMenu("File", TranslatedComponents.FILE);
JMenuItem menuOpen = new TranslatedJMenuItem("Open...", TranslatedComponents.OPEN);
JMenuItem menuRun = new TranslatedJMenuItem("Run", TranslatedComponents.RUN);
menuSaveAs = new TranslatedJMenuItem("Save As...", TranslatedComponents.SAVE_AS);
menuSave = new TranslatedJMenuItem("Save...", TranslatedComponents.SAVE);
menuSave.setVisible(false);
menuBar.add(menu);
menu.add(menuOpen);
menu.add(menuSaveAs);
menu.add(menuSave);
menu.add(menuRun);
setJMenuBar(menuBar);
add(area.getScrollPane());
add(run, BorderLayout.SOUTH);
menuOpen.addActionListener((l) -> openPlugin());
run.addActionListener((l) -> runPlugin());
menuRun.addActionListener((l) -> runPlugin());
menuSaveAs.addActionListener((l) -> save());
menuSave.addActionListener((l) -> save());
this.setLocationRelativeTo(null);
}
@Override
public void setVisible(boolean b)
{
if (Configuration.pluginWriterAsNewTab)
{
Component component = getComponent(0);
JPanel p = new JPanel(new BorderLayout());
JPanel p2 = new JPanel(new BorderLayout());
p.add(p2, BorderLayout.NORTH);
p.add(component, BorderLayout.CENTER);
p2.add(getJMenuBar(), BorderLayout.CENTER);
ComponentViewer.addComponentAsTab(pluginName, p);
}
else
{
super.setVisible(b);
}
}
public void setPluginName(String name)
{
this.pluginName = name;
setTitle("Editing BCV Plugin: " + name);
}
public void openPlugin()
{
final File file = DialogUtils.fileChooser("Select External Plugin", "External Plugin", Configuration.getLastPluginDirectory(), PluginManager.fileFilter(), Configuration::setLastPluginDirectory, FileChooser.EVERYTHING);
if (file == null || !file.exists())
return;
try
{
area.setText(DiskReader.readString(file.getAbsolutePath()));
area.setCaretPosition(0);
}
catch (Exception e)
{
e.printStackTrace();
}
setSourceFile(file);
}
public void runPlugin()
{
File tempFile = new File(TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(32) + FS + pluginName);
tempFile.getParentFile().mkdirs();
try
{
//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);
}
catch (Exception e)
{
BytecodeViewer.handleException(e);
}
catch (Throwable t)
{
t.printStackTrace();
}
finally
{
tempFile.getParentFile().delete();
}
}
public void save()
{
Thread exportThread = new Thread(() ->
{
if (!BytecodeViewer.autoCompileSuccessful())
return;
if (savePath == null)
{
try
{
final String ext = FilenameUtils.getExtension(pluginName);
JFileChooser fc = FileChooser.create(Configuration.getLastPluginDirectory(), "Save Plugin", "BCV Plugin", ext);
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
Configuration.setLastPluginDirectory(fc.getSelectedFile());
File file = fc.getSelectedFile();
String path = file.getAbsolutePath();
//auto append extension
if (!path.endsWith("." + ext))
path += "." + ext;
if (!DialogUtils.canOverwriteFile(path))
return;
//swap from save-as to having a defined path each save
setSourceFile(new File(path));
}
else
{
return;
}
}
catch (Exception e)
{
BytecodeViewer.handleException(e);
}
}
try
{
DiskWriter.write(savePath.getAbsolutePath(), area.getText());
}
catch (IOException e)
{
e.printStackTrace();
}
addRecentPlugin(savePath);
}, "Plugin Editor Save");
exportThread.start();
}
public void setSourceFile(File file)
{
menuSaveAs.setVisible(false);
menuSave.setVisible(true);
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.readString(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();
}
}
}