-
Notifications
You must be signed in to change notification settings - Fork 85
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
Replace CheckLane()
with extensions
#1461
base: master
Are you sure you want to change the base?
Conversation
Adds new extensions: - `laneId.IsValid()` - `laneId.IsValidWithSegment()` - `segmentId.IsValid()` These extensions check that the id is non-zero, in addition to usual flags check. Simplified some existing code in `Flags.cs` to use extensions. Also updated xmldoc to be more consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should wait until my laneconnectionmanager PR goes in to reduce conflicts.
/// </summary> | ||
/// <param name="laneId">The id of the lane to check.</param> | ||
/// <returns>Returns <c>true</c> if valid, otherwise <c>false</c>.</returns> | ||
public static bool IsValidWithSegment(this uint laneId) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we already have this in lane extensions:
public static bool IsValidWithSegment(this ref NetLane netLane) {
return netLane.IsValid()
&& netLane.m_segment.ToSegment().IsValid();
}
~~But it does not check for laneId < 0
there is the minor issue that we some times check for laneId < 0 and sometimes don't based on the mood of the programmer at the time!~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait uint
cannot be less than zero anyway so its fine.
I think we should use the old overload instead of introducing this new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I know unsigned numbers can't be negative (no idea why so much old code has the <=
check on ushort
and uint
).
But regardless, that's not why we check id != 0
, is it?
/// <param name="segmentId">The id of the segment to check.</param> | ||
/// <returns>Returns <c>true</c> if valid, otherwise <c>false</c>.</returns> | ||
public static bool IsValid(this ushort segmentId) | ||
=> (segmentId != 0) && segmentId.ToSegment().IsValid(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checking segmentId != 0 is redundant because m_segments[0] holds the default value and therefore is not valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should just use the old extension.
If checking segmentId != 0
is important - for example if you think someone can accidentally write something to m_segments[0]
- then we should do it consistently everywhere.
But That is OCD in my opinion. the old interface would do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're sure lane / segment zero will never have Created
flag set, then yeah, we could ditch the id != 0
check.
@krzychu124 thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First element is always thrown away on load so it should have flag None
unless other mod used it and then got serialized to the savegame (bug) (worth checking if they also skip first element or not)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it should be ok to ditch the id != 0
checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would check where CO is checking for != 0
-> they know where it is possible. We can only guess
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First element is always thrown away on load so it should have flag None unless other mod used it and then got serialized to the savegame (bug) (worth checking if they also skip first element or not)
If that is the case then we should always check for id != 0.
it would be inconsistent to have 2 extensions methods to check if valid and then we pick one or the other randomly.
In any case I consider this to be a low priority issue. I think its OK to merge the code like this but it might be worth improving it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've realised there are some other issues with this PR, notably that an IsValid()
extension of a uint
/ ushort
has no way to determine what class those ids refer to, which is a huge flaw in my thinking of this PR. That being the case, we should always be doing .ToWhatever()
first and then IsValid()
- but is there any way to, for example, get the lane id from a NetLane
instance in the IsValid()
stage?
If we need to check != 0
how do we implement that in the least crufty/cumbersome manner?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was what I was afraid of (I should have communicated that better). I thin we should minimize the use of (interger).Extensions()
. I think ToNode()
or ToSegment()
is fine but more than can cause trouble.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I was thinking to include id != 0
check in IsValid()
is extensions such as uint.IsValidLane()
and ushort.IsValidSegment()
, example:
internal static bool IsValidLane(this uint laneId)
=> laneId != 0 && laneId.ToLane().IsValid();
// call site example:
if (!someLaneId.IsValidLane())
return;
Adds new extensions:
laneId.IsValid()
laneId.IsValidWithSegment()
-- this replacesCheckLanes(laneId)
segmentId.IsValid()
Compared to existing extensions, these new extensions additionally check that the id's are non-zero.
The PR also simplifies some existing code in
Flags.cs
by using the new extensions, and also updates some xmldoc to be more consistent.