FabianTerhorst / coreclr-module

Old alt:V CoreClr (.NET Core Common Language Runtime) community made module. New: https://github.com/altmp/coreclr-module
MIT License
72 stars 68 forks source link

Two different IPlayer instances can be equal #594

Open LeShred opened 2 years ago

LeShred commented 2 years ago

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 commented 2 years ago

IPlayer.Equals() is inherited from SharedBaseObject.Equals(), which only compares type and native pointer. https://github.com/FabianTerhorst/coreclr-module/blob/6f356bef68ba88f19938a00da2699b4dad442d59/api/AltV.Net.Shared/Elements/Entities/SharedBaseObject.cs#L219 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?