Skip to content

Commit 0832ddd

Browse files
Optimize DreadKFVAdapter
1 parent 6ec5396 commit 0832ddd

File tree

1 file changed

+20
-25
lines changed
  • src/mercury_engine_data_structures/formats

1 file changed

+20
-25
lines changed

src/mercury_engine_data_structures/formats/bcskla.py

+20-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from enum import Enum
2-
31
import construct
42
from construct.core import (
53
Adapter,
@@ -22,50 +20,47 @@
2220
this,
2321
)
2422

25-
from mercury_engine_data_structures.adapters.enum_adapter import EnumAdapter
2623
from mercury_engine_data_structures.common_types import Float, VersionAdapter
2724
from mercury_engine_data_structures.construct_extensions.alignment import AlignTo
2825
from mercury_engine_data_structures.formats.base_resource import BaseResource
2926
from mercury_engine_data_structures.formats.property_enum import PropertyEnumDoubleUnsafe
3027
from mercury_engine_data_structures.game_check import Game
3128

3229

33-
class TimingTypeEnum(Enum):
34-
ONE_BYTE = 8
35-
TWO_BYTE = 0
36-
37-
3830
class DreadKFVAdapter(Adapter):
31+
class TimingType:
32+
ONE_BYTE = 8
33+
TWO_BYTE = 0
34+
3935
SUBCON = Struct(
40-
timing_type=EnumAdapter(TimingTypeEnum, Int16ul),
36+
timing_type=Int16ul,
4137
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]),
4339
_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()
4642

4743
def __init__(self):
4844
super().__init__(self.SUBCON)
4945

5046
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],
5952
)
60-
61-
return res
53+
for time, values in zip(obj["timings"], obj["values"], strict=True)
54+
)
6255

6356
def _encode(self, obj, context, path):
6457
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,
6661
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],
6964
)
7065

7166
return res

0 commit comments

Comments
 (0)