diff --git a/src/main/java/nsusbloader/COM/NET/NETCommunications.java b/src/main/java/nsusbloader/COM/NET/NETCommunications.java index 60c9dc3..2f2bbff 100644 --- a/src/main/java/nsusbloader/COM/NET/NETCommunications.java +++ b/src/main/java/nsusbloader/COM/NET/NETCommunications.java @@ -18,6 +18,7 @@ */ package nsusbloader.COM.NET; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.NSLDataTypes.EFileStatus; import nsusbloader.ModelControllers.Log; @@ -31,7 +32,7 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.*; -public class NETCommunications implements Runnable { +public class NETCommunications extends CancellableRunnable { private final ILogPrinter logPrinter; @@ -84,7 +85,7 @@ public class NETCommunications implements Runnable { @Override public void run() { - if (! isValid || Thread.interrupted() ) + if (! isValid || isCancelled() ) return; logPrinter.print("\tStart chain", EMsgType.INFO); @@ -175,7 +176,7 @@ public class NETCommunications implements Runnable { } } catch (Exception e){ - if (Thread.interrupted()) + if (isCancelled()) logPrinter.print("Interrupted by user.", EMsgType.INFO); else logPrinter.print(e.getMessage(), EMsgType.INFO); diff --git a/src/main/java/nsusbloader/COM/USB/GoldLeaf_05.java b/src/main/java/nsusbloader/COM/USB/GoldLeaf_05.java index 86dfeb7..04ae4d9 100644 --- a/src/main/java/nsusbloader/COM/USB/GoldLeaf_05.java +++ b/src/main/java/nsusbloader/COM/USB/GoldLeaf_05.java @@ -18,6 +18,7 @@ */ package nsusbloader.COM.USB; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.NSLDataTypes.EFileStatus; import nsusbloader.NSLDataTypes.EMsgType; @@ -52,8 +53,10 @@ public class GoldLeaf_05 extends TransferModule { private RandomAccessFile raf; // NSP File private NSSplitReader nsr; // It'a also NSP File - GoldLeaf_05(DeviceHandle handler, LinkedHashMap nspMap, Runnable task, ILogPrinter logPrinter){ + GoldLeaf_05(DeviceHandle handler, LinkedHashMap nspMap, CancellableRunnable task, ILogPrinter logPrinter){ super(handler, nspMap, task, logPrinter); + + this.task = task; status = EFileStatus.FAILED; logPrinter.print("============= GoldLeaf v0.5 =============\n" + @@ -334,7 +337,7 @@ public class GoldLeaf_05 extends TransferModule { IntBuffer writeBufTransferred = IntBuffer.allocate(1); int result; - while (! Thread.interrupted()) { + while (! task.isCancelled()) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint OUT = 0x01 switch (result){ @@ -367,7 +370,7 @@ public class GoldLeaf_05 extends TransferModule { IntBuffer readBufTransferred = IntBuffer.allocate(1); int result; - while (! Thread.interrupted()) { + while (! task.isCancelled()) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81 switch (result) { diff --git a/src/main/java/nsusbloader/COM/USB/GoldLeaf_07.java b/src/main/java/nsusbloader/COM/USB/GoldLeaf_07.java index eafe07a..70b733d 100644 --- a/src/main/java/nsusbloader/COM/USB/GoldLeaf_07.java +++ b/src/main/java/nsusbloader/COM/USB/GoldLeaf_07.java @@ -22,6 +22,7 @@ import javafx.application.Platform; import javafx.stage.FileChooser; import nsusbloader.COM.Helpers.NSSplitReader; import nsusbloader.MediatorControl; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.NSLDataTypes.EMsgType; import org.usb4java.DeviceHandle; @@ -68,7 +69,7 @@ class GoldLeaf_07 extends TransferModule { // For using in CMD_SelectFile with SPEC:/ prefix private File selectedFile; - GoldLeaf_07(DeviceHandle handler, LinkedHashMap nspMap, Runnable task, ILogPrinter logPrinter, boolean nspFilter){ + GoldLeaf_07(DeviceHandle handler, LinkedHashMap nspMap, CancellableRunnable task, ILogPrinter logPrinter, boolean nspFilter){ super(handler, nspMap, task, logPrinter); final byte CMD_GetDriveCount = 0x00; @@ -992,7 +993,7 @@ class GoldLeaf_07 extends TransferModule { int result; - while (! Thread.interrupted()) { + while (! task.isCancelled()) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81 switch (result) { @@ -1022,7 +1023,7 @@ class GoldLeaf_07 extends TransferModule { int result; - while (! Thread.interrupted()) { + while (! task.isCancelled()) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81 switch (result) { @@ -1082,7 +1083,7 @@ class GoldLeaf_07 extends TransferModule { IntBuffer writeBufTransferred = IntBuffer.allocate(1); int result; - while (! Thread.interrupted()) { + while (! task.isCancelled()) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint OUT = 0x01 switch (result){ diff --git a/src/main/java/nsusbloader/COM/USB/GoldLeaf_08.java b/src/main/java/nsusbloader/COM/USB/GoldLeaf_08.java index 885b6ae..723d5cb 100644 --- a/src/main/java/nsusbloader/COM/USB/GoldLeaf_08.java +++ b/src/main/java/nsusbloader/COM/USB/GoldLeaf_08.java @@ -21,6 +21,7 @@ package nsusbloader.COM.USB; import javafx.application.Platform; import javafx.stage.FileChooser; import nsusbloader.MediatorControl; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.NSLDataTypes.EMsgType; import nsusbloader.COM.Helpers.NSSplitReader; @@ -68,9 +69,13 @@ class GoldLeaf_08 extends TransferModule { // For using in CMD_SelectFile with SPEC:/ prefix private File selectedFile; - GoldLeaf_08(DeviceHandle handler, LinkedHashMap nspMap, Runnable task, ILogPrinter logPrinter, boolean nspFilter){ + private CancellableRunnable task; + + GoldLeaf_08(DeviceHandle handler, LinkedHashMap nspMap, CancellableRunnable task, ILogPrinter logPrinter, boolean nspFilter){ super(handler, nspMap, task, logPrinter); + this.task = task; + final byte CMD_GetDriveCount = 1; final byte CMD_GetDriveInfo = 2; final byte CMD_StatPath = 3; @@ -1011,7 +1016,7 @@ class GoldLeaf_08 extends TransferModule { int result; - while (! Thread.interrupted()) { + while (! task.isCancelled()) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81 switch (result) { @@ -1041,7 +1046,7 @@ class GoldLeaf_08 extends TransferModule { int result; - while (! Thread.interrupted() ) { + while (! task.isCancelled() ) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81 switch (result) { @@ -1101,7 +1106,7 @@ class GoldLeaf_08 extends TransferModule { IntBuffer writeBufTransferred = IntBuffer.allocate(1); int result; - while (! Thread.interrupted()) { + while (! task.isCancelled()) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint OUT = 0x01 switch (result){ diff --git a/src/main/java/nsusbloader/COM/USB/TinFoil.java b/src/main/java/nsusbloader/COM/USB/TinFoil.java index 3e853d6..15d2bda 100644 --- a/src/main/java/nsusbloader/COM/USB/TinFoil.java +++ b/src/main/java/nsusbloader/COM/USB/TinFoil.java @@ -18,6 +18,7 @@ */ package nsusbloader.COM.USB; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.NSLDataTypes.EFileStatus; import nsusbloader.NSLDataTypes.EMsgType; @@ -47,7 +48,7 @@ class TinFoil extends TransferModule { /* byte[] magic = new byte[4]; ByteBuffer bb = StandardCharsets.UTF_8.encode("TUC0").rewind().get(magic); // Let's rephrase this 'string' */ - TinFoil(DeviceHandle handler, LinkedHashMap nspMap, Runnable task, ILogPrinter logPrinter){ + TinFoil(DeviceHandle handler, LinkedHashMap nspMap, CancellableRunnable task, ILogPrinter logPrinter){ super(handler, nspMap, task, logPrinter); logPrinter.print("============= Tinfoil =============", EMsgType.INFO); @@ -304,7 +305,7 @@ class TinFoil extends TransferModule { IntBuffer writeBufTransferred = IntBuffer.allocate(1); int result; //int varVar = 0; //todo:remove - while (! Thread.interrupted() ) { + while (! task.isCancelled() ) { /* if (varVar != 0) logPrinter.print("writeUsb() retry cnt: "+varVar, EMsgType.INFO); //NOTE: DEBUG @@ -344,7 +345,7 @@ class TinFoil extends TransferModule { // We can limit it to 32 bytes, but there is a non-zero chance to got OVERFLOW from libusb. IntBuffer readBufTransferred = IntBuffer.allocate(1); int result; - while (! Thread.interrupted()) { + while (! task.isCancelled()) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81 switch (result) { diff --git a/src/main/java/nsusbloader/COM/USB/TransferModule.java b/src/main/java/nsusbloader/COM/USB/TransferModule.java index 27a5f38..f7a734b 100644 --- a/src/main/java/nsusbloader/COM/USB/TransferModule.java +++ b/src/main/java/nsusbloader/COM/USB/TransferModule.java @@ -18,6 +18,7 @@ */ package nsusbloader.COM.USB; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.NSLDataTypes.EFileStatus; import nsusbloader.NSLDataTypes.EMsgType; @@ -32,9 +33,9 @@ public abstract class TransferModule { LinkedHashMap nspMap; ILogPrinter logPrinter; DeviceHandle handlerNS; - Runnable task; + CancellableRunnable task; - TransferModule(DeviceHandle handler, LinkedHashMap nspMap, Runnable task, ILogPrinter printer){ + TransferModule(DeviceHandle handler, LinkedHashMap nspMap, CancellableRunnable task, ILogPrinter printer){ this.handlerNS = handler; this.nspMap = nspMap; this.task = task; diff --git a/src/main/java/nsusbloader/COM/USB/UsbCommunications.java b/src/main/java/nsusbloader/COM/USB/UsbCommunications.java index e4f5ec3..0fbd9da 100644 --- a/src/main/java/nsusbloader/COM/USB/UsbCommunications.java +++ b/src/main/java/nsusbloader/COM/USB/UsbCommunications.java @@ -18,6 +18,7 @@ */ package nsusbloader.COM.USB; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.Log; import nsusbloader.NSLDataTypes.EFileStatus; @@ -30,7 +31,7 @@ import java.io.*; import java.util.*; // TODO: add filter option to show only NSP files -public class UsbCommunications implements Runnable { +public class UsbCommunications extends CancellableRunnable { private final ILogPrinter logPrinter; private final LinkedHashMap nspMap; diff --git a/src/main/java/nsusbloader/Controllers/FrontController.java b/src/main/java/nsusbloader/Controllers/FrontController.java index c0b7c04..f14b29c 100644 --- a/src/main/java/nsusbloader/Controllers/FrontController.java +++ b/src/main/java/nsusbloader/Controllers/FrontController.java @@ -33,6 +33,7 @@ import nsusbloader.AppPreferences; import nsusbloader.COM.NET.NETCommunications; import nsusbloader.COM.USB.UsbCommunications; import nsusbloader.MediatorControl; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.NSLDataTypes.EModule; import nsusbloader.ServiceWindow; @@ -62,7 +63,7 @@ public class FrontController implements Initializable { private String previouslyOpenedPath; private Region btnUpStopImage; private ResourceBundle resourceBundle; - private Runnable usbNetCommunications; + private CancellableRunnable usbNetCommunications; private Thread workThread; @Override @@ -302,7 +303,7 @@ public class FrontController implements Initializable { * */ private void stopBtnAction(){ if (workThread != null && workThread.isAlive()){ - workThread.interrupt(); + usbNetCommunications.cancel(); if (usbNetCommunications instanceof NETCommunications){ try{ diff --git a/src/main/java/nsusbloader/Controllers/NxdtController.java b/src/main/java/nsusbloader/Controllers/NxdtController.java index 459d77d..7d696ec 100644 --- a/src/main/java/nsusbloader/Controllers/NxdtController.java +++ b/src/main/java/nsusbloader/Controllers/NxdtController.java @@ -26,6 +26,7 @@ import javafx.scene.layout.Region; import javafx.stage.DirectoryChooser; import nsusbloader.AppPreferences; import nsusbloader.MediatorControl; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.NSLDataTypes.EModule; import nsusbloader.Utilities.nxdumptool.NxdtTask; @@ -45,6 +46,7 @@ public class NxdtController implements Initializable { private Region btnDumpStopImage; private Thread workThread; + private CancellableRunnable nxdtTask; @Override public void initialize(URL url, ResourceBundle resourceBundle) { @@ -81,8 +83,8 @@ public class NxdtController implements Initializable { if ((workThread == null || ! workThread.isAlive())){ MediatorControl.getInstance().getContoller().logArea.clear(); - Runnable NxdtTask = new NxdtTask(saveToLocationLbl.getText()); - workThread = new Thread(NxdtTask); + nxdtTask = new NxdtTask(saveToLocationLbl.getText()); + workThread = new Thread(nxdtTask); workThread.setDaemon(true); workThread.start(); } @@ -93,7 +95,7 @@ public class NxdtController implements Initializable { * */ private void stopBtnAction(){ if (workThread != null && workThread.isAlive()){ - workThread.interrupt(); + nxdtTask.cancel(); } } diff --git a/src/main/java/nsusbloader/Controllers/SplitMergeController.java b/src/main/java/nsusbloader/Controllers/SplitMergeController.java index 0236a08..46be04f 100644 --- a/src/main/java/nsusbloader/Controllers/SplitMergeController.java +++ b/src/main/java/nsusbloader/Controllers/SplitMergeController.java @@ -18,7 +18,6 @@ */ package nsusbloader.Controllers; -import javafx.concurrent.Task; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.Node; @@ -31,6 +30,7 @@ import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import nsusbloader.AppPreferences; import nsusbloader.MediatorControl; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.NSLDataTypes.EModule; import nsusbloader.ServiceWindow; import nsusbloader.Utilities.splitmerge.MergeTask; @@ -61,8 +61,8 @@ public class SplitMergeController implements Initializable { private ResourceBundle resourceBundle; private Region convertRegion; - private Task smTask; private Thread smThread; + private CancellableRunnable smTask; @Override public void initialize(URL url, ResourceBundle resourceBundle) { @@ -188,8 +188,9 @@ public class SplitMergeController implements Initializable { * It's button listener when convert-process in progress * */ private void stopBtnAction(){ - if (smThread != null && smThread.isAlive()) - smTask.cancel(false); + if (smThread != null && smThread.isAlive()) { + smTask.cancel(); + } } /** * It's button listener when convert-process NOT in progress @@ -208,13 +209,6 @@ public class SplitMergeController implements Initializable { smTask = new SplitTask(fileFolderActualPathLbl.getText(), saveToPathLbl.getText()); else smTask = new MergeTask(fileFolderActualPathLbl.getText(), saveToPathLbl.getText()); - smTask.setOnCancelled(event -> statusLbl.setText(resourceBundle.getString("failure_txt"))); - smTask.setOnSucceeded(event -> { - if (smTask.getValue()) - statusLbl.setText(resourceBundle.getString("done_txt")); - else - statusLbl.setText(resourceBundle.getString("failure_txt")); - }); smThread = new Thread(smTask); smThread.setDaemon(true); smThread.start(); @@ -245,6 +239,13 @@ public class SplitMergeController implements Initializable { event.setDropCompleted(true); event.consume(); } + + public void setOneLineStatus(boolean status){ + if (status) + statusLbl.setText(resourceBundle.getString("done_txt")); + else + statusLbl.setText(resourceBundle.getString("failure_txt")); + } /** * Save application settings on exit * */ diff --git a/src/main/java/nsusbloader/ModelControllers/CancellableRunnable.java b/src/main/java/nsusbloader/ModelControllers/CancellableRunnable.java new file mode 100644 index 0000000..259985d --- /dev/null +++ b/src/main/java/nsusbloader/ModelControllers/CancellableRunnable.java @@ -0,0 +1,31 @@ +/* + Copyright 2019-2020 Dmitry Isaenko + + This file is part of NS-USBloader. + + NS-USBloader 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. + + NS-USBloader 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 NS-USBloader. If not, see . +*/ +package nsusbloader.ModelControllers; + +public abstract class CancellableRunnable implements Runnable { + private volatile boolean cancel; + + public void cancel(){ + cancel = true; + }; + + public boolean isCancelled(){ + return cancel; + }; +} \ No newline at end of file diff --git a/src/main/java/nsusbloader/ModelControllers/MessagesConsumer.java b/src/main/java/nsusbloader/ModelControllers/MessagesConsumer.java index 80e272a..25549d1 100644 --- a/src/main/java/nsusbloader/ModelControllers/MessagesConsumer.java +++ b/src/main/java/nsusbloader/ModelControllers/MessagesConsumer.java @@ -106,6 +106,9 @@ public class MessagesConsumer extends AnimationTimer { case NXDT: MediatorControl.getInstance().getContoller().getNXDTabController().setOneLineStatus(oneLinerStatus.get()); break; + case SPLIT_MERGE_TOOL: + MediatorControl.getInstance().getContoller().getSmCtrlr().setOneLineStatus(oneLinerStatus.get()); + break; } this.stop(); diff --git a/src/main/java/nsusbloader/Utilities/nxdumptool/NxdtTask.java b/src/main/java/nsusbloader/Utilities/nxdumptool/NxdtTask.java index f01428b..014f130 100644 --- a/src/main/java/nsusbloader/Utilities/nxdumptool/NxdtTask.java +++ b/src/main/java/nsusbloader/Utilities/nxdumptool/NxdtTask.java @@ -19,13 +19,14 @@ package nsusbloader.Utilities.nxdumptool; import nsusbloader.COM.USB.UsbConnect; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.Log; import nsusbloader.NSLDataTypes.EModule; import nsusbloader.NSLDataTypes.EMsgType; import org.usb4java.DeviceHandle; -public class NxdtTask implements Runnable { +public class NxdtTask extends CancellableRunnable { private final ILogPrinter logPrinter; private final String saveToLocation; @@ -49,7 +50,7 @@ public class NxdtTask implements Runnable { DeviceHandle handler = usbConnect.getNsHandler(); - new NxdtUsbAbi1(handler, logPrinter, saveToLocation); + new NxdtUsbAbi1(handler, logPrinter, saveToLocation, this); logPrinter.print(".:: Complete ::.", EMsgType.PASS); diff --git a/src/main/java/nsusbloader/Utilities/nxdumptool/NxdtUsbAbi1.java b/src/main/java/nsusbloader/Utilities/nxdumptool/NxdtUsbAbi1.java index 4d8ca3d..95b9e0d 100644 --- a/src/main/java/nsusbloader/Utilities/nxdumptool/NxdtUsbAbi1.java +++ b/src/main/java/nsusbloader/Utilities/nxdumptool/NxdtUsbAbi1.java @@ -32,9 +32,10 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; class NxdtUsbAbi1 { - private ILogPrinter logPrinter; - private DeviceHandle handlerNS; - private String saveToPath; + private final ILogPrinter logPrinter; + private final DeviceHandle handlerNS; + private final String saveToPath; + private final NxdtTask parent; private boolean isWindows; private boolean isWindows10; @@ -78,10 +79,12 @@ class NxdtUsbAbi1 { public NxdtUsbAbi1(DeviceHandle handler, ILogPrinter logPrinter, - String saveToPath + String saveToPath, + NxdtTask parent ){ this.handlerNS = handler; this.logPrinter = logPrinter; + this.parent = parent; this.isWindows = System.getProperty("os.name").toLowerCase().contains("windows"); if (isWindows) @@ -306,7 +309,7 @@ class NxdtUsbAbi1 { writeBuffer.put(message); IntBuffer writeBufTransferred = IntBuffer.allocate(1); - if ( Thread.interrupted() ) + if ( parent.isCancelled() ) throw new InterruptedException("Execution interrupted"); int result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 5050); @@ -335,7 +338,7 @@ class NxdtUsbAbi1 { // We can limit it to 32 bytes, but there is a non-zero chance to got OVERFLOW from libusb. IntBuffer readBufTransferred = IntBuffer.allocate(1); int result; - while (! Thread.interrupted()) { + while (! parent.isCancelled()) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81 switch (result) { @@ -364,7 +367,7 @@ class NxdtUsbAbi1 { IntBuffer readBufTransferred = IntBuffer.allocate(1); int result; int countDown = 0; - while (! Thread.interrupted() && countDown < 5) { + while (! parent.isCancelled() && countDown < 5) { result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); switch (result) { diff --git a/src/main/java/nsusbloader/Utilities/splitmerge/MergeTask.java b/src/main/java/nsusbloader/Utilities/splitmerge/MergeTask.java index 2517e87..4119a0f 100644 --- a/src/main/java/nsusbloader/Utilities/splitmerge/MergeTask.java +++ b/src/main/java/nsusbloader/Utilities/splitmerge/MergeTask.java @@ -18,7 +18,7 @@ */ package nsusbloader.Utilities.splitmerge; -import javafx.concurrent.Task; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.Log; import nsusbloader.NSLDataTypes.EModule; @@ -27,7 +27,7 @@ import nsusbloader.NSLDataTypes.EMsgType; import java.io.*; import java.util.Arrays; -public class MergeTask extends Task { +public class MergeTask extends CancellableRunnable { private final ILogPrinter logPrinter; private final String saveToPath; @@ -44,8 +44,9 @@ public class MergeTask extends Task { this.saveToPath = saveToPath; logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL); } + @Override - protected Boolean call() { + public void run() { try { logPrinter.print("Merge file: " + filePath, EMsgType.INFO); splitFile = new File(filePath); @@ -59,14 +60,14 @@ public class MergeTask extends Task { mergeChunksToFile(); validateFile(); - logPrinter.print("Merge task complete!", EMsgType.INFO); + logPrinter.print(".:: Merge complete ::.", EMsgType.INFO); + logPrinter.updateOneLinerStatus(true); logPrinter.close(); - return true; } catch (Exception e){ logPrinter.print(e.getMessage(), EMsgType.FAIL); + logPrinter.updateOneLinerStatus(false); logPrinter.close(); - return false; } } @@ -98,7 +99,7 @@ public class MergeTask extends Task { resultFile = new File(saveToPath+File.separator+"!_"+splitFileName); for (int i = 0; i < 50 ; i++){ - if (this.isCancelled()){ + if (isCancelled()){ throw new InterruptedException("Split task interrupted!"); } @@ -128,7 +129,7 @@ public class MergeTask extends Task { bis = new BufferedInputStream(new FileInputStream(chunkFile)); while (true){ - if (this.isCancelled()){ + if (isCancelled()){ bos.close(); bis.close(); boolean isDeleted = resultFile.delete(); diff --git a/src/main/java/nsusbloader/Utilities/splitmerge/SplitTask.java b/src/main/java/nsusbloader/Utilities/splitmerge/SplitTask.java index 8734e52..1c617b8 100644 --- a/src/main/java/nsusbloader/Utilities/splitmerge/SplitTask.java +++ b/src/main/java/nsusbloader/Utilities/splitmerge/SplitTask.java @@ -18,7 +18,7 @@ */ package nsusbloader.Utilities.splitmerge; -import javafx.concurrent.Task; +import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.Log; import nsusbloader.NSLDataTypes.EModule; @@ -27,7 +27,7 @@ import nsusbloader.NSLDataTypes.EMsgType; import java.io.*; import java.util.Arrays; -public class SplitTask extends Task { +public class SplitTask extends CancellableRunnable { private final ILogPrinter logPrinter; private final String saveToPath; @@ -40,11 +40,11 @@ public class SplitTask extends Task { public SplitTask(String filePath, String saveToPath){ this.filePath = filePath; this.saveToPath = saveToPath; - logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL); + this.logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL); } @Override - protected Boolean call() { + public void run() { try { logPrinter.print("Split file: "+filePath, EMsgType.INFO); this.file = new File(filePath); @@ -53,14 +53,14 @@ public class SplitTask extends Task { splitFileToChunks(); validateSplitFile(); - logPrinter.print("Split task complete!", EMsgType.INFO); + logPrinter.print(".:: Split complete ::.", EMsgType.INFO); + logPrinter.updateOneLinerStatus(true); logPrinter.close(); - return true; } catch (Exception e){ logPrinter.print(e.getMessage(), EMsgType.FAIL); + logPrinter.updateOneLinerStatus(false); logPrinter.close(); - return false; } } @@ -68,8 +68,7 @@ public class SplitTask extends Task { splitFile = new File(saveToPath+File.separator+"!_"+file.getName()); for (int i = 0; i < 50 ; i++){ - - if (this.isCancelled()){ + if (isCancelled()){ throw new InterruptedException("Split task interrupted!"); } @@ -112,7 +111,7 @@ public class SplitTask extends Task { while (counter < 1024){ // 0xffff0000 total - if (this.isCancelled()){ + if (isCancelled()){ fragmentBos.close(); bis.close(); boolean isDeleted = splitFile.delete(); @@ -170,6 +169,5 @@ public class SplitTask extends Task { throw new Exception("Sizes are different! Do NOT use this file for installations!"); logPrinter.print("Sizes are the same! Split file should be good!", EMsgType.PASS); - } } \ No newline at end of file diff --git a/src/main/java/nsusbloader/cli/CommandLineInterface.java b/src/main/java/nsusbloader/cli/CommandLineInterface.java index e5f931b..3d41958 100644 --- a/src/main/java/nsusbloader/cli/CommandLineInterface.java +++ b/src/main/java/nsusbloader/cli/CommandLineInterface.java @@ -78,6 +78,17 @@ public class CommandLineInterface { return; } */ + + if (cli.hasOption("s") || cli.hasOption("split")){ + final String[] arguments = cli.getOptionValues("split"); + new Split(arguments); + return; + } + if (cli.hasOption("m") || cli.hasOption("merge")){ + final String[] arguments = cli.getOptionValues("merge"); + new Merge(arguments); + return; + } } catch (ParseException pe){ System.out.println(pe.getLocalizedMessage() + @@ -138,7 +149,7 @@ public class CommandLineInterface { .longOpt("tinfoil") .desc("Install via Tinfoil/Awoo USB mode.") .hasArgs() - .argName("FILE1 ...") + .argName("FILE...") .build(); /* GoldLeaf USB */ final Option glOption = Option.builder("g") @@ -156,6 +167,18 @@ public class CommandLineInterface { .argName("DIRECTORY") .build(); */ + final Option splitOption = Option.builder("s") + .longOpt("split") + .desc("Split files. Check '-s help' for information.") + .hasArgs() + .argName("...") + .build(); + final Option mergeOption = Option.builder("m") + .longOpt("merge") + .desc("Merge files. Check '-m help' for information.") + .hasArgs() + .argName("...") + .build(); final OptionGroup group = new OptionGroup(); group.addOption(rcmOption); @@ -166,6 +189,8 @@ public class CommandLineInterface { group.addOption(tinfoilOption); group.addOption(glOption); //group.addOption(nxdtOption); + group.addOption(splitOption); + group.addOption(mergeOption); options.addOptionGroup(group); diff --git a/src/main/java/nsusbloader/cli/Merge.java b/src/main/java/nsusbloader/cli/Merge.java new file mode 100644 index 0000000..ec9c00c --- /dev/null +++ b/src/main/java/nsusbloader/cli/Merge.java @@ -0,0 +1,108 @@ +/* + Copyright 2019-2020 Dmitry Isaenko + + This file is part of NS-USBloader. + + NS-USBloader 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. + + NS-USBloader 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 NS-USBloader. If not, see . +*/ +package nsusbloader.cli; + +import nsusbloader.Utilities.splitmerge.MergeTask; +import nsusbloader.Utilities.splitmerge.SplitTask; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class Merge { + + private String[] arguments; + private String saveTo; + private String[] splitFiles; + + Merge(String[] arguments) throws InterruptedException, IncorrectSetupException{ + this.arguments = arguments; + checkArguments(); + parseArguments(); + printFilesForMerge(); + runBackend(); + } + + private void checkArguments() throws IncorrectSetupException{ + if (arguments == null || arguments.length == 0) { + throw new IncorrectSetupException("No arguments.\n" + + "Try 'ns-usbloader -m help' for more information."); + } + + if (arguments.length == 1){ + if (isHelpDirective(arguments[0])){ + showHelp(); + return; + } + + throw new IncorrectSetupException("Not enough arguments.\n" + + "Try 'ns-usbloader -m help' for more information."); + } + + this.saveTo = arguments[0]; + File saveToFile = new File(saveTo); + if (! saveToFile.exists() || saveToFile.isFile()){ + throw new IncorrectSetupException("First argument must be existing directory."); + } + } + + private boolean isHelpDirective(String argument){ + return argument.equals("help"); + } + + private void showHelp() throws IncorrectSetupException{ + throw new IncorrectSetupException("Usage:\n" + + "\tns-usbloader -m ...\n" + + "\n\nOptions:" + + "\n\tSAVE_TO_DIR\tWhere results should be saved" + + "\n\tSPLIT_FILE\tOne or more split-files (folders) to merge"); + } + + private void parseArguments() throws IncorrectSetupException{ + List files = new ArrayList<>(); + for (int i = 1; i < arguments.length; i++){ + File file = new File(arguments[i]); + if (file.isDirectory()) + files.add(file.getAbsolutePath()); + } + + if (files.isEmpty()){ + throw new IncorrectSetupException("No files specified.\n" + + "Try 'ns-usbloader -m help' for more information."); + } + + this.splitFiles = files.toArray(new String[0]); + } + + private void printFilesForMerge(){ + System.out.println("Next files will be merged:"); + for (String f : this.splitFiles) + System.out.println(" "+f); + } + + private void runBackend() throws InterruptedException{ + for (String filePath : splitFiles){ + Runnable mergeTask = new MergeTask(filePath, saveTo); + Thread thread = new Thread(mergeTask); + thread.setDaemon(true); + thread.start(); + thread.join(); + } + } +} diff --git a/src/main/java/nsusbloader/cli/Split.java b/src/main/java/nsusbloader/cli/Split.java new file mode 100644 index 0000000..816f7e8 --- /dev/null +++ b/src/main/java/nsusbloader/cli/Split.java @@ -0,0 +1,106 @@ +/* + Copyright 2019-2020 Dmitry Isaenko + + This file is part of NS-USBloader. + + NS-USBloader 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. + + NS-USBloader 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 NS-USBloader. If not, see . +*/ +package nsusbloader.cli; + +import nsusbloader.Utilities.splitmerge.SplitTask; + +import java.io.File; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class Split { + + private String[] arguments; + private String saveTo; + private String[] files; + + Split(String[] arguments) throws InterruptedException, IncorrectSetupException{ + this.arguments = arguments; + checkArguments(); + parseArguments(); + printFilesForSplit(); + runBackend(); + } + + private void checkArguments() throws IncorrectSetupException{ + if (arguments == null || arguments.length == 0) { + throw new IncorrectSetupException("No arguments.\n" + + "Try 'ns-usbloader -s help' for more information."); + } + + if (arguments.length == 1){ + if (isHelpDirective(arguments[0])){ + showHelp(); + return; + } + + throw new IncorrectSetupException("Not enough arguments.\n" + + "Try 'ns-usbloader -s help' for more information."); + } + + this.saveTo = arguments[0]; + File saveToFile = new File(saveTo); + if (! saveToFile.exists() || saveToFile.isFile()){ + throw new IncorrectSetupException("First argument must be existing directory."); + } + } + private boolean isHelpDirective(String argument){ + return argument.equals("help"); + } + private void showHelp() throws IncorrectSetupException{ + throw new IncorrectSetupException("Usage:\n" + + "\tns-usbloader -s ...\n" + + "\n\nOptions:" + + "\n\tSAVE_TO_DIR\tWhere results should be saved" + + "\n\tFILE\t\tOne or more files to split"); + } + + private void parseArguments() throws IncorrectSetupException{ + List files = new ArrayList<>(); + for (int i = 1; i < arguments.length; i++){ + File file = new File(arguments[i]); + if (file.isFile()) + files.add(file.getAbsolutePath()); + } + + if (files.isEmpty()){ + throw new IncorrectSetupException("No files specified.\n" + + "Try 'ns-usbloader -s help' for more information."); + } + + this.files = files.toArray(new String[0]); + } + + private void printFilesForSplit(){ + System.out.println("Next files will be splitted:"); + for (String f : this.files) + System.out.println(" "+f); + } + + private void runBackend() throws InterruptedException{ + for (String filePath : files){ + Runnable splitTaks = new SplitTask(filePath, saveTo); + Thread thread = new Thread(splitTaks); + thread.setDaemon(true); + thread.start(); + thread.join(); + } + } +}