Skip to content

Commit

Permalink
perf(Messages): Use Flags enum for isOwner / IsLocalPlayer (#3958)
Browse files Browse the repository at this point in the history
* fix(Messages): Use Flags enum for isOwner / IsLocalPlayer
Reduces SpawnMessage header by one byte

Since these are Mirror's internal messages, the Obsoletes may not be necessary, but included for completeness.

* fixed test

* Revert style change, added comments

* style

* Update Messages.cs

* Update Messages.cs

* Update Messages.cs

---------

Co-authored-by: mischa <16416509+miwarnec@users.noreply.github.com>
  • Loading branch information
MrGadget1024 and miwarnec authored Jan 4, 2025
1 parent 18f0808 commit cb7f990
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions Assets/Mirror/Core/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ public struct RpcMessage : NetworkMessage
public ArraySegment<byte> payload;
}

[Flags] public enum AuthorityFlags : byte { None, isOwner, isLocalPlayer }

public struct SpawnMessage : NetworkMessage
{
// netId of new or existing object
public uint netId;
public bool isLocalPlayer;
// Sets hasAuthority on the spawned object
public bool isOwner;
// isOwner and isLocalPlayer are merged into one byte via bitwise op
public AuthorityFlags authorityFlags;
public ulong sceneId;
// If sceneId != 0 then it is used instead of assetId
public uint assetId;
Expand All @@ -71,13 +72,53 @@ public struct SpawnMessage : NetworkMessage
// serialized component data
// ArraySegment to avoid unnecessary allocations
public ArraySegment<byte> payload;

// Backwards compatibility after implementing authorityFlags
public bool isOwner
{
get => authorityFlags.HasFlag(AuthorityFlags.isOwner);
set => authorityFlags =
value
? authorityFlags | AuthorityFlags.isOwner
: authorityFlags & ~AuthorityFlags.isOwner;
}

// Backwards compatibility after implementing authorityFlags
public bool isLocalPlayer
{
get => authorityFlags.HasFlag(AuthorityFlags.isLocalPlayer);
set => authorityFlags =
value
? authorityFlags | AuthorityFlags.isLocalPlayer
: authorityFlags & ~AuthorityFlags.isLocalPlayer;
}
}

public struct ChangeOwnerMessage : NetworkMessage
{
public uint netId;
public bool isOwner;
public bool isLocalPlayer;
// isOwner and isLocalPlayer are merged into one byte via bitwise op
public AuthorityFlags authorityFlags;

// Backwards compatibility after implementing authorityFlags
public bool isOwner
{
get => authorityFlags.HasFlag(AuthorityFlags.isOwner);
set => authorityFlags =
value
? authorityFlags | AuthorityFlags.isOwner
: authorityFlags & ~AuthorityFlags.isOwner;
}

// Backwards compatibility after implementing authorityFlags
public bool isLocalPlayer
{
get => authorityFlags.HasFlag(AuthorityFlags.isLocalPlayer);
set => authorityFlags =
value
? authorityFlags | AuthorityFlags.isLocalPlayer
: authorityFlags & ~AuthorityFlags.isLocalPlayer;
}
}

public struct ObjectSpawnStartedMessage : NetworkMessage {}
Expand Down

0 comments on commit cb7f990

Please sign in to comment.