From 6bfd2b54c237dd4e2cefe5b193f2e91474a6b504 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Sat, 3 Oct 2020 18:58:59 -0400 Subject: [PATCH] progress towards REO compatibility --- .../java/com/opensr5/ini/IniFileModel.java | 15 +++++-- .../java/com/opensr5/ini/ProtocolCommand.java | 44 +++++++++++++++++++ .../opensr5/ini/test/IniFileReaderTest.java | 34 ++++++++++---- 3 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 java_console/inifile/src/main/java/com/opensr5/ini/ProtocolCommand.java diff --git a/java_console/inifile/src/main/java/com/opensr5/ini/IniFileModel.java b/java_console/inifile/src/main/java/com/opensr5/ini/IniFileModel.java index c52ca7c8e4..1f37e8e898 100644 --- a/java_console/inifile/src/main/java/com/opensr5/ini/IniFileModel.java +++ b/java_console/inifile/src/main/java/com/opensr5/ini/IniFileModel.java @@ -32,6 +32,7 @@ public class IniFileModel { public Map allIniFields = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); public Map tooltips = new TreeMap<>(); + public Map protocolMeta = new TreeMap<>(); private boolean isConstantsSection; private String currentSection; @@ -141,10 +142,16 @@ public class IniFileModel { isConstantsSection = first.equals("[Constants]"); } - if (isInsidePageDefinition && isConstantsSection) { - if (list.size() > 1) - handleFieldDefinition(list); - return; + if (isConstantsSection) { + if (isInsidePageDefinition) { + if (list.size() > 1) + handleFieldDefinition(list); + return; + } else { + if (list.size() > 1) { + protocolMeta.put(list.get(0), list.get(1)); + } + } } diff --git a/java_console/inifile/src/main/java/com/opensr5/ini/ProtocolCommand.java b/java_console/inifile/src/main/java/com/opensr5/ini/ProtocolCommand.java new file mode 100644 index 0000000000..2853b7295a --- /dev/null +++ b/java_console/inifile/src/main/java/com/opensr5/ini/ProtocolCommand.java @@ -0,0 +1,44 @@ +package com.opensr5.ini; + +import java.util.ArrayList; +import java.util.List; + +public class ProtocolCommand { + + private final byte[] bytes; + + public ProtocolCommand(byte[] bytes) { + this.bytes = bytes; + } + + public byte[] getBytes() { + return bytes; + } + + public static ProtocolCommand parse(String meta) { + // todo: support placeholders + + List result = new ArrayList<>(); + + for (int i = 0; i < meta.length(); i++) { + char c = meta.charAt(i); + if (c == '\\') { + char next = meta.charAt(i + 1); + if (next != 'x') + throw new IllegalArgumentException("Unexpected symbol: " + next); + String hex = meta.substring(i + 2, i + 4); + result.add(Byte.parseByte(hex, 16)); + i += 3; + } else { + result.add((byte) c); + } + } + + byte[] bytes = new byte[result.size()]; + for (int i = 0; i < result.size(); i++) + bytes[i] = result.get(i); + + return new ProtocolCommand(bytes); + + } +} diff --git a/java_console/inifile/src/test/java/com/opensr5/ini/test/IniFileReaderTest.java b/java_console/inifile/src/test/java/com/opensr5/ini/test/IniFileReaderTest.java index bebc66b75e..a11dbf79b5 100644 --- a/java_console/inifile/src/test/java/com/opensr5/ini/test/IniFileReaderTest.java +++ b/java_console/inifile/src/test/java/com/opensr5/ini/test/IniFileReaderTest.java @@ -1,9 +1,6 @@ package com.opensr5.ini.test; -import com.opensr5.ini.IniFileMetaInfo; -import com.opensr5.ini.IniFileModel; -import com.opensr5.ini.IniFileReader; -import com.opensr5.ini.RawIniFile; +import com.opensr5.ini.*; import com.opensr5.ini.field.ArrayIniField; import com.opensr5.ini.field.EnumIniField; import com.opensr5.ini.field.IniField; @@ -11,9 +8,9 @@ import org.jetbrains.annotations.NotNull; import org.junit.Test; import java.io.ByteArrayInputStream; +import java.util.Arrays; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.*; /** * Andrey Belomutskiy, (c) 2013-2020 @@ -112,6 +109,26 @@ public class IniFileReaderTest { return IniFileReader.read(new ByteArrayInputStream(string.getBytes())); } + @Test + public void testProtocolMeta() { + String string = + "[Constants]\n" + + " crc32CheckCommand = \"k\\x00\\x00\\x00\\x00\\x00\\x00\"\n" + + "page = 1\n" + + "primingSquirtDurationMs\t\t\t= scalar, F32,\t96,\t\"*C\", 1, 0, -40, 200, 1\n" + + ""; + RawIniFile lines = IniFileReader.read(new ByteArrayInputStream(string.getBytes())); + IniFileModel model = new IniFileModel().readIniFile(lines); + assertEquals(1, model.allIniFields.size()); + + String crcProtocol = model.protocolMeta.get("crc32CheckCommand"); + assertEquals("k\\x00\\x00\\x00\\x00\\x00\\x00", crcProtocol); + + byte[] expected = {'k', 0, 0, 0, 0, 0, 0}; + + assertTrue(Arrays.equals(expected, ProtocolCommand.parse(crcProtocol).getBytes())); + } + @Test public void testEasyFields() { String string = "page = 1\n" + @@ -174,9 +191,8 @@ public class IniFileReaderTest { " \tname2\t\t\t= array, F32,\t108,\t[8],\t\"\", 1, 0, 0.0, 18000, 2\n" + "[Constants]\n" + " \tname\t\t\t= array, F32,\t108,\t[8],\t\"\", 1, 0, 0.0, 18000, 2\n" + - "[PcVariables]\n" + - " \tname3\t\t\t= array, F32,\t108,\t[8],\t\"\", 1, 0, 0.0, 18000, 2\n" - ; + "[PcVariables]\n" + + " \tname3\t\t\t= array, F32,\t108,\t[8],\t\"\", 1, 0, 0.0, 18000, 2\n"; RawIniFile lines = IniFileReader.read(new ByteArrayInputStream(string.getBytes())); IniFileModel model = new IniFileModel().readIniFile(lines);