From aa4c971b154b56323cd1dd7ccaaff8c07eeb0a28 Mon Sep 17 00:00:00 2001 From: Nathanael Gentry Date: Mon, 3 Jun 2024 07:27:33 -0400 Subject: [PATCH] Don't depend on title compiler version number when reading context names. --- src/MediaStation/System.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/MediaStation/System.py b/src/MediaStation/System.py index 0cbccb7..a77d674 100644 --- a/src/MediaStation/System.py +++ b/src/MediaStation/System.py @@ -123,20 +123,27 @@ def __init__(self, chunk): assert_equal(repeated_file_number, self.file_number) # READ THE CONTEXT NAME. - # Version 1 titles don't have context names. - version_has_context_names = (not global_variables.version.is_first_generation_engine) and \ - (global_variables.version.major_version >= 3) and (global_variables.version.minor_version > 7) - if version_has_context_names: - section_type = Datum(chunk).d - if ContextDeclaration.SectionType.CONTEXT_NAME == section_type: - self.context_name = Datum(chunk).d - else: - raise BinaryParsingError("Context name expected but not present.", chunk.stream) + # Only some titles have context names, and unfortunately we can't + # determine which just by relying on the title compiler version + # number. + # TODO: Find a better way to read the context name without relying + # on reading and rewinding. + rewind_pointer = chunk.stream.tell() + section_type = Datum(chunk).d + if ContextDeclaration.SectionType.CONTEXT_NAME == section_type: + # READ THE CONTEXT NAME. + self.context_name = Datum(chunk).d + else: + # THERE IS NO CONTEXT NAME. + # We have instead read into the next declaration, so let's undo that. + chunk.stream.seek(rewind_pointer) + elif ContextDeclaration.SectionType.EMPTY == section_type: # INDICATE THIS IS THE LAST CONTEXT DECLARATION. # This signals to the holder of this declaration that there # are no more declarations in the stream. self._is_empty = True + else: # INDICATE AN ERROR. raise ValueError(f'Received unexpected section type: 0x{section_type:04x}')