diff --git a/blender_asset_tracer/blendfile/__init__.py b/blender_asset_tracer/blendfile/__init__.py index 196ffcc..c1a6414 100644 --- a/blender_asset_tracer/blendfile/__init__.py +++ b/blender_asset_tracer/blendfile/__init__.py @@ -372,15 +372,13 @@ class BlendFileBlock: sdna_index = self.bfile.sdna_index_from_id[dna_type_id] self.refine_type_from_index(sdna_index) - def get_file_offset(self, path: bytes) -> (int, int): # TODO(Sybren): port to BAT - """Return (offset, length)""" - assert isinstance(path, bytes) + def abs_offset(self, path: dna.FieldPath) -> (int, int): + """Compute the absolute file offset of the field. - # TODO: refactor to just return the length, and check whether this isn't actually - # simply the same as self.size. - ofs = self.file_offset - field, _ = self.dna_type.field_from_path(self.bfile.header.pointer_size, path) - return ofs, field.name.array_size + :returns: tuple (offset in bytes, length of array in items) + """ + field, field_offset = self.dna_type.field_from_path(self.bfile.header.pointer_size, path) + return self.file_offset + field_offset, field.name.array_size def get(self, path: dna.FieldPath, diff --git a/tests/test_blendfile_loading.py b/tests/test_blendfile_loading.py index 7625296..1e132f7 100644 --- a/tests/test_blendfile_loading.py +++ b/tests/test_blendfile_loading.py @@ -206,6 +206,15 @@ class PointerTest(AbstractBlendFileTest): with self.assertRaises(exceptions.SegmentationFault): scene.get_pointer(b'ed') + def test_abs_offset(self): + scene = self.bf.code_index[b'SC'][0] + ed = scene.get_pointer(b'ed') + assert isinstance(ed, blendfile.BlendFileBlock) + + abs_offset, field_size = ed.abs_offset((b'seqbase', b'first')) + self.assertEqual(ed.file_offset + 8, abs_offset) + self.assertEqual(1, field_size) + class LoadCompressedTest(AbstractBlendFileTest): def test_loading(self):