Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unknown plane/helicopter types (mods) more gracefully/best effort #219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions dcs/coalition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from dcs.point import MovingPoint, StaticPoint
from dcs.country import Country
from dcs.status_message import StatusMessage, MessageType, MessageSeverity
from dcs.planes import PlaneType
from dcs.helicopters import HelicopterType

if TYPE_CHECKING:
from . import Mission
Expand Down Expand Up @@ -157,11 +159,19 @@ def load_from_dict(self, mission, d) -> List[StatusMessage]:
# units
for imp_unit_idx in pgroup["units"]:
imp_unit = pgroup["units"][imp_unit_idx]

if imp_unit["type"] not in planes.plane_map:
print("WARN:", "Unknown plane type", file=sys.stderr)
plane_type = type(imp_unit["type"], (PlaneType,), {})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty wary of this approach:

>>> with open(os.devnull, "w") as dev_null:
...     pickle.dump(type("Bar", (Foo,), {})(), dev_null)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
_pickle.PicklingError: Can't pickle <class '__main__.Bar'>: attribute lookup Bar on __main__ failed

I don't think it's important that a distinct type is created for each aircraft type, so PlaneType() is probably plenty good.

This also does call type() for every instance of the plane that isn't recognized. Experimentally that's not a problem in the repl (id(type("Foo")) == id(type("Foo")), but I can't find anything in the docs of type that specify the behavior one way or the other so I'd prefer to not rely on it without a good reason.

plane_type.id = imp_unit["type"]
else:
plane_type = planes.plane_map[imp_unit["type"]]

plane = Plane(
mission.terrain,
_id=imp_unit["unitId"],
name=self.get_name(mission, imp_unit["name"]),
_type=planes.plane_map[imp_unit["type"]],
_type=plane_type,
_country=_country)
plane.load_from_dict(imp_unit)

Expand Down Expand Up @@ -196,11 +206,19 @@ def load_from_dict(self, mission, d) -> List[StatusMessage]:
# units
for imp_unit_idx in pgroup["units"]:
imp_unit = pgroup["units"][imp_unit_idx]

if imp_unit["type"] not in helicopters.helicopter_map:
print("WARN:", "Unknown helicopter type", file=sys.stderr)
helicopter_type = type(imp_unit["type"], (HelicopterType,), {})
helicopter_type.id = imp_unit["type"]
else:
helicopter_type = helicopters.helicopter_map[imp_unit["type"]]

heli = Helicopter(
mission.terrain,
_id=imp_unit["unitId"],
name=self.get_name(mission, imp_unit["name"]),
_type=helicopters.helicopter_map[imp_unit["type"]],
_type=helicopter_type,
_country=_country)
heli.load_from_dict(imp_unit)

Expand Down