From ffde5308a6d80150f8460ff15f39294763fa92f9 Mon Sep 17 00:00:00 2001 From: MonsterDruide1 <5958456@gmail.com> Date: Fri, 3 Nov 2023 10:37:48 +0100 Subject: [PATCH] STAS: Fix saving scripts ending with empty line --- .../java/io/github/jadefalke2/script/STas.java | 15 ++++++++++----- .../io/github/jadefalke2/util/ByteDataStream.java | 12 ++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/github/jadefalke2/script/STas.java b/src/main/java/io/github/jadefalke2/script/STas.java index 86792ef..a0ad37d 100644 --- a/src/main/java/io/github/jadefalke2/script/STas.java +++ b/src/main/java/io/github/jadefalke2/script/STas.java @@ -138,8 +138,10 @@ public static byte[] write(Script script) { commands.add(new FrameCommand(i)); commands.add(new ControllerCommand((byte) 0, inputLines[i])); } - commands.add(new FrameCommand(lastNonEmptyLine+1)); - commands.add(new ControllerCommand((byte) 0, InputLine.getEmpty())); + if(!inputLines[inputLines.length-1].isEmpty()) { + commands.add(new FrameCommand(lastNonEmptyLine+1)); + commands.add(new ControllerCommand((byte) 0, InputLine.getEmpty())); + } ArrayList data = new ArrayList<>(); @@ -188,9 +190,12 @@ public static Script read(byte[] script) throws CorruptedScriptException { int commandCount = data.getInt(); int editingSeconds = data.getInt(); - data.expectBytes(new byte[] {1}, "player count"); // currently ignored + data.storePos(); + System.out.println(Arrays.toString(data.getBytes(0x20))); + data.loadPos(); + data.expectByte(1, "player count"); // currently ignored data.align(4); - data.expectBytes(new byte[] {0}, "controller type of player 0"); // procon + data.expectByte(0, "controller type of player 0"); // procon data.align(4); short authorLength = data.getShort(); @@ -205,7 +210,7 @@ public static Script read(byte[] script) throws CorruptedScriptException { if(c instanceof FrameCommand) { FrameCommand fc = (FrameCommand) c; if(fc.frameId < inputLines.size()) - throw new CorruptedScriptException("Line numbers misordered, got "+fc.frameId+" after "+inputLines.size(), inputLines.size()); + throw new CorruptedScriptException("Line numbers misordered, got "+fc.frameId+" after "+inputLines.size()+", offset "+Integer.toHexString(data.position()), inputLines.size()); while(inputLines.size() <= fc.frameId) { if(inputLines.isEmpty()) inputLines.add(InputLine.getEmpty()); diff --git a/src/main/java/io/github/jadefalke2/util/ByteDataStream.java b/src/main/java/io/github/jadefalke2/util/ByteDataStream.java index fb9716a..577a560 100644 --- a/src/main/java/io/github/jadefalke2/util/ByteDataStream.java +++ b/src/main/java/io/github/jadefalke2/util/ByteDataStream.java @@ -150,37 +150,37 @@ public void assertEOF() { public void expectByte(int value, String message) { byte val = getByte(); if(val != value) { - throw new UnsupportedOperationException("Unexpected "+message+". expected: "+value+", actual: "+val); + throw new UnsupportedOperationException("Unexpected "+message+" at "+Integer.toHexString(position()-1)+". expected: "+value+", actual: "+val); } } public void expectShort(int value, String message) { short val = getShort(); if(val != value) { - throw new UnsupportedOperationException("Unexpected "+message+". expected: "+value+", actual: "+val); + throw new UnsupportedOperationException("Unexpected "+message+" at "+Integer.toHexString(position()-1)+". expected: "+value+", actual: "+val); } } public void expectInt(int value, String message) { int val = getInt(); if(val != value) { - throw new UnsupportedOperationException("Unexpected "+message+". expected: "+value+", actual: "+val); + throw new UnsupportedOperationException("Unexpected "+message+" at "+Integer.toHexString(position()-1)+". expected: "+value+", actual: "+val); } } public void expectLong(long value, String message) { long val = getLong(); if(val != value) { - throw new UnsupportedOperationException("Unexpected "+message+". expected: "+value+", actual: "+val); + throw new UnsupportedOperationException("Unexpected "+message+" at "+Integer.toHexString(position()-1)+". expected: "+value+", actual: "+val); } } public void expectFloat(float value, String message) { float val = getFloat(); if(val != value) { - throw new UnsupportedOperationException("Unexpected "+message+". expected: "+value+", actual: "+val); + throw new UnsupportedOperationException("Unexpected "+message+" at "+Integer.toHexString(position()-1)+". expected: "+value+", actual: "+val); } } public void expectBytes(byte[] data, String message) { byte[] val = getBytes(data.length); if(!Arrays.equals(val, data)) { - throw new UnsupportedOperationException("Unexpected "+message+". expected: "+Arrays.toString(data)+", actual: "+Arrays.toString(val)); + throw new UnsupportedOperationException("Unexpected "+message+" at "+Integer.toHexString(position()-1)+". expected: "+Arrays.toString(data)+", actual: "+Arrays.toString(val)); } }