the_goth_cardgame_collection/controller/game_loader.py

49 lines
1.8 KiB
Python
Raw Normal View History

2026-11-14 04:25:51 +00:00
# -*- coding: utf-8 -*-
# controller/game_loader.py
import importlib.util
import sys
from pathlib import Path
from resources.code_res.game_constants import (
CAT_LUCK, ENTRY_FILENAME, FOLDER_CATEGORY_MAP, GAME_FOLDERS, GAMES_ROOT_DIR
)
from resources.code_res.i18n import t
class GameLoader:
def __init__(self, root_dir: Path):
self.games_dir = root_dir / GAMES_ROOT_DIR
def discover_games(self) -> list[dict]:
discovered = []
for rel_folder in GAME_FOLDERS:
entry_file = self.games_dir / rel_folder / ENTRY_FILENAME
if not entry_file.is_file():
continue
mod_name = "goth_game_" + rel_folder.replace("/", "_")
spec = importlib.util.spec_from_file_location(mod_name, entry_file)
if spec and spec.loader:
module = importlib.util.module_from_spec(spec)
sys.modules[mod_name] = module
spec.loader.exec_module(module)
if hasattr(module, "start_game"):
title_key = getattr(module, "GAME_TITLE_KEY", None)
title = (
t(title_key)
if title_key
else getattr(module, "GAME_TITLE", rel_folder.split("/")[-1].replace("_", " ").title())
)
category = getattr(
module,
"GAME_CATEGORY",
FOLDER_CATEGORY_MAP.get(rel_folder, CAT_LUCK),
)
discovered.append({
"folder": rel_folder,
"category": category,
"title": title,
"start": module.start_game,
})
return discovered