public static T[] ReadArray <T>(this NetworkReader reader) { int length = reader.ReadInt(); // we write -1 for null if (length < 0) { return(null); } // todo throw an exception for other negative values (we never write them, likely to be attacker) // this assumes that a reader for T reads at least 1 bytes // we can't know the exact size of T because it could have a user created reader // NOTE: don't add to length as it could overflow if value is int.max if (length > reader.Length - reader.Position) { throw new EndOfStreamException($"Received array that is too large: {length}"); } T[] result = new T[length]; for (int i = 0; i < length; i++) { result[i] = reader.Read <T>(); } return(result); }
/// <summary> /// Custom Deserialization /// </summary> /// <param name="reader"></param> /// <param name="initialState"></param> public override void OnDeserialize(NetworkReader reader, bool initialState) { base.OnDeserialize(reader, initialState); if (initialState) { for (int i = 0; i < animator.layerCount; i++) { int stateHash = reader.ReadInt(); float normalizedTime = reader.ReadFloat(); animator.SetLayerWeight(i, reader.ReadFloat()); animator.Play(stateHash, i, normalizedTime); } ReadParameters(reader); } }
public static List <T> ReadList <T>(this NetworkReader reader) { int length = reader.ReadInt(); if (length < 0) { return(null); } List <T> result = new List <T>(length); for (int i = 0; i < length; i++) { result.Add(reader.Read <T>()); } return(result); }
void ReadParameters(NetworkReader reader) { bool animatorEnabled = animator.enabled; // need to read values from NetworkReader even if animator is disabled ulong dirtyBits = reader.ReadULong(); for (int i = 0; i < parameters.Length; i++) { if ((dirtyBits & (1ul << i)) == 0) { continue; } AnimatorControllerParameter par = parameters[i]; if (par.type == AnimatorControllerParameterType.Int) { int newIntValue = reader.ReadInt(); if (animatorEnabled) { animator.SetInteger(par.nameHash, newIntValue); } } else if (par.type == AnimatorControllerParameterType.Float) { float newFloatValue = reader.ReadFloat(); if (animatorEnabled) { animator.SetFloat(par.nameHash, newFloatValue); } } else if (par.type == AnimatorControllerParameterType.Bool) { bool newBoolValue = reader.ReadBool(); if (animatorEnabled) { animator.SetBool(par.nameHash, newBoolValue); } } } }