Add validation for DirtyField strings #5713
Open
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.
This adds validation to
DirtyField<T>
method calls to make sure that the passedfieldName
string matches the name of a field on the target component.This is implemented as an attribute that can be applied to method parameters,
ValidateMemberAttribute
. The analyzer uses the first generic type argument of the method as the target - this is a little funky, but it doesn't seem to be possible to passT
into the attribute in the method signature, so this was the best I could do. It builds a HashSet of all the members of the target type, then iterates through the parameters of the method invocation looking for theValidateMember
attribute. If it finds one, it checks the compile-time constant string value of the parameter against the member HashSet and reports an error if it doesn't find a match.Note that this will not catch situations where two components with identically-named members are swapped. In the screenshot example, if
GunComponent
did have a field named "NextAttack", passing innameof(MeleeWeaponComponent.NextAttack)
would not cause an error (either in the analyzer or at runtime) because the string would match. I don't think that can be addressed without some major changes to the way delta fields are implemented.This PR applies the attribute to the
DirtyField<T>(EntityUid, Component, ...)
method of EntityManager and theDirtyField<T>(EntityUid, Component, ...)
andDirtyField<T>(Entity<T>, ...)
proxy methods of EntitySystem.Resolves #5706