Skip to content

improve: add bounds check for sprites loading#1457

Merged
mehah merged 7 commits intomainfrom
dudantas/improve-assets-loading
Nov 18, 2025
Merged

improve: add bounds check for sprites loading#1457
mehah merged 7 commits intomainfrom
dudantas/improve-assets-loading

Conversation

@dudantas
Copy link
Copy Markdown
Member

This pull request introduces several improvements and bug fixes related to sprite sheet management, error handling, and outfit assignment in the client codebase. The changes enhance robustness by adding bounds checking, improving error reporting, and ensuring proper handling of invalid data.

Sprite Sheet Management and Validation

  • Added the getSpritesPerSheet() method to SpriteSheet, and used it to clamp lastSpriteId if it exceeds the sheet's capacity when loading appearances, preventing out-of-bounds sprite access. [1] [2] [3]
  • Added bounds checking in SpriteAppearances::getSpriteImage() to log and prevent access to sprites outside the valid range for a sheet.

Error Reporting and Logging

  • Improved logging in ThingType::loadTexture() to report failures when fetching sprite images, including detailed context such as sprite ID, thing name, category, and pattern/frame info. [1] [2] [3]

Outfit Assignment and Validation

  • Enhanced Creature::setOutfit() to handle invalid outfits by setting the appropriate category and reverting to the old outfit if the thing type is invalid, with error logging. [1] [2]

Miscellaneous

  • Strengthened isValidDatId() in ThingTypeManager to ensure category bounds are checked before validating IDs.

Updated isValidDatId to verify that the provided category is less than ThingLastCategory, preventing out-of-bounds access to m_thingTypes.
Refactored Creature::setOutfit to validate and adjust the outfit category before assignment. Added error handling to revert to the previous outfit if the thing type is invalid, ensuring more robust state management.
Introduces validation to ensure the sprite ID is within the valid range for the sheet. Logs an error and returns nullptr if the ID is out of bounds, preventing potential crashes or undefined behavior.
The 'static' specifier was removed from the categoryName function in thingtype.cpp, as it is in an anonymous namespace and does not require static linkage.
Introduces a custom fmt::formatter specialization for TPoint<T>, enabling formatted output of point coordinates using the fmt library.
@sonarqubecloud
Copy link
Copy Markdown

@javiertringol
Copy link
Copy Markdown
Contributor

I get this error when error in to the .spr/.dat system.

ERROR: Failed to fetch sprite id 0 for thing  (7028, item), layer 1, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (128, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (128, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (128, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (128, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (128, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (128, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (128, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (128, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (128, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (128, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (130, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (130, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (130, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0
ERROR: Failed to fetch sprite id 0 for thing  (130, creature), layer 0, pattern 0x0x0, frame 0 0, offset 1x0

@mehah mehah merged commit 7d7edb6 into main Nov 18, 2025
8 checks passed
@mehah mehah deleted the dudantas/improve-assets-loading branch November 24, 2025 00:57
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