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) => { })); }
/// <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); } }
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); }
/// <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; } }