Allow factions to use YAML.

Comments! No more failures because you accidentally used a trailing
comma!

JSON still supported since it's basically free, but we should probably
remove that in 8 just so the docs can be less confusing for users who
will be confused if only one format is documented (and we definitely
won't maintain duplicate docs).
This commit is contained in:
Dan Albert
2023-04-17 22:14:44 -07:00
parent 42e9a6294b
commit 652e7d8d7b
4 changed files with 40 additions and 41 deletions

View File

@@ -4,33 +4,32 @@ import itertools
import logging
from dataclasses import dataclass, field
from functools import cached_property
from typing import Optional, Dict, Type, List, Any, Iterator, TYPE_CHECKING
from typing import Any, Dict, Iterator, List, Optional, TYPE_CHECKING, Type
import dcs
from dcs.countries import country_dict
from dcs.unittype import ShipType, StaticType
from dcs.unittype import UnitType as DcsUnitType
from dcs.unittype import ShipType, StaticType, UnitType as DcsUnitType
from game.armedforces.forcegroup import ForceGroup
from game.data.building_data import (
WW2_ALLIES_BUILDINGS,
DEFAULT_AVAILABLE_BUILDINGS,
WW2_GERMANY_BUILDINGS,
WW2_FREE,
REQUIRED_BUILDINGS,
IADS_BUILDINGS,
REQUIRED_BUILDINGS,
WW2_ALLIES_BUILDINGS,
WW2_FREE,
WW2_GERMANY_BUILDINGS,
)
from game.data.doctrine import (
COLDWAR_DOCTRINE,
Doctrine,
MODERN_DOCTRINE,
COLDWAR_DOCTRINE,
WWII_DOCTRINE,
)
from game.data.units import UnitClass
from game.data.groups import GroupRole
from game.data.units import UnitClass
from game.dcs.aircrafttype import AircraftType
from game.dcs.groundunittype import GroundUnitType
from game.dcs.shipunittype import ShipUnitType
from game.armedforces.forcegroup import ForceGroup
from game.dcs.unittype import UnitType
if TYPE_CHECKING:
@@ -168,7 +167,7 @@ class Faction:
return sorted(air_defenses)
@classmethod
def from_json(cls: Type[Faction], json: Dict[str, Any]) -> Faction:
def from_dict(cls: Type[Faction], json: Dict[str, Any]) -> Faction:
faction = Faction(locales=json.get("locales"))
faction.country = json.get("country", "/")

View File

@@ -5,6 +5,8 @@ import logging
from collections.abc import Iterator
from pathlib import Path
import yaml
from game import persistence
from .faction import Faction
@@ -22,6 +24,7 @@ class Factions:
@staticmethod
def iter_faction_files_in(path: Path) -> Iterator[Path]:
yield from path.glob("*.json")
yield from path.glob("*.yaml")
@classmethod
def iter_faction_files(cls) -> Iterator[Path]:
@@ -36,8 +39,11 @@ class 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)
if path.suffix == ".yaml":
data = yaml.safe_load(fdata)
else:
data = json.load(fdata)
faction = Faction.from_dict(data)
factions[faction.name] = faction
logging.info("Loaded faction from %s", path)
except Exception: