Files
Fojadrachi e5754cca18 feat: add FS25_FarmHUD mod and FarmHUD desktop companion app
Initial commit: the FS25 Lua mod (live.json data export), the
FS25-WebDashboard project (Node/web dashboard + PySide6 desktop
variant), and FarmHUD (standalone PySide6 companion app with
Discord Rich Presence, settings, and installer).
2026-09-27 15:25:07 +02:00

87 lines
3.1 KiB
Python

"""Locates and reads the FS25 WebDashboard mod's live.json file.
Replaces the file-locating part of ``server/server.js`` - same candidate
paths (including the OneDrive-redirected-Documents cases), same
``FS25_LIVE_JSON`` environment variable override, same "keep re-resolving
until the file actually appears" behaviour (the game may not have written it
yet on first launch).
"""
from __future__ import annotations
import json
import os
from pathlib import Path
from typing import Any, Optional
def candidate_live_data_paths() -> list[Path]:
home = Path.home()
candidates: list[Path] = []
env_path = os.environ.get("FS25_LIVE_JSON")
if env_path:
candidates.append(Path(env_path))
# OneDrive can redirect "Documents" - check both the plain and OneDrive path.
tail = Path("My Games") / "FarmingSimulator2025" / "modSettings" / "FS25_FarmHUD" / "live.json"
candidates.append(home / "Documents" / tail)
candidates.append(home / "OneDrive" / "Documents" / tail)
candidates.append(home / "OneDrive" / "Dokumente" / tail)
return candidates
class LiveDataSource:
"""Resolves live.json's path once, then keeps re-checking it exists.
Mirrors ``resolveLiveDataPath`` / ``serveLiveData`` from server.js:
picks the first candidate that exists, falls back to the first guess
otherwise, and re-resolves on every read if the current path stops
existing (e.g. the server/app was started before the game wrote it).
"""
def __init__(self) -> None:
self.path: Optional[Path] = self._resolve()
def _resolve(self) -> Optional[Path]:
candidates = candidate_live_data_paths()
for candidate in candidates:
if candidate.exists():
return candidate
return candidates[0] if candidates else None
def read(self) -> dict[str, Any]:
"""Returns the parsed live.json, or a `connected: false` stand-in."""
if self.path is None or not self.path.exists():
resolved = self._resolve()
if resolved is not None:
self.path = resolved
if self.path is None or not self.path.exists():
return {
"connected": False,
"error": (
"live.json not found yet - start Farming Simulator 25 "
"with the WebDashboard mod active and load a savegame."
),
"expectedPath": str(self.path) if self.path else None,
}
try:
text = self.path.read_text(encoding="utf-8")
data = json.loads(text)
except (OSError, json.JSONDecodeError):
# The mod writes this file 4x/second - reading it mid-write can
# occasionally race into a truncated file. Treat that the same
# as "not connected yet" rather than crashing the poll loop;
# the next tick reads it fine.
return {
"connected": False,
"error": "live.json is being written, retrying…",
"expectedPath": str(self.path),
}
if isinstance(data, dict):
data.setdefault("connected", True)
return data