Make TileDirection.INVALID safe to use (#397) - #998
Open
Billytifft wants to merge 1 commit into
Open
Billytifft wants to merge 1 commit into
Billytifft wants to merge 1 commit into
Conversation
Contributor
|
Seems reasonable. Not sure if an explicit Invalid enum is that much better than just raising an error, which we currently have, but I suppose this can reduce crashing in some unexpected situations. The AnimationManager change should go in this PR as well, as that's the driver for the change, the comment, and the GH issue. Returning |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #397
Summary
Adding a
TileDirection.INVALIDvalue used to crash the game (e.g.System.ArgumentOutOfRangeExceptioninTile.HasRiverCrossing) because several places iterated the enum withEnum.GetValues(typeof(TileDirection)), leaking the new member into the map'stile.neighborsdictionary and from there into movement/pathing.This PR:
TileDirection.INVALID— appended at the end of the enum, so the existing% 8arithmetic inMapGeneratorand the serializedfacingDirectionvalues inSaveUnitare unaffected.TileDirectionExtensions.All(an explicit array of the 8 real directions) and switchesGameMap.computeNeighborsandImportCiv3to iterate it instead ofEnum.GetValues(...), soINVALIDcan never be injected into the neighbors dictionary or tile knowledge.INVALID-safe instead of throwing:HasRiverCrossingreturnsfalse,Reversed/RotatedCounterClockwise90DegreesreturnINVALID, andToCoordDiffreturns(0, 0).Verified locally:
dotnet build(0 errors),dotnet test(56 passed, 13 skipped),dotnet format C7/C7.sln whitespace --verify-no-changes(clean).Question for reviewers
C7/Animations/AnimationManager.cs:143-144carries a TODO left over from this exact issue — the author wanted to addTileDirection.INVALIDas the fallback forflicRowToAnimationDirectionbut couldn't because adding the value crashed the game (the very bug #397 tracks). That fallback currently silently returnsTileDirection.SOUTHEAST.With this PR,
INVALIDis safe to use, so that TODO can finally be implemented (the fallback could returnTileDirection.INVALIDinstead of guessingSOUTHEAST). I leftAnimationManager.csuntouched to keep this PR small — would you prefer that TODO be implemented in this PR or in a separate one?