Add Split and Merge functions to CLI

Create CanellableRunnable abstraction
Change Split/Merge implementation to CanellableRunnable
This commit is contained in:
Dmitry Isaenko 2020-07-13 00:16:44 +03:00
parent 3592c9086b
commit dbb7c8e2e1
19 changed files with 358 additions and 65 deletions

View file

@ -18,6 +18,7 @@
*/ */
package nsusbloader.COM.NET; package nsusbloader.COM.NET;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.NSLDataTypes.EFileStatus; import nsusbloader.NSLDataTypes.EFileStatus;
import nsusbloader.ModelControllers.Log; import nsusbloader.ModelControllers.Log;
@ -31,7 +32,7 @@ import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
public class NETCommunications implements Runnable { public class NETCommunications extends CancellableRunnable {
private final ILogPrinter logPrinter; private final ILogPrinter logPrinter;
@ -84,7 +85,7 @@ public class NETCommunications implements Runnable {
@Override @Override
public void run() { public void run() {
if (! isValid || Thread.interrupted() ) if (! isValid || isCancelled() )
return; return;
logPrinter.print("\tStart chain", EMsgType.INFO); logPrinter.print("\tStart chain", EMsgType.INFO);
@ -175,7 +176,7 @@ public class NETCommunications implements Runnable {
} }
} }
catch (Exception e){ catch (Exception e){
if (Thread.interrupted()) if (isCancelled())
logPrinter.print("Interrupted by user.", EMsgType.INFO); logPrinter.print("Interrupted by user.", EMsgType.INFO);
else else
logPrinter.print(e.getMessage(), EMsgType.INFO); logPrinter.print(e.getMessage(), EMsgType.INFO);

View file

@ -18,6 +18,7 @@
*/ */
package nsusbloader.COM.USB; package nsusbloader.COM.USB;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.NSLDataTypes.EFileStatus; import nsusbloader.NSLDataTypes.EFileStatus;
import nsusbloader.NSLDataTypes.EMsgType; import nsusbloader.NSLDataTypes.EMsgType;
@ -52,8 +53,10 @@ public class GoldLeaf_05 extends TransferModule {
private RandomAccessFile raf; // NSP File private RandomAccessFile raf; // NSP File
private NSSplitReader nsr; // It'a also NSP File private NSSplitReader nsr; // It'a also NSP File
GoldLeaf_05(DeviceHandle handler, LinkedHashMap<String, File> nspMap, Runnable task, ILogPrinter logPrinter){ GoldLeaf_05(DeviceHandle handler, LinkedHashMap<String, File> nspMap, CancellableRunnable task, ILogPrinter logPrinter){
super(handler, nspMap, task, logPrinter); super(handler, nspMap, task, logPrinter);
this.task = task;
status = EFileStatus.FAILED; status = EFileStatus.FAILED;
logPrinter.print("============= GoldLeaf v0.5 =============\n" + logPrinter.print("============= GoldLeaf v0.5 =============\n" +
@ -334,7 +337,7 @@ public class GoldLeaf_05 extends TransferModule {
IntBuffer writeBufTransferred = IntBuffer.allocate(1); IntBuffer writeBufTransferred = IntBuffer.allocate(1);
int result; 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 result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint OUT = 0x01
switch (result){ switch (result){
@ -367,7 +370,7 @@ public class GoldLeaf_05 extends TransferModule {
IntBuffer readBufTransferred = IntBuffer.allocate(1); IntBuffer readBufTransferred = IntBuffer.allocate(1);
int result; 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 result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81
switch (result) { switch (result) {

View file

@ -22,6 +22,7 @@ import javafx.application.Platform;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import nsusbloader.COM.Helpers.NSSplitReader; import nsusbloader.COM.Helpers.NSSplitReader;
import nsusbloader.MediatorControl; import nsusbloader.MediatorControl;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.NSLDataTypes.EMsgType; import nsusbloader.NSLDataTypes.EMsgType;
import org.usb4java.DeviceHandle; import org.usb4java.DeviceHandle;
@ -68,7 +69,7 @@ class GoldLeaf_07 extends TransferModule {
// For using in CMD_SelectFile with SPEC:/ prefix // For using in CMD_SelectFile with SPEC:/ prefix
private File selectedFile; private File selectedFile;
GoldLeaf_07(DeviceHandle handler, LinkedHashMap<String, File> nspMap, Runnable task, ILogPrinter logPrinter, boolean nspFilter){ GoldLeaf_07(DeviceHandle handler, LinkedHashMap<String, File> nspMap, CancellableRunnable task, ILogPrinter logPrinter, boolean nspFilter){
super(handler, nspMap, task, logPrinter); super(handler, nspMap, task, logPrinter);
final byte CMD_GetDriveCount = 0x00; final byte CMD_GetDriveCount = 0x00;
@ -992,7 +993,7 @@ class GoldLeaf_07 extends TransferModule {
int result; 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 result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81
switch (result) { switch (result) {
@ -1022,7 +1023,7 @@ class GoldLeaf_07 extends TransferModule {
int result; 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 result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81
switch (result) { switch (result) {
@ -1082,7 +1083,7 @@ class GoldLeaf_07 extends TransferModule {
IntBuffer writeBufTransferred = IntBuffer.allocate(1); IntBuffer writeBufTransferred = IntBuffer.allocate(1);
int result; 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 result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint OUT = 0x01
switch (result){ switch (result){

View file

@ -21,6 +21,7 @@ package nsusbloader.COM.USB;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import nsusbloader.MediatorControl; import nsusbloader.MediatorControl;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.NSLDataTypes.EMsgType; import nsusbloader.NSLDataTypes.EMsgType;
import nsusbloader.COM.Helpers.NSSplitReader; import nsusbloader.COM.Helpers.NSSplitReader;
@ -68,9 +69,13 @@ class GoldLeaf_08 extends TransferModule {
// For using in CMD_SelectFile with SPEC:/ prefix // For using in CMD_SelectFile with SPEC:/ prefix
private File selectedFile; private File selectedFile;
GoldLeaf_08(DeviceHandle handler, LinkedHashMap<String, File> nspMap, Runnable task, ILogPrinter logPrinter, boolean nspFilter){ private CancellableRunnable task;
GoldLeaf_08(DeviceHandle handler, LinkedHashMap<String, File> nspMap, CancellableRunnable task, ILogPrinter logPrinter, boolean nspFilter){
super(handler, nspMap, task, logPrinter); super(handler, nspMap, task, logPrinter);
this.task = task;
final byte CMD_GetDriveCount = 1; final byte CMD_GetDriveCount = 1;
final byte CMD_GetDriveInfo = 2; final byte CMD_GetDriveInfo = 2;
final byte CMD_StatPath = 3; final byte CMD_StatPath = 3;
@ -1011,7 +1016,7 @@ class GoldLeaf_08 extends TransferModule {
int result; 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 result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81
switch (result) { switch (result) {
@ -1041,7 +1046,7 @@ class GoldLeaf_08 extends TransferModule {
int result; 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 result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81
switch (result) { switch (result) {
@ -1101,7 +1106,7 @@ class GoldLeaf_08 extends TransferModule {
IntBuffer writeBufTransferred = IntBuffer.allocate(1); IntBuffer writeBufTransferred = IntBuffer.allocate(1);
int result; 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 result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint OUT = 0x01
switch (result){ switch (result){

View file

@ -18,6 +18,7 @@
*/ */
package nsusbloader.COM.USB; package nsusbloader.COM.USB;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.NSLDataTypes.EFileStatus; import nsusbloader.NSLDataTypes.EFileStatus;
import nsusbloader.NSLDataTypes.EMsgType; import nsusbloader.NSLDataTypes.EMsgType;
@ -47,7 +48,7 @@ class TinFoil extends TransferModule {
/* byte[] magic = new byte[4]; /* byte[] magic = new byte[4];
ByteBuffer bb = StandardCharsets.UTF_8.encode("TUC0").rewind().get(magic); // Let's rephrase this 'string' */ ByteBuffer bb = StandardCharsets.UTF_8.encode("TUC0").rewind().get(magic); // Let's rephrase this 'string' */
TinFoil(DeviceHandle handler, LinkedHashMap<String, File> nspMap, Runnable task, ILogPrinter logPrinter){ TinFoil(DeviceHandle handler, LinkedHashMap<String, File> nspMap, CancellableRunnable task, ILogPrinter logPrinter){
super(handler, nspMap, task, logPrinter); super(handler, nspMap, task, logPrinter);
logPrinter.print("============= Tinfoil =============", EMsgType.INFO); logPrinter.print("============= Tinfoil =============", EMsgType.INFO);
@ -304,7 +305,7 @@ class TinFoil extends TransferModule {
IntBuffer writeBufTransferred = IntBuffer.allocate(1); IntBuffer writeBufTransferred = IntBuffer.allocate(1);
int result; int result;
//int varVar = 0; //todo:remove //int varVar = 0; //todo:remove
while (! Thread.interrupted() ) { while (! task.isCancelled() ) {
/* /*
if (varVar != 0) if (varVar != 0)
logPrinter.print("writeUsb() retry cnt: "+varVar, EMsgType.INFO); //NOTE: DEBUG 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. // We can limit it to 32 bytes, but there is a non-zero chance to got OVERFLOW from libusb.
IntBuffer readBufTransferred = IntBuffer.allocate(1); IntBuffer readBufTransferred = IntBuffer.allocate(1);
int result; 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 result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81
switch (result) { switch (result) {

View file

@ -18,6 +18,7 @@
*/ */
package nsusbloader.COM.USB; package nsusbloader.COM.USB;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.NSLDataTypes.EFileStatus; import nsusbloader.NSLDataTypes.EFileStatus;
import nsusbloader.NSLDataTypes.EMsgType; import nsusbloader.NSLDataTypes.EMsgType;
@ -32,9 +33,9 @@ public abstract class TransferModule {
LinkedHashMap<String, File> nspMap; LinkedHashMap<String, File> nspMap;
ILogPrinter logPrinter; ILogPrinter logPrinter;
DeviceHandle handlerNS; DeviceHandle handlerNS;
Runnable task; CancellableRunnable task;
TransferModule(DeviceHandle handler, LinkedHashMap<String, File> nspMap, Runnable task, ILogPrinter printer){ TransferModule(DeviceHandle handler, LinkedHashMap<String, File> nspMap, CancellableRunnable task, ILogPrinter printer){
this.handlerNS = handler; this.handlerNS = handler;
this.nspMap = nspMap; this.nspMap = nspMap;
this.task = task; this.task = task;

View file

@ -18,6 +18,7 @@
*/ */
package nsusbloader.COM.USB; package nsusbloader.COM.USB;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.ModelControllers.Log; import nsusbloader.ModelControllers.Log;
import nsusbloader.NSLDataTypes.EFileStatus; import nsusbloader.NSLDataTypes.EFileStatus;
@ -30,7 +31,7 @@ import java.io.*;
import java.util.*; import java.util.*;
// TODO: add filter option to show only NSP files // 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 ILogPrinter logPrinter;
private final LinkedHashMap<String, File> nspMap; private final LinkedHashMap<String, File> nspMap;

View file

@ -33,6 +33,7 @@ import nsusbloader.AppPreferences;
import nsusbloader.COM.NET.NETCommunications; import nsusbloader.COM.NET.NETCommunications;
import nsusbloader.COM.USB.UsbCommunications; import nsusbloader.COM.USB.UsbCommunications;
import nsusbloader.MediatorControl; import nsusbloader.MediatorControl;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.NSLDataTypes.EModule; import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.ServiceWindow; import nsusbloader.ServiceWindow;
@ -62,7 +63,7 @@ public class FrontController implements Initializable {
private String previouslyOpenedPath; private String previouslyOpenedPath;
private Region btnUpStopImage; private Region btnUpStopImage;
private ResourceBundle resourceBundle; private ResourceBundle resourceBundle;
private Runnable usbNetCommunications; private CancellableRunnable usbNetCommunications;
private Thread workThread; private Thread workThread;
@Override @Override
@ -302,7 +303,7 @@ public class FrontController implements Initializable {
* */ * */
private void stopBtnAction(){ private void stopBtnAction(){
if (workThread != null && workThread.isAlive()){ if (workThread != null && workThread.isAlive()){
workThread.interrupt(); usbNetCommunications.cancel();
if (usbNetCommunications instanceof NETCommunications){ if (usbNetCommunications instanceof NETCommunications){
try{ try{

View file

@ -26,6 +26,7 @@ import javafx.scene.layout.Region;
import javafx.stage.DirectoryChooser; import javafx.stage.DirectoryChooser;
import nsusbloader.AppPreferences; import nsusbloader.AppPreferences;
import nsusbloader.MediatorControl; import nsusbloader.MediatorControl;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.NSLDataTypes.EModule; import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.Utilities.nxdumptool.NxdtTask; import nsusbloader.Utilities.nxdumptool.NxdtTask;
@ -45,6 +46,7 @@ public class NxdtController implements Initializable {
private Region btnDumpStopImage; private Region btnDumpStopImage;
private Thread workThread; private Thread workThread;
private CancellableRunnable nxdtTask;
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
@ -81,8 +83,8 @@ public class NxdtController implements Initializable {
if ((workThread == null || ! workThread.isAlive())){ if ((workThread == null || ! workThread.isAlive())){
MediatorControl.getInstance().getContoller().logArea.clear(); MediatorControl.getInstance().getContoller().logArea.clear();
Runnable NxdtTask = new NxdtTask(saveToLocationLbl.getText()); nxdtTask = new NxdtTask(saveToLocationLbl.getText());
workThread = new Thread(NxdtTask); workThread = new Thread(nxdtTask);
workThread.setDaemon(true); workThread.setDaemon(true);
workThread.start(); workThread.start();
} }
@ -93,7 +95,7 @@ public class NxdtController implements Initializable {
* */ * */
private void stopBtnAction(){ private void stopBtnAction(){
if (workThread != null && workThread.isAlive()){ if (workThread != null && workThread.isAlive()){
workThread.interrupt(); nxdtTask.cancel();
} }
} }

View file

@ -18,7 +18,6 @@
*/ */
package nsusbloader.Controllers; package nsusbloader.Controllers;
import javafx.concurrent.Task;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.Node; import javafx.scene.Node;
@ -31,6 +30,7 @@ import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import nsusbloader.AppPreferences; import nsusbloader.AppPreferences;
import nsusbloader.MediatorControl; import nsusbloader.MediatorControl;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.NSLDataTypes.EModule; import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.ServiceWindow; import nsusbloader.ServiceWindow;
import nsusbloader.Utilities.splitmerge.MergeTask; import nsusbloader.Utilities.splitmerge.MergeTask;
@ -61,8 +61,8 @@ public class SplitMergeController implements Initializable {
private ResourceBundle resourceBundle; private ResourceBundle resourceBundle;
private Region convertRegion; private Region convertRegion;
private Task<Boolean> smTask;
private Thread smThread; private Thread smThread;
private CancellableRunnable smTask;
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { 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 * It's button listener when convert-process in progress
* */ * */
private void stopBtnAction(){ private void stopBtnAction(){
if (smThread != null && smThread.isAlive()) if (smThread != null && smThread.isAlive()) {
smTask.cancel(false); smTask.cancel();
}
} }
/** /**
* It's button listener when convert-process NOT in progress * 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()); smTask = new SplitTask(fileFolderActualPathLbl.getText(), saveToPathLbl.getText());
else else
smTask = new MergeTask(fileFolderActualPathLbl.getText(), saveToPathLbl.getText()); 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 = new Thread(smTask);
smThread.setDaemon(true); smThread.setDaemon(true);
smThread.start(); smThread.start();
@ -245,6 +239,13 @@ public class SplitMergeController implements Initializable {
event.setDropCompleted(true); event.setDropCompleted(true);
event.consume(); 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 * Save application settings on exit
* */ * */

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
package nsusbloader.ModelControllers;
public abstract class CancellableRunnable implements Runnable {
private volatile boolean cancel;
public void cancel(){
cancel = true;
};
public boolean isCancelled(){
return cancel;
};
}

View file

@ -106,6 +106,9 @@ public class MessagesConsumer extends AnimationTimer {
case NXDT: case NXDT:
MediatorControl.getInstance().getContoller().getNXDTabController().setOneLineStatus(oneLinerStatus.get()); MediatorControl.getInstance().getContoller().getNXDTabController().setOneLineStatus(oneLinerStatus.get());
break; break;
case SPLIT_MERGE_TOOL:
MediatorControl.getInstance().getContoller().getSmCtrlr().setOneLineStatus(oneLinerStatus.get());
break;
} }
this.stop(); this.stop();

View file

@ -19,13 +19,14 @@
package nsusbloader.Utilities.nxdumptool; package nsusbloader.Utilities.nxdumptool;
import nsusbloader.COM.USB.UsbConnect; import nsusbloader.COM.USB.UsbConnect;
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.ModelControllers.Log; import nsusbloader.ModelControllers.Log;
import nsusbloader.NSLDataTypes.EModule; import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.NSLDataTypes.EMsgType; import nsusbloader.NSLDataTypes.EMsgType;
import org.usb4java.DeviceHandle; import org.usb4java.DeviceHandle;
public class NxdtTask implements Runnable { public class NxdtTask extends CancellableRunnable {
private final ILogPrinter logPrinter; private final ILogPrinter logPrinter;
private final String saveToLocation; private final String saveToLocation;
@ -49,7 +50,7 @@ public class NxdtTask implements Runnable {
DeviceHandle handler = usbConnect.getNsHandler(); DeviceHandle handler = usbConnect.getNsHandler();
new NxdtUsbAbi1(handler, logPrinter, saveToLocation); new NxdtUsbAbi1(handler, logPrinter, saveToLocation, this);
logPrinter.print(".:: Complete ::.", EMsgType.PASS); logPrinter.print(".:: Complete ::.", EMsgType.PASS);

View file

@ -32,9 +32,10 @@ import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
class NxdtUsbAbi1 { class NxdtUsbAbi1 {
private ILogPrinter logPrinter; private final ILogPrinter logPrinter;
private DeviceHandle handlerNS; private final DeviceHandle handlerNS;
private String saveToPath; private final String saveToPath;
private final NxdtTask parent;
private boolean isWindows; private boolean isWindows;
private boolean isWindows10; private boolean isWindows10;
@ -78,10 +79,12 @@ class NxdtUsbAbi1 {
public NxdtUsbAbi1(DeviceHandle handler, public NxdtUsbAbi1(DeviceHandle handler,
ILogPrinter logPrinter, ILogPrinter logPrinter,
String saveToPath String saveToPath,
NxdtTask parent
){ ){
this.handlerNS = handler; this.handlerNS = handler;
this.logPrinter = logPrinter; this.logPrinter = logPrinter;
this.parent = parent;
this.isWindows = System.getProperty("os.name").toLowerCase().contains("windows"); this.isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
if (isWindows) if (isWindows)
@ -306,7 +309,7 @@ class NxdtUsbAbi1 {
writeBuffer.put(message); writeBuffer.put(message);
IntBuffer writeBufTransferred = IntBuffer.allocate(1); IntBuffer writeBufTransferred = IntBuffer.allocate(1);
if ( Thread.interrupted() ) if ( parent.isCancelled() )
throw new InterruptedException("Execution interrupted"); throw new InterruptedException("Execution interrupted");
int result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 5050); 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. // We can limit it to 32 bytes, but there is a non-zero chance to got OVERFLOW from libusb.
IntBuffer readBufTransferred = IntBuffer.allocate(1); IntBuffer readBufTransferred = IntBuffer.allocate(1);
int result; 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 result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81
switch (result) { switch (result) {
@ -364,7 +367,7 @@ class NxdtUsbAbi1 {
IntBuffer readBufTransferred = IntBuffer.allocate(1); IntBuffer readBufTransferred = IntBuffer.allocate(1);
int result; int result;
int countDown = 0; int countDown = 0;
while (! Thread.interrupted() && countDown < 5) { while (! parent.isCancelled() && countDown < 5) {
result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000); result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 1000);
switch (result) { switch (result) {

View file

@ -18,7 +18,7 @@
*/ */
package nsusbloader.Utilities.splitmerge; package nsusbloader.Utilities.splitmerge;
import javafx.concurrent.Task; import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.ModelControllers.Log; import nsusbloader.ModelControllers.Log;
import nsusbloader.NSLDataTypes.EModule; import nsusbloader.NSLDataTypes.EModule;
@ -27,7 +27,7 @@ import nsusbloader.NSLDataTypes.EMsgType;
import java.io.*; import java.io.*;
import java.util.Arrays; import java.util.Arrays;
public class MergeTask extends Task<Boolean> { public class MergeTask extends CancellableRunnable {
private final ILogPrinter logPrinter; private final ILogPrinter logPrinter;
private final String saveToPath; private final String saveToPath;
@ -44,8 +44,9 @@ public class MergeTask extends Task<Boolean> {
this.saveToPath = saveToPath; this.saveToPath = saveToPath;
logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL); logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL);
} }
@Override @Override
protected Boolean call() { public void run() {
try { try {
logPrinter.print("Merge file: " + filePath, EMsgType.INFO); logPrinter.print("Merge file: " + filePath, EMsgType.INFO);
splitFile = new File(filePath); splitFile = new File(filePath);
@ -59,14 +60,14 @@ public class MergeTask extends Task<Boolean> {
mergeChunksToFile(); mergeChunksToFile();
validateFile(); validateFile();
logPrinter.print("Merge task complete!", EMsgType.INFO); logPrinter.print(".:: Merge complete ::.", EMsgType.INFO);
logPrinter.updateOneLinerStatus(true);
logPrinter.close(); logPrinter.close();
return true;
} }
catch (Exception e){ catch (Exception e){
logPrinter.print(e.getMessage(), EMsgType.FAIL); logPrinter.print(e.getMessage(), EMsgType.FAIL);
logPrinter.updateOneLinerStatus(false);
logPrinter.close(); logPrinter.close();
return false;
} }
} }
@ -98,7 +99,7 @@ public class MergeTask extends Task<Boolean> {
resultFile = new File(saveToPath+File.separator+"!_"+splitFileName); resultFile = new File(saveToPath+File.separator+"!_"+splitFileName);
for (int i = 0; i < 50 ; i++){ for (int i = 0; i < 50 ; i++){
if (this.isCancelled()){ if (isCancelled()){
throw new InterruptedException("Split task interrupted!"); throw new InterruptedException("Split task interrupted!");
} }
@ -128,7 +129,7 @@ public class MergeTask extends Task<Boolean> {
bis = new BufferedInputStream(new FileInputStream(chunkFile)); bis = new BufferedInputStream(new FileInputStream(chunkFile));
while (true){ while (true){
if (this.isCancelled()){ if (isCancelled()){
bos.close(); bos.close();
bis.close(); bis.close();
boolean isDeleted = resultFile.delete(); boolean isDeleted = resultFile.delete();

View file

@ -18,7 +18,7 @@
*/ */
package nsusbloader.Utilities.splitmerge; package nsusbloader.Utilities.splitmerge;
import javafx.concurrent.Task; import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.ModelControllers.Log; import nsusbloader.ModelControllers.Log;
import nsusbloader.NSLDataTypes.EModule; import nsusbloader.NSLDataTypes.EModule;
@ -27,7 +27,7 @@ import nsusbloader.NSLDataTypes.EMsgType;
import java.io.*; import java.io.*;
import java.util.Arrays; import java.util.Arrays;
public class SplitTask extends Task<Boolean> { public class SplitTask extends CancellableRunnable {
private final ILogPrinter logPrinter; private final ILogPrinter logPrinter;
private final String saveToPath; private final String saveToPath;
@ -40,11 +40,11 @@ public class SplitTask extends Task<Boolean> {
public SplitTask(String filePath, String saveToPath){ public SplitTask(String filePath, String saveToPath){
this.filePath = filePath; this.filePath = filePath;
this.saveToPath = saveToPath; this.saveToPath = saveToPath;
logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL); this.logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL);
} }
@Override @Override
protected Boolean call() { public void run() {
try { try {
logPrinter.print("Split file: "+filePath, EMsgType.INFO); logPrinter.print("Split file: "+filePath, EMsgType.INFO);
this.file = new File(filePath); this.file = new File(filePath);
@ -53,14 +53,14 @@ public class SplitTask extends Task<Boolean> {
splitFileToChunks(); splitFileToChunks();
validateSplitFile(); validateSplitFile();
logPrinter.print("Split task complete!", EMsgType.INFO); logPrinter.print(".:: Split complete ::.", EMsgType.INFO);
logPrinter.updateOneLinerStatus(true);
logPrinter.close(); logPrinter.close();
return true;
} }
catch (Exception e){ catch (Exception e){
logPrinter.print(e.getMessage(), EMsgType.FAIL); logPrinter.print(e.getMessage(), EMsgType.FAIL);
logPrinter.updateOneLinerStatus(false);
logPrinter.close(); logPrinter.close();
return false;
} }
} }
@ -68,8 +68,7 @@ public class SplitTask extends Task<Boolean> {
splitFile = new File(saveToPath+File.separator+"!_"+file.getName()); splitFile = new File(saveToPath+File.separator+"!_"+file.getName());
for (int i = 0; i < 50 ; i++){ for (int i = 0; i < 50 ; i++){
if (isCancelled()){
if (this.isCancelled()){
throw new InterruptedException("Split task interrupted!"); throw new InterruptedException("Split task interrupted!");
} }
@ -112,7 +111,7 @@ public class SplitTask extends Task<Boolean> {
while (counter < 1024){ // 0xffff0000 total while (counter < 1024){ // 0xffff0000 total
if (this.isCancelled()){ if (isCancelled()){
fragmentBos.close(); fragmentBos.close();
bis.close(); bis.close();
boolean isDeleted = splitFile.delete(); boolean isDeleted = splitFile.delete();
@ -170,6 +169,5 @@ public class SplitTask extends Task<Boolean> {
throw new Exception("Sizes are different! Do NOT use this file for installations!"); 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); logPrinter.print("Sizes are the same! Split file should be good!", EMsgType.PASS);
} }
} }

View file

@ -78,6 +78,17 @@ public class CommandLineInterface {
return; 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){ catch (ParseException pe){
System.out.println(pe.getLocalizedMessage() + System.out.println(pe.getLocalizedMessage() +
@ -138,7 +149,7 @@ public class CommandLineInterface {
.longOpt("tinfoil") .longOpt("tinfoil")
.desc("Install via Tinfoil/Awoo USB mode.") .desc("Install via Tinfoil/Awoo USB mode.")
.hasArgs() .hasArgs()
.argName("FILE1 ...") .argName("FILE...")
.build(); .build();
/* GoldLeaf USB */ /* GoldLeaf USB */
final Option glOption = Option.builder("g") final Option glOption = Option.builder("g")
@ -156,6 +167,18 @@ public class CommandLineInterface {
.argName("DIRECTORY") .argName("DIRECTORY")
.build(); .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(); final OptionGroup group = new OptionGroup();
group.addOption(rcmOption); group.addOption(rcmOption);
@ -166,6 +189,8 @@ public class CommandLineInterface {
group.addOption(tinfoilOption); group.addOption(tinfoilOption);
group.addOption(glOption); group.addOption(glOption);
//group.addOption(nxdtOption); //group.addOption(nxdtOption);
group.addOption(splitOption);
group.addOption(mergeOption);
options.addOptionGroup(group); options.addOptionGroup(group);

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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 <SAVE_TO_DIR> <SPLIT_FILE>...\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<String> 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();
}
}
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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 <SAVE_TO_DIR> <FILE>...\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<String> 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();
}
}
}