From c3841d65d3833fea0d9d6befe21a243762af8eaa Mon Sep 17 00:00:00 2001 From: lcdr Date: Fri, 12 Jan 2018 23:41:25 +0100 Subject: [PATCH] Made the BitStream interface more stream-like. --- captureviewer.pyw | 4 ++-- luzviewer.pyw | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/captureviewer.pyw b/captureviewer.pyw index 256372a..55809b2 100644 --- a/captureviewer.pyw +++ b/captureviewer.pyw @@ -202,7 +202,7 @@ class CaptureViewer(viewer.Viewer): print("Parsing creations") creations = [i for i in files if "[24]" in i] for packet_name in creations: - packet = ReadStream(capture.read(packet_name)) + packet = ReadStream(capture.read(packet_name), unlocked=True) self.parse_creation(packet_name, packet) if self.parse_serializations.get(): @@ -333,7 +333,7 @@ class CaptureViewer(viewer.Viewer): parser_output.text += "\n"+name+"\n\n" parser_output.append(parser.parse(packet, {"creation":is_creation})) if not packet.all_read(): - raise IndexError("Not completely read, %i bytes unread" % (len(packet) - math.ceil(packet.read_offset / 8))) + raise IndexError("Not completely read, %i bytes unread" % len(packet.read_remaining())) def parse_serialization_packet(self, packet_name, packet): network_id = packet.read(c_ushort) diff --git a/luzviewer.pyw b/luzviewer.pyw index 1407de0..03e91c8 100644 --- a/luzviewer.pyw +++ b/luzviewer.pyw @@ -53,7 +53,9 @@ class LUZViewer(viewer.Viewer): self.tree.set_children("") print("Loading", luz_path) with open(luz_path, "rb") as file: - stream = ReadStream(file.read()) + data = file.read() + luz_len = len(data) + stream = ReadStream(data, unlocked=True) version = stream.read(c_uint) assert version in (36, 38, 39, 40, 41), version @@ -85,7 +87,7 @@ class LUZViewer(viewer.Viewer): with open(lvl_path, "rb") as lvl: print("Loading lvl", filename) try: - self.parse_lvl(ReadStream(lvl.read()), scene) + self.parse_lvl(ReadStream(lvl.read(), unlocked=True), scene) except Exception: import traceback traceback.print_exc() @@ -115,7 +117,7 @@ class LUZViewer(viewer.Viewer): self.tree.insert(scene_transition, END, text="Transition Point", values=(transition_point_scene_id, transition_point_position)) remaining_length = stream.read(c_uint) - assert len(stream) - stream.read_offset//8 == remaining_length + assert luz_len - stream.read_offset//8 == remaining_length assert stream.read(c_uint) == 1 ### paths @@ -227,7 +229,9 @@ class LUZViewer(viewer.Viewer): self.tree.insert(waypoint, END, text="Config", values=(config_name, config_type_and_value)) def parse_lvl(self, stream, scene): - if stream[0:4] == b"CHNK": + header = stream.read(bytes, length=4) + stream.read_offset = 0 + if header == b"CHNK": # newer lvl file structure # chunk based while not stream.all_read():