//////////////////////////////////////////////////////////////////////////////////////////////////////////////// internal void OnDeserializeSafely(NetworkBehaviour comp, NetworkReader reader, bool initialState) { // read header as 4 bytes int contentSize = reader.ReadInt32(); // read content byte[] bytes = reader.ReadBytes(contentSize); if (LogFilter.Debug) { Debug.Log("OnDeserializeSafely extracted: " + comp.name + " component=" + comp.GetType() + " sceneId=" + m_SceneId + " length=" + bytes.Length); } // call OnDeserialize with a temporary reader, so that the // original one can't be messed with. we also wrap it in a // try-catch block so there's no way to mess up another // component's deserialization try { NetworkReader componentReader = new NetworkReader(bytes); comp.OnDeserialize(componentReader, initialState); if (componentReader.Position != componentReader.Length) { Debug.LogWarning("OnDeserialize didn't read the full " + bytes.Length + " bytes for object:" + name + " component=" + comp.GetType() + " sceneId=" + m_SceneId + ". Make sure that OnSerialize and OnDeserialize write/read the same amount of data in all cases."); } } catch (Exception e) { // show a detailed error and let the user know what went wrong Debug.LogError("OnDeserialize failed for: object=" + name + " component=" + comp.GetType() + " sceneId=" + m_SceneId + " length=" + bytes.Length + ". Possible Reasons:\n * Do " + comp.GetType() + "'s OnSerialize and OnDeserialize calls write the same amount of data(" + bytes.Length + " bytes)? \n * Was there an exception in " + comp.GetType() + "'s OnSerialize/OnDeserialize code?\n * Are the server and client the exact same project?\n * Maybe this OnDeserialize call was meant for another GameObject? The sceneIds can easily get out of sync if the Hierarchy was modified only in the client OR the server. Try rebuilding both.\n\n" + e.ToString()); } }
public static byte[] ReadBytes(this NetworkReader reader, int count) { var bytes = new byte[count]; reader.ReadBytes(bytes, count); return(bytes); }
// Use checked() to force it to throw OverflowException if data is invalid // null support, see NetworkWriter public static byte[] ReadBytesAndSize(this NetworkReader reader) { // count = 0 means the array was null // otherwise count -1 is the length of the array uint count = reader.ReadPackedUInt32(); return(count == 0 ? null : reader.ReadBytes(checked ((int)(count - 1u)))); }
public static byte[] ReadBytesAndSize(this NetworkReader reader) { // count = 0 means the array was null // otherwise count -1 is the length of the array uint count = reader.ReadUInt(); // Use checked() to force it to throw OverflowException if data is invalid return(count == 0 ? null : reader.ReadBytes(checked ((int)(count - 1u)))); }
// unpack message after receiving public static bool UnpackMessage(byte[] message, out ushort msgType, out byte[] content) { NetworkReader reader = new NetworkReader(message); // read message type (varint) msgType = (UInt16)reader.ReadPackedUInt32(); // read content (remaining data in message) content = reader.ReadBytes(reader.Length - reader.Position); return(true); }
public static Guid ReadGuid(this NetworkReader reader) => new Guid(reader.ReadBytes(16));
public static Guid ReadGuid(this NetworkReader reader) { return(new Guid(reader.ReadBytes(16))); }