mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Skip & Log incompatible loadouts
Solution exists in using the 'get' method of the "Weapons dictionary", and subsequentially guarding against None. Aside from that I created a method to validate a payload, which uses this None value to determine validity.
This commit is contained in:
parent
d44bf98ab9
commit
647032ca48
@ -1,8 +1,9 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import logging
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
from typing import Iterator, Mapping, Optional, TYPE_CHECKING, Type
|
from typing import Iterator, Mapping, Optional, TYPE_CHECKING, Type, Dict, Any
|
||||||
|
|
||||||
from dcs.unittype import FlyingType
|
from dcs.unittype import FlyingType
|
||||||
|
|
||||||
@ -120,6 +121,10 @@ class Loadout:
|
|||||||
# }
|
# }
|
||||||
payloads = aircraft.dcs_unit_type.load_payloads()
|
payloads = aircraft.dcs_unit_type.load_payloads()
|
||||||
for payload in payloads.values():
|
for payload in payloads.values():
|
||||||
|
if not cls.valid_payload(payload["pylons"]):
|
||||||
|
msg = f'Incompatible loadout for {aircraft} skipped: {payload["name"]}'
|
||||||
|
logging.warning(msg)
|
||||||
|
continue
|
||||||
name = payload["name"]
|
name = payload["name"]
|
||||||
pylons = payload["pylons"]
|
pylons = payload["pylons"]
|
||||||
yield Loadout(
|
yield Loadout(
|
||||||
@ -128,6 +133,13 @@ class Loadout:
|
|||||||
date=None,
|
date=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def valid_payload(pylons: Dict[int, Dict[str, str]]) -> bool:
|
||||||
|
for p in pylons.values():
|
||||||
|
if Weapon.with_clsid(p["CLSID"]) is None:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def default_loadout_names_for(cls, task: FlightType) -> Iterator[str]:
|
def default_loadout_names_for(cls, task: FlightType) -> Iterator[str]:
|
||||||
# This is a list of mappings from the FlightType of a Flight to the type of
|
# This is a list of mappings from the FlightType of a Flight to the type of
|
||||||
@ -189,6 +201,12 @@ class Loadout:
|
|||||||
dcs_unit_type.load_payloads()
|
dcs_unit_type.load_payloads()
|
||||||
payload = dcs_unit_type.loadout_by_name(name)
|
payload = dcs_unit_type.loadout_by_name(name)
|
||||||
if payload is not None:
|
if payload is not None:
|
||||||
|
pylons = {i: {"CLSID": d["clsid"]} for i, d in payload}
|
||||||
|
if not cls.valid_payload(pylons):
|
||||||
|
aircraft = dcs_unit_type.id
|
||||||
|
msg = f"Incompatible loadout for {aircraft} skipped: {name}"
|
||||||
|
logging.warning(msg)
|
||||||
|
continue
|
||||||
return Loadout(
|
return Loadout(
|
||||||
name,
|
name,
|
||||||
{i: Weapon.with_clsid(d["clsid"]) for i, d in payload},
|
{i: Weapon.with_clsid(d["clsid"]) for i, d in payload},
|
||||||
|
|||||||
@ -53,8 +53,9 @@ class Weapon:
|
|||||||
def __setstate__(self, state: dict[str, Any]) -> None:
|
def __setstate__(self, state: dict[str, Any]) -> None:
|
||||||
# Update any existing models with new data on load.
|
# Update any existing models with new data on load.
|
||||||
updated = Weapon.with_clsid(state["clsid"])
|
updated = Weapon.with_clsid(state["clsid"])
|
||||||
state.update(updated.__dict__)
|
if updated is not None:
|
||||||
self.__dict__.update(state)
|
state.update(updated.__dict__)
|
||||||
|
self.__dict__.update(state)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def register(cls, weapon: Weapon) -> None:
|
def register(cls, weapon: Weapon) -> None:
|
||||||
@ -67,10 +68,10 @@ class Weapon:
|
|||||||
cls._by_clsid[weapon.clsid] = weapon
|
cls._by_clsid[weapon.clsid] = weapon
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def with_clsid(cls, clsid: str) -> Weapon:
|
def with_clsid(cls, clsid: str) -> Optional[Weapon]:
|
||||||
if not cls._loaded:
|
if not cls._loaded:
|
||||||
cls._load_all()
|
cls._load_all()
|
||||||
return cls._by_clsid[clsid]
|
return cls._by_clsid.get(clsid)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _load_all(cls) -> None:
|
def _load_all(cls) -> None:
|
||||||
@ -267,7 +268,9 @@ class Pylon:
|
|||||||
pylon_number, weapon = value
|
pylon_number, weapon = value
|
||||||
if pylon_number != number:
|
if pylon_number != number:
|
||||||
continue
|
continue
|
||||||
allowed.add(Weapon.with_clsid(weapon["clsid"]))
|
allowed_weapon = Weapon.with_clsid(weapon["clsid"])
|
||||||
|
if allowed_weapon is not None:
|
||||||
|
allowed.add(allowed_weapon)
|
||||||
|
|
||||||
return cls(number, allowed)
|
return cls(number, allowed)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user