private void Start() { base.Start(); if (NetworkManager.Instance == null) { throw new Exception( "Network Manager not found! You should have a GameObject called \"NetworkManager\" in your project, that has the <NetworkManager> script on it!"); } if (entityId == 0) { throw new Exception("A NetworkMonoBehaviour Object must be instantiated over the network!"); } _fixedDealy = NetworkManager.Instance.ServerSettings.sendUpdateThresholdPerSecond / 60f; IsLocalPlayer = (NetworkManager.Instance.ClientId == clientId); FieldInfo[] objectFields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); for (int i = 0; i < objectFields.Length; i++) { SyncVar attribute = Attribute.GetCustomAttribute(objectFields[i], typeof(SyncVar)) as SyncVar; if (attribute != null) { _syncVars.Add(objectFields[i].Name, objectFields[i].GetValue(this)); } } OnStart(); }
public void EqualsEqualsT() { // == should compare .Value SyncVar <int> field = 42; Assert.That(field == 42, Is.True); }
/// <summary> /// Loads all SyncVars in networkObj. /// </summary> /// <param name="networkObj">The object that the SyncVars are loaded from.</param> private void loadSyncVars <T>(T networkObj) where T : NetworkBehaviour { Type objType = typeof(T); FieldInfo[] objectFields = objType.GetFields(); foreach (FieldInfo field in objectFields) { SyncVar syncVar = (SyncVar)Attribute.GetCustomAttribute(field, typeof(SyncVar)); //Found a SyncVar if (syncVar != null) { //Add a new Dictionary for object type if it doesn't exist if (!syncVars.ContainsKey(objType)) { syncVars.Add(objType, new Dictionary <string, SyncVarData>()); } object value = field.GetValue(networkObj); //Can only use SyncVars supported by the SyncVarMessageFactory if (SyncVarMessageFactory.isTypeSupported(field.GetType())) { syncVars[objType].Add(field.Name, new SyncVarData(networkObj, field, value)); //Debug.Log("[SyncVar Loaded] ObjectType: " + objType + " | Field: " + field.Name + " | Value: " + value); } else { Debug.LogWarning("The type, " + field.GetType() + ", is not a supported SyncVar type."); } } } }
public void EqualsT() { // .Equals should compare .Value SyncVar <int> field = 42; Assert.That(field.Equals(42), Is.True); }
public void EqualsNull() { // .Equals(null) should always be false. so that == null works. SyncVar <int> field = 42; Assert.That(field.Equals(null), Is.False); }
public void DeserializeDelta_CallsHook() { // create field with hook int called = 0; void OnChanged(int oldValue, int newValue) { ++called; Assert.That(oldValue, Is.EqualTo(42)); Assert.That(newValue, Is.EqualTo(1337)); } SyncVar <int> fieldWithHook = 42; fieldWithHook.Callback += OnChanged; // avoid 'not initialized' exception fieldWithHook.OnDirty = () => {}; // create reader with data NetworkWriter writer = new NetworkWriter(); writer.WriteInt(1337); NetworkReader reader = new NetworkReader(writer.ToArraySegment()); // deserialize fieldWithHook.OnDeserializeDelta(reader); Assert.That(called, Is.EqualTo(1)); }
public void Hook_Set_DoesntDeadlock() { // Value.set calls the hook. // calling Value.set inside the hook would deadlock. // this needs to be prevented. SyncVar <int> field = null; int called = 0; void OnChanged(int oldValue, int newValue) { // setting a different value calls setter -> hook again field.Value = 0; ++called; } field = 42; field.Callback += OnChanged; // avoid 'not initialized' exception field.OnDirty = () => {}; // setting a different value will call the hook field.Value = 1337; // in the end, hook should've been called exactly once Assert.That(called, Is.EqualTo(1)); }
public void ImplicitFrom_SetsValue() { // field = T implicit conversion should set .Value SyncVar <int> field = 42; Assert.That(field.Value, Is.EqualTo(42)); }
public void ImplicitTo() { SyncVar <int> field = new SyncVar <int>(42); // T = field implicit conversion should get .Value int value = field; Assert.That(value, Is.EqualTo(42)); }
public void SendVariableUpdate(SyncVar var) { if (!ServerStage.active) { return; } NetworkMessage.SyncVarUpdateMessage msg = new NetworkMessage.SyncVarUpdateMessage(instanceId, var.VariableName, var.currentItem); ServerStage.SendHighPriorityNetworkMessage(msg); }
public void SetValue_CallsOnDirty() { SyncVar <int> field = 42; int dirtyCalled = 0; field.OnDirty = () => ++ dirtyCalled; // setting SyncField<T>.Value should call dirty field.Value = 1337; Assert.That(dirtyCalled, Is.EqualTo(1)); }
public void SetValue_CallsOnDirty_OnlyIfValueChanged() { SyncVar <int> field = 42; int dirtyCalled = 0; field.OnDirty = () => ++ dirtyCalled; // setting same value should not call OnDirty again field.Value = 42; Assert.That(dirtyCalled, Is.EqualTo(0)); }
public void SetValue_SetsValue() { // .Value is a property which does several things. // make sure it .set actually sets the value SyncVar <int> field = 42; // avoid 'not initialized' exception field.OnDirty = () => {}; field.Value = 1337; Assert.That(field.Value, Is.EqualTo(1337)); }
public void Hook_OnlyCalledIfValueChanged() { int called = 0; void OnChanged(int oldValue, int newValue) { ++called; Assert.That(oldValue, Is.EqualTo(42)); Assert.That(newValue, Is.EqualTo(1337)); } SyncVar <int> field = new SyncVar <int>(42, OnChanged); // assign same value again. hook shouldn't be called again. field.Value = 42; Assert.That(called, Is.EqualTo(0)); }
public void Hook_IsCalled() { int called = 0; void OnChanged(int oldValue, int newValue) { ++called; Assert.That(oldValue, Is.EqualTo(42)); Assert.That(newValue, Is.EqualTo(1337)); } SyncVar <int> field = new SyncVar <int>(42, OnChanged); // avoid 'not initialized' exception field.OnDirty = () => {}; field.Value = 1337; Assert.That(called, Is.EqualTo(1)); }
private void UpdateSyncVars() { FieldInfo[] objectFields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < objectFields.Length; i++) { SyncVar attribute = Attribute.GetCustomAttribute(objectFields[i], typeof(SyncVar)) as SyncVar; if (attribute != null) { List <UpdateSyncVarMessage.SyncVarData> changes = new List <UpdateSyncVarMessage.SyncVarData>(); if (_syncVars.ContainsKey(objectFields[i].Name) && !_syncVars[objectFields[i].Name].Equals(objectFields[i].GetValue(this))) { _syncVars[objectFields[i].Name] = objectFields[i].GetValue(this); changes.Add(new UpdateSyncVarMessage.SyncVarData(objectFields[i].Name, objectFields[i].GetValue(this))); } if (changes.Count > 0) { UpdateSyncVarMessage msg = new UpdateSyncVarMessage(changes.ToArray(), this.entityId); NetworkManager.Instance.TcpSendToServer(msg); } } } }
public void SyncFieldNetworkIdentity_Recommendation() { // should show even if value is null since T is <NetworkIdentity> LogAssert.Expect(LogType.Warning, new Regex($"Use explicit {nameof(SyncVarNetworkIdentity)}.*")); SyncVar <NetworkIdentity> _ = new SyncVar <NetworkIdentity>(null); }
public void RegisterSyncVar(SyncVar var) { syncVarDictionary.Add(var.VariableName, var); }
public void ToString_CallsValueToString() { SyncVar <int> field = 42; Assert.That(field.ToString(), Is.EqualTo("42")); }
public void SyncFieldGameObject_Recommendation() { // should show even if value is null since T is <GameObject> LogAssert.Expect(LogType.Warning, new Regex($"Use explicit {nameof(SyncVarGameObject)}.*")); SyncVar <GameObject> _ = new SyncVar <GameObject>(null); }
public void SyncFieldNetworkBehaviour_Recommendation() { // should show even if value is null since T is <NetworkBehaviour> LogAssert.Expect(LogType.Warning, new Regex($"Use explicit SyncVarNetworkBehaviour.*")); SyncVar <NetworkBehaviour> _ = new SyncVar <NetworkBehaviour>(null); }