1111import club .bytecode .the .jda .gui .fileviewer .ViewerFile ;
1212import club .bytecode .the .jda .gui .navigation .FileNavigationPane ;
1313import club .bytecode .the .jda .settings .Settings ;
14+ import club .bytecode .the .jda .util .BytecodeUtils ;
1415import club .bytecode .the .jda .util .GuiUtils ;
1516import club .bytecode .the .jda .util .MiscUtils ;
1617import org .apache .commons .io .FileUtils ;
1920import org .fife .ui .rsyntaxtextarea .TokenMakerFactory ;
2021import org .fife .ui .rsyntaxtextarea .folding .CurlyFoldParser ;
2122import org .fife .ui .rsyntaxtextarea .folding .FoldParserManager ;
22- import org .objectweb .asm .ClassWriter ;
2323import org .objectweb .asm .tree .ClassNode ;
2424
2525import javax .swing .*;
3535import java .util .function .Supplier ;
3636
3737public class JDA {
38- /*per version*/
3938 public static final String version = "1.1.1" ;
40- /* Constants */
39+
4140 public static final String ISSUE_TRACKER_URL = "https://github.com/LLVM-but-worse/jda/issues" ;
4241 public static final String fs = System .getProperty ("file.separator" );
4342 public static final String nl = System .getProperty ("line.separator" );
@@ -46,7 +45,7 @@ public class JDA {
4645 public static final File recentsFile = new File (dataDir , "recentfiles.jda" );
4746 public static final File settingsFile = new File (dataDir , "settings.jda" );
4847 private static final long start = System .currentTimeMillis ();
49- /*the rest*/
48+
5049 public static MainViewerGUI viewer = null ;
5150 private static List <FileContainer > files = new ArrayList <>(); //all of BCV's loaded files/classes/etc
5251 private static int maxRecentFiles = 25 ;
@@ -57,17 +56,15 @@ public class JDA {
5756 public static final JDANamespace namespace = JDADefaultNamespace .INSTANCE ;
5857 private static List <JDAPlugin > plugins = new ArrayList <>();
5958
60- public static Supplier <JDAPlugin > injectedPlugin = null ; // for testing purposes only .
59+ public static Supplier <JDAPlugin > autoloadPlugin = null ; // for testing purposes, a plugin to load on startup .
6160
6261 /**
6362 * Main startup
6463 *
6564 * @param args files you want to open or CLI
6665 */
6766 public static void main (String [] args ) {
68- // Fix antialiasing
69- System .setProperty ("awt.useSystemAAFontSettings" , "lcd" );
70- System .setProperty ("swing.aatext" , "true" );
67+ GuiUtils .setAntialiasingSettings ();
7168 if (SystemUtils .IS_OS_LINUX ) {
7269 GuiUtils .setWmClassName ("JDA" );
7370 }
@@ -107,10 +104,10 @@ public static List<JDAPlugin> getLoadedPlugins() {
107104 }
108105
109106 private static void loadPlugins () throws MalformedURLException {
110- if (injectedPlugin != null ) {
111- JDAPlugin plugin = injectedPlugin .get ();
112- System .out .println ("Loading dependency-injected plugin " + plugin .getName ());
113- loadPlugin (injectedPlugin .get ());
107+ if (autoloadPlugin != null ) {
108+ JDAPlugin plugin = autoloadPlugin .get ();
109+ System .out .println ("Loading statically-loaded plugin " + plugin .getName ());
110+ loadPlugin (autoloadPlugin .get ());
114111 System .out .println ("Skipping other plugins." );
115112 return ;
116113 }
@@ -142,7 +139,6 @@ public static void onGUILoad() {
142139 plugins .forEach (JDAPlugin ::onGUILoad );
143140 }
144141
145- // todo: rewrite
146142 /**
147143 * The version checker thread
148144 */
@@ -229,7 +225,7 @@ public static boolean hasFile(ViewerFile file) {
229225
230226 // try to get class bytes by exporting classnode, else fallback to getting the bytes from the actual file itself
231227 public static byte [] getClassBytes (FileContainer container , ClassNode cn ) {
232- byte [] result = dumpClassToBytes (cn );
228+ byte [] result = BytecodeUtils . dumpClassToBytes (cn );
233229 if (result != null )
234230 return result ;
235231 result = getClassFileBytes (container , cn .name );
@@ -242,19 +238,6 @@ public static byte[] getClassFileBytes(FileContainer container, String className
242238 return null ;
243239 return bytes ;
244240 }
245-
246- public static byte [] dumpClassToBytes (ClassNode cn ) {
247- // we have to do this, or else decompile filters don't work.
248- try {
249- ClassWriter writer = new ClassWriter (ClassWriter .COMPUTE_MAXS );
250- cn .accept (writer );
251- return writer .toByteArray ();
252- } catch (Exception e ) {
253- System .err .println ("Exception while dumping class " + cn .name + ": " );
254- e .printStackTrace ();
255- return null ;
256- }
257- }
258241
259242 public static final String HACK_PREFIX = "\0 JDA-hack" ;
260243
@@ -279,15 +262,6 @@ public static String getClassName(String fullyQualifiedName) {
279262 return fullyQualifiedName .substring (fullyQualifiedName .lastIndexOf ('/' ) + 1 );
280263 }
281264
282- // WTF????
283- public static Map <String , byte []> getLoadedBytes () {
284- Map <String , byte []> data = new HashMap <>();
285- for (FileContainer container : files ) {
286- data .putAll (container .getFiles ());
287- }
288- return data ;
289- }
290-
291265 /**
292266 * Opens a file, optional if it should append to the recent files menu
293267 *
@@ -315,7 +289,7 @@ public static void openFiles(final File[] files, boolean recentFiles, FileNaviga
315289 if (!fileToOpen .exists ()) {
316290 showMessage ("The file " + fileToOpen .getAbsolutePath () + " could not be found." );
317291 } else if (fileToOpen .isDirectory ()) {
318- FileNavigationPane .FileNode newNode = fnp .addTreeElement (new FileContainer (fileToOpen ), parent );
292+ FileNavigationPane .FileNode newNode = fnp .addTreeElement (new FileContainer (fileToOpen , new HashMap <>() ), parent );
319293 openFiles (fileToOpen .listFiles (), false , newNode );
320294 } else if (fn .endsWith (".jar" ) || fn .endsWith (".zip" )) {
321295 try {
@@ -329,8 +303,7 @@ public static void openFiles(final File[] files, boolean recentFiles, FileNaviga
329303 HashMap <String , byte []> files1 = new HashMap <>();
330304 byte [] bytes = JarUtils .getBytes (new FileInputStream (fileToOpen ));
331305 files1 .put (fileToOpen .getName (), bytes );
332- FileContainer container = new FileContainer (fileToOpen );
333- container .files = files1 ;
306+ FileContainer container = new FileContainer (fileToOpen , files1 );
334307 openFile (container );
335308 fnp .addTreeElement (container , parent );
336309 }
@@ -363,6 +336,20 @@ public static List<FileContainer> getOpenFiles() {
363336 return Collections .unmodifiableList (files );
364337 }
365338
339+ /**
340+ *
341+ * @param filename the filename to search all open FileContainers for
342+ * @return the FileContainer which holds the specified file or null if not found
343+ */
344+ public static FileContainer findContainerForFile (String filename ) {
345+ for (FileContainer container : files ) {
346+ if (container .getFiles ().containsKey (filename )) {
347+ return container ;
348+ }
349+ }
350+ return null ;
351+ }
352+
366353 /**
367354 * Send a message to alert the user
368355 *
@@ -372,22 +359,34 @@ public static void showMessage(String message) {
372359 JOptionPane .showMessageDialog (viewer , message );
373360 }
374361
362+ /**
363+ * Ask a yes/no dialog
364+ * @param question the body text of the dialog box to be created
365+ * @param title the window title of the dialog box to be created
366+ * @return true or false for yes or no
367+ */
368+ public static boolean askYesNoDialog (String question , String title ) {
369+ JOptionPane pane = new JOptionPane (question );
370+ Object [] options = new String []{"Yes" , "No" };
371+ pane .setOptions (options );
372+ JDialog dialog = pane .createDialog (viewer , "JDA - " + title );
373+ dialog .setVisible (true );
374+ Object obj = pane .getValue ();
375+ for (int k = 0 ; k < options .length ; k ++)
376+ if (options [k ].equals (obj ) && k == 0 )
377+ return true ;
378+ return false ;
379+ }
380+
375381 /**
376382 * Resets the workspace with optional user input required
377383 *
378384 * @param ask if should require user input or not
379385 */
380386 public static void resetWorkSpace (boolean ask ) {
381387 if (ask ) {
382- JOptionPane pane = new JOptionPane ("Are you sure you want to reset the workspace?\n \r It will also reset your file navigator and search." );
383- Object [] options = new String []{"Yes" , "No" };
384- pane .setOptions (options );
385- JDialog dialog = pane .createDialog (viewer , "JDA - Reset Workspace" );
386- dialog .setVisible (true );
387- Object obj = pane .getValue ();
388- for (int k = 0 ; k < options .length ; k ++)
389- if (options [k ].equals (obj ) && k != 0 )
390- return ;
388+ if (!askYesNoDialog ("Are you sure you want to reset the workspace?\n \r It will also reset your file navigator and search." , "Reset Workspace" ))
389+ return ;
391390 }
392391
393392 closeResources (false );
@@ -396,15 +395,8 @@ public static void resetWorkSpace(boolean ask) {
396395
397396 public static void closeResources (boolean ask ) {
398397 if (ask ) {
399- JOptionPane pane = new JOptionPane ("Are you sure you want to close all resources?" );
400- Object [] options = new String []{"Yes" , "No" };
401- pane .setOptions (options );
402- JDialog dialog = pane .createDialog (viewer , "JDA - Close Resources" );
403- dialog .setVisible (true );
404- Object obj = pane .getValue ();
405- for (int k = 0 ; k < options .length ; k ++)
406- if (options [k ].equals (obj ) && k != 0 )
407- return ;
398+ if (!askYesNoDialog ("Are you sure you want to close all resources?" , "Close Resources" ))
399+ return ;
408400 }
409401
410402 JDA .setBusy (true );
@@ -441,8 +433,6 @@ public static void addRecentFile(File f) {
441433 resetRecentFilesMenu ();
442434 }
443435
444- private static ArrayList <String > killList2 = new ArrayList <>();
445-
446436 /**
447437 * resets the recent files menu
448438 */
@@ -459,27 +449,6 @@ public static void resetRecentFilesMenu() {
459449 }
460450 }
461451
462- public static ArrayList <String > createdRandomizedNames = new ArrayList <>();
463-
464- /**
465- * Ensures it will only return a uniquely generated names, contains a dupe checker to be sure
466- *
467- * @return the unique randomized name of 25 characters.
468- */
469- public static String getRandomizedName () {
470- boolean generated = false ;
471- String name = "" ;
472- while (!generated ) {
473- String randomizedName = MiscUtils .randomString (25 );
474- if (!createdRandomizedNames .contains (randomizedName )) {
475- createdRandomizedNames .add (randomizedName );
476- name = randomizedName ;
477- generated = true ;
478- }
479- }
480- return name ;
481- }
482-
483452 /**
484453 * Returns the BCV directory
485454 *
@@ -519,8 +488,6 @@ private static boolean isShiftDown(KeyEvent e) {
519488
520489 /**
521490 * Checks the hotkeys
522- *
523- * @param e
524491 */
525492 public static void checkHotKey (KeyEvent e ) {
526493 if ((e .getKeyCode () == KeyEvent .VK_O ) && isCtrlDown (e )) {
0 commit comments