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

Fix NPE on partially initialized square grids #4964

Conversation

kwvanderlinde
Copy link
Collaborator

@kwvanderlinde kwvanderlinde commented Oct 1, 2024

Identify the Bug or Feature request

Fixes #3315

Description of the Change

When our objects are deserialized by XStream, the constructors are not run. In the case of Grid implementations, the constructors are relied on to set static state such as SquareGrid.FACING_ANGLES. Without the constructor running, the state remains null. The bug is not likely reproducible in recent versions since we recreate any Grid instances as part of our protobuf logic. But if there is any change there, the bug would resurface.

This PR address this by:

  1. Avoid keeping facing arrays in static state, instead creating them on the fly.
  2. Augment Grid with new methods so callers don't have to care about the facing arrays.
  3. Remove facing arrays altogether by changing the new Grid method to calculate results instead.

The end result is:

  • no more need to build facing arrays based on preferences
  • no more need to notify Grids to keep the facing arrays up to date
  • some small bugs were fixed, such as tokens with low facing values (e.g., -179°) not getting the correct nearest or next facing.

Test cases were added for the new nearestFacing() and nextFacing() methods.

Possible Drawbacks

The new snapFacingInternal() is not as easy to read as a facing array.

Documentation Notes

N/A

Release Notes

  • Fixed a bug where token facing was incorrectly updated when scrolling.

This change is Reviewable

Instead, the edge facing and vertex facing preference need to be provided by the caller, and the `Grid` will dynamically
decide its response. There is no more static state that needs to be kept up-to-date - state which previously was shared
between instances despite being treated like instance state.
- Avoid uninitialized state where reasonable.
  - Only remaining case is the footprint list which is loaded lazily.
- `final`ize fields where possible.
- Remove unused alternative cell highlight fields and associated assets
Tools no longer call `Grid.getFacingAngles()`, which requires each caller to interpret the resposne to get the nearest
or next facing. Now there are two new methods in on `Grid`:
- `int nearestFacing(int facing, boolean faceEdges, boolean faceVertices)`: snaps a facing to one of the standard
  facings.
- `int nextFacing(int facing, boolean faceEdges, boolean faceVertices, boolean clockwise)`: snaps a facing as in
  `nearestFacing()`, but also advances one step clockwise or counterclockwise.

There is no new behaviour introduced by this change.

Also included are tests cases for interesting input facings. These make it clear there are some bugs in the current
behaviour, such as -179° being considered closer to -135° than to 180° (these are 44° and 1° away, respectively).
Instead of a provide a hardcoded array of possible facings, we now calculate the nearest facing using some modular
arithmetic and rounding. This fixes the bugs mentioned in the prior commit, and test cases have been updated to match.

Internally to `Grid`, the use of `getFacingAngles(boolean faceEdges, boolean faceVertices)` and been replaced with
`snapFacingInternal(int facing, boolean faceEdges, boolean faceVertices, int addedSteps)`, which is enough to provide
the functionality of both `getNearestFacing()` and `getNextFacing()`.
@kwvanderlinde kwvanderlinde force-pushed the bugfix/3315-facing-uninitialized-static-state branch from c2caaf6 to 399c740 Compare October 1, 2024 15:33
@kwvanderlinde kwvanderlinde self-assigned this Oct 1, 2024
@cwisniew cwisniew added this pull request to the merge queue Oct 2, 2024
Merged via the queue into RPTools:develop with commit 8c88df5 Oct 2, 2024
4 checks passed
@kwvanderlinde kwvanderlinde deleted the bugfix/3315-facing-uninitialized-static-state branch October 2, 2024 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

[Bug]: Panning (Horizontal Scroll) produces NPE with token selected.
2 participants