Skip to content

Commit 7303404

Browse files
committed
Use struct in unit and task classes
1 parent c2c9c34 commit 7303404

File tree

3 files changed

+572
-383
lines changed

3 files changed

+572
-383
lines changed

src/genieutils/task.py

Lines changed: 103 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import struct
12
from dataclasses import dataclass
23

34
from genieutils.common import ByteHandler, GenieClass
45
from genieutils.versions import Version
56

7+
FORMAT = '<hhbhhhhhhhhfffbfbbhhbbbhhhhhhll'
8+
FORMAT_LENGTH = 67
9+
610

711
@dataclass(slots=True)
812
class Task(GenieClass):
@@ -40,71 +44,106 @@ class Task(GenieClass):
4044

4145
@classmethod
4246
def from_bytes(cls, content: ByteHandler) -> 'Task':
47+
task_type, \
48+
id_, \
49+
is_default, \
50+
action_type, \
51+
class_id, \
52+
unit_id, \
53+
terrain_id, \
54+
resource_in, \
55+
resource_multiplier, \
56+
resource_out, \
57+
unused_resource, \
58+
work_value_1, \
59+
work_value_2, \
60+
work_range, \
61+
auto_search_targets, \
62+
search_wait_time, \
63+
enable_targeting, \
64+
combat_level_flag, \
65+
gather_type, \
66+
work_flag_2, \
67+
target_diplomacy, \
68+
carry_check, \
69+
pick_for_construction, \
70+
moving_graphic_id, \
71+
proceeding_graphic_id, \
72+
working_graphic_id, \
73+
carrying_graphic_id, \
74+
resource_gathering_sound_id, \
75+
resource_deposit_sound_id, \
76+
wwise_resource_gathering_sound_id, \
77+
wwise_resource_deposit_sound_id = struct.unpack(
78+
FORMAT,
79+
content.consume_range(FORMAT_LENGTH)
80+
)
81+
4382
return cls(
44-
task_type=content.read_int_16(),
45-
id=content.read_int_16(),
46-
is_default=content.read_int_8(),
47-
action_type=content.read_int_16(),
48-
class_id=content.read_int_16(),
49-
unit_id=content.read_int_16(),
50-
terrain_id=content.read_int_16(),
51-
resource_in=content.read_int_16(),
52-
resource_multiplier=content.read_int_16(),
53-
resource_out=content.read_int_16(),
54-
unused_resource=content.read_int_16(),
55-
work_value_1=content.read_float(),
56-
work_value_2=content.read_float(),
57-
work_range=content.read_float(),
58-
auto_search_targets=content.read_int_8(),
59-
search_wait_time=content.read_float(),
60-
enable_targeting=content.read_int_8(),
61-
combat_level_flag=content.read_int_8(),
62-
gather_type=content.read_int_16(),
63-
work_flag_2=content.read_int_16(),
64-
target_diplomacy=content.read_int_8(),
65-
carry_check=content.read_int_8(),
66-
pick_for_construction=content.read_int_8(),
67-
moving_graphic_id=content.read_int_16(),
68-
proceeding_graphic_id=content.read_int_16(),
69-
working_graphic_id=content.read_int_16(),
70-
carrying_graphic_id=content.read_int_16(),
71-
resource_gathering_sound_id=content.read_int_16(),
72-
resource_deposit_sound_id=content.read_int_16(),
73-
wwise_resource_gathering_sound_id=content.read_int_32(),
74-
wwise_resource_deposit_sound_id=content.read_int_32(),
83+
task_type=task_type,
84+
id=id_,
85+
is_default=is_default,
86+
action_type=action_type,
87+
class_id=class_id,
88+
unit_id=unit_id,
89+
terrain_id=terrain_id,
90+
resource_in=resource_in,
91+
resource_multiplier=resource_multiplier,
92+
resource_out=resource_out,
93+
unused_resource=unused_resource,
94+
work_value_1=work_value_1,
95+
work_value_2=work_value_2,
96+
work_range=work_range,
97+
auto_search_targets=auto_search_targets,
98+
search_wait_time=search_wait_time,
99+
enable_targeting=enable_targeting,
100+
combat_level_flag=combat_level_flag,
101+
gather_type=gather_type,
102+
work_flag_2=work_flag_2,
103+
target_diplomacy=target_diplomacy,
104+
carry_check=carry_check,
105+
pick_for_construction=pick_for_construction,
106+
moving_graphic_id=moving_graphic_id,
107+
proceeding_graphic_id=proceeding_graphic_id,
108+
working_graphic_id=working_graphic_id,
109+
carrying_graphic_id=carrying_graphic_id,
110+
resource_gathering_sound_id=resource_gathering_sound_id,
111+
resource_deposit_sound_id=resource_deposit_sound_id,
112+
wwise_resource_gathering_sound_id=wwise_resource_gathering_sound_id,
113+
wwise_resource_deposit_sound_id=wwise_resource_deposit_sound_id,
75114
)
76115

