public OnSerialize ( Mirror.NetworkWriter writer, bool initialState ) : bool | ||
writer | Mirror.NetworkWriter | Writer to use to write to the stream. |
initialState | bool | If this is being called to send initial state. |
return | bool |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// // vis2k: readstring bug prevention: https://issuetracker.unity3d.com/issues/unet-networkwriter-dot-write-causing-readstring-slash-readbytes-out-of-range-errors-in-clients // -> OnSerialize writes length,componentData,length,componentData,... // -> OnDeserialize carefully extracts each data, then deserializes each component with separate readers // -> it will be impossible to read too many or too few bytes in OnDeserialize // -> we can properly track down errors internal bool OnSerializeSafely(NetworkBehaviour comp, NetworkWriter writer, bool initialState) { // serialize into a temporary writer NetworkWriter temp = new NetworkWriter(); bool result = false; try { result = comp.OnSerialize(temp, initialState); } catch (Exception e) { // show a detailed error and let the user know what went wrong Debug.LogError("OnSerialize failed for: object=" + name + " component=" + comp.GetType() + " sceneId=" + m_SceneId + "\n\n" + e.ToString()); } byte[] bytes = temp.ToArray(); if (LogFilter.logDebug) { Debug.Log("OnSerializeSafely written for object=" + comp.name + " component=" + comp.GetType() + " sceneId=" + m_SceneId + " length=" + bytes.Length); } // original HLAPI had a warning in UNetUpdate() in case of large state updates. let's move it here, might // be useful for debugging. if (bytes.Length > Transport.MaxPacketSize) { if (LogFilter.logWarn) { Debug.LogWarning("Large state update of " + bytes.Length + " bytes for netId:" + netId + " from script:" + comp); } } // serialize length,data into the real writer, untouched by user code writer.WriteBytesAndSize(bytes); return(result); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// // vis2k: readstring bug prevention: https://issuetracker.unity3d.com/issues/unet-networkwriter-dot-write-causing-readstring-slash-readbytes-out-of-range-errors-in-clients // -> OnSerialize writes length,componentData,length,componentData,... // -> OnDeserialize carefully extracts each data, then deserializes each component with separate readers // -> it will be impossible to read too many or too few bytes in OnDeserialize // -> we can properly track down errors internal bool OnSerializeSafely(NetworkBehaviour comp, NetworkWriter writer, bool initialState) { // write placeholder length bytes // (jumping back later is WAY faster than allocating a temporary // writer for the payload, then writing payload.size, payload) int headerPosition = writer.Position; writer.Write((int)0); int contentPosition = writer.Position; // write payload bool result = false; try { result = comp.OnSerialize(writer, initialState); } catch (Exception e) { // show a detailed error and let the user know what went wrong Debug.LogError("OnSerialize failed for: object=" + name + " component=" + comp.GetType() + " sceneId=" + m_SceneId + "\n\n" + e.ToString()); } int endPosition = writer.Position; // fill in length now writer.Position = headerPosition; writer.Write(endPosition - contentPosition); writer.Position = endPosition; if (LogFilter.Debug) { Debug.Log("OnSerializeSafely written for object=" + comp.name + " component=" + comp.GetType() + " sceneId=" + m_SceneId + "header@" + headerPosition + " content@" + contentPosition + " end@" + endPosition + " contentSize=" + (endPosition - contentPosition)); } return(result); }