Skip to content

load_payloads: tolerate globbed-but-uncached payload files#440

Closed
MacrayBlackhand wants to merge 1 commit into
pydcs:masterfrom
MacrayBlackhand:fix-load-payloads-uncached
Closed

load_payloads: tolerate globbed-but-uncached payload files#440
MacrayBlackhand wants to merge 1 commit into
pydcs:masterfrom
MacrayBlackhand:fix-load-payloads-uncached

Conversation

@MacrayBlackhand

Copy link
Copy Markdown
Contributor

Problem

FlyingType.load_payloads() aborts the entire mission load with a KeyError
when a payload directory contains a *.lua file that scan_payload_dir()
globbed but did not cache.

The two halves of the payload machinery use mismatched guards:

  • scan_payload_dir() globs every *.lua in each payload dir, but only inserts
    a cache entry when the file content matches the regex
    \["unitType"]\s*=\s*"([^"]*) (dcs/unittype.py, ~L108-122). A file whose
    unitType is written in a form the regex doesn't match is globbed but never
    cached.
  • load_payloads() then re-globs the same *.lua files and reads the cache with
    a bare subscript (dcs/unittype.py:140):
  if FlyingType._payload_cache[payload_path] == cls.id and payload_path.exists():

For any globbed-but-uncached path, _payload_cache[path] raises KeyError,
which propagates out of load_payloads() and aborts loading the whole mission.

Concrete current motivating case: the shipping ED F-100D module ships
CoreMods/aircraft/F-100D/UnitPayloads/F-100D.lua. That directory is on the
payload search path, so the file globs -- but its content isn't matched by the
["unitType"] regex, so it isn't cached. The result: loading any mission that
references an F-100D crashes in load_payloads().

Relationship to #317

Issue #317 ("replace our lua parser with lupa?") diagnoses the same underlying
cause -- the regex-based payload scanner can't handle certain payload-file
syntaxes (the F-100D among them) -- and proposes a larger lupa-based parser
rewrite.

This PR is the small, complementary, defensive fix: it does not attempt to fix
the parser or make the uncacheable file parse. It only stops one
unparseable-but-globbed file from taking down the entire mission load, regardless
of whether or when the larger parser rewrite in #317 lands. A file the scanner
can't cache is simply skipped (which is already the intended outcome for a file
that doesn't match a unit) instead of raising.

Fix

One call, in load_payloads():

-                if FlyingType._payload_cache[payload_path] == cls.id and payload_path.exists():
+                if FlyingType._payload_cache.get(payload_path) == cls.id and payload_path.exists():

An uncached path -> .get() returns None -> None != cls.id -> the file is
skipped. Cached files are unaffected; nothing else changes.

Test

Added test_load_payloads_skips_globbed_but_uncached_file to
tests/test_unittype.py (matching the existing tmp_path/monkeypatch style).
It creates a payload directory containing:

  • an uncacheable .lua (no ["unitType"]="..." line -- the F-100D failure
    shape), and
  • a normal cacheable payload .lua for the unit under test,

points the scanner at the temp directory, and asserts that load_payloads() does
not raise and still loads the valid payload. The test fails with KeyError
against the pre-fix bare subscript and passes with .get().

scan_payload_dir only caches a payload .lua when its content matches the
["unitType"]="..." regex, but load_payloads globs all *.lua and reads the
cache with a bare subscript -- so a globbed file that wasn't cached (e.g. an
aircraft module whose payload file uses a syntax the regex doesn't match,
such as the shipping F-100D) raises KeyError and aborts the entire mission
load. Use dict.get() so an uncached path is simply skipped.
@rp-

rp- commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

#439

was faster, sorry

@rp- rp- closed this Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants