summaryrefslogtreecommitdiff
path: root/tools/assets/descriptor/z64resources.py
blob: 9f241c3118730f7376be4718e8729685fc091eeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# SPDX-FileCopyrightText: © 2025 ZeldaRET
# SPDX-License-Identifier: CC0-1.0

import dataclasses
import enum
from typing import Optional
from xml.etree.ElementTree import Element

from .base import (
    ResourceDesc,
    ResourcesDescCollection,
    ResourceHandlerNeedsPass2Exception,
    ResourceHasNoSizeError,
)
from . import xml_errors


@dataclasses.dataclass(eq=False)
class CollisionResourceDesc(ResourceDesc):
    def get_size(self):
        return 0x2C


def handler_Collision(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name"}, {"Offset"})
    return CollisionResourceDesc(symbol_name, offset, collection, reselem)


@dataclasses.dataclass(eq=False)
class AnimationResourceDesc(ResourceDesc):
    def get_size(self):
        return 0x10


def handler_Animation(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name"}, {"Offset"})
    return AnimationResourceDesc(symbol_name, offset, collection, reselem)


@dataclasses.dataclass(eq=False)
class PlayerAnimationResourceDesc(ResourceDesc):
    pass


def handler_PlayerAnimation(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name"}, {"Offset"})
    return PlayerAnimationResourceDesc(symbol_name, offset, collection, reselem)


@dataclasses.dataclass(eq=False)
class LegacyAnimationResourceDesc(ResourceDesc):
    pass


def handler_LegacyAnimation(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name"}, {"Offset"})
    return LegacyAnimationResourceDesc(symbol_name, offset, collection, reselem)


@dataclasses.dataclass(eq=False)
class CutsceneResourceDesc(ResourceDesc):
    pass


def handler_Cutscene(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name"}, {"Offset"})
    return CutsceneResourceDesc(symbol_name, offset, collection, reselem)


@dataclasses.dataclass(eq=False)
class SceneResourceDesc(ResourceDesc):
    pass


def handler_Scene(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name"}, {"Offset"})
    return SceneResourceDesc(symbol_name, offset, collection, reselem)


@dataclasses.dataclass(eq=False)
class RoomResourceDesc(ResourceDesc):
    pass


def handler_Room(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name"}, {"Offset", "HackMode"})
    res = RoomResourceDesc(symbol_name, offset, collection, reselem)
    if reselem.attrib.get("HackMode") == "syotes_room":
        res.hack_modes.add("hackmode_syotes_room")
    return res


@dataclasses.dataclass(eq=False)
class PlayerAnimationDataResourceDesc(ResourceDesc):
    frame_count: int


def handler_PlayerAnimationData(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name", "FrameCount"}, {"Offset"})
    frame_count = int(reselem.attrib["FrameCount"])
    return PlayerAnimationDataResourceDesc(
        symbol_name, offset, collection, reselem, frame_count
    )


@dataclasses.dataclass(eq=False)
class PathListResourceDesc(ResourceDesc):
    num_paths: int


def handler_PathList(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name", "NumPaths"}, {"Offset"})
    num_paths = int(reselem.attrib["NumPaths"])
    return PathListResourceDesc(symbol_name, offset, collection, reselem, num_paths)


class SkeletonType(enum.Enum):
    NORMAL = enum.auto()
    FLEX = enum.auto()
    CURVE = enum.auto()


class LimbType(enum.Enum):
    STANDARD = enum.auto()
    LOD = enum.auto()
    SKIN = enum.auto()
    CURVE = enum.auto()
    LEGACY = enum.auto()


@dataclasses.dataclass(eq=False)
class SkeletonResourceDesc(ResourceDesc):
    type: SkeletonType
    limb_type: LimbType
    limb_enum_name: Optional[str]
    limb_enum_none_member_name: Optional[str]
    limb_enum_max_member_name: Optional[str]

    def get_size(self):
        skel_size = {
            SkeletonType.NORMAL: 0x8,
            SkeletonType.FLEX: 0xC,
        }.get(self.type)
        if skel_size is None:
            raise ResourceHasNoSizeError()
        return skel_size


def handler_Skeleton(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(
        reselem,
        {"Name", "Type", "LimbType"},
        {"Offset", "EnumName", "LimbNone", "LimbMax"},
    )
    skel_type = SkeletonType[reselem.attrib["Type"].upper()]
    limb_type = LimbType[reselem.attrib["LimbType"].upper()]
    return SkeletonResourceDesc(
        symbol_name,
        offset,
        collection,
        reselem,
        skel_type,
        limb_type,
        reselem.attrib.get("EnumName"),
        reselem.attrib.get("LimbNone"),
        reselem.attrib.get("LimbMax"),
    )


@dataclasses.dataclass(eq=False)
class LimbResourceDesc(ResourceDesc):
    limb_type: LimbType
    limb_enum_member_name: Optional[str]

    def get_size(self):
        limb_size = {
            LimbType.STANDARD: 0xC,
        }.get(self.limb_type)
        if limb_size is None:
            raise ResourceHasNoSizeError()
        return limb_size


def handler_Limb(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name", "LimbType"}, {"Offset", "EnumName"})
    limb_type = LimbType[reselem.attrib["LimbType"].upper()]
    return LimbResourceDesc(
        symbol_name,
        offset,
        collection,
        reselem,
        limb_type,
        reselem.attrib.get("EnumName"),
    )


@dataclasses.dataclass(eq=False)
class LimbTableResourceDesc(ResourceDesc):
    limb_type: LimbType
    count: int

    def get_size(self):
        return self.count * 4


def handler_LimbTable(symbol_name, offset, collection, reselem: Element):
    xml_errors.check_attrib(reselem, {"Name", "LimbType", "Count"}, {"Offset"})
    limb_type = LimbType[reselem.attrib["LimbType"].upper()]
    count = int(reselem.attrib["Count"])
    return LimbTableResourceDesc(
        symbol_name, offset, collection, reselem, limb_type, count
    )


@dataclasses.dataclass(eq=False)
class CurveAnimationResourceDesc(ResourceDesc):
    skeleton: SkeletonResourceDesc


def handler_CurveAnimation(
    symbol_name, offset, collection: ResourcesDescCollection, reselem: Element
):
    xml_errors.check_attrib(reselem, {"Name", "SkelOffset"}, {"Offset"})
    res = CurveAnimationResourceDesc(symbol_name, offset, collection, reselem, None)

    skel_offset = int(reselem.attrib["SkelOffset"], 16)

    def pass2_callback(pool):
        matching_tlut_resources = [
            res for res in collection.resources if res.offset == skel_offset
        ]
        assert len(matching_tlut_resources) == 1, (
            f"Found {len(matching_tlut_resources)} resources at SkelOffset "
            f"0x{skel_offset:X} instead of exactly one"
        )
        assert isinstance(
            matching_tlut_resources[0], SkeletonResourceDesc
        ), matching_tlut_resources[0]
        res.skeleton = matching_tlut_resources[0]

    raise ResourceHandlerNeedsPass2Exception(res, pass2_callback)