public static void DeserializeDelta( BinaryReader reader, NetworkedComponentTypeInfo networkedComponentTypeInfo, ref NetworkedComponentInfo oldComponentInfo ) { oldComponentInfo.ChangeMask = reader.ReadUInt32(); DeserializeGivenChangeMask(reader, networkedComponentTypeInfo, oldComponentInfo.ComponentState, oldComponentInfo.ChangeMask); }
public static void DeserializeGivenChangeMask( BinaryReader reader, NetworkedComponentTypeInfo networkedComponentTypeInfo, object oldValue, uint changeMask ) { byte changeMaskBitIndex = 0; foreach (var field in networkedComponentTypeInfo.ThingsToSynchronize) { if (BitUtilities.GetBit(changeMask, changeMaskBitIndex)) { if (field.FieldInfo != null) { var newFieldValue = Deserialize( reader, field.FieldInfo.FieldType, field.IsNullableIfReferenceType, field.AreElementsNullableIfReferenceType ); field.FieldInfo.SetValue(oldValue, newFieldValue); } else if (field.PropertyInfo != null) { var newFieldValue = Deserialize( reader, field.PropertyInfo.PropertyType, field.IsNullableIfReferenceType, field.AreElementsNullableIfReferenceType ); field.PropertyInfo.SetValue(oldValue, newFieldValue); } } changeMaskBitIndex++; } }
public static uint GetIdFromState(NetworkedComponentTypeInfo networkedComponentTypeInfo, object state) { UnityEngine.Profiling.Profiler.BeginSample("GetIdFromState"); var id = (uint)networkedComponentTypeInfo.StateIdField.GetValue(state); UnityEngine.Profiling.Profiler.EndSample(); return(id); }
public static void ApplyState( NetworkedComponentTypeInfo networkedComponentTypeInfo, List <NetworkedComponentInfo> oldComponentInfos, List <NetworkedComponentInfo> newComponentInfos, Func <object, UnityEngine.GameObject> createGameObjectFromState ) { Func <NetworkedComponentInfo, NetworkedComponentInfo, bool> doIdsMatch = (nci1, nci2) => GetIdFromState(networkedComponentTypeInfo, nci1.ComponentState) == GetIdFromState(networkedComponentTypeInfo, nci2.ComponentState); Action <NetworkedComponentInfo> handleRemovedState = removedComponentInfo => { var monoBehaviour = GetMonoBehaviourByState(networkedComponentTypeInfo, removedComponentInfo.ComponentState); UnityEngine.Object.Destroy(monoBehaviour.gameObject); }; Action <NetworkedComponentInfo> handleAddedState = addedComponentInfo => { createGameObjectFromState(addedComponentInfo.ComponentState); if ( (networkedComponentTypeInfo != null) && (networkedComponentTypeInfo.MonoBehaviourApplyStateMethod != null) ) { var stateId = GetIdFromState(networkedComponentTypeInfo, addedComponentInfo.ComponentState); var monoBehaviour = GetMonoBehaviourByStateId(networkedComponentTypeInfo, stateId); networkedComponentTypeInfo.MonoBehaviourApplyStateMethod.Invoke(monoBehaviour, new[] { addedComponentInfo.ComponentState }); } }; Action <NetworkedComponentInfo, NetworkedComponentInfo> handleUpdatedState = (oldComponentInfo, newComponentInfo) => { var oldStateId = GetIdFromState(networkedComponentTypeInfo, oldComponentInfo.ComponentState); var monoBehaviour = GetMonoBehaviourByStateId(networkedComponentTypeInfo, oldStateId); if (networkedComponentTypeInfo.MonoBehaviourApplyStateMethod == null) { networkedComponentTypeInfo.MonoBehaviourStateField.SetValue(monoBehaviour, newComponentInfo.ComponentState); } else { networkedComponentTypeInfo.MonoBehaviourApplyStateMethod?.Invoke(monoBehaviour, new[] { newComponentInfo.ComponentState }); } }; ApplyStates( oldComponentInfos, newComponentInfos, doIdsMatch, handleRemovedState, handleAddedState, handleUpdatedState ); }
public static UnityEngine.MonoBehaviour GetMonoBehaviourByStateId( NetworkedComponentTypeInfo networkedComponentTypeInfo, uint stateId ) { var monoBehaviourObjects = (ICollection)networkedComponentTypeInfo.MonoBehaviourInstancesField.GetValue(null); foreach (var obj in monoBehaviourObjects) { var state = networkedComponentTypeInfo.MonoBehaviourStateField.GetValue(obj); var currentStateId = GetIdFromState(networkedComponentTypeInfo, state); if (currentStateId == stateId) { return((UnityEngine.MonoBehaviour)obj); } } return(null); }
public static uint GetChangeMask( NetworkedComponentTypeInfo networkedComponentTypeInfo, object newValue, object oldValue ) { Assert.IsTrue(networkedComponentTypeInfo.ThingsToSynchronize.Count <= (8 * sizeof(uint))); if (oldValue == null) { return(uint.MaxValue); } uint changeMask = 0; uint changeMaskBitIndex = 0; foreach (var field in networkedComponentTypeInfo.ThingsToSynchronize) { object oldFieldValue, newFieldValue; if (field.FieldInfo != null) { oldFieldValue = field.FieldInfo.GetValue(oldValue); newFieldValue = field.FieldInfo.GetValue(newValue); } else if (field.PropertyInfo != null) { oldFieldValue = field.PropertyInfo.GetValue(oldValue); newFieldValue = field.PropertyInfo.GetValue(newValue); } else { throw new Exception("Invalid field to synchronize."); } BitUtilities.SetBit(ref changeMask, (byte)changeMaskBitIndex, !object.Equals(newFieldValue, oldFieldValue)); changeMaskBitIndex++; } return(changeMask); }
public static void SerializeGivenChangeMask( BinaryWriter writer, NetworkedComponentTypeInfo networkedComponentTypeInfo, object value, uint changeMask ) { uint changeMaskBitIndex = 0; foreach (var field in networkedComponentTypeInfo.ThingsToSynchronize) { if (BitUtilities.GetBit(changeMask, (byte)changeMaskBitIndex)) { object fieldValue; Type fieldType; if (field.FieldInfo != null) { fieldValue = field.FieldInfo.GetValue(value); fieldType = field.FieldInfo.FieldType; } else if (field.PropertyInfo != null) { fieldValue = field.PropertyInfo.GetValue(value); fieldType = field.PropertyInfo.PropertyType; } else { throw new Exception("Invalid field to synchronize."); } SerializeObject( writer, fieldValue, fieldType, field.IsNullableIfReferenceType, field.AreElementsNullableIfReferenceType ); } changeMaskBitIndex++; } }
public static UnityEngine.MonoBehaviour GetMonoBehaviourByState(NetworkedComponentTypeInfo networkedComponentTypeInfo, object state) { var stateId = GetIdFromState(networkedComponentTypeInfo, state); return(GetMonoBehaviourByStateId(networkedComponentTypeInfo, stateId)); }