Skip to content

Commit

Permalink
perf: spawn with client authority only takes 1 message (#1206)
Browse files Browse the repository at this point in the history
* perf: spawn with client authority only takes 1 message now

* Fix javadocs and warning

* Rename parameters

* Make sure we spawn players with authority
  • Loading branch information
paulpach authored and miwarnec committed Nov 24, 2019
1 parent 5b04836 commit 3b9414f
Showing 1 changed file with 18 additions and 62 deletions.
80 changes: 18 additions & 62 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ static void FinishPlayerForConnection(NetworkIdentity identity, GameObject playe
{
// it is allowed to provide an already spawned object as the new player object.
// so dont spawn it again.
Spawn(playerGameObject);
Spawn(playerGameObject, identity.connectionToClient);
}
}

Expand Down Expand Up @@ -1041,7 +1041,7 @@ static void OnCommandMessage(NetworkConnection conn, CommandMessage msg)
identity.HandleCommand(msg.componentIndex, msg.functionHash, new NetworkReader(msg.payload));
}

internal static void SpawnObject(GameObject obj)
internal static void SpawnObject(GameObject obj, NetworkConnection ownerConnection)
{
if (!active)
{
Expand All @@ -1056,13 +1056,13 @@ internal static void SpawnObject(GameObject obj)
return;
}
identity.Reset();
identity.connectionToClient = (NetworkConnectionToClient)ownerConnection;

identity.OnStartServer(false);

if (LogFilter.Debug) Debug.Log("SpawnObject instance ID " + identity.netId + " asset ID " + identity.assetId);

identity.RebuildObservers(true);
//SendSpawnMessage(objNetworkIdentity, null);
}

internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnection conn)
Expand Down Expand Up @@ -1151,11 +1151,12 @@ public static void DestroyPlayerForConnection(NetworkConnection conn)
/// <para>This will cause a new object to be instantiated from the registered prefab, or from a custom spawn function.</para>
/// </summary>
/// <param name="obj">Game object with NetworkIdentity to spawn.</param>
public static void Spawn(GameObject obj)
/// <param name="ownerConnection">The connection that has authority over the object</param>
public static void Spawn(GameObject obj, NetworkConnection ownerConnection = null)
{
if (VerifyCanSpawn(obj))
{
SpawnObject(obj);
SpawnObject(obj, ownerConnection);
}
}

Expand Down Expand Up @@ -1219,71 +1220,25 @@ public static void Spawn(GameObject obj, GameObject player)
Spawn(obj, identity.connectionToClient);
}

/// <summary>
/// Obsolete: Use <see cref="Spawn(GameObject, NetworkConnection)"/> instead.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use Spawn(GameObject, NetworkConnection) instead.")]
public static bool SpawnWithClientAuthority(GameObject obj, NetworkConnection conn)
{
Spawn(obj, conn);
return true;
}

/// <summary>
/// This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client.
/// <para>This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object.</para>
/// Use <see cref="Spawn(GameObject, NetworkConnection)"/> instead
/// </summary>
/// <param name="obj">The object to spawn.</param>
/// <param name="conn">The connection to set Client Authority to.</param>
public static void Spawn(GameObject obj, NetworkConnection conn)
[Obsolete("Use Spawn(obj,connection) instead")]
public static bool SpawnWithClientAuthority(GameObject obj, NetworkConnection ownerConnection)
{
if (!conn.isReady)
{
Debug.LogError("NetworkConnection is not ready!");
return;
}

Spawn(obj);

NetworkIdentity identity = obj.GetComponent<NetworkIdentity>();
if (identity == null || !identity.isServer)
{
// spawning the object failed.
return;
}

identity.AssignClientAuthority(conn);
}

/// <summary>
/// Obsolete: Use <see cref="Spawn(GameObject, Guid, NetworkConnection)"/> instead.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use Spawn(GameObject, Guid, NetworkConnection) instead.")]
public static bool SpawnWithClientAuthority(GameObject obj, Guid assetId, NetworkConnection conn)
{
Spawn(obj, assetId, conn);
Spawn(obj, ownerConnection);
return true;
}

/// <summary>
/// This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client.
/// <para>This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object.</para>
/// Use <see cref="Spawn(GameObject, Guid, NetworkConnection)"/> instead
/// </summary>
/// <param name="obj">The object to spawn.</param>
/// <param name="assetId">The assetId of the object to spawn. Used for custom spawn handlers.</param>
/// <param name="conn">The connection to set Client Authority to.</param>
public static void Spawn(GameObject obj, Guid assetId, NetworkConnection conn)
[Obsolete("Use Spawn(obj,assetId, connection) instead")]
public static bool SpawnWithClientAuthority(GameObject obj, Guid assetId, NetworkConnection ownerConnection)
{
Spawn(obj, assetId);

NetworkIdentity identity = obj.GetComponent<NetworkIdentity>();
if (identity == null || !identity.isServer)
{
// spawning the object failed.
return;
}

identity.AssignClientAuthority(conn);
Spawn(obj, assetId, ownerConnection);
return true;
}

/// <summary>
Expand All @@ -1292,15 +1247,16 @@ public static void Spawn(GameObject obj, Guid assetId, NetworkConnection conn)
/// </summary>
/// <param name="obj">The object to spawn.</param>
/// <param name="assetId">The assetId of the object to spawn. Used for custom spawn handlers.</param>
public static void Spawn(GameObject obj, Guid assetId)
/// <param name="ownerConnection">The connection that has authority over the object</param>
public static void Spawn(GameObject obj, Guid assetId, NetworkConnection ownerConnection = null)
{
if (VerifyCanSpawn(obj))
{
if (GetNetworkIdentity(obj, out NetworkIdentity identity))
{
identity.assetId = assetId;
}
SpawnObject(obj);
SpawnObject(obj, ownerConnection);
}
}

Expand Down

0 comments on commit 3b9414f

Please sign in to comment.