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

Replace CheckLane() with extensions #1461

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

originalfoo
Copy link
Member

@originalfoo originalfoo commented Mar 9, 2022

Adds new extensions:

  • laneId.IsValid()
  • laneId.IsValidWithSegment() -- this replaces CheckLanes(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.

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.
@originalfoo originalfoo added the code cleanup Refactor code, remove old code, improve maintainability label Mar 9, 2022
@originalfoo originalfoo added this to the 11.6.5.1 milestone Mar 9, 2022
@originalfoo originalfoo self-assigned this Mar 9, 2022
Copy link
Collaborator

@kianzarrin kianzarrin left a 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) {
Copy link
Collaborator

@kianzarrin kianzarrin Mar 9, 2022

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!~~

Copy link
Collaborator

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.

Copy link
Member Author

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();
Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Member Author

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?

Copy link
Member

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)

Copy link
Member Author

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?

Copy link
Member

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

Copy link
Collaborator

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.

Copy link
Member Author

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?

Copy link
Collaborator

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.

Copy link
Member Author

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;

@kianzarrin kianzarrin added the DO NOT MERGE YET Don't merge this PR, even if approved, until further notice label Mar 9, 2022
@originalfoo originalfoo marked this pull request as draft March 11, 2022 14:31
@originalfoo originalfoo removed this from the 11.6.5.1 milestone Mar 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code cleanup Refactor code, remove old code, improve maintainability DO NOT MERGE YET Don't merge this PR, even if approved, until further notice
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants