def gate_factory(class_dict): normal_color_struct = cs.Struct(cs.Padding(2), 'category' / cs.Byte, 'color' / cs.Byte) offset_color_struct = cs.Struct('source' / cs.Int, cs.Const(class_dict['color_triplet']), 'hsv' / cs.Single[3]) color_entry_struct = cs.Struct( 'type' / cs.RawCopy(cs.Short), 'colors' / cs.Switch( cs.this.type.data, { class_dict['normal_color']: normal_color_struct, class_dict['offset_color']: offset_color_struct })) color_struct = cs.Struct( cs.Const(class_dict['colorization']), 'num_entries' / cs.Int, 'colorizations' / color_entry_struct[cs.this.num_entries]) level_struct = cs.Struct( cs.Const(class_dict['gate_level']), cs.Const(b'\x01'), 'level_name' / cs.PascalString(cs.Short, 'ascii'), cs.Const(b'\x01'), 'level_icon' / cs.PascalString(cs.Short, 'ascii'), 'colorization' / color_struct, cs.Const(b'\x01'), 'description' / cs.PascalString(cs.Short, 'ascii'), cs.Const(class_dict['level_type']), 'level_type' / cs.Byte, 'restricted' / cs.Byte) velocity_struct = cs.Struct('velocity' / cs.Single, cs.Const(b'\x01'), 'num_entries' / cs.Int, 'lengths' / cs.Single[cs.this.num_entries], 'start' / cs.Long) wheel_struct = cs.Struct( 'class_id' / cs.RawCopy(cs.Short), 'unknown' / cs.Byte[0x05], 'num_levels' / cs.Short, 'levels' / level_struct[cs.this.num_levels], 'velocity' / cs.If(cs.this.class_id.data != class_dict['random_depth'], velocity_struct)) gate_struct = cs.Struct( cs.Const(b'\x01'), 'gate_id' / cs.Int, cs.Const(b'\x01'), 'gate_name' / cs.PascalString(cs.Short, 'ascii'), cs.Const(b'\x01'), 'gate_icon' / cs.PascalString(cs.Short, 'ascii'), 'colorization' / color_struct, cs.Const(b'\x01'), 'description' / cs.PascalString(cs.Short, 'ascii'), 'unknown' / cs.Byte[0x16], 'num_wheels' / cs.Int, 'wheels' / wheel_struct[cs.this.num_wheels], 'class_id' / cs.Int16sb, cs.If(cs.this.class_id < 0, cs.PascalString(cs.Short, 'ascii')), 'themes' / cs.Struct('unknown' / cs.Byte[0x07], 'themes' / cs.Byte[0x06])) return gate_struct
def getFrame(self): try: parsed = construct.RawCopy(frameParser.frameParser).parse_stream(self.file) print(f"Getframe {parsed['offset2']} {self.file.tell()}",flush=True) self.file.seek(parsed['offset2']) #print(parsed) #print("=================") if self.recluster: frame = self.reclusterFrame(parsed['value']) else: frame = frameParser.parseFrame(parsed['data']) self.frameReceived.emit(frame) except Exception as e: print(e) self.file.seek(10,1) #probably a file error, skip a bit
def LifeScanPacket(include_link_control: bool, ) -> construct.Struct: # pylint: disable=invalid-name if include_link_control: link_control_construct = _LINK_CONTROL else: link_control_construct = construct.Const(b"\x00") return construct.Struct( "data" / construct.RawCopy( construct.Struct( construct.Const(b"\x02"), # stx "length" / construct.Rebuild( construct.Byte, lambda this: len(this.message) + 6), "link_control" / link_control_construct, "message" / construct.Bytes(lambda this: this.length - 6), construct.Const(b"\x03"), # etx ), ), "checksum" / construct.Checksum(construct.Int16ul, lifescan.crc_ccitt, construct.this.data.data), )
def LifeScanPacket(include_link_control): # pylint: disable=invalid-name # type: (bool) -> construct.Struct if include_link_control: link_control_construct = _LINK_CONTROL else: link_control_construct = construct.Const(b'\x00') return construct.Struct( 'data' / construct.RawCopy( construct.Struct( construct.Const(b'\x02'), # stx 'length' / construct.Rebuild( construct.Byte, lambda this: len(this.message) + 6), 'link_control' / link_control_construct, 'message' / construct.Bytes(lambda this: this.length - 6), construct.Const(b'\x03'), # etx ), ), 'checksum' / construct.Checksum(construct.Int16ul, lifescan.crc_ccitt, construct.this.data.data), )
def LifeScanPacket(command_prefix, include_link_control): if include_link_control: link_control_construct = _LINK_CONTROL else: link_control_construct = construct.Const(b'\x00') command_prefix_construct = construct.Const(command_prefix, construct.Byte) return construct.Struct( 'data' / construct.RawCopy( construct.Struct( construct.Const(b'\x02'), # stx 'length' / construct.Rebuild( construct.Byte, lambda this: len(this.message) + 7), 'link_control' / link_control_construct, 'command_prefix' / command_prefix_construct, 'message' / construct.Bytes(lambda this: this.length - 7), construct.Const(b'\x03'), # etx ), ), 'checksum' / construct.Checksum(construct.Int16ul, lifescan.crc_ccitt, construct.this.data.data), )
PermalinkBinary = construct.FocusedSeq( "fields", schema_version=construct.Const(_CURRENT_SCHEMA_VERSION, construct.Byte), fields=construct.RawCopy( construct.Aligned( 3, construct.Struct( header=construct.BitStruct( has_seed_hash=construct.Rebuild( construct.Flag, construct.this._.seed_hash != None), bytes_rotation=construct.Rebuild( construct.BitsInteger(7), lambda ctx: single_byte_hash(ctx._.generator_params) >> 1, )), seed_hash=construct.If(construct.this.header.has_seed_hash, construct.Bytes(5)), randovania_version=construct.Bytes(4), # short git hash generator_params=construct.ExprAdapter( construct.Prefixed(construct.VarInt, construct.GreedyBytes), # parsing decoder=create_rotator(inverse=True), # building encoder=create_rotator(inverse=False), ), ))), permalink_checksum=construct.Checksum( construct.Bytes(2), lambda data: hashlib.blake2b(data, digest_size=2).digest(), construct.this.fields.data,
class Direction(enum.Enum): In = 0xa5 Out = 0xa3 def byte_checksum(data): return functools.reduce(operator.add, data) & 0xFF _PACKET = construct.Struct( 'data' / construct.RawCopy( construct.Struct( construct.Const(b'\x51'), 'command' / construct.Byte, 'message' / construct.Bytes(4), 'direction' / construct.Mapping(construct.Byte, {e: e.value for e in Direction}), ), ), 'checksum' / construct.Checksum(construct.Byte, byte_checksum, construct.this.data.data), ) _EMPTY_MESSAGE = 0 _CONNECT_REQUEST = 0x22 _VALID_CONNECT_RESPONSE = {0x22, 0x24, 0x54} _GET_DATETIME = 0x23 _SET_DATETIME = 0x33
class Direction(enum.Enum): In = 0xA5 Out = 0xA3 def byte_checksum(data): return functools.reduce(operator.add, data) & 0xFF _PACKET = construct.Struct( data=construct.RawCopy( construct.Struct( const=construct.Const(b"\x51"), command=construct.Byte, message=construct.Bytes(4), direction=construct.Mapping(construct.Byte, {e: e.value for e in Direction}), ), ), checksum=construct.Checksum(construct.Byte, byte_checksum, construct.this.data.data), ) _EMPTY_MESSAGE = b"\x00\x00\x00\x00" _CONNECT_REQUEST = 0x22 _VALID_CONNECT_RESPONSE = {0x22, 0x24, 0x54} _GET_DATETIME = 0x23 _SET_DATETIME = 0x33
class Direction(enum.Enum): In = 0xA5 Out = 0xA3 def byte_checksum(data): return functools.reduce(operator.add, data) & 0xFF _PACKET = construct.Struct( "data" / construct.RawCopy( construct.Struct( construct.Const(b"\x51"), "command" / construct.Byte, "message" / construct.Bytes(4), "direction" / construct.Mapping(construct.Byte, {e: e.value for e in Direction}), ), ), "checksum" / construct.Checksum(construct.Byte, byte_checksum, construct.this.data.data), ) _EMPTY_MESSAGE = b"\x00\x00\x00\x00" _CONNECT_REQUEST = 0x22 _VALID_CONNECT_RESPONSE = {0x22, 0x24, 0x54} _GET_DATETIME = 0x23 _SET_DATETIME = 0x33
from dataclasses import dataclass from datetime import datetime from typing import Optional, Sequence import construct as c import numpy as np from numpy.ma import log message_id_schema = c.Enum( c.Int16ul, NMEA0183=9, PROFILE6=1308, ) ping_schema = c.Struct( start=c.RawCopy(c.Const(b'BR')), payload_length=c.RawCopy(c.Int16ul), message_id=c.RawCopy(message_id_schema), src_device_id=c.RawCopy(c.Int8ul), dest_device_id=c.RawCopy(c.Int8ul), payload=c.RawCopy(c.Bytes(c.this.payload_length.value)), checksum=c.Checksum( c.Int16ul, lambda b: sum(b) % (1 << 16), lambda cxt: b''.join(cxt[attr].data for attr in [ 'start', 'payload_length', 'message_id', 'src_device_id', 'dest_device_id', 'payload' ])), ) profile6_schema = c.Struct(ping_number=c.Int32ul, start_mm=c.Int32ul,