mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
Eager loading meant that users would need to restart Liberation to pick up changes to faction files. That's annoying for modders, slows down start up, and uselessly sits in RAM when it's not needed after game creation. Also removes the __getitem__ and __iter__ methods in favor of named methods, since the dunder methods are more or less impenetrable for IDEs and grep.
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
from collections.abc import Iterator
|
|
from pathlib import Path
|
|
|
|
from game import persistence
|
|
from .faction import Faction
|
|
|
|
|
|
class Factions:
|
|
def __init__(self, factions: dict[str, Faction]) -> None:
|
|
self.factions = factions
|
|
|
|
def get_by_name(self, name: str) -> Faction:
|
|
return self.factions[name]
|
|
|
|
def iter_faction_names(self) -> Iterator[str]:
|
|
return iter(self.factions.keys())
|
|
|
|
@staticmethod
|
|
def iter_faction_files_in(path: Path) -> Iterator[Path]:
|
|
yield from path.glob("*.json")
|
|
|
|
@classmethod
|
|
def iter_faction_files(cls) -> Iterator[Path]:
|
|
yield from cls.iter_faction_files_in(Path("resources/factions/"))
|
|
yield from cls.iter_faction_files_in(
|
|
Path(persistence.base_path()) / "Liberation/Factions"
|
|
)
|
|
|
|
@classmethod
|
|
def load(cls) -> Factions:
|
|
factions = {}
|
|
for path in cls.iter_faction_files():
|
|
try:
|
|
with path.open("r", encoding="utf-8") as fdata:
|
|
data = json.load(fdata)
|
|
faction = Faction.from_json(data)
|
|
factions[faction.name] = faction
|
|
logging.info("Loaded faction from %s", path)
|
|
except Exception:
|
|
logging.exception(f"Unable to load faction from %s", path)
|
|
|
|
return Factions(factions)
|