Files
Spellcast3d/code/spellcast/forge_config.py
Igor AherneandClaude Opus 5.5 56d85f6eeb Spellcast3D 0.1.0
A free, open-source Windows app that takes an artist from an idea to game-ready 3D props on their own PC: pictures with Qwen-Image 2.1, 3D models with Pixal3D, an optional repaint of their textures, retopology with TriFlow, LATO.2 or Instant Meshes, and a baker for the final textures. See README.md, and DEVELOPERS.md for building from source.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-25 02:38:33 +01:00

53 lines
2.2 KiB
Python

"""Forge's own config.json (code/forge/config.json), which both the installer and the hub prepare. STDLIB ONLY.
Forge's launch.py runs verify_version() at every start, and that blocks on a "Press Enter"
input() whenever config.json exists without the VERSION_UID it expects (with stdin=DEVNULL the
input() crashes the start instead). So the installer seeds the value before Forge's own setup
step, and the hub checks it again before every launch.
"""
from __future__ import annotations
import json
import os
import re
from pathlib import Path
from . import paths
CONFIG = paths.FORGE_DIR / "config.json"
def forge_version_uid() -> str:
"""The VERSION_UID Forge wants in its config.json (read from its modules/launch_utils.py)."""
try:
text = (paths.FORGE_DIR / "modules" / "launch_utils.py").read_text(encoding="utf-8", errors="replace")
except OSError:
text = ""
m = re.search(r'^VERSION_UID\b[^=\n]*=\s*"([^"]+)"', text, re.MULTILINE)
return m.group(1) if m else "PY313"
def ensure_forge_config(cfg: Path = CONFIG) -> str | None:
"""Make config.json carry Forge's VERSION_UID. Returns a message when an unreadable file was set aside.
A new config also gets auto_launch_browser=Disable (Forge's default "Local" would open a browser
tab; under the hub SD_WEBUI_RESTARTING=1 already prevents that). A choice the user made in Forge's
own settings is kept. The file is rewritten only when something changed, in Forge's own format.
Raises OSError when the file cannot be read or written.
"""
note = None
data: dict = {}
if cfg.exists():
try:
loaded = json.loads(cfg.read_text(encoding="utf-8"))
data = loaded if isinstance(loaded, dict) else {}
except ValueError: # includes UnicodeDecodeError; Forge would crash on it too
os.replace(cfg, cfg.with_name("config.json.broken"))
note = "Forge's config.json could not be read; it was renamed to config.json.broken."
wanted = {**data, "VERSION_UID": forge_version_uid()}
wanted.setdefault("auto_launch_browser", "Disable")
if wanted != data or not cfg.exists():
paths.write_text_atomic(cfg, json.dumps(wanted, indent=4, ensure_ascii=False))
return note