Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 16, 2024
1 parent 3221acc commit ed811ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
48 changes: 22 additions & 26 deletions src/mercury_engine_data_structures/formats/bcskla.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
from dataclasses import dataclass
from pprint import pprint
from typing import Union

import construct
from construct.core import (
Array,
Const,
Construct,
Container,
Hex,
If,
IfThenElse,
Int8ul,
Int16ul,
Int32ul,
Int64ul,
ListContainer,
Struct,
Tell
Tell,
)

from pprint import pprint
from dataclasses import dataclass
from typing import Union

from mercury_engine_data_structures.common_types import CVector2D, Float
from mercury_engine_data_structures.construct_extensions.alignment import AlignTo
from mercury_engine_data_structures.common_types import Float, CVector2D
from mercury_engine_data_structures.formats.base_resource import BaseResource
from mercury_engine_data_structures.formats.property_enum import PropertyEnumDoubleUnsafe
from mercury_engine_data_structures.game_check import Game
Expand Down Expand Up @@ -52,18 +48,18 @@ def auto_set_timing_type(self) -> int:
tt = 0
if kf.time > 0xFFFF:
raise ValueError(f"Keyframe has too large of a time! Max size is 65535, keyframe has {kf.time}")

self.timing_type = tt
return tt

def add(self, time: int, val: int, derivative: int):
if time > 0xFFFF:
raise ValueError(f"Cannot add time {time} as max value is {0xFFFF}")

new_kf = KeyFrameValue(time, val, derivative)
self.keyframes.append(new_kf)
self.keyframes.sort(key=lambda kfv: kfv.time)


class KeyFramedValuesConstruct(Construct):
@classmethod
Expand All @@ -74,18 +70,18 @@ def _parse(self, stream, context, path) -> KeyFramedValues:
# timings
timings = Array(kf_count, Int8ul if timing_type == 8 else Int16ul)._parsereport(stream, context, f"{path} -> Timings")
AlignTo(4, b"\xff")._parse(stream, context, path)

values = Array(kf_count, CVector2D)._parsereport(stream, context, f"{path} -> Values")

kf_data = [KeyFrameValue(timings[i], values[i][0], values[i][1]) for i in range(kf_count)]
return KeyFramedValues(timing_type=timing_type, keyframes=kf_data)

@classmethod
def _build(self, obj: KeyFramedValues, stream, context, path) -> None:
count = len(obj.keyframes)
Int16ul._build(obj.timing_type, stream, context, path)
Int16ul._build(count, stream, context, path)

field = Int8ul if obj.timing_type == 8 else Int16ul
Array(count, field)._build([k.time for k in obj.keyframes], stream, context, path)
AlignTo(4, b"\xff")._build(None, stream, context, path)
Expand Down Expand Up @@ -159,16 +155,16 @@ def _parse(self, stream, context, path) -> BcsklaTrack:
values.append(KeyFramedValuesConstruct._parse(stream, context, f"{path} -> KF Value {i}"))
else:
values.append(Float._parsereport(stream, context, path))

return BcsklaTrack(data.bone_hash, values)

@classmethod
def _build(self, obj: BcsklaTrack, stream, context, path) -> None:
flags = 0
for i, val in enumerate(obj.values):
if type(val) == KeyFramedValues:
flags += 2**i

self.STRUCT._build(Container(bone_hash=obj.bone_name, flags=flags), stream, context, path)
offset = construct.stream_tell(stream, path)

Expand All @@ -191,7 +187,7 @@ class BcsklaData:
def validate(self) -> None:
if not self.frame_count.is_integer():
raise ValueError("Frame count must be an integer!")

expected_timing_type = 8 if self.frame_count <= 0xFF else 0

for track in self.tracks:
Expand All @@ -200,7 +196,7 @@ def validate(self) -> None:
value.auto_set_timing_type()
if expected_timing_type != value.timing_type:
raise ValueError(f"Track {track.bone_name} ({i}) has unexpected TT!")

if value.keyframes[-1].time != self.frame_count:
raise ValueError(f"KFV for {track.bone_name}, value {i} does not end on keyframe {self.frame_count}!")

Expand All @@ -224,15 +220,15 @@ class BcsklaConstruct(Construct):

def _parse(self, stream, context, path) -> BcsklaData:
hdr = self.HEADER_STRUCT._parse(stream, context, f"{path} -> Header")

offset = construct.stream_tell(stream, path)
tracks: list[BcsklaTrack] = []
for i in range(hdr.track_count):
construct.stream_seek(stream, offset + (i * 0x30), 0, path)
tracks.append(BcsklaTrackConstruct._parse(stream, context, f"{path} -> Track {i}"))

return BcsklaData(hdr.unk, hdr.frame_count, tracks)

def _build(self, obj: BcsklaData, stream, context, path):
obj.validate()
hdr = Container(
Expand All @@ -247,12 +243,12 @@ def _build(self, obj: BcsklaData, stream, context, path):
construct.stream_seek(stream, 0x18 + (i * 0x30), 0, path)
BcsklaTrackConstruct._build(track, stream, context, f"{path} -> Track {i}")


class Bcskla(BaseResource):
@classmethod
def construct_class(cls, target_game: Game) -> Construct:
return BcsklaConstruct()

def validate(self) -> None:
# shortcut for the BcsklaData validator
return self.raw.validate()
return self.raw.validate()
1 change: 0 additions & 1 deletion tests/formats/test_bcskla.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from mercury_engine_data_structures import dread_data
from mercury_engine_data_structures.formats.bcskla import Bcskla


all_dread_bcskla = [name for name in dread_data.all_name_to_asset_id().keys()
if name.endswith(".bcskla")]

Expand Down

0 comments on commit ed811ca

Please sign in to comment.