Give default values for TextureEntry fields
This commit is contained in:
@@ -1022,8 +1022,18 @@ class TEExceptionField(se.SerializableBase):
|
||||
return dict
|
||||
|
||||
|
||||
def _te_dataclass_field(spec: se.SERIALIZABLE_TYPE, first=False, optional=False):
|
||||
return se.dataclass_field(TEExceptionField(spec, first=first, optional=optional))
|
||||
def _te_field(spec: se.SERIALIZABLE_TYPE, first=False, optional=False,
|
||||
default_factory=dataclasses.MISSING, default=dataclasses.MISSING):
|
||||
if default_factory is not dataclasses.MISSING:
|
||||
new_default_factory = lambda: {None: default_factory()}
|
||||
elif default is not None:
|
||||
new_default_factory = lambda: {None: default}
|
||||
else:
|
||||
new_default_factory = dataclasses.MISSING
|
||||
return se.dataclass_field(
|
||||
TEExceptionField(spec, first=first, optional=optional),
|
||||
default_factory=new_default_factory,
|
||||
)
|
||||
|
||||
|
||||
_T = TypeVar("_T")
|
||||
@@ -1032,18 +1042,25 @@ TE_FIELD_TYPE = Dict[Optional[Sequence[int]], _T]
|
||||
|
||||
@dataclasses.dataclass
|
||||
class TextureEntry:
|
||||
Textures: TE_FIELD_TYPE[UUID] = _te_dataclass_field(se.UUID, first=True)
|
||||
Textures: TE_FIELD_TYPE[UUID] = _te_field(
|
||||
# Plywood texture
|
||||
se.UUID, first=True, default=UUID('89556747-24cb-43ed-920b-47caed15465f'))
|
||||
# Bytes are inverted so fully opaque white is \x00\x00\x00\x00
|
||||
Color: TE_FIELD_TYPE[bytes] = _te_dataclass_field(Color4(invert_bytes=True))
|
||||
ScalesS: TE_FIELD_TYPE[float] = _te_dataclass_field(se.F32)
|
||||
ScalesT: TE_FIELD_TYPE[float] = _te_dataclass_field(se.F32)
|
||||
OffsetsS: TE_FIELD_TYPE[int] = _te_dataclass_field(se.S16)
|
||||
OffsetsT: TE_FIELD_TYPE[int] = _te_dataclass_field(se.S16)
|
||||
Rotation: TE_FIELD_TYPE[int] = _te_dataclass_field(se.S16)
|
||||
BasicMaterials: TE_FIELD_TYPE["BasicMaterials"] = _te_dataclass_field(BUMP_SHINY_FULLBRIGHT)
|
||||
MediaFlags: TE_FIELD_TYPE["MediaFlags"] = _te_dataclass_field(MEDIA_FLAGS)
|
||||
Glow: TE_FIELD_TYPE[int] = _te_dataclass_field(se.U8)
|
||||
Materials: TE_FIELD_TYPE[UUID] = _te_dataclass_field(se.UUID, optional=True)
|
||||
Color: TE_FIELD_TYPE[bytes] = _te_field(Color4(invert_bytes=True), default=b"\xff\xff\xff\xff")
|
||||
ScalesS: TE_FIELD_TYPE[float] = _te_field(se.F32, default=1.0)
|
||||
ScalesT: TE_FIELD_TYPE[float] = _te_field(se.F32, default=1.0)
|
||||
OffsetsS: TE_FIELD_TYPE[int] = _te_field(se.S16, default=0)
|
||||
OffsetsT: TE_FIELD_TYPE[int] = _te_field(se.S16, default=0)
|
||||
Rotation: TE_FIELD_TYPE[int] = _te_field(se.S16, default=0)
|
||||
BasicMaterials: TE_FIELD_TYPE["BasicMaterials"] = _te_field(
|
||||
BUMP_SHINY_FULLBRIGHT, default_factory=lambda: BasicMaterials(Bump=0, FullBright=False, Shiny=0),
|
||||
)
|
||||
MediaFlags: TE_FIELD_TYPE["MediaFlags"] = _te_field(
|
||||
MEDIA_FLAGS,
|
||||
default_factory=lambda: MediaFlags(WebPage=False, TexGen=TexGen.DEFAULT, _Unused=0),
|
||||
)
|
||||
Glow: TE_FIELD_TYPE[int] = _te_field(se.U8, default=0)
|
||||
Materials: TE_FIELD_TYPE[UUID] = _te_field(se.UUID, optional=True, default=UUID())
|
||||
|
||||
|
||||
TE_SERIALIZER = se.Dataclass(TextureEntry)
|
||||
|
||||
@@ -9,4 +9,4 @@ universal = 1
|
||||
[flake8]
|
||||
max-line-length = 160
|
||||
exclude = build/*, .eggs/*
|
||||
ignore = F405, F403, E501, F841, E722, W503, E741
|
||||
ignore = F405, F403, E501, F841, E722, W503, E741, E731
|
||||
|
||||
@@ -3,7 +3,7 @@ import unittest
|
||||
import hippolyzer.lib.base.serialization as se
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.message.message_formatting import HumanMessageSerializer
|
||||
from hippolyzer.lib.base.templates import TextureEntrySubfieldSerializer, TEFaceBitfield
|
||||
from hippolyzer.lib.base.templates import TextureEntrySubfieldSerializer, TEFaceBitfield, TextureEntry
|
||||
|
||||
EXAMPLE_TE = b"\x89UgG$\xcbC\xed\x92\x0bG\xca\xed\x15F_\x08\xe7\xb2\x98\x04\xca\x10;\x85\x94\x05Lj\x8d\xd4" \
|
||||
b"\x0b\x1f\x01B\xcb\xe6|\x1d,\xa7sc\xa6\x1a\xa2L\xb1u\x01\x00\x00\x00\x00\x00\x00\x00\x00\x80?" \
|
||||
@@ -31,8 +31,8 @@ class TemplateTests(unittest.TestCase):
|
||||
pod_te = {
|
||||
'Textures': {
|
||||
None: '89556747-24cb-43ed-920b-47caed15465f',
|
||||
(3,): 'e7b29804-ca10-3b85-9405-4c6a8dd40b1f',
|
||||
(0,): '42cbe67c-1d2c-a773-63a6-1aa24cb17501'
|
||||
(3,): 'ca2a983a-1802-2c0d-f41e-c6f591015d83',
|
||||
(0,): '34009069-2b10-80a1-aaa2-67116fa85dc6'
|
||||
},
|
||||
'Color': {None: b'\xff\xff\xff\xff'},
|
||||
'ScalesS': {None: 1.0},
|
||||
@@ -61,6 +61,10 @@ class TemplateTests(unittest.TestCase):
|
||||
deser = spec.deserialize(None, msg["ObjectData"]["TextureEntry"], pod=True)
|
||||
self.assertEqual(deser, pod_te)
|
||||
|
||||
def test_textureentry_defaults(self):
|
||||
te = TextureEntry()
|
||||
self.assertEqual(UUID('89556747-24cb-43ed-920b-47caed15465f'), te.Textures[None])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user