diff --git a/.idea/artifacts/Useful_Autoclicker.xml b/.idea/artifacts/Useful_Autoclicker.xml index 6829c36..0a6e3b3 100644 --- a/.idea/artifacts/Useful_Autoclicker.xml +++ b/.idea/artifacts/Useful_Autoclicker.xml @@ -4,7 +4,6 @@ - \ No newline at end of file diff --git a/.idea/libraries/mslinks_1_1_0.xml b/.idea/libraries/mslinks_1_1_0.xml deleted file mode 100644 index 78f5ebe..0000000 --- a/.idea/libraries/mslinks_1_1_0.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb index 1eef99d..0965408 100644 --- a/.idea/sonarlint/issuestore/index.pb +++ b/.idea/sonarlint/issuestore/index.pb @@ -1,9 +1,16 @@ F Useful-Autoclicker.iml,4\0\405ee4f65d459e8196811c48edbd71d4caf8efc2 -R -"src/fileUtilities/RegisterApp.java,d\e\dea0f984810201b8176be87916a124df350bde80 -L -src/resources/Constants.java,3\d\3db1f869f54a7c582127834c1493f5b00d64e4c3 -B -src/main/Main.java,f\e\fe03b4fd3afe162c6e01309b98a22588e093d274 \ No newline at end of file +] +-.idea/sonarlint/securityhotspotstore/index.pb,8\8\88a9255124c95bdc913197c120a8d560edc59c8e +D +src/GUI/HelpGUI.java,3\8\381fda3a9e806e954f5dcca5de27d6f16a129a2f +N +src/GUI/JComponentFactory.java,d\0\d05f2921ff975e328977efe86f48e5d45f552910 +H +src/META-INF/MANIFEST.MF,e\4\e4b1fd2293ba1e598e6bec4e0ee3b6b6851eb5a2 +D +src/Autoclicker.java,0\e\0e1464319a571b8a663e4689841c81dc0b6a1ff9 +: + +.gitignore,a\5\a5cc2925ca8258af241be7e5b0381edf30266302 \ No newline at end of file diff --git a/.idea/sonarlint/securityhotspotstore/index.pb b/.idea/sonarlint/securityhotspotstore/index.pb index 68ec96d..3444d92 100644 --- a/.idea/sonarlint/securityhotspotstore/index.pb +++ b/.idea/sonarlint/securityhotspotstore/index.pb @@ -3,9 +3,16 @@ README.md,8\e\8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d F Useful-Autoclicker.iml,4\0\405ee4f65d459e8196811c48edbd71d4caf8efc2 -R -"src/fileUtilities/RegisterApp.java,d\e\dea0f984810201b8176be87916a124df350bde80 -B -src/main/Main.java,f\e\fe03b4fd3afe162c6e01309b98a22588e093d274 -L -src/resources/Constants.java,3\d\3db1f869f54a7c582127834c1493f5b00d64e4c3 \ No newline at end of file +] +-.idea/sonarlint/securityhotspotstore/index.pb,8\8\88a9255124c95bdc913197c120a8d560edc59c8e +D +src/GUI/HelpGUI.java,3\8\381fda3a9e806e954f5dcca5de27d6f16a129a2f +N +src/GUI/JComponentFactory.java,d\0\d05f2921ff975e328977efe86f48e5d45f552910 +H +src/META-INF/MANIFEST.MF,e\4\e4b1fd2293ba1e598e6bec4e0ee3b6b6851eb5a2 +D +src/Autoclicker.java,0\e\0e1464319a571b8a663e4689841c81dc0b6a1ff9 +: + +.gitignore,a\5\a5cc2925ca8258af241be7e5b0381edf30266302 \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 899ef1a..a3ab7c3 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -10,16 +10,22 @@ + + + + + + - - + + + - - - + + + + \ No newline at end of file diff --git a/libraries/mslinks-1.1.0.jar b/libraries/mslinks-1.1.0.jar deleted file mode 100644 index a20062e..0000000 Binary files a/libraries/mslinks-1.1.0.jar and /dev/null differ diff --git a/src/Autoclicker.java b/src/Autoclicker.java index b0dff20..f0d3cf9 100644 --- a/src/Autoclicker.java +++ b/src/Autoclicker.java @@ -36,7 +36,7 @@ public boolean isRunning() { } public void start() { - Logger.trace("Starting Autoclicker"); + Logger.info("Starting Autoclicker"); autoclickerThread = new Thread(this::autoclickerMain); autoclickerThread.start(); } diff --git a/src/GUI/GUI.java b/src/GUI/GUI.java index 9df643a..7ffadea 100644 --- a/src/GUI/GUI.java +++ b/src/GUI/GUI.java @@ -2,7 +2,7 @@ import resources.Constants; import settings.Settings; -import settings.Settings; +import utils.Logger; import javax.swing.*; import javax.swing.border.Border; @@ -14,7 +14,7 @@ public class GUI extends JFrame { // title/version - private static final double AUTOCLICKER_VERSION = 2.1; + private static final double AUTOCLICKER_VERSION = 2.2; public static final String MAIN_FRAME_TITLE = "Useful Autoclicker " + AUTOCLICKER_VERSION; // components @@ -34,6 +34,7 @@ public class GUI extends JFrame { * Makes GUI */ public GUI() { + Logger.trace("Starting GUI"); JComponentFactory components = new JComponentFactory(); Settings settings = Settings.getInstance(); diff --git a/src/InputListener.java b/src/InputListener.java index 5d53f00..a13adea 100644 --- a/src/InputListener.java +++ b/src/InputListener.java @@ -7,6 +7,7 @@ import com.github.kwhat.jnativehook.mouse.NativeMouseEvent; import com.github.kwhat.jnativehook.mouse.NativeMouseListener; import settings.Settings; +import utils.ApplicationDirectory; import utils.FileVisibility; import utils.Logger; @@ -28,6 +29,7 @@ public class InputListener implements NativeKeyListener, NativeMouseListener { * @param gui GUI for making components */ public InputListener(GUI gui) { + Logger.trace("Starting InputListener"); this.gui = gui; this.clicker = new Autoclicker(this); this.settings = Settings.getInstance(); @@ -207,7 +209,7 @@ public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throw GlobalScreen.addNativeMouseListener(this); // hide JNativeHook file - FileVisibility.changeVisibility("JNativeHook.x86_64.dll", true); + FileVisibility.changeVisibility(ApplicationDirectory.getApplicationDirectory() + "JNativeHook.x86_64.dll", true); } /** diff --git a/src/Main.java b/src/Main.java index b6e342e..6c77882 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,6 +4,8 @@ import utils.OneInstance; import utils.StartMenuShortcut; +import static utils.ApplicationDirectory.getApplicationDirectory; + public class Main { /** * Starts the program @@ -12,20 +14,12 @@ public class Main { */ public static void main(String[] args) { - // CLASSES REORDER - - Logger.trace("Activating OneInstance"); OneInstance.Activate(); - Logger.trace("Getting Settings from file"); SaveSettings.initialize(); + StartMenuShortcut.createStartMenuShortcut(); - Logger.trace("Making start menu shortcut"); - new StartMenuShortcut(); - - Logger.trace("Starting GUI"); GUI gui = new GUI(); - Logger.trace("Starting InputListener"); new InputListener(gui); } } \ No newline at end of file diff --git a/src/settings/SaveSettings.java b/src/settings/SaveSettings.java index 4940806..bc36e35 100644 --- a/src/settings/SaveSettings.java +++ b/src/settings/SaveSettings.java @@ -1,5 +1,6 @@ package settings; +import utils.ApplicationDirectory; import utils.FileVisibility; import utils.Logger; @@ -10,6 +11,7 @@ public class SaveSettings { private static final String SETTING_FILE_NAME = "settings"; + private static final String SETTING_FILE_ABSOLUTE_PATH = ApplicationDirectory.getApplicationDirectory() + SETTING_FILE_NAME; private SaveSettings() { throw new IllegalStateException("Utility class"); @@ -19,6 +21,7 @@ private SaveSettings() { * Tries to load data from settings file. */ public static void initialize() { + Logger.trace("Getting Settings from file"); getSettings(); saveSettings(); // so the settings file is created if it doesn't exist yet. } @@ -29,9 +32,9 @@ public static void initialize() { */ private static void getSettings() { try { - FileVisibility.changeVisibility(SETTING_FILE_NAME, false); + FileVisibility.changeVisibility(SETTING_FILE_ABSOLUTE_PATH, false); - FileInputStream fileIn = new FileInputStream(SETTING_FILE_NAME); + FileInputStream fileIn = new FileInputStream(SETTING_FILE_ABSOLUTE_PATH); ObjectInputStream in = new ObjectInputStream(fileIn); Settings loadedSettings = (Settings) in.readObject(); @@ -40,7 +43,7 @@ private static void getSettings() { } catch (Exception e) { Logger.warn("No compatible settings file found " + e); } finally { - FileVisibility.changeVisibility(SETTING_FILE_NAME, true); + FileVisibility.changeVisibility(SETTING_FILE_ABSOLUTE_PATH, true); } } @@ -49,9 +52,9 @@ private static void getSettings() { */ public static void saveSettings() { try { - FileVisibility.changeVisibility(SETTING_FILE_NAME, false); + FileVisibility.changeVisibility(SETTING_FILE_ABSOLUTE_PATH, false); - FileOutputStream fileOut = new FileOutputStream(SETTING_FILE_NAME); + FileOutputStream fileOut = new FileOutputStream(SETTING_FILE_ABSOLUTE_PATH); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(Settings.getInstance()); @@ -59,7 +62,7 @@ public static void saveSettings() { } catch (Exception e) { Logger.error("Settings could not be saved " + e); } finally { - FileVisibility.changeVisibility(SETTING_FILE_NAME, true); + FileVisibility.changeVisibility(SETTING_FILE_ABSOLUTE_PATH, true); } } } diff --git a/src/utils/ApplicationDirectory.java b/src/utils/ApplicationDirectory.java new file mode 100644 index 0000000..4b9a062 --- /dev/null +++ b/src/utils/ApplicationDirectory.java @@ -0,0 +1,25 @@ +package utils; + +import java.io.File; + +public class ApplicationDirectory { + + private ApplicationDirectory() { + throw new IllegalStateException("Utility class"); + } + + private static String applicationDirectory = null; + + public static String getApplicationDirectory() { + if (applicationDirectory == null) { + try { + applicationDirectory = new File(StartMenuShortcut.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath() + "\\"; + Logger.info("Application directory: " + applicationDirectory); + } catch (Exception e) { + Logger.error("Failed getting application path " + e); + } + } + + return applicationDirectory; + } +} diff --git a/src/utils/FileVisibility.java b/src/utils/FileVisibility.java index db7dc52..453060c 100644 --- a/src/utils/FileVisibility.java +++ b/src/utils/FileVisibility.java @@ -31,7 +31,7 @@ public static void changeVisibility(String filename, boolean hidden) { Logger.info(filename + " is now " + (hidden ? "hidden" : "visible")); } catch (IOException e) { - Logger.warn("(FileHider) " + filename + " could not be hidden"); + Logger.error(filename + " could not be hidden " + e); } } } diff --git a/src/utils/Logger.java b/src/utils/Logger.java index 00be31e..4a1f45d 100644 --- a/src/utils/Logger.java +++ b/src/utils/Logger.java @@ -58,11 +58,20 @@ public static void fatal(String message) { } /** - * Shows an error message + * Shows an error message popup * * @param message message of the error popup */ public static void showError(String message) { JOptionPane.showMessageDialog(null, message, "Useful-Autoclicker", JOptionPane.WARNING_MESSAGE); } + + /** + * Shows an info message popup + * + * @param message message of the info popup + */ + public static void showInfo(String message) { + JOptionPane.showMessageDialog(null, message, "Useful-Autoclicker", JOptionPane.INFORMATION_MESSAGE); + } } diff --git a/src/utils/OneInstance.java b/src/utils/OneInstance.java index b973dd0..a620a66 100644 --- a/src/utils/OneInstance.java +++ b/src/utils/OneInstance.java @@ -16,7 +16,7 @@ private OneInstance() { * Checks if the program is already running. Exits if it is. */ public static void Activate() { - + Logger.trace("Activating OneInstance"); if (isRunning()) { Logger.showError("Program is already running. Exiting..."); Logger.fatal("Program is already running. Exiting..."); diff --git a/src/utils/ShortcutFactory.java b/src/utils/ShortcutFactory.java new file mode 100644 index 0000000..0cf2dc2 --- /dev/null +++ b/src/utils/ShortcutFactory.java @@ -0,0 +1,79 @@ +package utils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; + +/* + * @author Jackson N. Brienen + * Content Protected VIA GPL-2.0-only + * https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html + * Copyright (c) 2023 Jackson Nicholas Brienen + * https://github.com/JacksonBrienen/VBS-Shortcut + * + * **PLEASE DO NOT REMOVE THIS HEADER** + */ + +/** + * Simple Class for making Windows Shortcuts using a VBS script + * + * @author Jackson N. Brienen + * @version 1.2 + */ +public class ShortcutFactory { + + private ShortcutFactory() { + throw new IllegalStateException("Utility class"); + } + + /** + * Creates a Shortcut at the passed location linked to the passed source
+ * Note - this will pause thread until shortcut has been created + * + * @param source - The path to the source file to create a Shortcut to + * @param linkPath - The path of the Shortcut that will be created + * @throws FileNotFoundException if the source file cannot be found + */ + public static void createShortcut(String source, String linkPath) throws FileNotFoundException { + File sourceFile = new File(source); + if (!sourceFile.exists()) { + throw new FileNotFoundException("The Path: " + sourceFile.getAbsolutePath() + " does not exist!"); + } + try { + source = sourceFile.getAbsolutePath(); + + String vbsCode = String.format( + "Set wsObj = WScript.CreateObject(\"WScript.shell\")%n" + + "scPath = \"%s\"%n" + + "Set scObj = wsObj.CreateShortcut(scPath)%n" + + "\tscObj.TargetPath = \"%s\"%n" + + "scObj.Save%n", + linkPath, source + ); + + newVBS(vbsCode); + } catch (IOException | InterruptedException e) { + System.err.println("Could not create and run VBS!"); + e.printStackTrace(); + } + } + + /* + * Creates a VBS file with the passed code and runs it, deleting it after the run has completed + */ + private static void newVBS(String code) throws IOException, InterruptedException { + File script = File.createTempFile("scvbs", ".vbs"); // File where script will be created + + // Writes to script file + FileWriter writer = new FileWriter(script); + writer.write(code); + writer.close(); + + Process p = Runtime.getRuntime().exec("wscript \"" + script.getAbsolutePath() + "\""); // executes vbs code via cmd + p.waitFor(); // waits for process to finish + if (!script.delete()) { // deletes script + System.err.println("Warning Failed to delete temporary VBS File at: \"" + script.getAbsolutePath() + "\""); + } + } +} \ No newline at end of file diff --git a/src/utils/StartMenuShortcut.java b/src/utils/StartMenuShortcut.java index 74fba78..f49225f 100644 --- a/src/utils/StartMenuShortcut.java +++ b/src/utils/StartMenuShortcut.java @@ -1,52 +1,30 @@ package utils; -import mslinks.ShellLinkHelper; - -import javax.swing.filechooser.FileSystemView; -import java.io.File; -import java.nio.file.Paths; - public class StartMenuShortcut { + private StartMenuShortcut() { + throw new IllegalStateException("Utility class"); + } /** * Creates a shortcut in the start menu */ - public StartMenuShortcut() { + public static void createStartMenuShortcut() { + Logger.trace("Making start menu shortcut"); + try { - File windowsDrive = getWindowsDrive(); - String user = System.getProperty("user.name"); - // get data paths - String programFileName = "\\Useful-Autoclicker.jar"; - String linkFileName = "\\Useful-Autoclicker.lnk"; - String targetPath = Paths.get("").toAbsolutePath().normalize() + programFileName; - String linkPath = Paths.get(windowsDrive + "Users\\" + user + "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs").toAbsolutePath().normalize() + linkFileName; + String applicationFileName = "Useful-Autoclicker.jar"; + String shortcutFileName = "Useful-Autoclicker.lnk"; - ShellLinkHelper.createLink(targetPath, linkPath); + String shortcutTarget = ApplicationDirectory.getApplicationDirectory() + applicationFileName; + String shortcutLocation = System.getProperty("user.home") + "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\" + shortcutFileName; - Logger.info("Created shortcut in start menu"); - } catch (Exception ignored) { - } - } - /** - * Gets windows drive - * - * @return windows drive - */ - private File getWindowsDrive() { - FileSystemView fileSystemView = FileSystemView.getFileSystemView(); - File[] drives = File.listRoots(); - File windowsDrive = null; - for (File drive : drives) { - // if drive is local disk (windows disk) - String systemDriveDescription = fileSystemView.getSystemTypeDescription(drive); - if (systemDriveDescription.equals("Local Disk")) { - // set this drive to Windows Drive - windowsDrive = drive; - break; - } + ShortcutFactory.createShortcut(shortcutTarget, shortcutLocation); + + Logger.info("Created shortcut in start menu"); + } catch (Exception e) { + Logger.error("Failed making start menu shortcut " + e); } - return windowsDrive; } }