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

Two different IPlayer instances can be equal #594

Open
LeShred opened this issue May 29, 2022 · 1 comment
Open

Two different IPlayer instances can be equal #594

LeShred opened this issue May 29, 2022 · 1 comment

Comments

@LeShred
Copy link

LeShred commented May 29, 2022

Description of the problem

When a player connects he gets a IPlayer instance with a particular Id.
When he disconnects and reconnects back later he gets another IPlayer instance with different Id but the two IPlayer instance are equal.
It seems that the Equal() is not based on content of the IPlayer instance but on something else because the behavior isn't 100% consistant.

Reproduction steps

Create and in Alt.OnPlayerConnect event that add the IPlayer instance to an HashSet and display an error if the add returned false:

    public static class LoginManager
    {
        private static HashSet<IPlayer> _players = new();

        public static void Init()
        {
            Alt.OnPlayerConnect += (IPlayer client, string reason) =>
            {
                Alt.Log($"Client {client.Id} Connected");
                if (!_players.Add(client))
                    Alt.Log("IPlayer already in HashSet");
            };
        }
    }

Result:

[20:10:19] Client 164 Connected
[20:10:34] Client 165 Connected
[20:10:52] Client 166 Connected
[20:10:52] IPlayer already in HashSet

Here it takes me 2 reconnects to have equal IPlayer instances but sometimes it can take morre or less attempts.

Expected behaviour

Different IPlayer instances with different Id should not be equal.

Additional context

No response

Operating system

Windows 10 21H2

Version

release/10.8

@duydang2311
Copy link
Contributor

IPlayer.Equals() is inherited from SharedBaseObject.Equals(), which only compares type and native pointer.

public override bool Equals(object obj)

This seems like a breaking change for others if the maintainer overrides IPlayer.Equals() in the module. How about you override it on your side to add your own checks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants