|
2 | 2 | from typing import Annotated |
3 | 3 |
|
4 | 4 | from murmurhash2 import murmurhash2 |
5 | | -from pydantic import ConfigDict, Field, BaseModel, computed_field, WithJsonSchema |
| 5 | +from pydantic import ConfigDict, Field, BaseModel, computed_field, WithJsonSchema, field_validator |
6 | 6 |
|
7 | | -LOGGER = logging.getLogger(__name__) |
| 7 | +from deadlock_assets_api.models.v1.colors import ColorV1 |
8 | 8 |
|
9 | | -Color = tuple[int, int, int] | tuple[int, int, int, int] |
| 9 | +LOGGER = logging.getLogger(__name__) |
10 | 10 |
|
11 | 11 |
|
12 | 12 | class ModifierValue(BaseModel): |
@@ -70,7 +70,7 @@ class MiscV2(BaseModel): |
70 | 70 | model_config = ConfigDict(populate_by_name=True) |
71 | 71 |
|
72 | 72 | class_name: str |
73 | | - color: Color | None = Field(None, validation_alias="m_Color") |
| 73 | + color: ColorV1 | None = Field(None, validation_alias="m_Color") |
74 | 74 |
|
75 | 75 | # Spawning & Timing |
76 | 76 | initial_spawn_time: float | None = Field(None, validation_alias="m_flInitialSpawnTime") |
@@ -134,3 +134,16 @@ class MiscV2(BaseModel): |
134 | 134 | @property |
135 | 135 | def id(self) -> Annotated[int, WithJsonSchema({"format": "int64", "type": "integer"})]: |
136 | 136 | return murmurhash2(self.class_name.encode(), 0x31415926) |
| 137 | + |
| 138 | + @field_validator("color", mode="before") |
| 139 | + @classmethod |
| 140 | + def validate_color(cls, v: ColorV1 | list[int] | None | dict[str, int]) -> ColorV1 | None: |
| 141 | + if v is None: |
| 142 | + return v |
| 143 | + if isinstance(v, ColorV1): |
| 144 | + return v |
| 145 | + if isinstance(v, dict): |
| 146 | + return ColorV1.model_validate(v) |
| 147 | + if isinstance(v, list): |
| 148 | + return ColorV1.from_list(v) |
| 149 | + raise TypeError(f"Invalid type for color field: {type(v)}") |
0 commit comments