示例#1
0
        public void Test_Can_Register_Callback_Without_Throw(long guid, EntityDataFieldType fieldType)
        {
            //arrange
            EntityDataChangeCallbackManager callbackManager = new EntityDataChangeCallbackManager();

            Assert.DoesNotThrow(() => callbackManager.RegisterCallback <float>(new NetworkEntityGuid((ulong)guid), fieldType, (eg, args) => { }));
        }
示例#2
0
        /// <inheritdoc />
        public void InvokeChangeEvents(NetworkEntityGuid entity, EntityDataFieldType field, int newValueAsInt)
        {
            //We aren't watching ANY data changes for this particular entity.
            if (!CallbackMap.ContainsKey(entity))
            {
                return;
            }

            //If we have any registered callbacks for this entity's data change we should dispatch it (they will all be called)
            if (CallbackMap[entity].ContainsKey(field))
            {
                CallbackMap[entity][field]?.Invoke(newValueAsInt);
            }
        }
示例#3
0
        public void Test_Can_Registered_Callback_Called(long guid, EntityDataFieldType fieldType)
        {
            //arrange
            Mock <IEnumerable> testCallback = new Mock <IEnumerable>(MockBehavior.Loose);
            EntityDataChangeCallbackManager callbackManager = new EntityDataChangeCallbackManager();

            //act
            callbackManager.RegisterCallback <float>(new NetworkEntityGuid((ulong)guid), fieldType, (eg, args) =>
            {
                //Call so we can check for test purposes
                testCallback.Object.GetEnumerator();
            });

            callbackManager.InvokeChangeEvents(new NetworkEntityGuid((ulong)guid), fieldType, 5);

            //assert
            testCallback.Verify(enumerable => enumerable.GetEnumerator(), Times.Once);
        }
示例#4
0
        /// <inheritdoc />
        public void RegisterCallback <TCallbackValueCastType>(NetworkEntityGuid entity, EntityDataFieldType dataField, Action <NetworkEntityGuid, EntityDataChangedArgs <TCallbackValueCastType> > callback)
            where TCallbackValueCastType : struct
        {
            //TODO: Anyway we can avoid this for registering callbacks, wasted cycles kinda
            if (!CallbackMap.ContainsKey(entity))
            {
                CallbackMap.Add(entity, new Dictionary <EntityDataFieldType, Action <int> >());
            }

            //TODO: This isn't thread safe, this whole thinjg isn't. That could be problematic
            Action <int> dataChangeEvent = newValue =>
            {
                //TODO: If we ever support original value we should change this
                //So, the callback needs to send the entity guid and the entity data change args which contain the original (not working yet) and new value.
                callback(entity, new EntityDataChangedArgs <TCallbackValueCastType>(default(TCallbackValueCastType), Unsafe.As <int, TCallbackValueCastType>(ref newValue)));
            };

            //We need to add a null action here or it will throw when we try to add the action. But if one exists we need to Delegate.Combine
            if (!CallbackMap[entity].ContainsKey(dataField))
            {
                CallbackMap[entity].Add(dataField, dataChangeEvent);
            }
            else
            {
                CallbackMap[entity][dataField] += dataChangeEvent;
            }
        }