2323import java .io .FileInputStream ;
2424import java .io .IOException ;
2525import java .net .MalformedURLException ;
26- import java .nio .file .Files ;
27- import java .nio .file .Paths ;
28- import java .util .*;
26+ import java .util .ArrayList ;
27+ import java .util .HashMap ;
28+ import java .util .List ;
29+ import java .util .Map ;
2930
3031public class JDA {
3132 /*per version*/
@@ -151,15 +152,6 @@ private static void onExit() {
151152 Settings .saveGUI ();
152153 }
153154
154- /**
155- * Returns the currently opened ClassNode
156- *
157- * @return the currently opened ClassNode
158- */
159- public static ClassNode getCurrentlyOpenedClassNode () {
160- return viewer .FileViewerPane .getCurrentViewer ().cn ;
161- }
162-
163155 public static byte [] getFileBytes (FileContainer container , String name ) {
164156 if (container != null )
165157 return container .getData ().get (name );
@@ -168,7 +160,7 @@ public static byte[] getFileBytes(FileContainer container, String name) {
168160 }
169161
170162 public static byte [] getClassBytes (FileContainer container , ClassNode cn ) {
171- byte [] bytes = getFileBytes (container , getClassfileName (cn ));
163+ byte [] bytes = getFileBytes (container , container . findClassfile (cn . name ));
172164 if (bytes == null )
173165 return null ;
174166 if (cn .version < 49 )
@@ -179,19 +171,19 @@ public static byte[] getClassBytes(FileContainer container, ClassNode cn) {
179171 public static final String HACK_PREFIX = "\0 JDA-hack" ;
180172
181173 public static File getClassFileProxy (ClassNode cn ) {
182- return new File ('/' + HACK_PREFIX , getClassfileName ( cn ) );
174+ return new File ('/' + HACK_PREFIX , cn + ".class" );
183175 }
184176
185- public static String getClassfileName ( ClassNode cn ) {
186- return cn . name + ".class" ;
177+ public static String extractProxyClassName ( String fileName ) {
178+ return getClassFilePath ( fileName . substring ( fileName . indexOf ( HACK_PREFIX ) + HACK_PREFIX . length () + 1 )) ;
187179 }
188180
189- public static String extractProxyClassName (String fileName ) {
190- return extractClassName ( fileName .substring (fileName .indexOf ( HACK_PREFIX ) + HACK_PREFIX . length () + 1 ));
181+ public static String getClassFilePath (String fileName ) {
182+ return fileName .replace ( File . separator , "/" ). substring (0 , fileName .length () - ".class" . length ());
191183 }
192184
193- public static String extractClassName (String fileName ) {
194- return fileName . replace ( File . separator , "/" ). substring (0 , fileName . length () - 6 );
185+ public static String getClassName (String fullyQualifiedName ) {
186+ return fullyQualifiedName . substring (fullyQualifiedName . lastIndexOf ( '/' ) + 1 );
195187 }
196188
197189 protected static byte [] fixBytes (byte [] in ) {
@@ -248,6 +240,7 @@ public static ArrayList<ClassNode> loadAllClasses() {
248240 return a ;
249241 }
250242
243+ // WTF????
251244 public static Map <String , byte []> getLoadedBytes () {
252245 Map <String , byte []> data = new HashMap <>();
253246 for (FileContainer container : files ) {
@@ -281,35 +274,7 @@ public static void openFiles(final File[] files, boolean recentFiles) {
281274 update = false ;
282275 showMessage ("The file " + fileToOpen .getAbsolutePath () + " could not be found." );
283276 } else if (fileToOpen .isDirectory ()) {
284- FileContainer container = new FileContainer (fileToOpen );
285- HashMap <String , byte []> openedFiles = new HashMap <>();
286- Set <File > totalFiles = new HashSet <>();
287- Deque <File > queue = new ArrayDeque <>();
288- queue .add (fileToOpen );
289- String dir = fileToOpen .getAbsolutePath ();//f.getAbsolutePath().substring(0, f.getAbsolutePath().length()-f.getName().length());
290-
291- while (!queue .isEmpty ()) {
292- File file = queue .remove ();
293- if (!totalFiles .add (file ))
294- continue ;
295- if (file .listFiles () != null ) { // is directory
296- for (File child : file .listFiles ()) {
297- if (!totalFiles .contains (child )) {
298- queue .add (child );
299- }
300- }
301- }
302- }
303-
304- for (File file : totalFiles ) {
305- if (file .isFile ()) {
306- String fileName = file .getAbsolutePath ().substring (dir .length () + 1 , file .getAbsolutePath ().length ()).replaceAll ("\\ \\ " , "\\ /" );
307- openedFiles .put (fileName , Files .readAllBytes (Paths .get (file .getAbsolutePath ())));
308- }
309- }
310-
311- container .files = openedFiles ;
312- addFile (container );
277+ openFiles (fileToOpen .listFiles (), false );
313278 } else if (fn .endsWith (".jar" ) || fn .endsWith (".zip" )) {
314279 try {
315280 JarUtils .put (fileToOpen );
@@ -318,30 +283,12 @@ public static void openFiles(final File[] files, boolean recentFiles) {
318283 update = false ;
319284 }
320285 } else if (fn .endsWith (".class" )) {
321- try {
322- byte [] bytes = JarUtils .getBytes (new FileInputStream (fileToOpen ));
323- String cafebabe = String .format ("%02X%02X%02X%02X" , bytes [0 ], bytes [1 ], bytes [2 ], bytes [3 ]);
324- if (cafebabe .toLowerCase ().equals ("cafebabe" )) {
325- final ClassNode cn = JarUtils .getNode (bytes );
326-
327- FileContainer container = new FileContainer (fileToOpen );
328- container .files .put (getClassfileName (cn ), bytes );
329- container .add (cn );
330- addFile (container );
331- } else {
332- showMessage (fn + ": Header does not start with CAFEBABE, ignoring." );
333- update = false ;
334- }
335- } catch (final Exception e ) {
336- new ExceptionUI (e );
337- update = false ;
338- }
286+ FileContainer container = loadClassfile (fileToOpen , fn );
287+ addFile (container );
339288 } else {
340289 HashMap <String , byte []> files1 = new HashMap <>();
341290 byte [] bytes = JarUtils .getBytes (new FileInputStream (fileToOpen ));
342291 files1 .put (fileToOpen .getName (), bytes );
343-
344-
345292 FileContainer container = new FileContainer (fileToOpen );
346293 container .files = files1 ;
347294 addFile (container );
@@ -362,6 +309,27 @@ public static void openFiles(final File[] files, boolean recentFiles) {
362309 })).start ();
363310 }
364311
312+ public static FileContainer loadClassfile (File fileToOpen , String fn ) {
313+ try {
314+ byte [] bytes = JarUtils .getBytes (new FileInputStream (fileToOpen ));
315+ String cafebabe = String .format ("%02X%02X%02X%02X" , bytes [0 ], bytes [1 ], bytes [2 ], bytes [3 ]);
316+ if (cafebabe .toLowerCase ().equals ("cafebabe" )) {
317+ final ClassNode cn = JarUtils .getNode (bytes );
318+ FileContainer container = new FileContainer (fileToOpen );
319+ container .files .put (cn .name + ".class" , bytes );
320+ container .add (cn );
321+ return container ;
322+ } else {
323+ showMessage (fn + ": Header does not start with CAFEBABE, ignoring." );
324+ update = false ;
325+ }
326+ } catch (final Exception e ) {
327+ new ExceptionUI (e );
328+ update = false ;
329+ }
330+ return null ;
331+ }
332+
365333 public static void addFile (FileContainer fc ) {
366334 JDA .files .add (fc );
367335 plugins .forEach ((plugin -> plugin .onAddFile (fc )));
0 commit comments