|
1 |
| -from enum import Enum |
2 |
| - |
3 | 1 | import construct
|
4 | 2 | from construct.core import (
|
5 | 3 | Adapter,
|
|
22 | 20 | this,
|
23 | 21 | )
|
24 | 22 |
|
25 |
| -from mercury_engine_data_structures.adapters.enum_adapter import EnumAdapter |
26 | 23 | from mercury_engine_data_structures.common_types import Float, VersionAdapter
|
27 | 24 | from mercury_engine_data_structures.construct_extensions.alignment import AlignTo
|
28 | 25 | from mercury_engine_data_structures.formats.base_resource import BaseResource
|
29 | 26 | from mercury_engine_data_structures.formats.property_enum import PropertyEnumDoubleUnsafe
|
30 | 27 | from mercury_engine_data_structures.game_check import Game
|
31 | 28 |
|
32 | 29 |
|
33 |
| -class TimingTypeEnum(Enum): |
34 |
| - ONE_BYTE = 8 |
35 |
| - TWO_BYTE = 0 |
36 |
| - |
37 |
| - |
38 | 30 | class DreadKFVAdapter(Adapter):
|
| 31 | + class TimingType: |
| 32 | + ONE_BYTE = 8 |
| 33 | + TWO_BYTE = 0 |
| 34 | + |
39 | 35 | SUBCON = Struct(
|
40 |
| - timing_type=EnumAdapter(TimingTypeEnum, Int16ul), |
| 36 | + timing_type=Int16ul, |
41 | 37 | count=Int16ul,
|
42 |
| - timings=IfThenElse(this.timing_type == TimingTypeEnum.TWO_BYTE, Int16ul[this.count], Int8ul[this.count]), |
| 38 | + timings=IfThenElse(this.timing_type == TimingType.TWO_BYTE, Int16ul[this.count], Int8ul[this.count]), |
43 | 39 | _padding=AlignTo(4, b"\xff"),
|
44 |
| - values=Array(this.count, Struct(value=Float, derivative=Float)), |
45 |
| - ) |
| 40 | + values=Array(this.count, Float[2]), |
| 41 | + ).compile() |
46 | 42 |
|
47 | 43 | def __init__(self):
|
48 | 44 | super().__init__(self.SUBCON)
|
49 | 45 |
|
50 | 46 | def _decode(self, obj, context, path):
|
51 |
| - res = ListContainer() |
52 |
| - for i in range(obj.count): |
53 |
| - res.append( |
54 |
| - Container( |
55 |
| - time=obj.timings[i], |
56 |
| - value=obj["values"][i].value, |
57 |
| - derivative=obj["values"][i].derivative, |
58 |
| - ) |
| 47 | + return ListContainer( |
| 48 | + Container( |
| 49 | + time=time, |
| 50 | + value=values[0], |
| 51 | + derivative=values[1], |
59 | 52 | )
|
60 |
| - |
61 |
| - return res |
| 53 | + for time, values in zip(obj["timings"], obj["values"], strict=True) |
| 54 | + ) |
62 | 55 |
|
63 | 56 | def _encode(self, obj, context, path):
|
64 | 57 | res = Container(
|
65 |
| - timing_type=TimingTypeEnum.TWO_BYTE if obj[-1].time > 0xFF else TimingTypeEnum.ONE_BYTE, |
| 58 | + timing_type=DreadKFVAdapter.TimingType.TWO_BYTE |
| 59 | + if obj[-1].time > 0xFF |
| 60 | + else DreadKFVAdapter.TimingType.ONE_BYTE, |
66 | 61 | count=len(obj),
|
67 |
| - timings=ListContainer([v.time for v in obj]), |
68 |
| - values=ListContainer([Container(value=v.value, derivative=v.derivative) for v in obj]), |
| 62 | + timings=[v.time for v in obj], |
| 63 | + values=[[v.value, v.derivative] for v in obj], |
69 | 64 | )
|
70 | 65 |
|
71 | 66 | return res
|
|
0 commit comments