Backwards compatibility

This commit is contained in:
Michael Mikovsky
2025-04-09 22:07:52 -06:00
parent 2210d8d654
commit ea41c5db24
7 changed files with 38 additions and 35 deletions
@@ -6,6 +6,7 @@ import com.ridgebotics.ridgescout.utility.ByteBuilder;
import com.ridgebotics.ridgescout.utility.FileEditor; import com.ridgebotics.ridgescout.utility.FileEditor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
public class ScoutingFile { public class ScoutingFile {
@@ -53,19 +54,11 @@ public class ScoutingFile {
ScoutingFile f = new ScoutingFile(); ScoutingFile f = new ScoutingFile();
f.filename = (String) objects.get(0).get(); f.filename = (String) objects.get(0).get();
// ByteArrayOutputStream fileData = new ByteArrayOutputStream();
//
// for(int i = 1; i < objects.size(); i++){
// byte[] blockBytes = (byte[]) objects.get(i).get();
// fileData.write(blockBytes, (i-1)*65535, blockBytes.length);
// }
f.data = (byte[]) objects.get(1).get(); f.data = (byte[]) objects.get(1).get();
return f; return f;
}catch (BuiltByteParser.byteParsingExeption e){ }catch (Exception e){
AlertManager.error(e); AlertManager.error(e);
return null; return null;
} }
@@ -27,7 +27,7 @@ public class frcMatch {
.addInt(redAlliance[1]) .addInt(redAlliance[1])
.addInt(redAlliance[2]) .addInt(redAlliance[2])
.build(); .build();
} catch (ByteBuilder.buildingException e) { } catch (Exception e) {
AlertManager.error(e); AlertManager.error(e);
return new byte[1]; return new byte[1];
} }
@@ -49,7 +49,7 @@ public class frcMatch {
return frc; return frc;
} catch (BuiltByteParser.byteParsingExeption e) { } catch (Exception e) {
AlertManager.error(e); AlertManager.error(e);
return null; return null;
} }
@@ -101,7 +101,7 @@ public class BluetoothSenderFragment extends Fragment {
private void sendData() { private void sendData() {
try { try {
byte[] compressed = FileEditor.blockCompress(data_to_send); byte[] compressed = FileEditor.blockCompress(data_to_send, FileEditor.lengthHeaderBytes);
for(int i = 0; i < Math.ceil((double) compressed.length/1024); i++){ for(int i = 0; i < Math.ceil((double) compressed.length/1024); i++){
bluetoothSender.sendData(FileEditor.getByteBlock(compressed, i*1024, (i+1)*1024)); bluetoothSender.sendData(FileEditor.getByteBlock(compressed, i*1024, (i+1)*1024));
@@ -76,7 +76,7 @@ public class CodeGeneratorView extends Fragment {
qrIndexN = binding.qrIndexN; qrIndexN = binding.qrIndexN;
qrIndexD = binding.qrIndexD; qrIndexD = binding.qrIndexD;
String compressed = new String(FileEditor.blockCompress(data), StandardCharsets.ISO_8859_1); String compressed = new String(FileEditor.blockCompress(data, FileEditor.lengthHeaderBytes), StandardCharsets.ISO_8859_1);
if(compressed.isEmpty()){ if(compressed.isEmpty()){
AlertManager.alert("Error!", "Empty data!"); AlertManager.alert("Error!", "Empty data!");
@@ -190,10 +190,10 @@ public class CodeGeneratorView extends Fragment {
try { try {
// alert("test", ""+Math.ceil((double)data.length()/(double)qrSize)); // alert("test", ""+Math.ceil((double)data.length()/(double)qrSize));
qrBitmaps.add(generateQrCode( qrBitmaps.add(generateQrCode(
FileEditor.byteToChar(FileEditor.internalDataVersion) + FileEditor.byteToChar(FileEditor.internalDataVersion, FileEditor.lengthHeaderBytes) +
String.valueOf(FileEditor.byteToChar(randID)) + String.valueOf(FileEditor.byteToChar(randID, FileEditor.lengthHeaderBytes)) +
FileEditor.byteToChar(i) + FileEditor.byteToChar(i, FileEditor.lengthHeaderBytes) +
FileEditor.byteToChar(qrCount - 1) + FileEditor.byteToChar(qrCount - 1, FileEditor.lengthHeaderBytes) +
data.substring(start, end) data.substring(start, end)
)); ));
// alert("title", ""+(qrCount-1)); // alert("title", ""+(qrCount-1));
@@ -77,26 +77,36 @@ public class BuiltByteParser {
byte[] bytes; byte[] bytes;
ArrayList<parsedObject> objects = new ArrayList<>(); ArrayList<parsedObject> objects = new ArrayList<>();
public BuiltByteParser(byte[] bytes){ public BuiltByteParser(byte[] bytes){
this.bytes = bytes; this.bytes = bytes;
} }
public ArrayList<parsedObject> parse() throws byteParsingExeption { public ArrayList<parsedObject> parse() throws byteParsingExeption {
if(bytes.length < lengthHeaderBytes + 1){throw new byteParsingExeption("Invalid length");} try{
return parse(lengthHeaderBytes); // Try decoding with new format
}catch (byteParsingExeption e){
objects.clear(); // Clear previous attempt at decoding
return parse(lengthHeaderBytes - 1); // Try parsing with old format
}
}
private ArrayList<parsedObject> parse(final int size) throws byteParsingExeption {
if(bytes.length < size + 1){throw new byteParsingExeption("Invalid length");}
int curIndex = 0; int curIndex = 0;
while(true){ while(true){
// Log.i("t", String.valueOf(curIndex)); // Log.i("t", String.valueOf(curIndex));
final int length = FileEditor.fromBytes(FileEditor.getByteBlock(bytes, curIndex, curIndex+lengthHeaderBytes), lengthHeaderBytes); final int length = FileEditor.fromBytes(FileEditor.getByteBlock(bytes, curIndex, curIndex+size), size);
final int type = bytes[curIndex+lengthHeaderBytes] & 0xFF; final int type = bytes[curIndex+size] & 0xFF;
if(length == 0){ if(length == 0){
curIndex += lengthHeaderBytes; curIndex += size;
continue; continue;
} }
final byte[] block; final byte[] block;
try { try {
block = FileEditor.getByteBlock(bytes, curIndex + lengthHeaderBytes + 1, curIndex + length + lengthHeaderBytes + 1); block = FileEditor.getByteBlock(bytes, curIndex + size + 1, curIndex + length + size + 1);
} catch(Exception e){ } catch(Exception e){
throw new byteParsingExeption("Array out of bounds"); throw new byteParsingExeption("Array out of bounds");
} }
@@ -129,7 +139,7 @@ public class BuiltByteParser {
intArrayObject ia = new intArrayObject(); intArrayObject ia = new intArrayObject();
ia.arr = intArr; ia.arr = intArr;
// System.out.println(Arrays.toString(intArr)); // System.out.println(Arrays.toString(intArr));lengthHeaderBytes
objects.add(ia); objects.add(ia);
break; break;
case 4: case 4:
@@ -159,7 +169,7 @@ public class BuiltByteParser {
break; break;
} }
curIndex += length + lengthHeaderBytes + 1; curIndex += length + size + 1;
if(curIndex == bytes.length){ if(curIndex == bytes.length){
break; break;
@@ -58,7 +58,7 @@ public class ByteBuilder {
public byte getType(){return int_id;} public byte getType(){return int_id;}
public int length(){return precision;} public int length(){return precision;}
public byte[] build(){ public byte[] build(){
return FileEditor.toBytes(num, precision); return FileEditor.toBytes(num, precision, lengthHeaderBytes);
} }
} }
private int getLeastBytePrecision(int num){ private int getLeastBytePrecision(int num){
@@ -162,7 +162,7 @@ public class ByteBuilder {
public byte getType(){return long_id;} public byte getType(){return long_id;}
public int length(){return precision;} public int length(){return precision;}
public byte[] build(){ public byte[] build(){
return FileEditor.toBytes(num, precision); return FileEditor.toBytes(num, precision, lengthHeaderBytes);
} }
} }
private int getLeastBytePrecision(long num){ private int getLeastBytePrecision(long num){
@@ -225,7 +225,7 @@ public class ByteBuilder {
for(byteType bt : bytesToBuild){ for(byteType bt : bytesToBuild){
byte[] blockLength = FileEditor.toBytes(bt.length(), lengthHeaderBytes + 1); byte[] blockLength = FileEditor.toBytes(bt.length(), lengthHeaderBytes + 1, lengthHeaderBytes);
for(int i = 0; i < lengthHeaderBytes; i++) { for(int i = 0; i < lengthHeaderBytes; i++) {
bytes[bytesFilled] = blockLength[i]; bytes[bytesFilled] = blockLength[i];
@@ -51,14 +51,14 @@ public final class FileEditor {
} }
public static char byteToChar(int num){ public static char byteToChar(int num, int headerSize){
return new String(toBytes(num, 1), StandardCharsets.ISO_8859_1).charAt(0); return new String(toBytes(num, 1, headerSize), StandardCharsets.ISO_8859_1).charAt(0);
} }
public static byte[] toBytes(int num, int byteCount){ public static byte[] toBytes(int num, int byteCount, int headerSize){
if(num > (Math.pow(lengthHeaderBytes,byteCount*8)-1)){ if(num > (Math.pow(headerSize,byteCount*8)-1)){
throw new BufferOverflowException(); throw new BufferOverflowException();
} }
byte[] bytes = new byte[byteCount]; byte[] bytes = new byte[byteCount];
@@ -68,8 +68,8 @@ public final class FileEditor {
return bytes; return bytes;
} }
public static byte[] toBytes(long num, int byteCount){ public static byte[] toBytes(long num, int byteCount, int headerSize){
if(num > (Math.pow(lengthHeaderBytes,byteCount*8)-1)){ if(num > (Math.pow(headerSize,byteCount*8)-1)){
throw new BufferOverflowException(); throw new BufferOverflowException();
} }
byte[] bytes = new byte[byteCount]; byte[] bytes = new byte[byteCount];
@@ -133,7 +133,7 @@ public final class FileEditor {
return outputStream.toByteArray(); return outputStream.toByteArray();
} }
public static byte[] blockCompress(byte[] inputData) { public static byte[] blockCompress(byte[] inputData, int headerSize) {
List<byte[]> compiledData = new ArrayList<>(); List<byte[]> compiledData = new ArrayList<>();
for(int i = 0; i<Math.ceil((double) inputData.length / FileEditor.maxCompressedBlockSize); i++){ for(int i = 0; i<Math.ceil((double) inputData.length / FileEditor.maxCompressedBlockSize); i++){
@@ -147,7 +147,7 @@ public final class FileEditor {
final byte[] compressedBlock = FileEditor.compress(dataBlock); final byte[] compressedBlock = FileEditor.compress(dataBlock);
compiledData.add(FileEditor.toBytes(compressedBlock.length, 2)); compiledData.add(FileEditor.toBytes(compressedBlock.length, 2, headerSize));
compiledData.add(compressedBlock); compiledData.add(compressedBlock);
} }
return combineByteArrays(compiledData); return combineByteArrays(compiledData);