Keep simplifying GL flow

This commit is contained in:
Dmitry Isaenko 2025-08-20 00:13:02 +03:00
parent 0ce56eb9b4
commit cf7d54d8af

View file

@ -37,22 +37,22 @@ import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
/** /**
* GoldLeaf 0.8 processing * GoldLeaf 1.1.1 processing
*/ */
class GoldLeaf_111 extends TransferModule { class GoldLeaf_111 extends TransferModule {
private final boolean nspFilterForGl; private final boolean nspFilterForGl;
// CMD // CMD
private final byte[] CMD_GLCO_SUCCESS = new byte[]{0x47, 0x4c, 0x43, 0x4F, 0x00, 0x00, 0x00, 0x00}; // used @ writeToUsb_GLCMD private final byte[] CMD_GLCO_SUCCESS = new byte[]{0x47, 0x4c, 0x43, 0x4F, 0x00, 0x00, 0x00, 0x00}; // used @ writeToUsb_GLCMD
private final byte[] CMD_GLCO_FAILURE = new byte[]{0x47, 0x4c, 0x43, 0x4F, 0x00, 0x00, (byte) 0xAD, (byte) 0xDE}; // used @ writeToUsb_GLCMD TODO: TEST private final byte[] CMD_GLCO_FAILURE = new byte[]{0x47, 0x4c, 0x43, 0x4F, 0x00, 0x00, (byte) 0xAD, (byte) 0xDE}; // used @ writeToUsb_GLCMD
// System.out.println((356 & 0x1FF) | ((1 + 100) & 0x1FFF) << 9); // 52068 // 0x00 0x00 0xCB 0x64 // System.out.println((356 & 0x1FF) | ((1 + 100) & 0x1FFF) << 9); // 52068 // 0x00 0x00 0xCB 0x64
private final byte[] GL_OBJ_TYPE_FILE = new byte[]{0x01, 0x00, 0x00, 0x00}; private final byte[] GL_OBJ_TYPE_FILE = new byte[]{0x01, 0x00, 0x00, 0x00};
private final byte[] GL_OBJ_TYPE_DIR = new byte[]{0x02, 0x00, 0x00, 0x00}; private final byte[] GL_OBJ_TYPE_DIR = new byte[]{0x02, 0x00, 0x00, 0x00};
private String recentPath = null; private String recentPath;
private String[] recentDirs = null; private String[] recentDirs;
private String[] recentFiles = null; private String[] recentFiles;
private final String[] nspMapKeySetIndexes; private final String[] nspMapKeySetIndexes;
@ -236,17 +236,16 @@ class GoldLeaf_111 extends TransferModule {
if (startOrEndFile()) if (startOrEndFile())
break main_loop; break main_loop;
break; break;
case CMD_UNKNOWN:
default: default:
writeGL_FAIL("GL Unknown command: "+readByte[4]+" [it's a very bad sign]"); writeGL_FAIL("GL Unknown command: "+readByte[4]+" [it's a very bad sign]");
} }
} }
// Close (and flush) all opened streams. // Close (and flush) all opened streams.
if (! writeFilesMap.isEmpty()){ for (var bufferedOutputStream: writeFilesMap.values()){
for (var bufferedOutputStream: writeFilesMap.values()){ try{
try{ bufferedOutputStream.close();
bufferedOutputStream.close(); } catch (IOException | NullPointerException ignored){}
} catch (IOException | NullPointerException ignored){}
}
} }
closeOpenedReadFilesGl(); closeOpenedReadFilesGl();
} }
@ -258,31 +257,36 @@ class GoldLeaf_111 extends TransferModule {
* Close files opened for read/write * Close files opened for read/write
*/ */
private void closeOpenedReadFilesGl(){ private void closeOpenedReadFilesGl(){
if (openReadFileNameAndPath != null){ // Perfect time to close our opened files if (openReadFileNameAndPath != null){
try{ closeRAFandSplitReader();
randAccessFile.close();
}
catch (IOException | NullPointerException ignored){}
try{
splitReader.close();
}
catch (IOException | NullPointerException ignored){}
openReadFileNameAndPath = null; openReadFileNameAndPath = null;
randAccessFile = null; randAccessFile = null;
splitReader = null; splitReader = null;
} }
} }
private void closeRAFandSplitReader(){
try{
randAccessFile.close();
}
catch (IOException ioe_){
print("Unable to close: "+openReadFileNameAndPath+"\n\t"+ioe_.getMessage(), EMsgType.WARNING);
}
catch (Exception ignored){}
try{
splitReader.close();
}
catch (IOException ioe_){
print("Unable to close: "+openReadFileNameAndPath+"\n\t"+ioe_.getMessage(), EMsgType.WARNING);
}
catch (Exception ignored){}
}
/** /**
* Handle StartFile & EndFile * Handle StartFile & EndFile
* NOTE: It's something internal for GL and used somehow by GL-PC-app, so just ignore this, at least for v0.8. * NOTE: It's something internal for GL and used somehow by GL-PC-app, so just ignore this, at least for v0.8.
* @return true - failed, false - passed * @return true - failed, false - passed
* */ * */
private boolean startOrEndFile(){ private boolean startOrEndFile(){
if (writeGL_PASS()){ return writeGL_PASS("GL Handle 'StartFile' command");
print("GL Handle 'StartFile' command", EMsgType.FAIL);
return true;
}
return false;
} }
/** /**
* Handle GetDriveCount * Handle GetDriveCount
@ -290,11 +294,7 @@ class GoldLeaf_111 extends TransferModule {
* @return true - failed, false - passed * @return true - failed, false - passed
*/ */
private boolean getDriveCount(){ private boolean getDriveCount(){
if (writeGL_PASS(intToArrLE(2))) { return writeGL_PASS(intToArrLE(2),"GL Handle 'ListDrives' command");
print("GL Handle 'ListDrives' command", EMsgType.FAIL);
return true;
}
return false;
} }
/** /**
* Handle GetDriveInfo * Handle GetDriveInfo
@ -315,16 +315,16 @@ class GoldLeaf_111 extends TransferModule {
if (driveNo == 0){ // 0 == VIRTUAL DRIVE if (driveNo == 0){ // 0 == VIRTUAL DRIVE
driveLabel = "Virtual".getBytes(StandardCharsets.UTF_8); driveLabel = "Virtual".getBytes(StandardCharsets.UTF_8);
driveLabelLen = intToArrLE(driveLabel.length); driveLabelLen = intToArrLE(driveLabel.length);
driveLetter = "VIRT".getBytes(StandardCharsets.UTF_8); // TODO: Consider moving to class field declaration driveLetter = "VIRT".getBytes(StandardCharsets.UTF_8);
driveLetterLen = intToArrLE(driveLetter.length);// since GL 0.7 driveLetterLen = intToArrLE(driveLetter.length);
totalFreeSpace = new byte[4]; totalFreeSpace = new byte[4];
totalSizeLong = virtDriveSize; totalSizeLong = virtDriveSize;
} }
else { //1 == User home dir else { //1 == User home dir
driveLabel = "Home".getBytes(StandardCharsets.UTF_8); driveLabel = "Home".getBytes(StandardCharsets.UTF_8);
driveLabelLen = intToArrLE(driveLabel.length);// since GL 0.7 driveLabelLen = intToArrLE(driveLabel.length);
driveLetter = "HOME".getBytes(StandardCharsets.UTF_8); driveLetter = "HOME".getBytes(StandardCharsets.UTF_8);
driveLetterLen = intToArrLE(driveLetter.length);// since GL 0.7 driveLetterLen = intToArrLE(driveLetter.length);
var userHomeDir = new File(System.getProperty("user.home")); var userHomeDir = new File(System.getProperty("user.home"));
totalFreeSpace = Arrays.copyOfRange(longToArrLE(userHomeDir.getFreeSpace()), 0, 4);; totalFreeSpace = Arrays.copyOfRange(longToArrLE(userHomeDir.getFreeSpace()), 0, 4);;
totalSizeLong = userHomeDir.getTotalSpace(); totalSizeLong = userHomeDir.getTotalSpace();
@ -339,24 +339,15 @@ class GoldLeaf_111 extends TransferModule {
totalFreeSpace, totalFreeSpace,
totalSize); totalSize);
if (writeGL_PASS(command)) { return writeGL_PASS(command, "GL Handle 'GetDriveInfo' command");
print("GL Handle 'GetDriveInfo' command", EMsgType.FAIL);
return true;
}
return false;
} }
/** /**
* Handle SpecialPathCount * Handle SpecialPathCount
* Let's declare nothing. Write count of special paths
* @return true - failed, false - passed * @return true - failed, false - passed
* */ * */
private boolean getSpecialPathCount(){ private boolean getSpecialPathCount(){
// Let's declare nothing =) Write count of special paths return writeGL_PASS(intToArrLE(0), "GL Handle 'SpecialPathCount' command");
if (writeGL_PASS(intToArrLE(0))) {
print("GL Handle 'SpecialPathCount' command", EMsgType.FAIL);
return true;
}
return false;
} }
/** /**
* Handle SpecialPath * Handle SpecialPath
@ -371,18 +362,9 @@ class GoldLeaf_111 extends TransferModule {
* */ * */
private boolean getDirectoryOrFileCount(String path, boolean isGetDirectoryCount) { private boolean getDirectoryOrFileCount(String path, boolean isGetDirectoryCount) {
if (path.equals("VIRT:/")) { if (path.equals("VIRT:/")) {
if (isGetDirectoryCount){ return isGetDirectoryCount ?
if (writeGL_PASS()) { writeGL_PASS("GL Handle 'GetDirectoryCount' command") :
print("GL Handle 'GetDirectoryCount' command", EMsgType.FAIL); writeGL_PASS(intToArrLE(nspMap.size()), "GL Handle 'GetFileCount' command Count = " + nspMap.size());
return true;
}
}
else {
if (writeGL_PASS(intToArrLE(nspMap.size()))) {
print("GL Handle 'GetFileCount' command Count = "+nspMap.size(), EMsgType.FAIL);
return true;
}
}
} }
else if (path.startsWith("HOME:/")){ else if (path.startsWith("HOME:/")){
// Let's make it normal path // Let's make it normal path
@ -394,15 +376,10 @@ class GoldLeaf_111 extends TransferModule {
return writeGL_FAIL("GL Handle 'GetDirectoryOrFileCount' command [doesn't exist or not a folder]"); return writeGL_FAIL("GL Handle 'GetDirectoryOrFileCount' command [doesn't exist or not a folder]");
// Save recent dir path // Save recent dir path
this.recentPath = path; this.recentPath = path;
String[] filesOrDirs = getFilesOrDirs(isGetDirectoryCount, pathDir); var filesOrDirs = getFilesOrDirs(isGetDirectoryCount, pathDir);
// If somehow there are no folders, let's say 0; // If somehow there are no folders, let's say 0;
if (filesOrDirs == null){ if (filesOrDirs == null)
if (writeGL_PASS()) { return writeGL_PASS("GL Handle 'GetDirectoryOrFileCount' command");
print("GL Handle 'GetDirectoryOrFileCount' command", EMsgType.FAIL);
return true;
}
return false;
}
// Sorting is mandatory NOTE: Proxy tail // Sorting is mandatory NOTE: Proxy tail
Arrays.sort(filesOrDirs, String.CASE_INSENSITIVE_ORDER); Arrays.sort(filesOrDirs, String.CASE_INSENSITIVE_ORDER);
@ -411,31 +388,17 @@ class GoldLeaf_111 extends TransferModule {
else else
this.recentFiles = filesOrDirs; this.recentFiles = filesOrDirs;
// Otherwise, let's tell how may folders are in there // Otherwise, let's tell how may folders are in there
if (writeGL_PASS(intToArrLE(filesOrDirs.length))) { return writeGL_PASS(intToArrLE(filesOrDirs.length), "GL Handle 'GetDirectoryOrFileCount' command");
print("GL Handle 'GetDirectoryOrFileCount' command", EMsgType.FAIL);
return true;
}
} }
else if (path.startsWith("SPEC:/")){ else if (path.startsWith("SPEC:/")){
if (isGetDirectoryCount){ // If dir request then 0 dirs if (isGetDirectoryCount) // If dir request then 0 dirs
if (writeGL_PASS()) { return writeGL_PASS("GL Handle 'GetDirectoryCount' command");
print("GL Handle 'GetDirectoryCount' command", EMsgType.FAIL); else if (selectedFile != null) // Else it's file request, if we have selected then we will report 1.
return true; return writeGL_PASS(intToArrLE(1), "GL Handle 'GetFileCount' command Count = 1");
} return writeGL_FAIL("GL Handle 'GetDirectoryOrFileCount' command [unknown drive request] (file) - "+path);
}
else if (selectedFile != null){ // Else it's file request, if we have selected then we will report 1.
if (writeGL_PASS(intToArrLE(1))) {
print("GL Handle 'GetFileCount' command Count = 1", EMsgType.FAIL);
return true;
}
}
else
return writeGL_FAIL("GL Handle 'GetDirectoryOrFileCount' command [unknown drive request] (file) - "+path);
} }
else { // If requested drive is not VIRT and not HOME then reply error // If requested drive is not VIRT and not HOME then reply error
return writeGL_FAIL("GL Handle 'GetDirectoryOrFileCount' command [unknown drive request] "+(isGetDirectoryCount?"(dir) - ":"(file) - ")+path); return writeGL_FAIL("GL Handle 'GetDirectoryOrFileCount' command [unknown drive request] "+(isGetDirectoryCount?"(dir) - ":"(file) - ")+path);
}
return false;
} }
private String[] getFilesOrDirs(boolean isGetDirectoryCount, File pathDir) { private String[] getFilesOrDirs(boolean isGetDirectoryCount, File pathDir) {
String[] filesOrDirs; String[] filesOrDirs;
@ -447,18 +410,12 @@ class GoldLeaf_111 extends TransferModule {
}); });
} }
else { else {
if (nspFilterForGl){ filesOrDirs = pathDir.list((current, name) -> {
filesOrDirs = pathDir.list((current, name) -> { var dir = new File(current, name);
var dir = new File(current, name); return (! dir.isDirectory() && nspFilterForGl ?
return (! dir.isDirectory() && name.toLowerCase().endsWith(".nsp")); name.toLowerCase().endsWith(".nsp") :
! dir.isHidden());
}); });
}
else {
filesOrDirs = pathDir.list((current, name) -> {
var dir = new File(current, name);
return (! dir.isDirectory() && (! dir.isHidden()));
});
}
} }
return filesOrDirs; return filesOrDirs;
} }
@ -499,12 +456,7 @@ class GoldLeaf_111 extends TransferModule {
else else
return writeGL_FAIL("GL Handle 'GetDirectory' command [doesn't exist or not a folder]"); return writeGL_FAIL("GL Handle 'GetDirectory' command [doesn't exist or not a folder]");
} }
return writeGL_PASS(command, "GL Handle 'GetDirectory' command.");
if (writeGL_PASS(command)) {
print("GL Handle 'GetDirectory' command.", EMsgType.FAIL);
return true;
}
return false;
} }
// VIRT:// and any other // VIRT:// and any other
return writeGL_FAIL("GL Handle 'GetDirectory' command for virtual drive [no folders support]"); return writeGL_FAIL("GL Handle 'GetDirectory' command for virtual drive [no folders support]");
@ -520,9 +472,8 @@ class GoldLeaf_111 extends TransferModule {
dirName = updateHomePath(dirName); dirName = updateHomePath(dirName);
if (dirName.equals(recentPath) && recentFiles != null && recentFiles.length != 0){ if (dirName.equals(recentPath) && recentFiles != null && recentFiles.length != 0){
byte[] fileNameBytes = recentFiles[subDirNo].getBytes(StandardCharsets.UTF_8); var fileNameBytes = recentFiles[subDirNo].getBytes(StandardCharsets.UTF_8);
command.add(intToArrLE(fileNameBytes.length));
command.add(intToArrLE(fileNameBytes.length)); //Since GL 0.7
command.add(fileNameBytes); command.add(fileNameBytes);
} }
else { else {
@ -535,55 +486,38 @@ class GoldLeaf_111 extends TransferModule {
if (nspFilterForGl){ if (nspFilterForGl){
this.recentFiles = pathDir.list((current, name) -> { this.recentFiles = pathDir.list((current, name) -> {
var dir = new File(current, name); var dir = new File(current, name);
return (! dir.isDirectory() && name.toLowerCase().endsWith(".nsp")); // TODO: FIX FOR WIN ? MOVE TO PROD return (! dir.isDirectory() && name.toLowerCase().endsWith(".nsp"));
}); });
} }
else { else {
this.recentFiles = pathDir.list((current, name) -> { this.recentFiles = pathDir.list((current, name) -> {
var dir = new File(current, name); var dir = new File(current, name);
return (! dir.isDirectory() && (! dir.isHidden())); // TODO: FIX FOR WIN return (! dir.isDirectory() && (! dir.isHidden()));
}); });
} }
// Check that we still don't have any fuckups // Check that we still don't have any fuckups
if (this.recentFiles != null && this.recentFiles.length > subDirNo){ if (this.recentFiles != null && this.recentFiles.length > subDirNo){
Arrays.sort(recentFiles, String.CASE_INSENSITIVE_ORDER); // TODO: NOTE: array sorting is an overhead for using poxy loops Arrays.sort(recentFiles, String.CASE_INSENSITIVE_ORDER);
var fileNameBytes = recentFiles[subDirNo].getBytes(StandardCharsets.UTF_8); var fileNameBytes = recentFiles[subDirNo].getBytes(StandardCharsets.UTF_8);
command.add(intToArrLE(fileNameBytes.length)); //Since GL 0.7 command.add(intToArrLE(fileNameBytes.length));
command.add(fileNameBytes); command.add(fileNameBytes);
} }
else else
return writeGL_FAIL("GL Handle 'GetFile' command [doesn't exist or not a folder]"); return writeGL_FAIL("GL Handle 'GetFile' command [doesn't exist or not a folder]");
} }
return writeGL_PASS(command, "GL Handle 'GetFile' command.");
if (writeGL_PASS(command)) {
print("GL Handle 'GetFile' command.", EMsgType.FAIL);
return true;
}
return false;
} }
else if (dirName.equals("VIRT:/")){ else if (dirName.equals("VIRT:/") && (! nspMap.isEmpty())){ // thus nspMapKeySetIndexes also != 0
if (! nspMap.isEmpty()){ // therefore nspMapKeySetIndexes also != 0 var fileNameBytes = nspMapKeySetIndexes[subDirNo].getBytes(StandardCharsets.UTF_8);
var fileNameBytes = nspMapKeySetIndexes[subDirNo].getBytes(StandardCharsets.UTF_8); command.add(intToArrLE(fileNameBytes.length));
command.add(intToArrLE(fileNameBytes.length)); command.add(fileNameBytes);
command.add(fileNameBytes); return writeGL_PASS(command, "GL Handle 'GetFile' command.");
if (writeGL_PASS(command)) {
print("GL Handle 'GetFile' command.", EMsgType.FAIL);
return true;
}
return false;
}
} }
else if (dirName.equals("SPEC:/")){ else if (dirName.equals("SPEC:/") && (selectedFile != null)){
if (selectedFile != null){ byte[] fileNameBytes = selectedFile.getName().getBytes(StandardCharsets.UTF_8);
byte[] fileNameBytes = selectedFile.getName().getBytes(StandardCharsets.UTF_8); command.add(intToArrLE(fileNameBytes.length));
command.add(intToArrLE(fileNameBytes.length)); command.add(fileNameBytes);
command.add(fileNameBytes); return writeGL_PASS(command, "GL Handle 'GetFile' command.");
if (writeGL_PASS(command)) {
print("GL Handle 'GetFile' command.", EMsgType.FAIL);
return true;
}
return false;
}
} }
// any other cases // any other cases
return writeGL_FAIL("GL Handle 'GetFile' command for virtual drive [no folders support?]"); return writeGL_FAIL("GL Handle 'GetFile' command for virtual drive [no folders support?]");
@ -606,11 +540,7 @@ class GoldLeaf_111 extends TransferModule {
command.add(GL_OBJ_TYPE_FILE); command.add(GL_OBJ_TYPE_FILE);
command.add(longToArrLE(fileDirElement.length())); command.add(longToArrLE(fileDirElement.length()));
} }
if (writeGL_PASS(command)) { return writeGL_PASS(command, "GL Handle 'StatPath' command.");
print("GL Handle 'StatPath' command.", EMsgType.FAIL);
return true;
}
return false;
} }
} }
else if (filePath.startsWith("VIRT:/")) { else if (filePath.startsWith("VIRT:/")) {
@ -623,11 +553,7 @@ class GoldLeaf_111 extends TransferModule {
else else
command.add(longToArrLE(nspMap.get(filePath).length())); // YES, THIS IS LONG! command.add(longToArrLE(nspMap.get(filePath).length())); // YES, THIS IS LONG!
if (writeGL_PASS(command)) { return writeGL_PASS(command, "GL Handle 'StatPath' command.");
print("GL Handle 'StatPath' command.", EMsgType.FAIL);
return true;
}
return false;
} }
} }
else if (filePath.startsWith("SPEC:/")){ else if (filePath.startsWith("SPEC:/")){
@ -636,11 +562,7 @@ class GoldLeaf_111 extends TransferModule {
if (selectedFile.getName().equals(filePath)){ if (selectedFile.getName().equals(filePath)){
command.add(GL_OBJ_TYPE_FILE); command.add(GL_OBJ_TYPE_FILE);
command.add(longToArrLE(selectedFile.length())); command.add(longToArrLE(selectedFile.length()));
if (writeGL_PASS(command)) { return writeGL_PASS(command, "GL Handle 'StatPath' command.");
print("GL Handle 'StatPath' command.", EMsgType.FAIL);
return true;
}
return false;
} }
} }
return writeGL_FAIL("GL Handle 'StatPath' command [no such folder] - "+filePath); return writeGL_FAIL("GL Handle 'StatPath' command [no such folder] - "+filePath);
@ -662,11 +584,7 @@ class GoldLeaf_111 extends TransferModule {
if (! newFile.exists()){ // Else, report error if (! newFile.exists()){ // Else, report error
try { try {
if (new File(fileName).renameTo(newFile)){ if (new File(fileName).renameTo(newFile)){
if (writeGL_PASS()) { return writeGL_PASS("GL Handle 'Rename' command.");
print("GL Handle 'Rename' command.", EMsgType.FAIL);
return true;
}
return false;
} }
} }
catch (SecurityException ignored){} // Ah, leave it catch (SecurityException ignored){} // Ah, leave it
@ -686,11 +604,7 @@ class GoldLeaf_111 extends TransferModule {
File fileToDel = new File(fileName); File fileToDel = new File(fileName);
try { try {
if (fileToDel.delete()){ if (fileToDel.delete()){
if (writeGL_PASS()) { return writeGL_PASS("GL Handle 'Rename' command.");
print("GL Handle 'Rename' command.", EMsgType.FAIL);
return true;
}
return false;
} }
} }
catch (SecurityException ignored){} // Ah, leave it catch (SecurityException ignored){} // Ah, leave it
@ -721,12 +635,7 @@ class GoldLeaf_111 extends TransferModule {
catch (SecurityException | IOException ignored){} catch (SecurityException | IOException ignored){}
if (result) { if (result) {
if (writeGL_PASS()) { return writeGL_PASS("GL Handle 'Create' command.");
print("GL Handle 'Create' command.", EMsgType.FAIL);
return true;
}
//print("GL Handle 'Create' command.", EMsgType.PASS);
return false;
} }
return writeGL_FAIL("GL Handle 'Delete' command [not supported for virtual drive/wrong drive/read-only directory]"); return writeGL_FAIL("GL Handle 'Delete' command [not supported for virtual drive/wrong drive/read-only directory]");
@ -747,14 +656,8 @@ class GoldLeaf_111 extends TransferModule {
// If we don't have this file opened, let's open it // If we don't have this file opened, let's open it
if (openReadFileNameAndPath == null || (! openReadFileNameAndPath.equals(fNamePath))) { if (openReadFileNameAndPath == null || (! openReadFileNameAndPath.equals(fNamePath))) {
// Try close what opened // Try close what opened
if (openReadFileNameAndPath != null){ if (openReadFileNameAndPath != null)
try{ closeRAFandSplitReader();
randAccessFile.close();
}catch (Exception ignored){}
try{
splitReader.close();
}catch (Exception ignored){}
}
// Open what has to be opened // Open what has to be opened
try{ try{
var tempFile = nspMap.get(fileName.substring(6)); var tempFile = nspMap.get(fileName.substring(6));
@ -782,7 +685,7 @@ class GoldLeaf_111 extends TransferModule {
if (openReadFileNameAndPath != null){ if (openReadFileNameAndPath != null){
try{ try{
randAccessFile.close(); randAccessFile.close();
}catch (IOException | NullPointerException ignored){} }catch (Exception ignored){}
} }
// Open what has to be opened // Open what has to be opened
try{ try{
@ -812,8 +715,7 @@ class GoldLeaf_111 extends TransferModule {
"\n At offset: " + offset + "\n At offset: " + offset +
"\n Requested: " + size + "\n Requested: " + size +
"\n Received: " + bytesRead); "\n Received: " + bytesRead);
if (writeGL_PASS(longToArrLE(size))) { // Let's tell as a command about our result. if (writeGL_PASS(longToArrLE(size), "GL Handle 'ReadFile' command [CMD]")) { // Let's tell as a command about our result.
print("GL Handle 'ReadFile' command [CMD]", EMsgType.FAIL);
return true; return true;
} }
if (writeToUsb(chunk)) { // Let's bypass bytes we read total if (writeToUsb(chunk)) { // Let's bypass bytes we read total
@ -823,23 +725,7 @@ class GoldLeaf_111 extends TransferModule {
return false; return false;
} }
catch (Exception ioe){ catch (Exception ioe){
try{ closeOpenedReadFilesGl();
randAccessFile.close();
}
catch (NullPointerException ignored){}
catch (IOException ioe_){
print("GL Handle 'ReadFile' command: unable to close: "+openReadFileNameAndPath+"\n\t"+ioe_.getMessage(), EMsgType.WARNING);
}
try{
splitReader.close();
}
catch (NullPointerException ignored){}
catch (IOException ioe_){
print("GL Handle 'ReadFile' command: unable to close: "+openReadFileNameAndPath+"\n\t"+ioe_.getMessage(), EMsgType.WARNING);
}
openReadFileNameAndPath = null;
randAccessFile = null;
splitReader = null;
return writeGL_FAIL("GL Handle 'ReadFile' command\n\t"+ioe.getMessage()); return writeGL_FAIL("GL Handle 'ReadFile' command\n\t"+ioe.getMessage());
} }
} }
@ -855,7 +741,7 @@ class GoldLeaf_111 extends TransferModule {
fileName = updateHomePath(fileName); fileName = updateHomePath(fileName);
// Check if this file being used during this session // Check if this file being used during this session
if (writeFilesMap.isEmpty() || (! writeFilesMap.containsKey(fileName))){ if (! writeFilesMap.containsKey(fileName)){
try{ // If this file exists GL will take care; Otherwise, let's add it try{ // If this file exists GL will take care; Otherwise, let's add it
writeFilesMap.put(fileName, writeFilesMap.put(fileName,
new BufferedOutputStream(new FileOutputStream(fileName, true))); // Open what we have to open new BufferedOutputStream(new FileOutputStream(fileName, true))); // Open what we have to open
@ -877,11 +763,7 @@ class GoldLeaf_111 extends TransferModule {
return writeGL_FAIL("GL Handle 'WriteFile' command [1/1]\n\t"+ioe.getMessage()); return writeGL_FAIL("GL Handle 'WriteFile' command [1/1]\n\t"+ioe.getMessage());
} }
// Report we're good // Report we're good
if (writeGL_PASS()) { return writeGL_PASS("GL Handle 'WriteFile' command");
print("GL Handle 'WriteFile' command", EMsgType.FAIL);
return true;
}
return false;
} }
/** /**
@ -906,8 +788,7 @@ class GoldLeaf_111 extends TransferModule {
var command = Arrays.asList( var command = Arrays.asList(
intToArrLE(selectedFileNameBytes.length), intToArrLE(selectedFileNameBytes.length),
selectedFileNameBytes); selectedFileNameBytes);
if (writeGL_PASS(command)) { if (writeGL_PASS(command, "GL Handle 'SelectFile' command")) {
print("GL Handle 'SelectFile' command", EMsgType.FAIL);
this.selectedFile = null; this.selectedFile = null;
return true; return true;
} }
@ -995,7 +876,7 @@ class GoldLeaf_111 extends TransferModule {
switch (result) { switch (result) {
case LibUsb.SUCCESS: case LibUsb.SUCCESS:
byte[] receivedBytes = new byte[readBufTransferred.get()]; var receivedBytes = new byte[readBufTransferred.get()];
readBuffer.get(receivedBytes); readBuffer.get(receivedBytes);
return receivedBytes; return receivedBytes;
case LibUsb.ERROR_TIMEOUT: case LibUsb.ERROR_TIMEOUT:
@ -1011,31 +892,44 @@ class GoldLeaf_111 extends TransferModule {
return null; return null;
} }
/** /**
* Write new command. Shitty implementation. * Write new command
* */ * */
private boolean writeGL_PASS(byte[] message){ private boolean writeGL_PASS(String onFailureText){
return writeToUsb( if (writeToUsb(Arrays.copyOf(CMD_GLCO_SUCCESS, 4096))){
ByteBuffer.allocate(4096) print(onFailureText, EMsgType.FAIL);
.put(CMD_GLCO_SUCCESS) return true;
.put(message) }
.array()); return false;
} }
private boolean writeGL_PASS(){ private boolean writeGL_PASS(byte[] message, String onFailureText){
return writeToUsb(Arrays.copyOf(CMD_GLCO_SUCCESS, 4096)); var result = writeToUsb(ByteBuffer.allocate(4096)
.put(CMD_GLCO_SUCCESS)
.put(message)
.array());
if(result){
print(onFailureText, EMsgType.FAIL);
return true;
}
return false;
} }
private boolean writeGL_PASS(List<byte[]> messages){ private boolean writeGL_PASS(List<byte[]> messages, String onFailureText){
var writeBuffer = ByteBuffer.allocate(4096) var writeBuffer = ByteBuffer.allocate(4096)
.put(CMD_GLCO_SUCCESS); .put(CMD_GLCO_SUCCESS);
messages.forEach(writeBuffer::put); messages.forEach(writeBuffer::put);
return writeToUsb(writeBuffer.array()); if (writeToUsb(writeBuffer.array())){
} print(onFailureText, EMsgType.FAIL);
private boolean writeGL_FAIL(String reportToUImsg){
if (writeToUsb(Arrays.copyOf(CMD_GLCO_FAILURE, 4096))){
print(reportToUImsg, EMsgType.WARNING);
return true; return true;
} }
print(reportToUImsg, EMsgType.FAIL); return false;
}
private boolean writeGL_FAIL(String failureMessage){
if (writeToUsb(Arrays.copyOf(CMD_GLCO_FAILURE, 4096))){
print(failureMessage, EMsgType.WARNING);
return true;
}
print(failureMessage, EMsgType.FAIL);
return false; return false;
} }
/** /**
@ -1044,14 +938,15 @@ class GoldLeaf_111 extends TransferModule {
* 'true' if errors happened * 'true' if errors happened
* */ * */
private boolean writeToUsb(byte[] message){ private boolean writeToUsb(byte[] message){
//System.out.println(">");
//RainbowHexDump.hexDumpUTF16LE(message); // DEBUG //RainbowHexDump.hexDumpUTF16LE(message); // DEBUG
var writeBuffer = ByteBuffer.allocateDirect(message.length); //writeBuffer.order() equals BIG_ENDIAN;
writeBuffer.put(message); // Don't do writeBuffer.rewind();
var writeBufTransferred = IntBuffer.allocate(1); var writeBufTransferred = IntBuffer.allocate(1);
while (! task.isCancelled()) { while (! task.isCancelled()) {
int result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 1000); // last one is TIMEOUT. 0 stands for unlimited. Endpoint OUT = 0x01 int result = LibUsb.bulkTransfer(handlerNS,
(byte) 0x01,
ByteBuffer.allocateDirect(message.length).put(message), // order -> BIG_ENDIAN; Don't writeBuffer.rewind();
writeBufTransferred,
1000); // TIMEOUT. 0 stands for infinite. Endpoint OUT = 0x01
switch (result){ switch (result){
case LibUsb.SUCCESS: case LibUsb.SUCCESS: