148 lines
5.1 KiB
Python
148 lines
5.1 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# models/db_locator.py
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from resources.code_res.game_constants import USER_DB_FILENAME
|
||
|
|
|
||
|
|
APP_CONFIG_DIR_WIN = "TheGothCardgameCollection"
|
||
|
|
APP_CONFIG_DIR_UNIX = "the_goth_cardgame_collection"
|
||
|
|
LOCATOR_FILENAME = "db_locator.json"
|
||
|
|
|
||
|
|
|
||
|
|
class DbLocator:
|
||
|
|
"""
|
||
|
|
Speichert außerhalb des Installationsordners (im OS-Nutzerprofil), wo die aktive
|
||
|
|
user_data.db liegt und welche Online-Serveradresse zuletzt verwendet wurde.
|
||
|
|
Dadurch bleiben Updates & spätere Installer komplett unabhängig vom Programmordner.
|
||
|
|
"""
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_os_config_dir() -> Path:
|
||
|
|
if sys.platform.startswith("win"):
|
||
|
|
base = Path(os.environ.get("APPDATA", Path.home() / "AppData" / "Roaming"))
|
||
|
|
cfg_dir = base / APP_CONFIG_DIR_WIN
|
||
|
|
elif sys.platform == "darwin":
|
||
|
|
cfg_dir = Path.home() / "Library" / "Application Support" / APP_CONFIG_DIR_WIN
|
||
|
|
else:
|
||
|
|
xdg = os.environ.get("XDG_CONFIG_HOME")
|
||
|
|
base = Path(xdg) if xdg else (Path.home() / ".config")
|
||
|
|
cfg_dir = base / APP_CONFIG_DIR_UNIX
|
||
|
|
cfg_dir.mkdir(parents=True, exist_ok=True)
|
||
|
|
return cfg_dir
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def get_locator_file(cls) -> Path:
|
||
|
|
return cls.get_os_config_dir() / LOCATOR_FILENAME
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def load_config(cls) -> dict:
|
||
|
|
p = cls.get_locator_file()
|
||
|
|
if p.is_file():
|
||
|
|
try:
|
||
|
|
data = json.loads(p.read_text(encoding="utf-8"))
|
||
|
|
if isinstance(data, dict):
|
||
|
|
return data
|
||
|
|
except Exception:
|
||
|
|
pass
|
||
|
|
return {}
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def save_config(cls, updates: dict) -> None:
|
||
|
|
cfg = cls.load_config()
|
||
|
|
cfg.update(updates)
|
||
|
|
try:
|
||
|
|
cls.get_locator_file().write_text(
|
||
|
|
json.dumps(cfg, indent=2, ensure_ascii=False),
|
||
|
|
encoding="utf-8",
|
||
|
|
)
|
||
|
|
except Exception:
|
||
|
|
pass
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def resolve_initial_db_path(cls, default_fallback_dir: Path) -> Path:
|
||
|
|
"""
|
||
|
|
Ermittelt beim App-Start den aktiven Pfad zur user_data.db:
|
||
|
|
1. Gespeicherter Pfad aus db_locator.json (falls Verzeichnis erreichbar/beschreibbar).
|
||
|
|
2. Bestehende user_data.db im bisherigen Standardordner (Abwärtskompatibilität).
|
||
|
|
3. Sonst Standard-Nutzerdatenordner (für das Erst-Setup).
|
||
|
|
"""
|
||
|
|
cfg = cls.load_config()
|
||
|
|
saved_raw = str(cfg.get("user_db_path", "")).strip()
|
||
|
|
if saved_raw:
|
||
|
|
cand = Path(saved_raw).expanduser()
|
||
|
|
try:
|
||
|
|
if cand.is_file():
|
||
|
|
return cand.resolve()
|
||
|
|
cand.parent.mkdir(parents=True, exist_ok=True)
|
||
|
|
if os.access(cand.parent, os.W_OK):
|
||
|
|
return cand.resolve()
|
||
|
|
except Exception:
|
||
|
|
pass
|
||
|
|
|
||
|
|
legacy_file = (default_fallback_dir / USER_DB_FILENAME).resolve()
|
||
|
|
if legacy_file.is_file():
|
||
|
|
cls.remember_db_path(legacy_file)
|
||
|
|
return legacy_file
|
||
|
|
|
||
|
|
if getattr(sys, "frozen", False):
|
||
|
|
os_data_dir = cls._default_installed_data_dir()
|
||
|
|
return (os_data_dir / USER_DB_FILENAME).resolve()
|
||
|
|
|
||
|
|
return legacy_file
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _default_installed_data_dir() -> Path:
|
||
|
|
if sys.platform.startswith("win"):
|
||
|
|
base = Path(os.environ.get("LOCALAPPDATA", Path.home() / "AppData" / "Local"))
|
||
|
|
d = base / APP_CONFIG_DIR_WIN
|
||
|
|
elif sys.platform == "darwin":
|
||
|
|
d = Path.home() / "Library" / "Application Support" / APP_CONFIG_DIR_WIN
|
||
|
|
else:
|
||
|
|
xdg = os.environ.get("XDG_DATA_HOME")
|
||
|
|
base = Path(xdg) if xdg else (Path.home() / ".local" / "share")
|
||
|
|
d = base / APP_CONFIG_DIR_UNIX
|
||
|
|
d.mkdir(parents=True, exist_ok=True)
|
||
|
|
return d
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def remember_db_path(cls, db_file_path: Path) -> None:
|
||
|
|
resolved = db_file_path.expanduser().resolve()
|
||
|
|
cfg = cls.load_config()
|
||
|
|
history: list[str] = list(cfg.get("known_db_paths", []))
|
||
|
|
s_path = str(resolved)
|
||
|
|
if s_path in history:
|
||
|
|
history.remove(s_path)
|
||
|
|
history.insert(0, s_path)
|
||
|
|
cls.save_config({
|
||
|
|
"user_db_path": s_path,
|
||
|
|
"user_db_dir": str(resolved.parent),
|
||
|
|
"known_db_paths": history[:8],
|
||
|
|
})
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def load_online_prefs(cls) -> dict:
|
||
|
|
cfg = cls.load_config()
|
||
|
|
return {
|
||
|
|
"addr_mode": cfg.get("online_addr_mode", "url"),
|
||
|
|
"url": cfg.get("online_url", "https://"),
|
||
|
|
"ip": cfg.get("online_ip", "127.0.0.1"),
|
||
|
|
"port": str(cfg.get("online_port", "8443")),
|
||
|
|
"username": cfg.get("online_username", ""),
|
||
|
|
}
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def save_online_prefs(
|
||
|
|
cls, addr_mode: str, url: str, ip: str, port: str, username: str
|
||
|
|
) -> None:
|
||
|
|
cls.save_config({
|
||
|
|
"online_addr_mode": addr_mode,
|
||
|
|
"online_url": url.strip(),
|
||
|
|
"online_ip": ip.strip(),
|
||
|
|
"online_port": str(port).strip(),
|
||
|
|
"online_username": username.strip(),
|
||
|
|
})
|