This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Runtime/Messaging/Messages/SnapshotDataMessage.cs
Unity Technologies 36d07fad5e com.unity.netcode.gameobjects@1.0.0-pre.4
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [1.0.0-pre.4] - 2021-01-04

### Added

- Added `com.unity.modules.physics` and `com.unity.modules.physics2d` package dependencies (#1565)

### Removed

- Removed `com.unity.modules.ai` package dependency (#1565)
- Removed `FixedQueue`, `StreamExtensions`, `TypeExtensions` (#1398)

### Fixed
- Fixed in-scene NetworkObjects that are moved into the DDOL scene not getting restored to their original active state (enabled/disabled) after a full scene transition (#1354)
- Fixed invalid IL code being generated when using `this` instead of `this ref` for the FastBufferReader/FastBufferWriter parameter of an extension method. (#1393)
- Fixed an issue where if you are running as a server (not host) the LoadEventCompleted and UnloadEventCompleted events would fire early by the NetworkSceneManager (#1379)
- Fixed a runtime error when sending an array of an INetworkSerializable type that's implemented as a struct (#1402)
- NetworkConfig will no longer throw an OverflowException in GetConfig() when ForceSamePrefabs is enabled and the number of prefabs causes the config blob size to exceed 1300 bytes. (#1385)
- Fixed NetworkVariable not calling NetworkSerialize on INetworkSerializable types (#1383)
- Fixed NullReferenceException on ImportReferences call in NetworkBehaviourILPP (#1434)
- Fixed NetworkObjects not being despawned before they are destroyed during shutdown for client, host, and server instances. (#1390)
- Fixed KeyNotFound exception when removing ownership of a newly spawned NetworkObject that is already owned by the server. (#1500)
- Fixed NetworkManager.LocalClient not being set when starting as a host. (#1511)
- Fixed a few memory leak cases when shutting down NetworkManager during Incoming Message Queue processing. (#1323)

### Changed
- The SDK no longer limits message size to 64k. (The transport may still impose its own limits, but the SDK no longer does.) (#1384)
- Updated com.unity.collections to 1.1.0 (#1451)
2021-01-04 00:00:00 +00:00

173 lines
6.0 KiB
C#

using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
namespace Unity.Netcode
{
internal struct SnapshotDataMessage : INetworkMessage
{
public int CurrentTick;
public ushort Sequence;
public ushort Range;
public byte[] SendMainBuffer;
public NativeArray<byte> ReceiveMainBuffer;
public struct AckData
{
public ushort LastReceivedSequence;
public ushort ReceivedSequenceMask;
}
public AckData Ack;
public struct EntryData
{
public ulong NetworkObjectId;
public ushort BehaviourIndex;
public ushort VariableIndex;
public int TickWritten;
public ushort Position;
public ushort Length;
}
public NativeList<EntryData> Entries;
public struct SpawnData
{
public ulong NetworkObjectId;
public uint Hash;
public bool IsSceneObject;
public bool IsPlayerObject;
public ulong OwnerClientId;
public ulong ParentNetworkId;
public Vector3 Position;
public Quaternion Rotation;
public Vector3 Scale;
public int TickWritten;
}
public NativeList<SpawnData> Spawns;
public struct DespawnData
{
public ulong NetworkObjectId;
public int TickWritten;
}
public NativeList<DespawnData> Despawns;
public unsafe void Serialize(FastBufferWriter writer)
{
if (!writer.TryBeginWrite(
FastBufferWriter.GetWriteSize(CurrentTick) +
FastBufferWriter.GetWriteSize(Sequence) +
FastBufferWriter.GetWriteSize(Range) + Range +
FastBufferWriter.GetWriteSize(Ack) +
FastBufferWriter.GetWriteSize<ushort>() +
Entries.Length * sizeof(EntryData) +
FastBufferWriter.GetWriteSize<ushort>() +
Spawns.Length * sizeof(SpawnData) +
FastBufferWriter.GetWriteSize<ushort>() +
Despawns.Length * sizeof(DespawnData)
))
{
Entries.Dispose();
Spawns.Dispose();
Despawns.Dispose();
throw new OverflowException($"Not enough space to serialize {nameof(SnapshotDataMessage)}");
}
writer.WriteValue(CurrentTick);
writer.WriteValue(Sequence);
writer.WriteValue(Range);
writer.WriteBytes(SendMainBuffer, Range);
writer.WriteValue(Ack);
writer.WriteValue((ushort)Entries.Length);
writer.WriteBytes((byte*)Entries.GetUnsafePtr(), Entries.Length * sizeof(EntryData));
writer.WriteValue((ushort)Spawns.Length);
writer.WriteBytes((byte*)Spawns.GetUnsafePtr(), Spawns.Length * sizeof(SpawnData));
writer.WriteValue((ushort)Despawns.Length);
writer.WriteBytes((byte*)Despawns.GetUnsafePtr(), Despawns.Length * sizeof(DespawnData));
Entries.Dispose();
Spawns.Dispose();
Despawns.Dispose();
}
public static unsafe void Receive(FastBufferReader reader, in NetworkContext context)
{
var message = new SnapshotDataMessage();
if (!reader.TryBeginRead(
FastBufferWriter.GetWriteSize(message.CurrentTick) +
FastBufferWriter.GetWriteSize(message.Sequence) +
FastBufferWriter.GetWriteSize(message.Range)
))
{
throw new OverflowException($"Not enough space to deserialize {nameof(SnapshotDataMessage)}");
}
reader.ReadValue(out message.CurrentTick);
reader.ReadValue(out message.Sequence);
reader.ReadValue(out message.Range);
message.ReceiveMainBuffer = new NativeArray<byte>(message.Range, Allocator.Temp);
reader.ReadBytesSafe((byte*)message.ReceiveMainBuffer.GetUnsafePtr(), message.Range);
reader.ReadValueSafe(out message.Ack);
reader.ReadValueSafe(out ushort length);
message.Entries = new NativeList<EntryData>(length, Allocator.Temp);
message.Entries.Length = length;
reader.ReadBytesSafe((byte*)message.Entries.GetUnsafePtr(), message.Entries.Length * sizeof(EntryData));
reader.ReadValueSafe(out length);
message.Spawns = new NativeList<SpawnData>(length, Allocator.Temp);
message.Spawns.Length = length;
reader.ReadBytesSafe((byte*)message.Spawns.GetUnsafePtr(), message.Spawns.Length * sizeof(SpawnData));
reader.ReadValueSafe(out length);
message.Despawns = new NativeList<DespawnData>(length, Allocator.Temp);
message.Despawns.Length = length;
reader.ReadBytesSafe((byte*)message.Despawns.GetUnsafePtr(), message.Despawns.Length * sizeof(DespawnData));
using (message.ReceiveMainBuffer)
using (message.Entries)
using (message.Spawns)
using (message.Despawns)
{
message.Handle(context.SenderId, context.SystemOwner);
}
}
public void Handle(ulong senderId, object systemOwner)
{
if (systemOwner is NetworkManager)
{
var networkManager = (NetworkManager)systemOwner;
// todo: temporary hack around bug
if (!networkManager.IsServer)
{
senderId = networkManager.ServerClientId;
}
var snapshotSystem = networkManager.SnapshotSystem;
snapshotSystem.HandleSnapshot(senderId, this);
}
else
{
var ownerData = (Tuple<SnapshotSystem, ulong>)systemOwner;
var snapshotSystem = ownerData.Item1;
snapshotSystem.HandleSnapshot(ownerData.Item2, this);
return;
}
}
}
}