Files
Launcher/manager/settings_models.py
T

97 lines
3.0 KiB
Python
Raw Normal View History

2026-08-03 22:15:55 +08:00
"""Launcher settings schemas persisted by SettingsManager."""
import warnings
from typing import Literal, Any
2026-08-03 22:15:55 +08:00
from uuid import UUID
2026-10-30 00:16:04 +08:00
from pydantic import BaseModel, ConfigDict, field_validator, model_validator, Field
2026-08-03 22:15:55 +08:00
from constant import CLIENT_ID
class WarnExtraModel(BaseModel):
model_config = ConfigDict(extra="ignore")
2026-08-03 22:15:55 +08:00
@model_validator(mode="before")
@classmethod
def warn_extra_fields(cls, data: Any) -> Any:
if isinstance(data, dict):
extra = set(data) - set(cls.model_fields)
if extra:
warnings.warn(
f"{cls.__name__}: ignored extra fields: {sorted(extra)}",
UserWarning,
stacklevel=2,
)
return data
class GeneralSettings(WarnExtraModel):
model_config = ConfigDict()
2026-08-03 22:15:55 +08:00
language: Literal["en", "zh-TW"] = "zh-TW"
2026-11-14 17:10:33 +08:00
compatibility_mode: Literal["none", "official-launcher"] = 'none'
2026-08-03 22:15:55 +08:00
class AppearanceSettings(WarnExtraModel):
model_config = ConfigDict()
2026-08-03 22:15:55 +08:00
theme: str = "system"
2026-11-10 22:05:18 +08:00
launch_background: str = ""
@field_validator("theme")
@classmethod
def validate_theme_name(cls, value: str) -> str:
value = value.strip()
if not value or "/" in value or "\\" in value or ".." in value:
raise ValueError("invalid theme name")
return value
2026-08-03 22:15:55 +08:00
class AccountSettings(WarnExtraModel):
model_config = ConfigDict()
2026-08-03 22:15:55 +08:00
credentials_save_method: Literal["system-keyring", "file"] = "system-keyring"
custom_client_id: UUID | None = None
2026-11-14 17:10:33 +08:00
enable_token_updater: bool = True
token_refresh_interval: int = 600
@field_validator("custom_client_id", mode="before")
@classmethod
def validate_custom_client_id(cls, value: str | None | UUID) -> UUID | None:
if value is None:
return None
if isinstance(value, UUID):
return value
if isinstance(value, str):
value = value.strip()
if not value:
return None
try:
return UUID(value)
except (TypeError, ValueError, AttributeError) as error:
raise ValueError("invalid client ID") from error
@field_validator("token_refresh_interval", mode="before")
@classmethod
def validate_token_refresh_interval(cls, value: int) -> int:
if value < 60 or value > 86400:
raise ValueError("token refresh interval must be at least 60 seconds and at most 1 day")
2026-10-30 00:16:04 +08:00
return value
class PluginSettings(WarnExtraModel):
2026-10-30 23:24:28 +08:00
enabled: list[str] = Field(default_factory=list)
is_plugin_warning_shown: bool = False
class PageBarSettings(WarnExtraModel):
focus_at_after_restart: bool = False
last_focused_page: str = ""
2026-11-10 22:05:18 +08:00
page_bar_position: Literal["left", "right", "top", "bottom"] = "left"
class GameSettings(WarnExtraModel):
verify_hash_on_download: bool = True
verify_hash_on_launch: bool = True
2026-11-14 17:10:33 +08:00
open_log_view_when_launch: bool = True
action_after_launch: Literal["close", "stay-open", "minimize"] = "stay-open"