Fix error during faction loading

ForceGroup loading failed due to wrong unit parsing and prevented factions without naval units to load before the first faction with naval units got loaded (this affected the Allies 1940 and 44 factions)
This commit is contained in:
RndName 2022-03-22 17:30:28 +01:00
parent d67d32610d
commit c7270e8654
2 changed files with 20 additions and 3 deletions

View File

@ -281,8 +281,15 @@ class ForceGroup:
logging.error(f"ForceGroup {name} has no valid tasking")
continue
units = [UnitType.named(unit) for unit in data.get("units")]
if not units:
units: list[UnitType[Any]] = []
for unit in data.get("units"):
if GroundUnitType.exists(unit):
units.append(GroundUnitType.named(unit))
elif ShipUnitType.exists(unit):
units.append(ShipUnitType.named(unit))
else:
logging.error(f"Unit {unit} of ForceGroup {name} is invalid")
if len(units) == 0:
logging.error(f"ForceGroup {name} has no valid units")
continue

View File

@ -45,7 +45,7 @@ class UnitType(ABC, Generic[DcsUnitTypeT]):
@classmethod
def named(cls, name: str) -> UnitType[Any]:
return cls._by_name[name]
raise NotImplementedError
@classmethod
def for_dcs_type(cls, dcs_unit_type: DcsUnitTypeT) -> Iterator[UnitType[Any]]:
@ -69,3 +69,13 @@ class UnitType(ABC, Generic[DcsUnitTypeT]):
@cached_property
def eplrs_capable(self) -> bool:
return getattr(self.dcs_unit_type, "eplrs", False)
@classmethod
def exists(cls, name: str) -> bool:
if not cls._loaded:
cls._load_all()
try:
cls.named(name)
return True
except (KeyError, AssertionError):
return False