mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
Remove eager loading of factions.
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.
This commit is contained in:
@@ -1,4 +1 @@
|
||||
from .faction import Faction
|
||||
from .factionloader import FactionLoader
|
||||
|
||||
FACTIONS = FactionLoader()
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Dict, Iterator, List, Optional, Type
|
||||
|
||||
from game import persistence
|
||||
from game.factions.faction import Faction
|
||||
|
||||
FACTION_DIRECTORY = Path("./resources/factions/")
|
||||
|
||||
|
||||
class FactionLoader:
|
||||
def __init__(self) -> None:
|
||||
self._factions: Optional[Dict[str, Faction]] = None
|
||||
|
||||
@property
|
||||
def factions(self) -> Dict[str, Faction]:
|
||||
self.initialize()
|
||||
assert self._factions is not None
|
||||
return self._factions
|
||||
|
||||
def initialize(self) -> None:
|
||||
if self._factions is None:
|
||||
self._factions = self.load_factions()
|
||||
|
||||
@staticmethod
|
||||
def find_faction_files_in(path: Path) -> List[Path]:
|
||||
return [f for f in path.glob("*.json") if f.is_file()]
|
||||
|
||||
@classmethod
|
||||
def load_factions(cls: Type[FactionLoader]) -> Dict[str, Faction]:
|
||||
user_faction_path = Path(persistence.base_path()) / "Liberation/Factions"
|
||||
files = cls.find_faction_files_in(
|
||||
FACTION_DIRECTORY
|
||||
) + cls.find_faction_files_in(user_faction_path)
|
||||
factions = {}
|
||||
|
||||
for f in files:
|
||||
try:
|
||||
with f.open("r", encoding="utf-8") as fdata:
|
||||
data = json.load(fdata)
|
||||
factions[data["name"]] = Faction.from_json(data)
|
||||
logging.info("Loaded faction : " + str(f))
|
||||
except Exception:
|
||||
logging.exception(f"Unable to load faction : {f}")
|
||||
|
||||
return factions
|
||||
|
||||
def __getitem__(self, name: str) -> Faction:
|
||||
return self.factions[name]
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(self.factions.keys())
|
||||
46
game/factions/factions.py
Normal file
46
game/factions/factions.py
Normal file
@@ -0,0 +1,46 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user