Skip to content

fix: Fix asset prefix double-prefixing and sprite name stripping#3869

Open
gnarhard wants to merge 4 commits intoflame-engine:mainfrom
gnarhard:fix/asset-prefix-image-loading
Open

fix: Fix asset prefix double-prefixing and sprite name stripping#3869
gnarhard wants to merge 4 commits intoflame-engine:mainfrom
gnarhard:fix/asset-prefix-image-loading

Conversation

@gnarhard
Copy link
Copy Markdown
Contributor

Description

Bug 1: Image loading double-prefixes paths

loadAtlasDataImages applied assetsPrefix (designed for Flame.assets, prefix assets/) to Flame.images (prefix assets/images/), causing paths like assets/images/images/packs/textures.png. The image path should be passed directly to img.load() without the assets prefix, as it was in 5.0.5.

Bug 2: _parseRegion strips trailing digits from sprite names

The regex (_?)(\d+)$ in _parseRegion strips trailing digits from all sprite names, assuming they're animation frame indices. This breaks any sprite whose name naturally contains trailing digits (e.g. drohne2drohne, cu3cu, crystal2crystal). In 5.0.5, the name was always kept as-is and the index only came from the explicit index: field in the atlas.

Fix 1: Double-prefixed image paths in loadAtlasDataImages

assetsPrefix (default 'images') was applied to both Flame.assets (prefix assets/) and Flame.images (prefix assets/images/), causing paths like assets/images/images/packs/texture.png.

The else branch in loadAtlasDataImages prepended assetsPrefix to texturePath before passing it to images.load(). Since Flame.images already adds assets/images/, this created a double prefix. The
subsequent stripping logic was fragile — it relied on AssetsCache.prefix aligning with Images.prefix, which silently failed in non-standard configurations.

Changes:

  • lib/src/texture_packer_parser.dartloadAtlasDataImages:
    • Removed assetsPrefix and assets parameters (no longer needed for image loading)
    • Removed the assetsPrefix prefix application block
    • Replaced the toRelative/relativePrefix stripping logic with a simpler approach that directly checks for overlap with img.prefix (both full and partial), independent of AssetsCache configuration
    • Changed texturePath from var to final (no longer reassigned)
  • lib/src/texture_packer_atlas.dartloadAtlas:
    • Removed the now-unnecessary assetsPrefix and assets arguments from the loadAtlasDataImages call
  • test/atlas_path_resolution_test.dart:
    • Added test: assetsPrefix is not applied to image paths loaded via Images
    • Added test: subdirectory image paths are not double-prefixed

Fix 2: Aggressive digit stripping from sprite names in _parseRegion

The regex r'(_?)(\d+)$' unconditionally stripped trailing digits from all sprite names and treated them as animation frame indices. This broke any sprite whose name naturally ends in digits (e.g. drohne2
drohne, cu3cu, vine1vine, corruption_sack-1corruption_sack-).

v5.1.0 added name-based index extraction that can't distinguish between animation indices (walk_01) and names that end in digits (drohne2). In v5.0.5, the index only came from the explicit index: field in the
atlas file.

Changes:

  • lib/src/texture_packer_parser.dart_parseRegion:
    • Removed the regex-based index extraction block (the indexMatch logic and nameBeforeIndex/extractedIndex variables)
    • finalIndex now only uses the explicit index: field from the atlas, defaulting to -1
    • Region name is the original name from the atlas (with file extension stripped), never modified
  • test/naming_index_test.dart:
    • Updated all tests to expect names preserved as-is (image1, image_01, etc.) with index -1
    • Renamed test for explicit index: field to verify it still works
  • test/separated_parsing_test.dart:
    • Updated to expect sprite1 preserved as name (not stripped to sprite)
    • Updated getAnimation test to use explicit index: fields in atlas content
  • test/atlas_path_resolution_test.dart:
    • Updated existing test to expect preserved names (knight_walk_01, not knight_walk)
    • Added new test verifying explicit index: field still works correctly

Tests

  • All 60 existing + new tests pass
  • Dart analyzer reports no errors
  • New/legacy/whitelist format tests unaffected (they use explicit index: fields)
  • Image path tests verify no double-prefixing with default and custom assetsPrefix
  • Naming tests verify sprite names with trailing digits are preserved
  • Animation test updated to use explicit index: fields (the supported mechanism)

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

Copy link
Copy Markdown
Member

@spydon spydon left a comment

Choose a reason for hiding this comment

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

Looks sensible to me.
Do you want to have a look @s1r1m1r1, since you made these changes?
So that we don't bounce back and forth,

s1r1m1r1 added a commit to s1r1m1r1/flame that referenced this pull request Mar 23, 2026
s1r1m1r1 added a commit to s1r1m1r1/flame that referenced this pull request Mar 23, 2026
@s1r1m1r1
Copy link
Copy Markdown
Contributor

I tested this fix,
but when loading from a package, the PNG file for the atlas doesn't load correctly.

Perhaps I should add some fixes for loading images for the atlas from the package.

@s1r1m1r1
Copy link
Copy Markdown
Contributor

If there are too many changes, it's possible to
revert to just one index pattern: image1, image2, image3, image4, etc.
Revert changes only for package integration.

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.

3 participants