77116
def to_bytes(self, version: Version) -> bytes:
78-
return b''.join([
79-
self.write_int_16(self.task_type),
80-
self.write_int_16(self.id),
81-
self.write_int_8(self.is_default),
82-
self.write_int_16(self.action_type),
83-
self.write_int_16(self.class_id),
84-
self.write_int_16(self.unit_id),
85-
self.write_int_16(self.terrain_id),
86-
self.write_int_16(self.resource_in),
87-
self.write_int_16(self.resource_multiplier),
88-
self.write_int_16(self.resource_out),
89-
self.write_int_16(self.unused_resource),
90-
self.write_float(self.work_value_1),
91-
self.write_float(self.work_value_2),
92-
self.write_float(self.work_range),
93-
self.write_int_8(self.auto_search_targets),
94-
self.write_float(self.search_wait_time),
95-
self.write_int_8(self.enable_targeting),
96-
self.write_int_8(self.combat_level_flag),
97-
self.write_int_16(self.gather_type),
98-
self.write_int_16(self.work_flag_2),
99-
self.write_int_8(self.target_diplomacy),
100-
self.write_int_8(self.carry_check),
101-
self.write_int_8(self.pick_for_construction),
102-
self.write_int_16(self.moving_graphic_id),
103-
self.write_int_16(self.proceeding_graphic_id),
104-
self.write_int_16(self.working_graphic_id),
105-
self.write_int_16(self.carrying_graphic_id),
106-
self.write_int_16(self.resource_gathering_sound_id),
107-
self.write_int_16(self.resource_deposit_sound_id),
108-
self.write_int_32(self.wwise_resource_gathering_sound_id),
109-
self.write_int_32(self.wwise_resource_deposit_sound_id),
110-
])
117+
return struct.pack(FORMAT,
118+
self.task_type,
119+
self.id,
120+
self.is_default,
121+
self.action_type,
122+
self.class_id,
123+
self.unit_id,
124+
self.terrain_id,
125+
self.resource_in,
126+
self.resource_multiplier,
127+
self.resource_out,
128+
self.unused_resource,
129+
self.work_value_1,
130+
self.work_value_2,
131+
self.work_range,
132+
self.auto_search_targets,
133+
self.search_wait_time,
134+
self.enable_targeting,
135+
self.combat_level_flag,
136+
self.gather_type,
137+
self.work_flag_2,
138+
self.target_diplomacy,
139+
self.carry_check,
140+
self.pick_for_construction,
141+
self.moving_graphic_id,
142+
self.proceeding_graphic_id,
143+
self.working_graphic_id,
144+
self.carrying_graphic_id,
145+
self.resource_gathering_sound_id,
146+
self.resource_deposit_sound_id,
147+
self.wwise_resource_gathering_sound_id,
148+
self.wwise_resource_deposit_sound_id,
149+
)

0 commit comments

Comments
 (0)