Add Split and Merge functions to CLI
Create CanellableRunnable abstraction Change Split/Merge implementation to CanellableRunnable
This commit is contained in:
parent
3592c9086b
commit
dbb7c8e2e1
19 changed files with 358 additions and 65 deletions
|
@ -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);
|
||||
|
|
|
@ -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<String, File> nspMap, Runnable task, ILogPrinter logPrinter){
|
||||
GoldLeaf_05(DeviceHandle handler, LinkedHashMap<String, File> 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) {
|
||||
|
|
|
@ -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<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);
|
||||
|
||||
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){
|
||||
|
|
|
@ -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<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);
|
||||
|
||||
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){
|
||||
|
|
|
@ -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<String, File> nspMap, Runnable task, ILogPrinter logPrinter){
|
||||
TinFoil(DeviceHandle handler, LinkedHashMap<String, File> 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) {
|
||||
|
|
|
@ -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<String, File> nspMap;
|
||||
ILogPrinter logPrinter;
|
||||
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.nspMap = nspMap;
|
||||
this.task = task;
|
||||
|
|
|
@ -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<String, File> nspMap;
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Boolean> 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
|
||||
* */
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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<Boolean> {
|
||||
public class MergeTask extends CancellableRunnable {
|
||||
|
||||
private final ILogPrinter logPrinter;
|
||||
private final String saveToPath;
|
||||
|
@ -44,8 +44,9 @@ public class MergeTask extends Task<Boolean> {
|
|||
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<Boolean> {
|
|||
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<Boolean> {
|
|||
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<Boolean> {
|
|||
bis = new BufferedInputStream(new FileInputStream(chunkFile));
|
||||
while (true){
|
||||
|
||||
if (this.isCancelled()){
|
||||
if (isCancelled()){
|
||||
bos.close();
|
||||
bis.close();
|
||||
boolean isDeleted = resultFile.delete();
|
||||
|
|
|
@ -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<Boolean> {
|
||||
public class SplitTask extends CancellableRunnable {
|
||||
|
||||
private final ILogPrinter logPrinter;
|
||||
private final String saveToPath;
|
||||
|
@ -40,11 +40,11 @@ public class SplitTask extends Task<Boolean> {
|
|||
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<Boolean> {
|
|||
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<Boolean> {
|
|||
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<Boolean> {
|
|||
|
||||
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<Boolean> {
|
|||
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);
|
||||
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
108
src/main/java/nsusbloader/cli/Merge.java
Normal file
108
src/main/java/nsusbloader/cli/Merge.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
106
src/main/java/nsusbloader/cli/Split.java
Normal file
106
src/main/java/nsusbloader/cli/Split.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue