Skip to content

feat: Add name property for network variables #891

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 11 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ internal void InitializeVariables()
}

instance.SetNetworkBehaviour(this);

var instanceNameProperty = fieldType.GetProperty(nameof(INetworkVariable.Name));
instanceNameProperty?.SetValue(instance, sortedFields[i].Name);

NetworkVariableFields.Add(instance);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public NetworkDictionary(IDictionary<TKey, TValue> value)
m_Dictionary = value;
}

/// <summary>
/// Gets or sets the name of the network variable's instance
/// (MemberInfo) where it was declared.
/// </summary>
public string Name { get; internal set; }

/// <inheritdoc />
public void ResetDirty()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public NetworkList(IList<T> value)
m_List = value;
}

public string Name { get; internal set; }
Copy link
Contributor

@mattwalsh-unity mattwalsh-unity Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, forgot the <summary> comment here


/// <inheritdoc />
public void ResetDirty()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public NetworkSet(ISet<T> value)
m_Set = value;
}

/// <summary>
/// Gets or sets the name of the network variable's instance
/// (MemberInfo) where it was declared.
/// </summary>
public string Name { get; internal set; }

/// <inheritdoc />
public void ResetDirty()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace MLAPI.NetworkVariable
/// </summary>
public interface INetworkVariable
{
/// <summary>
/// Gets or sets the name of the network variable's instance
/// (MemberInfo) where it was declared.
/// </summary>
string Name { get; }

/// <summary>
/// Returns the name of the channel to be used for syncing
/// </summary>
Expand Down Expand Up @@ -73,6 +79,5 @@ public interface INetworkVariable
/// </summary>
/// <param name="behaviour">The behaviour the container behaves to</param>
void SetNetworkBehaviour(NetworkBehaviour behaviour);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public T Value

private bool m_IsDirty = false;

/// <summary>
/// Gets or sets the name of the network variable's instance
/// (MemberInfo) where it was declared.
/// </summary>
public string Name { get; internal set; }

/// <summary>
/// Sets whether or not the variable needs to be delta synced
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions com.unity.multiplayer.mlapi/Tests/Runtime/Profiling.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using MLAPI.NetworkVariable;
using MLAPI.NetworkVariable.Collections;
using NUnit.Framework;
using UnityEngine;

namespace MLAPI.RuntimeTests.Profiling
{
public sealed class NetworkVariableNameTests
{
private NetworkVariableNameComponent m_NetworkVariableNameComponent;

[SetUp]
public void SetUp()
{
NetworkManagerHelper.StartNetworkManager(out _);

var gameObjectId = NetworkManagerHelper.AddGameNetworkObject(Guid.NewGuid().ToString());
m_NetworkVariableNameComponent= NetworkManagerHelper.AddComponentToObject<NetworkVariableNameComponent>(gameObjectId);
NetworkManagerHelper.SpawnNetworkObject(gameObjectId);
}

[TearDown]
public void TearDown()
{
NetworkManagerHelper.ShutdownNetworkManager();
}

[Test]
public void VerifyNetworkVariableNameInitialization()
{
// Properties have the following name format: "<PropertyName>k__BackingField"
StringAssert.Contains(nameof(NetworkVariableNameComponent.NetworkVarString), m_NetworkVariableNameComponent.NetworkVarString.Name);
StringAssert.Contains(nameof(NetworkVariableNameComponent.NetworkVarSet), m_NetworkVariableNameComponent.NetworkVarSet.Name);

// Fields have regular naming
Assert.AreEqual(nameof(NetworkVariableNameComponent.NetworkVarList), m_NetworkVariableNameComponent.NetworkVarList.Name);
Assert.AreEqual(nameof(NetworkVariableNameComponent.NetworkVarDictionary), m_NetworkVariableNameComponent.NetworkVarDictionary.Name);
}

private class NetworkVariableNameComponent : NetworkBehaviour
{
public NetworkVariableString NetworkVarString { get; } = new NetworkVariableString();

public NetworkSet<ulong> NetworkVarSet { get; } = new NetworkSet<ulong>();

public NetworkList<ulong> NetworkVarList = new NetworkList<ulong>();

public NetworkDictionary<ulong, ulong> NetworkVarDictionary = new NetworkDictionary<ulong, ulong>();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.