NetworkIdentity.Deserialize(NetworkBehaviour) moved to NetworkBehaviour.Deserialize

This commit is contained in:
vis2k 2022-10-09 13:57:43 +02:00
parent 882b3a69b3
commit 389726b011
2 changed files with 39 additions and 39 deletions

View File

@ -1079,6 +1079,44 @@ internal void Serialize(NetworkWriter writer, bool initialState)
//Debug.Log($"OnSerializeSafely written for object {name} component:{GetType()} sceneId:{sceneId:X} header:{headerPosition} content:{contentPosition} end:{endPosition} contentSize:{endPosition - contentPosition}");
}
internal void Deserialize(NetworkReader reader, bool initialState)
{
// read header as 4 bytes and calculate this chunk's start+end
int contentSize = reader.ReadInt();
int chunkStart = reader.Position;
int chunkEnd = reader.Position + contentSize;
// call OnDeserialize and wrap it in a try-catch block so there's no
// way to mess up another component's deserialization
try
{
//Debug.Log($"OnDeserializeSafely: {name} component:{GetType()} sceneId:{sceneId:X} length:{contentSize}");
OnDeserialize(reader, initialState);
}
catch (Exception e)
{
// show a detailed error and let the user know what went wrong
Debug.LogError($"OnDeserialize failed Exception={e.GetType()} (see below) object={name} component={GetType()} sceneId={netIdentity.sceneId:X} length={contentSize}. Possible Reasons:\n" +
$" * Do {GetType()}'s OnSerialize and OnDeserialize calls write the same amount of data({contentSize} bytes)? \n" +
$" * Was there an exception in {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" +
$"Exception {e}");
}
// now the reader should be EXACTLY at 'before + size'.
// otherwise the component read too much / too less data.
if (reader.Position != chunkEnd)
{
// warn the user
int bytesRead = reader.Position - chunkStart;
Debug.LogWarning($"OnDeserialize was expected to read {contentSize} instead of {bytesRead} bytes for object:{name} component={GetType()} sceneId={netIdentity.sceneId:X}. Make sure that OnSerialize and OnDeserialize write/read the same amount of data in all cases.");
// fix the position, so the following components don't all fail
reader.Position = chunkEnd;
}
}
internal void ResetSyncObjects()
{
foreach (SyncObject syncObject in syncObjects)

View File

@ -924,44 +924,6 @@ internal void SerializeAll(bool initialState, NetworkWriter ownerWriter, Network
}
}
void Deserialize(NetworkBehaviour comp, NetworkReader reader, bool initialState)
{
// read header as 4 bytes and calculate this chunk's start+end
int contentSize = reader.ReadInt();
int chunkStart = reader.Position;
int chunkEnd = reader.Position + contentSize;
// call OnDeserialize and wrap it in a try-catch block so there's no
// way to mess up another component's deserialization
try
{
//Debug.Log($"OnDeserializeSafely: {comp.name} component:{comp.GetType()} sceneId:{sceneId:X} length:{contentSize}");
comp.OnDeserialize(reader, initialState);
}
catch (Exception e)
{
// show a detailed error and let the user know what went wrong
Debug.LogError($"OnDeserialize failed Exception={e.GetType()} (see below) object={name} component={comp.GetType()} sceneId={sceneId:X} length={contentSize}. Possible Reasons:\n" +
$" * Do {comp.GetType()}'s OnSerialize and OnDeserialize calls write the same amount of data({contentSize} 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" +
$"Exception {e}");
}
// now the reader should be EXACTLY at 'before + size'.
// otherwise the component read too much / too less data.
if (reader.Position != chunkEnd)
{
// warn the user
int bytesRead = reader.Position - chunkStart;
Debug.LogWarning($"OnDeserialize was expected to read {contentSize} instead of {bytesRead} bytes for object:{name} component={comp.GetType()} sceneId={sceneId:X}. Make sure that OnSerialize and OnDeserialize write/read the same amount of data in all cases.");
// fix the position, so the following components don't all fail
reader.Position = chunkEnd;
}
}
internal void DeserializeAll(NetworkReader reader, bool initialState)
{
if (NetworkBehaviours == null)
@ -982,7 +944,7 @@ internal void DeserializeAll(NetworkReader reader, bool initialState)
if (index < components.Length)
{
// deserialize this component
Deserialize(components[index], reader, initialState);
components[index].Deserialize(reader, initialState);
}
}
}