Skip to content

Commit 0cb945a

Browse files
committed
Cosmetic updates of GL code and working prototype of NXDT
1 parent 6e21c51 commit 0cb945a

File tree

2 files changed

+83
-76
lines changed

2 files changed

+83
-76
lines changed

src/main/java/nsusbloader/Utilities/nxdumptool/NxdtUsbAbi1.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class NxdtUsbAbi1 {
4545
private final boolean isWindows;
4646
private boolean isWindows10;
4747

48-
private static final int NXDT_MAX_DIRECTIVE_SIZE = 0x1000;
48+
private static final int NXDT_MAX_DIRECTIVE_SIZE = 0x800000;//0x1000;
4949
private static final int NXDT_FILE_CHUNK_SIZE = 0x800000;
5050
private static final int NXDT_FILE_PROPERTIES_MAX_NAME_LENGTH = 0x300;
5151

@@ -212,8 +212,6 @@ private void handleSendFileProperties(byte[] message) throws Exception{
212212
String absoluteFilePath = getAbsoluteFilePath(filename);
213213
File fileToDump = new File(absoluteFilePath);
214214

215-
boolean nspTransferMode = false;
216-
217215
if (checkSizes(fullSize, headerSize))
218216
return;
219217

@@ -225,14 +223,9 @@ private void handleSendFileProperties(byte[] message) throws Exception{
225223
return;
226224

227225
if (headerSize > 0){ // if NSP
228-
logPrinter.print("Receiving NSP file entry: '"+filename+"' ("+fullSize+" b)", EMsgType.INFO);
229-
if (filename.equals(nspFile.getName())){
230-
nspTransferMode = true;
231-
}
232-
else {
233-
createNewNsp(filename, headerSize, fullSize, fileToDump);
234-
return;
235-
}
226+
logPrinter.print("Receiving NSP file: '"+filename+"' ("+formatByteSize(fullSize)+")", EMsgType.PASS);
227+
createNewNsp(filename, headerSize, fullSize, fileToDump);
228+
return;
236229
}
237230
else {
238231
// TODO: Note, in case of a big amount of small files performance decreases dramatically. It's better to handle this only in case of 1-big-file-transfer
@@ -244,7 +237,7 @@ private void handleSendFileProperties(byte[] message) throws Exception{
244237
if (fullSize == 0)
245238
return;
246239

247-
if (nspTransferMode)
240+
if (isNspTransfer())
248241
dumpNspFile(nspFile, fullSize);
249242
else
250243
dumpFile(fileToDump, fullSize);
@@ -261,8 +254,8 @@ private boolean checkFileNameLen(int fileNameLen) throws Exception{
261254
return false;
262255
}
263256
private boolean checkSizes(long fileSize, int headerSize) throws Exception{
264-
if (fileSize >= headerSize){
265-
logPrinter.print("File size should not be equal to header size for NSP files!", EMsgType.FAIL);
257+
if (headerSize >= fileSize){
258+
logPrinter.print(String.format("File size (%d) should not be less or equal to header size (%d)!", fileSize, headerSize), EMsgType.FAIL);
266259
resetNsp();
267260
writeUsb(USBSTATUS_MALFORMED_REQUEST);
268261
return true;
@@ -275,7 +268,6 @@ private boolean checkSizes(long fileSize, int headerSize) throws Exception{
275268
}
276269
return false;
277270
}
278-
279271
private boolean checkFileSystem(File fileToDump, long fileSize) throws Exception{
280272
// Check if enough space
281273
if (fileToDump.getParentFile().getFreeSpace() <= fileSize){
@@ -297,7 +289,8 @@ private void createNewNsp(String filename, int headerSize, long fileSize, File f
297289
nspFile = new NxdtNspFile(filename, headerSize, fileSize, fileToDump);
298290
}
299291
catch (Exception e){
300-
logPrinter.print(e.getMessage(), EMsgType.FAIL);
292+
logPrinter.print("Unable to create new file for: "+filename+" :"+e.getMessage(), EMsgType.FAIL);
293+
e.printStackTrace();
301294
writeUsb(USBSTATUS_HOSTIOERROR);
302295
return;
303296
}
@@ -310,6 +303,9 @@ private int getLEint(byte[] bytes, int fromOffset){
310303
private long getLElong(byte[] bytes, int fromOffset){
311304
return ByteBuffer.wrap(bytes, fromOffset, 0x8).order(ByteOrder.LITTLE_ENDIAN).getLong();
312305
}
306+
private boolean isNspTransfer(){
307+
return nspFile != null;
308+
}
313309

314310
private String getAbsoluteFilePath(String filename) throws Exception{
315311
if (isRomFs(filename) && isWindows) // Since RomFS entry starts from '/' it should be replaced to '\'.
@@ -367,9 +363,10 @@ private void dumpFile(File file, long size) throws Exception{
367363
private void dumpNspFile(NxdtNspFile nsp, long size) throws Exception{
368364
FileOutputStream fos = new FileOutputStream(nsp.getFile(), true);
369365
long nspSize = nsp.getFullSize();
370-
long nspRemainingSize = nsp.getNspRemainingSize();
371366

372367
try (BufferedOutputStream bos = new BufferedOutputStream(fos)) {
368+
long nspRemainingSize = nsp.getNspRemainingSize();
369+
373370
FileDescriptor fd = fos.getFD();
374371
byte[] readBuffer;
375372
long received = 0;
@@ -393,8 +390,6 @@ private void dumpNspFile(NxdtNspFile nsp, long size) throws Exception{
393390
if (isWindows10)
394391
fd.sync();
395392
nspRemainingSize -= (lastChunkSize - 1);
396-
}
397-
finally {
398393
nsp.setNspRemainingSize(nspRemainingSize);
399394
}
400395
}
@@ -427,7 +422,8 @@ private void handleSendNspHeader(byte[] message) throws Exception{
427422
raf.seek(0);
428423
raf.write(headerData);
429424
}
430-
425+
logPrinter.print("NSP file: '"+nsp.getName()+"' successfully received!", EMsgType.PASS);
426+
logPrinter.updateProgress(1.0);
431427
writeUsb(USBSTATUS_SUCCESS);
432428
}
433429
private void resetNsp(){
@@ -534,4 +530,13 @@ private byte[] readUsbFileDebug(int chunkSize) throws Exception {
534530
"\n Returned: " + UsbErrorCodes.getErrCode(result) +
535531
"\n (execution stopped)");
536532
}
533+
534+
private String formatByteSize(double length) {
535+
final String[] unitNames = { "bytes", "KiB", "MiB", "GiB", "TiB"};
536+
int i;
537+
for (i = 0; length > 1024 && i < unitNames.length - 1; i++) {
538+
length = length / 1024;
539+
}
540+
return String.format("%,.2f %s", length, unitNames[i]);
541+
}
537542
}

src/main/java/nsusbloader/com/usb/GoldLeaf_08.java

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,19 @@ class GoldLeaf_08 extends TransferModule {
6464
private long virtDriveSize;
6565
private HashMap<String, Long> splitFileSize;
6666

67-
private boolean isWindows;
68-
private String homePath;
67+
private final boolean isWindows;
68+
private final String homePath;
6969
// For using in CMD_SelectFile with SPEC:/ prefix
7070
private File selectedFile;
7171

72-
private CancellableRunnable task;
72+
private final CancellableRunnable task;
7373

74-
GoldLeaf_08(DeviceHandle handler, LinkedHashMap<String, File> nspMap, CancellableRunnable task, ILogPrinter logPrinter, boolean nspFilter){
74+
GoldLeaf_08(DeviceHandle handler,
75+
LinkedHashMap<String, File> nspMap,
76+
CancellableRunnable task,
77+
ILogPrinter logPrinter,
78+
boolean nspFilter)
79+
{
7580
super(handler, nspMap, task, logPrinter);
7681

7782
this.task = task;
@@ -890,43 +895,42 @@ private boolean writeFile(String fileName) {
890895
if (fileName.startsWith("VIRT:/")){
891896
return writeGL_FAIL("GL Handle 'WriteFile' command [not supported for virtual drive]");
892897
}
893-
else {
894-
fileName = updateHomePath(fileName);
895-
// Check if we didn't see this (or any) file during this session
896-
if (writeFilesMap.size() == 0 || (! writeFilesMap.containsKey(fileName))){
897-
// Open what we have to open
898-
File writeFile = new File(fileName);
899-
// If this file exists GL will take care
900-
// Otherwise, let's add it
901-
try{
902-
BufferedOutputStream writeFileBufOutStream = new BufferedOutputStream(new FileOutputStream(writeFile, true));
903-
writeFilesMap.put(fileName, writeFileBufOutStream);
904-
} catch (IOException ioe){
905-
return writeGL_FAIL("GL Handle 'WriteFile' command [IOException]\n\t"+ioe.getMessage());
906-
}
907-
}
908-
// Now we have stream
909-
BufferedOutputStream myStream = writeFilesMap.get(fileName);
910898

911-
byte[] transferredData;
912-
913-
if ((transferredData = readGL_file()) == null){
914-
logPrinter.print("GL Handle 'WriteFile' command [1/1]", EMsgType.FAIL);
915-
return true;
916-
}
899+
fileName = updateHomePath(fileName);
900+
// Check if we didn't see this (or any) file during this session
901+
if (writeFilesMap.size() == 0 || (! writeFilesMap.containsKey(fileName))){
902+
// Open what we have to open
903+
File writeFile = new File(fileName);
904+
// If this file exists GL will take care
905+
// Otherwise, let's add it
917906
try{
918-
myStream.write(transferredData, 0, transferredData.length);
919-
}
920-
catch (IOException ioe){
921-
return writeGL_FAIL("GL Handle 'WriteFile' command [1/1]\n\t"+ioe.getMessage());
907+
BufferedOutputStream writeFileBufOutStream = new BufferedOutputStream(new FileOutputStream(writeFile, true));
908+
writeFilesMap.put(fileName, writeFileBufOutStream);
909+
} catch (IOException ioe){
910+
return writeGL_FAIL("GL Handle 'WriteFile' command [IOException]\n\t"+ioe.getMessage());
922911
}
923-
// Report we're good
924-
if (writeGL_PASS()) {
925-
logPrinter.print("GL Handle 'WriteFile' command", EMsgType.FAIL);
926-
return true;
927-
}
928-
return false;
929912
}
913+
// Now we have stream
914+
BufferedOutputStream myStream = writeFilesMap.get(fileName);
915+
916+
byte[] transferredData;
917+
918+
if ((transferredData = readGL_file()) == null){
919+
logPrinter.print("GL Handle 'WriteFile' command [1/1]", EMsgType.FAIL);
920+
return true;
921+
}
922+
try{
923+
myStream.write(transferredData, 0, transferredData.length);
924+
}
925+
catch (IOException ioe){
926+
return writeGL_FAIL("GL Handle 'WriteFile' command [1/1]\n\t"+ioe.getMessage());
927+
}
928+
// Report we're good
929+
if (writeGL_PASS()) {
930+
logPrinter.print("GL Handle 'WriteFile' command", EMsgType.FAIL);
931+
return true;
932+
}
933+
return false;
930934
}
931935

932936
/**
@@ -938,27 +942,27 @@ private boolean selectFile(){
938942
File selectedFile = CompletableFuture.supplyAsync(() -> {
939943
FileChooser fChooser = new FileChooser();
940944
fChooser.setTitle(MediatorControl.getInstance().getContoller().getResourceBundle().getString("btn_OpenFile")); // TODO: FIX BAD IMPLEMENTATION
941-
fChooser.setInitialDirectory(new File(System.getProperty("user.home"))); // TODO: Consider fixing; not a prio.
945+
fChooser.setInitialDirectory(new File(System.getProperty("user.home")));// TODO: Consider fixing; not a prio.
942946
fChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("*", "*"));
943947
return fChooser.showOpenDialog(null); // Leave as is for now.
944948
}, Platform::runLater).join();
945949

946-
if (selectedFile != null){
947-
List<byte[]> command = new LinkedList<>();
948-
byte[] selectedFileNameBytes = ("SPEC:/"+selectedFile.getName()).getBytes(StandardCharsets.UTF_16LE);
949-
command.add(intToArrLE(selectedFileNameBytes.length / 2)); // since GL 0.7
950-
command.add(selectedFileNameBytes);
951-
if (writeGL_PASS(command)) {
952-
logPrinter.print("GL Handle 'SelectFile' command", EMsgType.FAIL);
953-
this.selectedFile = null;
954-
return true;
955-
}
956-
this.selectedFile = selectedFile;
957-
return false;
950+
if (selectedFile == null){ // Nothing selected
951+
this.selectedFile = null;
952+
return writeGL_FAIL("GL Handle 'SelectFile' command: Nothing selected");
958953
}
959-
// Nothing selected; Report failure.
960-
this.selectedFile = null;
961-
return writeGL_FAIL("GL Handle 'SelectFile' command: Nothing selected");
954+
955+
List<byte[]> command = new LinkedList<>();
956+
byte[] selectedFileNameBytes = ("SPEC:/"+selectedFile.getName()).getBytes(StandardCharsets.UTF_16LE);
957+
command.add(intToArrLE(selectedFileNameBytes.length / 2)); // since GL 0.7
958+
command.add(selectedFileNameBytes);
959+
if (writeGL_PASS(command)) {
960+
logPrinter.print("GL Handle 'SelectFile' command", EMsgType.FAIL);
961+
this.selectedFile = null;
962+
return true;
963+
}
964+
this.selectedFile = selectedFile;
965+
return false;
962966
}
963967

964968
/*----------------------------------------------------*/
@@ -1039,9 +1043,7 @@ private byte[] readGL(){
10391043
return null;
10401044
}
10411045
private byte[] readGL_file(){
1042-
ByteBuffer readBuffer;
1043-
readBuffer = ByteBuffer.allocateDirect(8388608); // Just don't ask..
1044-
1046+
ByteBuffer readBuffer = ByteBuffer.allocateDirect(8388608); // Just don't ask..
10451047
IntBuffer readBufTransferred = IntBuffer.allocate(1);
10461048

10471049
int result;

0 commit comments

Comments
 (0)