Skip to content

chore: remove authority & netvar perms from NetworkTransform #1059

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

Merged
merged 6 commits into from
Aug 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 1 addition & 65 deletions com.unity.netcode.gameobjects/Prototyping/NetworkTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ namespace Unity.Netcode.Prototyping
[AddComponentMenu("Netcode/" + nameof(NetworkTransform))]
public class NetworkTransform : NetworkBehaviour
{
/// <summary>
/// Server authority allows only the server to update this transform
/// Client authority allows only the owner client to update this transform
/// Shared authority allows everyone to update this transform
/// </summary>
public enum NetworkAuthority
{
Server = 0,
Client,
Shared
}

internal class NetworkState : INetworkSerializable
{
internal const int InLocalSpaceBit = 0;
Expand Down Expand Up @@ -140,13 +128,6 @@ public void NetworkSerialize(NetworkSerializer serializer)
}
}

/// <summary>
/// TODO this will need refactoring
/// Specifies who can update this transform
/// </summary>
[Tooltip("Defines who can update this transform")]
public NetworkAuthority Authority = NetworkAuthority.Server;

/// <summary>
/// The network channel to use send updates
/// </summary>
Expand Down Expand Up @@ -178,14 +159,6 @@ public void NetworkSerialize(NetworkSerializer serializer)
internal readonly NetworkVariable<NetworkState> ReplNetworkState = new NetworkVariable<NetworkState>(new NetworkState());
internal NetworkState PrevNetworkState;

/// <summary>
/// Does this instance (client or server) has authority to update transform?
/// </summary>
public bool CanUpdateTransform =>
Authority == NetworkAuthority.Client && IsClient && IsOwner ||
Authority == NetworkAuthority.Server && IsServer ||
Authority == NetworkAuthority.Shared;

// updates `NetworkState` properties if they need to and returns a `bool` indicating whether or not there was any changes made
// returned boolean would be useful to change encapsulating `NetworkVariable<NetworkState>`'s dirty state, e.g. ReplNetworkState.SetDirty(isDirty);
internal bool UpdateNetworkState(NetworkState networkState)
Expand Down Expand Up @@ -390,39 +363,13 @@ private void OnNetworkStateChanged(NetworkState oldState, NetworkState newState)
return;
}

if (Authority == NetworkAuthority.Client && IsClient && IsOwner)
{
// todo MTT-768 this shouldn't happen anymore with new tick system (tick written will be higher than tick read, so netvar wouldn't change in that case)
return;
}

ApplyNetworkState(newState);
}

private void UpdateNetVarPerms()
{
switch (Authority)
{
default:
throw new NotImplementedException($"Authority: {Authority} is not handled");
case NetworkAuthority.Server:
ReplNetworkState.Settings.WritePermission = NetworkVariablePermission.ServerOnly;
break;
case NetworkAuthority.Client:
ReplNetworkState.Settings.WritePermission = NetworkVariablePermission.OwnerOnly;
break;
case NetworkAuthority.Shared:
ReplNetworkState.Settings.WritePermission = NetworkVariablePermission.Everyone;
break;
}
}

private void Awake()
{
m_Transform = transform;

UpdateNetVarPerms();

ReplNetworkState.Settings.SendNetworkChannel = Channel;
ReplNetworkState.Settings.SendTickrate = FixedSendsPerSecond;

Expand All @@ -446,7 +393,7 @@ private void FixedUpdate()
return;
}

if (CanUpdateTransform)
if (IsServer)
{
ReplNetworkState.SetDirty(UpdateNetworkState(ReplNetworkState.Value));
}
Expand All @@ -459,17 +406,6 @@ private void FixedUpdate()
}
}

/// <summary>
/// Updates the NetworkTransform's authority model at runtime
/// </summary>
internal void SetAuthority(NetworkAuthority authority)
{
Authority = authority;
UpdateNetVarPerms();
// todo this should be synced with the other side.
// let's wait for a more final solution before adding more code here
}

/// <summary>
/// Teleports the transform to the given values without interpolating
/// </summary>
Expand Down