From ad493e1b95f82d5c0a5597beff79438a84461e51 Mon Sep 17 00:00:00 2001 From: Thanatos Date: Sun, 1 Oct 2023 14:15:18 +0200 Subject: [PATCH] Support for SR TXT files --- src/mercury_engine_data_structures/formats/txt.py | 10 ++++++++-- tests/formats/test_txt.py | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/mercury_engine_data_structures/formats/txt.py b/src/mercury_engine_data_structures/formats/txt.py index f63f7368..8400dcad 100644 --- a/src/mercury_engine_data_structures/formats/txt.py +++ b/src/mercury_engine_data_structures/formats/txt.py @@ -2,8 +2,10 @@ from typing import Dict import construct +from construct import IfThenElse from construct.core import Const, Construct, GreedyRange, Struct +from mercury_engine_data_structures import game_check from mercury_engine_data_structures.common_types import DictAdapter, DictElement from mercury_engine_data_structures.construct_extensions.strings import CStringRobust from mercury_engine_data_structures.formats import BaseResource @@ -38,7 +40,11 @@ def _parse_{n}(io, this): TXT = Struct( "magic" / Const(b'BTXT'), - "version" / Const(b'\x01\x00\x0a\x00'), + "version" / IfThenElse( + game_check.current_game_at_most(Game.SAMUS_RETURNS), + Const(b'\x01\x00\x08\x00'), + Const(b'\x01\x00\x0a\x00'), + ), "strings" / DictAdapter(_string_range), "_end" / construct.Terminated, ) @@ -48,7 +54,7 @@ class Txt(BaseResource): @classmethod @functools.lru_cache def construct_class(cls, target_game: Game) -> Construct: - return TXT.compile() + return TXT @property def strings(self) -> Dict[str, str]: diff --git a/tests/formats/test_txt.py b/tests/formats/test_txt.py index 9c6d46d3..14d37c5e 100644 --- a/tests/formats/test_txt.py +++ b/tests/formats/test_txt.py @@ -9,3 +9,9 @@ def test_compare_dread(dread_path): parse_and_build_compare( TXT, Game.DREAD, file_path ) + +def test_compare_sr(samus_returns_path): + file_path = samus_returns_path.joinpath("system/localization/us_english.txt") + parse_and_build_compare( + TXT, Game.SAMUS_RETURNS, file_path + )