From 2dbcfcbd01081faa9a4652fca12c30a834c7d11e Mon Sep 17 00:00:00 2001 From: Daniel Kalinowski Date: Mon, 18 Jun 2018 12:32:11 +0200 Subject: [PATCH 1/3] Simple cli support --- pom.xml | 258 +++++++++++------------ src/us/deathmarine/luyten/FileSaver.java | 91 ++++++-- src/us/deathmarine/luyten/Luyten.java | 90 ++++++-- src/us/deathmarine/luyten/LuytenCLI.java | 42 ++++ 4 files changed, 305 insertions(+), 176 deletions(-) create mode 100644 src/us/deathmarine/luyten/LuytenCLI.java diff --git a/pom.xml b/pom.xml index 2eba1f1..4f9558a 100644 --- a/pom.xml +++ b/pom.xml @@ -1,9 +1,13 @@ - 4.0.0 us.deathmarine luyten 0.5.3 + + 3.1.2.RELEASE + com.fifesoft @@ -15,33 +19,35 @@ AppleJavaExtensions LATEST - - org.bitbucket.mstrobel - procyon-core - LATEST - - - org.bitbucket.mstrobel - procyon-expressions - LATEST - - - org.bitbucket.mstrobel - procyon-reflection - LATEST - - - org.bitbucket.mstrobel - procyon-compilertools - LATEST - - + + org.bitbucket.mstrobel + procyon-reflection + LATEST + + + org.bitbucket.mstrobel + procyon-compilertools + LATEST + + + + net.sourceforge.argparse4j + argparse4j + 0.8.1 + + + @@ -70,27 +76,12 @@ - + maven-compiler-plugin 3.1 @@ -100,54 +91,36 @@ - org.apache.maven.plugins - maven-shade-plugin - 2.4.2 - - - package - - shade - - - ${project.artifactId}-${project.version} - - - ${project.groupId}.${project.artifactId}.Luyten - - - - - - - + + - com.akathist.maven.plugins.launch4j - launch4j-maven-plugin + com.akathist.maven.plugins.launch4j + launch4j-maven-plugin 1.7.4 @@ -186,28 +159,28 @@ - - - com.googlecode.maven-download-plugin - download-maven-plugin - 1.3.0 - - - process-resources - - wget - - - https://raw.githubusercontent.com/tofi86/universalJavaApplicationStub/master/src/universalJavaApplicationStub - ${project.build.directory}/resources - - universalJavaApplicationStub.sh - - - - - + + com.googlecode.maven-download-plugin + download-maven-plugin + 1.3.0 + + + process-resources + + wget + + + https://raw.githubusercontent.com/tofi86/universalJavaApplicationStub/master/src/universalJavaApplicationStub + ${project.build.directory}/resources + + universalJavaApplicationStub.sh + + + + + + maven-antrun-plugin 1.7 @@ -219,33 +192,45 @@ - - - - - - - - - + + + + + + + + + - - - - - - + + + + + + @@ -258,10 +243,11 @@ - + - + org.eclipse.m2e lifecycle-mapping @@ -313,6 +299,6 @@ - + \ No newline at end of file diff --git a/src/us/deathmarine/luyten/FileSaver.java b/src/us/deathmarine/luyten/FileSaver.java index e6ee49b..35edd82 100644 --- a/src/us/deathmarine/luyten/FileSaver.java +++ b/src/us/deathmarine/luyten/FileSaver.java @@ -54,19 +54,24 @@ public FileSaver(JProgressBar bar, JLabel label) { this.label = label; final JPopupMenu menu = new JPopupMenu("Cancel"); final JMenuItem item = new JMenuItem("Cancel"); - item.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent arg0) { - setCancel(true); - } - }); - menu.add(item); - this.label.addMouseListener(new MouseAdapter() { - public void mouseClicked(MouseEvent ev) { - if (SwingUtilities.isRightMouseButton(ev) && isExtracting()) - menu.show(ev.getComponent(), ev.getX(), ev.getY()); - } - }); + + //Adjustments for CLI + if(bar!=null && label!=null) { + item.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent arg0) { + setCancel(true); + } + }); + menu.add(item); + this.label.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent ev) { + if (SwingUtilities.isRightMouseButton(ev) && isExtracting()) + menu.show(ev.getComponent(), ev.getX(), ev.getY()); + } + }); + } + } public void saveText(final String text, final File file) { @@ -108,13 +113,7 @@ public void run() { System.out.println("[SaveAll]: " + inFile.getName() + " -> " + outFile.getName()); String inFileName = inFile.getName().toLowerCase(); - if (inFileName.endsWith(".jar") || inFileName.endsWith(".zip")) { - doSaveJarDecompiled(inFile, outFile); - } else if (inFileName.endsWith(".class")) { - doSaveClassDecompiled(inFile, outFile); - } else { - doSaveUnknownFile(inFile, outFile); - } + performSaveOperation(inFileName,inFile,outFile); if (cancel) { label.setText("Cancelled"); outFile.delete(); @@ -132,14 +131,51 @@ public void run() { } }).start(); } + + public void saveAllDecompiledCli(final File inFile, final File outFile) { + new Thread(new Runnable() { + @Override + public void run() { + long time = System.currentTimeMillis(); + try { + System.out.println("[SaveAll]: " + inFile.getName() + " -> " + outFile.getName()); + String inFileName = inFile.getName().toLowerCase(); + performSaveOperation(inFileName,inFile,outFile); + if (cancel) { + + } else { + System.out.println("Completed: " + getTime(time)); + System.exit(0); + } + } catch (Exception e1) { + System.out.println("Cannot save file: " + outFile.getName()); + //Luyten.showExceptionDialog("Unable to save file!\n", e1); + } finally { + outFile.delete(); + } + } + }).start(); + } + private void performSaveOperation(String inFileName,File inFile,File outFile) throws Exception { + if (inFileName.endsWith(".jar") || inFileName.endsWith(".zip")) { + doSaveJarDecompiled(inFile, outFile); + } else if (inFileName.endsWith(".class")) { + doSaveClassDecompiled(inFile, outFile); + } else { + doSaveUnknownFile(inFile, outFile); + } + } private void doSaveJarDecompiled(File inFile, File outFile) throws Exception { try (JarFile jfile = new JarFile(inFile); FileOutputStream dest = new FileOutputStream(outFile); BufferedOutputStream buffDest = new BufferedOutputStream(dest); ZipOutputStream out = new ZipOutputStream(buffDest);) { - bar.setMinimum(0); - bar.setMaximum(jfile.size()); + if(bar!=null) { + bar.setMinimum(0); + bar.setMaximum(jfile.size()); + } + byte data[] = new byte[1024]; DecompilerSettings settings = cloneSettings(); LuytenTypeLoader typeLoader = new LuytenTypeLoader(); @@ -164,15 +200,22 @@ private void doSaveJarDecompiled(File inFile, File outFile) throws Exception { Set history = new HashSet(); int tick = 0; while (ent.hasMoreElements() && !cancel) { + if(bar!=null) { bar.setValue(++tick); + } JarEntry entry = ent.nextElement(); if (!mass.contains(entry.getName())) continue; + if(bar!=null) { label.setText("Extracting: " + entry.getName()); bar.setVisible(true); + } if (entry.getName().endsWith(".class")) { JarEntry etn = new JarEntry(entry.getName().replace(".class", ".java")); - label.setText("Extracting: " + etn.getName()); + if(bar!=null) { + label.setText("Extracting: " + etn.getName()); + } + System.out.println("[SaveAll]: " + etn.getName() + " -> " + outFile.getName()); if (history.add(etn.getName())) { @@ -192,8 +235,10 @@ private void doSaveJarDecompiled(File inFile, File outFile) throws Exception { settings.getLanguage().decompileType(resolvedType, plainTextOutput, decompilationOptions); writer.flush(); } catch (Exception e) { + if(bar!=null) { label.setText("Cannot decompile file: " + entry.getName()); Luyten.showExceptionDialog("Unable to Decompile file!\nSkipping file...", e); + } } finally { out.closeEntry(); } diff --git a/src/us/deathmarine/luyten/Luyten.java b/src/us/deathmarine/luyten/Luyten.java index d8139d7..3100796 100644 --- a/src/us/deathmarine/luyten/Luyten.java +++ b/src/us/deathmarine/luyten/Luyten.java @@ -16,6 +16,7 @@ import java.net.URI; import java.util.concurrent.atomic.AtomicReference; import java.util.List; +import java.util.Map; import java.util.ArrayList; import javax.swing.BorderFactory; @@ -33,6 +34,16 @@ import javax.swing.border.CompoundBorder; import javax.swing.text.DefaultEditorKit; +import net.sourceforge.argparse4j.ArgumentParsers; +import net.sourceforge.argparse4j.helper.HelpScreenException; +import net.sourceforge.argparse4j.impl.Arguments; +import net.sourceforge.argparse4j.inf.Argument; +import net.sourceforge.argparse4j.inf.ArgumentAction; +import net.sourceforge.argparse4j.inf.ArgumentParser; +import net.sourceforge.argparse4j.inf.ArgumentParserException; +import net.sourceforge.argparse4j.inf.Namespace; +import us.deathmarine.luyten.LuytenCLI.OpenAndExtractAction; + /** * Starter, the main class */ @@ -49,23 +60,51 @@ public static void main(String[] args) { e.printStackTrace(); } - // for TotalCommander External Viewer setting: - // javaw -jar "c:\Program Files\Luyten\luyten.jar" - // (TC will not complain about temporary file when opening .class from - // .zip or .jar) - final File fileFromCommandLine = getFileFromCommandLine(args); + ArgumentParser parser = ArgumentParsers.newFor("Luyten").build().defaultHelp(true).description("Java Decompiler") + .epilog("").version(getVersion()); + parser.addArgument("-v", "--version").action(Arguments.version()); + parser.addArgument("-f", "--file").help("File to open").action(Arguments.store()); + OpenAndExtractAction openAndExtractAction = new OpenAndExtractAction(); + parser.addArgument("-e", "--extract-as").help("Extract files to zip. (File-> Save as) Ex. foo-bar.zip").action(openAndExtractAction); - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - if (!mainWindowRef.compareAndSet(null, new MainWindow(fileFromCommandLine))) { - // Already set - so add the files to open - openFileInInstance(fileFromCommandLine); + Namespace res; + final int length = args.length; + try { + res = parser.parseArgs(args); + + String fileToOpen = res.getString("filea"); + + final File fileFromCommandLine = getFileFromCommandLine(fileToOpen); + + // for TotalCommander External Viewer setting: + // javaw -jar "c:\Program Files\Luyten\luyten.jar" + // (TC will not complain about temporary file when opening .class from + // .zip or .jar) + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + //System.out.println(length); + + if (!mainWindowRef.compareAndSet(null, new MainWindow(fileFromCommandLine))) { + // Already set - so add the files to open + openFileInInstance(fileFromCommandLine); + } + processPendingFiles(); + // + mainWindowRef.get().setVisible(!(length>2)); + } - processPendingFiles(); - mainWindowRef.get().setVisible(true); - } - }); + }); + + } catch (ArgumentParserException e) { + parser.handleError(e); + + // + // System.exit(1); + } catch(Exception e) { + Luyten.showExceptionDialog("Exception!", e); + } + } // Private function which processes all pending files - synchronized on the @@ -114,6 +153,23 @@ public static File getFileFromCommandLine(String[] args) { return fileFromCommandLine; } + public static File getFileFromCommandLine(String fileName) { + File fileFromCommandLine = null; + try { + if (fileName != null) { + File file = new File(fileName); + if (file != null) { + String realFileName = new File(fileName).getCanonicalPath(); + fileFromCommandLine = new File(realFileName); + } + + } + } catch (Exception e) { + e.printStackTrace(); + } + return fileFromCommandLine; + } + public static String getVersion() { String result = ""; try { @@ -133,8 +189,8 @@ public static String getVersion() { } /** - * Method allows for users to copy the stacktrace for reporting any issues. - * Add Cool Hyperlink Enhanced for mouse users. + * Method allows for users to copy the stacktrace for reporting any issues. Add + * Cool Hyperlink Enhanced for mouse users. * * @param message * @param e diff --git a/src/us/deathmarine/luyten/LuytenCLI.java b/src/us/deathmarine/luyten/LuytenCLI.java new file mode 100644 index 0000000..de3a77f --- /dev/null +++ b/src/us/deathmarine/luyten/LuytenCLI.java @@ -0,0 +1,42 @@ +package us.deathmarine.luyten; + +import java.io.File; +import java.util.Map; + +import net.sourceforge.argparse4j.inf.Argument; +import net.sourceforge.argparse4j.inf.ArgumentAction; +import net.sourceforge.argparse4j.inf.ArgumentParser; +import net.sourceforge.argparse4j.inf.ArgumentParserException; + + +public class LuytenCLI { + + + public static class OpenAndExtractAction implements ArgumentAction { + + @Override + public void run(ArgumentParser parser, Argument arg, + Map attrs, String flag, Object value) + throws ArgumentParserException { + System.out.printf("FooAction"); + System.out.printf("%s '%s' %s\n", attrs, value, flag); + attrs.put(arg.getDest(), value); + String inFileName = (String)attrs.get("file"); + File inFile=new File(inFileName); + System.out.printf("extracto to %s \n",value); + File outFile=new File((String)value); + FileSaver fileSaver=new FileSaver(null, null); + fileSaver.saveAllDecompiledCli(inFile, outFile); + + } + + @Override + public void onAttach(Argument arg) { + } + + @Override + public boolean consumeArgument() { + return true; + } + } +} From 3b8306634fb28f4eb523842f98346877e0cafee8 Mon Sep 17 00:00:00 2001 From: Daniel Kalinowski Date: Mon, 18 Jun 2018 12:37:38 +0200 Subject: [PATCH 2/3] Pom clean-up --- pom.xml | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 4f9558a..dfb16d8 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ luyten 0.5.3 - 3.1.2.RELEASE + 0.5.32 @@ -22,22 +22,22 @@ org.bitbucket.mstrobel procyon-core - LATEST + ${procyon.version} org.bitbucket.mstrobel procyon-expressions - LATEST + ${procyon.version} org.bitbucket.mstrobel procyon-reflection - LATEST + ${procyon.version} org.bitbucket.mstrobel procyon-compilertools - LATEST + ${procyon.version} @@ -46,8 +46,6 @@ 0.8.1 - From 9a2a1ecb14817f54a51344faf0fff9f833897f70 Mon Sep 17 00:00:00 2001 From: Daniel Kalinowski Date: Mon, 18 Jun 2018 12:50:29 +0200 Subject: [PATCH 3/3] Typo fix --- src/us/deathmarine/luyten/Luyten.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/us/deathmarine/luyten/Luyten.java b/src/us/deathmarine/luyten/Luyten.java index 3100796..f55e656 100644 --- a/src/us/deathmarine/luyten/Luyten.java +++ b/src/us/deathmarine/luyten/Luyten.java @@ -72,7 +72,7 @@ public static void main(String[] args) { try { res = parser.parseArgs(args); - String fileToOpen = res.getString("filea"); + String fileToOpen = res.getString("file"); final File fileFromCommandLine = getFileFromCommandLine(fileToOpen);