Mirror Networking
Mirror.NetworkReaderExtensions Class Reference

Static Public Member Functions

static byte ReadByte (this NetworkReader reader)
 
static ? byte ReadByteNullable (this NetworkReader reader)
 
static sbyte ReadSByte (this NetworkReader reader)
 
static ? sbyte ReadSByteNullable (this NetworkReader reader)
 
static char ReadChar (this NetworkReader reader)
 
static ? char ReadCharNullable (this NetworkReader reader)
 
static bool ReadBool (this NetworkReader reader)
 
static ? bool ReadBoolNullable (this NetworkReader reader)
 
static short ReadShort (this NetworkReader reader)
 
static ? short ReadShortNullable (this NetworkReader reader)
 
static ushort ReadUShort (this NetworkReader reader)
 
static ? ushort ReadUShortNullable (this NetworkReader reader)
 
static int ReadInt (this NetworkReader reader)
 
static ? int ReadIntNullable (this NetworkReader reader)
 
static uint ReadUInt (this NetworkReader reader)
 
static ? uint ReadUIntNullable (this NetworkReader reader)
 
static long ReadLong (this NetworkReader reader)
 
static ? long ReadLongNullable (this NetworkReader reader)
 
static ulong ReadULong (this NetworkReader reader)
 
static ? ulong ReadULongNullable (this NetworkReader reader)
 
static float ReadFloat (this NetworkReader reader)
 
static ? float ReadFloatNullable (this NetworkReader reader)
 
static double ReadDouble (this NetworkReader reader)
 
static ? double ReadDoubleNullable (this NetworkReader reader)
 
static decimal ReadDecimal (this NetworkReader reader)
 
static ? decimal ReadDecimalNullable (this NetworkReader reader)
 
static string ReadString (this NetworkReader reader)
 
Exceptions
T:System.ArgumentExceptionif an invalid utf8 string is sent
More...
 
static byte[] ReadBytesAndSize (this NetworkReader reader)
 
Exceptions
T:OverflowExceptionif count is invalid
More...
 
static byte[] ReadBytes (this NetworkReader reader, int count)
 
static ArraySegment< byte > ReadBytesAndSizeSegment (this NetworkReader reader)
 
Exceptions
T:OverflowExceptionif count is invalid
More...
 
static Vector2 ReadVector2 (this NetworkReader reader)
 
static ? Vector2 ReadVector2Nullable (this NetworkReader reader)
 
static Vector3 ReadVector3 (this NetworkReader reader)
 
static ? Vector3 ReadVector3Nullable (this NetworkReader reader)
 
static Vector4 ReadVector4 (this NetworkReader reader)
 
static ? Vector4 ReadVector4Nullable (this NetworkReader reader)
 
static Vector2Int ReadVector2Int (this NetworkReader reader)
 
static ? Vector2Int ReadVector2IntNullable (this NetworkReader reader)
 
static Vector3Int ReadVector3Int (this NetworkReader reader)
 
static ? Vector3Int ReadVector3IntNullable (this NetworkReader reader)
 
static Color ReadColor (this NetworkReader reader)
 
static ? Color ReadColorNullable (this NetworkReader reader)
 
static Color32 ReadColor32 (this NetworkReader reader)
 
static ? Color32 ReadColor32Nullable (this NetworkReader reader)
 
static Quaternion ReadQuaternion (this NetworkReader reader)
 
static ? Quaternion ReadQuaternionNullable (this NetworkReader reader)
 
static Rect ReadRect (this NetworkReader reader)
 
static ? Rect ReadRectNullable (this NetworkReader reader)
 
static Plane ReadPlane (this NetworkReader reader)
 
static ? Plane ReadPlaneNullable (this NetworkReader reader)
 
static Ray ReadRay (this NetworkReader reader)
 
static ? Ray ReadRayNullable (this NetworkReader reader)
 
static Matrix4x4 ReadMatrix4x4 (this NetworkReader reader)
 
static ? Matrix4x4 ReadMatrix4x4Nullable (this NetworkReader reader)
 
static Guid ReadGuid (this NetworkReader reader)
 
static ? Guid ReadGuidNullable (this NetworkReader reader)
 
static NetworkIdentity ReadNetworkIdentity (this NetworkReader reader)
 
static NetworkBehaviour ReadNetworkBehaviour (this NetworkReader reader)
 
static T ReadNetworkBehaviour< T > (this NetworkReader reader)
 
static NetworkBehaviour.NetworkBehaviourSyncVar ReadNetworkBehaviourSyncVar (this NetworkReader reader)
 
static Transform ReadTransform (this NetworkReader reader)
 
static GameObject ReadGameObject (this NetworkReader reader)
 
static List< T > ReadList< T > (this NetworkReader reader)
 
static T[] ReadArray< T > (this NetworkReader reader)
 
static Uri ReadUri (this NetworkReader reader)
 
static Texture2D ReadTexture2D (this NetworkReader reader)
 
static Sprite ReadSprite (this NetworkReader reader)
 

Detailed Description

Definition at line 11 of file NetworkReaderExtensions.cs.

Member Function Documentation

◆ ReadArray< T >()

static T[] Mirror.NetworkReaderExtensions.ReadArray< T > ( this NetworkReader  reader)
static

Definition at line 240 of file NetworkReaderExtensions.cs.

241 {
242 int length = reader.ReadInt();
243
244 // we write -1 for null
245 if (length < 0)
246 return null;
247
248 // todo throw an exception for other negative values (we never write them, likely to be attacker)
249
250 // this assumes that a reader for T reads at least 1 bytes
251 // we can't know the exact size of T because it could have a user created reader
252 // NOTE: don't add to length as it could overflow if value is int.max
253 if (length > reader.Length - reader.Position)
254 {
255 throw new EndOfStreamException($"Received array that is too large: {length}");
256 }
257
258 T[] result = new T[length];
259 for (int i = 0; i < length; i++)
260 {
261 result[i] = reader.Read<T>();
262 }
263 return result;
264 }

◆ ReadBoolNullable()

static ? bool Mirror.NetworkReaderExtensions.ReadBoolNullable ( this NetworkReader  reader)
static

Definition at line 30 of file NetworkReaderExtensions.cs.

31 {
32 byte? value = reader.ReadBlittableNullable<byte>();
33 return value.HasValue ? (value.Value != 0) : default(bool?);
34 }

◆ ReadBytes()

static byte[] Mirror.NetworkReaderExtensions.ReadBytes ( this NetworkReader  reader,
int  count 
)
static

Definition at line 97 of file NetworkReaderExtensions.cs.

98 {
99 byte[] bytes = new byte[count];
100 reader.ReadBytes(bytes, count);
101 return bytes;
102 }

◆ ReadBytesAndSize()

static byte[] Mirror.NetworkReaderExtensions.ReadBytesAndSize ( this NetworkReader  reader)
static

Exceptions
T:OverflowExceptionif count is invalid

Definition at line 88 of file NetworkReaderExtensions.cs.

89 {
90 // count = 0 means the array was null
91 // otherwise count -1 is the length of the array
92 uint count = reader.ReadUInt();
93 // Use checked() to force it to throw OverflowException if data is invalid
94 return count == 0 ? null : reader.ReadBytes(checked((int)(count - 1u)));
95 }

◆ ReadBytesAndSizeSegment()

static ArraySegment< byte > Mirror.NetworkReaderExtensions.ReadBytesAndSizeSegment ( this NetworkReader  reader)
static

Exceptions
T:OverflowExceptionif count is invalid

Definition at line 105 of file NetworkReaderExtensions.cs.

106 {
107 // count = 0 means the array was null
108 // otherwise count - 1 is the length of the array
109 uint count = reader.ReadUInt();
110 // Use checked() to force it to throw OverflowException if data is invalid
111 return count == 0 ? default : reader.ReadBytesSegment(checked((int)(count - 1u)));
112 }

◆ ReadGameObject()

static GameObject Mirror.NetworkReaderExtensions.ReadGameObject ( this NetworkReader  reader)
static

Definition at line 220 of file NetworkReaderExtensions.cs.

221 {
222 // Don't use null propagation here as it could lead to MissingReferenceException
223 NetworkIdentity networkIdentity = reader.ReadNetworkIdentity();
224 return networkIdentity != null ? networkIdentity.gameObject : null;
225 }

◆ ReadList< T >()

static List< T > Mirror.NetworkReaderExtensions.ReadList< T > ( this NetworkReader  reader)
static

Definition at line 227 of file NetworkReaderExtensions.cs.

228 {
229 int length = reader.ReadInt();
230 if (length < 0)
231 return null;
232 List<T> result = new List<T>(length);
233 for (int i = 0; i < length; i++)
234 {
235 result.Add(reader.Read<T>());
236 }
237 return result;
238 }

◆ ReadNetworkBehaviour()

static NetworkBehaviour Mirror.NetworkReaderExtensions.ReadNetworkBehaviour ( this NetworkReader  reader)
static

Definition at line 166 of file NetworkReaderExtensions.cs.

167 {
168 // read netId first.
169 //
170 // IMPORTANT: if netId != 0, writer always writes componentIndex.
171 // reusing ReadNetworkIdentity() might return a null NetworkIdentity
172 // even if netId was != 0 but the identity disappeared on the client,
173 // resulting in unequal amounts of data being written / read.
174 // https://github.com/vis2k/Mirror/issues/2972
175 uint netId = reader.ReadUInt();
176 if (netId == 0)
177 return null;
178
179 // read component index in any case, BEFORE searching the spawned
180 // NetworkIdentity by netId.
181 byte componentIndex = reader.ReadByte();
182
183 // NOTE: a netId not being in spawned is common.
184 // for example, "[SyncVar] NetworkIdentity target" netId would not
185 // be known on client if the monster walks out of proximity for a
186 // moment. no need to log any error or warning here.
187 NetworkIdentity identity = Utils.GetSpawnedInServerOrClient(netId);
188
189 return identity != null
190 ? identity.NetworkBehaviours[componentIndex]
191 : null;
192 }

◆ ReadNetworkBehaviour< T >()

static T Mirror.NetworkReaderExtensions.ReadNetworkBehaviour< T > ( this NetworkReader  reader)
static
Type Constraints
T :NetworkBehaviour 

Definition at line 194 of file NetworkReaderExtensions.cs.

194 : NetworkBehaviour
195 {
196 return reader.ReadNetworkBehaviour() as T;
197 }

◆ ReadNetworkBehaviourSyncVar()

static NetworkBehaviour.NetworkBehaviourSyncVar Mirror.NetworkReaderExtensions.ReadNetworkBehaviourSyncVar ( this NetworkReader  reader)
static

Definition at line 199 of file NetworkReaderExtensions.cs.

200 {
201 uint netId = reader.ReadUInt();
202 byte componentIndex = default;
203
204 // if netId is not 0, then index is also sent to read before returning
205 if (netId != 0)
206 {
207 componentIndex = reader.ReadByte();
208 }
209
210 return new NetworkBehaviour.NetworkBehaviourSyncVar(netId, componentIndex);
211 }

◆ ReadNetworkIdentity()

static NetworkIdentity Mirror.NetworkReaderExtensions.ReadNetworkIdentity ( this NetworkReader  reader)
static

Definition at line 153 of file NetworkReaderExtensions.cs.

154 {
155 uint netId = reader.ReadUInt();
156 if (netId == 0)
157 return null;
158
159 // NOTE: a netId not being in spawned is common.
160 // for example, "[SyncVar] NetworkIdentity target" netId would not
161 // be known on client if the monster walks out of proximity for a
162 // moment. no need to log any error or warning here.
163 return Utils.GetSpawnedInServerOrClient(netId);
164 }

◆ ReadSprite()

static Sprite Mirror.NetworkReaderExtensions.ReadSprite ( this NetworkReader  reader)
static

Definition at line 293 of file NetworkReaderExtensions.cs.

294 {
295 // support 'null' textures for [SyncVar]s etc.
296 // https://github.com/vis2k/Mirror/issues/3144
297 Texture2D texture = reader.ReadTexture2D();
298 if (texture == null) return null;
299
300 // otherwise create a valid sprite
301 return Sprite.Create(texture, reader.ReadRect(), reader.ReadVector2());
302 }

◆ ReadString()

static string Mirror.NetworkReaderExtensions.ReadString ( this NetworkReader  reader)
static

Exceptions
T:System.ArgumentExceptionif an invalid utf8 string is sent

Definition at line 64 of file NetworkReaderExtensions.cs.

65 {
66 // read number of bytes
67 ushort size = reader.ReadUShort();
68
69 // null support, see NetworkWriter
70 if (size == 0)
71 return null;
72
73 int realSize = size - 1;
74
75 // make sure it's within limits to avoid allocation attacks etc.
76 if (realSize >= NetworkWriter.MaxStringLength)
77 {
78 throw new EndOfStreamException($"ReadString too long: {realSize}. Limit is: {NetworkWriter.MaxStringLength}");
79 }
80
81 ArraySegment<byte> data = reader.ReadBytesSegment(realSize);
82
83 // convert directly from buffer to string via encoding
84 return encoding.GetString(data.Array, data.Offset, data.Count);
85 }

◆ ReadTexture2D()

static Texture2D Mirror.NetworkReaderExtensions.ReadTexture2D ( this NetworkReader  reader)
static

Definition at line 272 of file NetworkReaderExtensions.cs.

273 {
274 // TODO allocation protection when sending textures to server.
275 // currently can allocate 32k x 32k x 4 byte = 3.8 GB
276
277 // support 'null' textures for [SyncVar]s etc.
278 // https://github.com/vis2k/Mirror/issues/3144
279 short width = reader.ReadShort();
280 if (width == -1) return null;
281
282 // read height
283 short height = reader.ReadShort();
284 Texture2D texture2D = new Texture2D(width, height);
285
286 // read pixel content
287 Color32[] pixels = reader.ReadArray<Color32>();
288 texture2D.SetPixels32(pixels);
289 texture2D.Apply();
290 return texture2D;
291 }

◆ ReadTransform()

static Transform Mirror.NetworkReaderExtensions.ReadTransform ( this NetworkReader  reader)
static

Definition at line 213 of file NetworkReaderExtensions.cs.

214 {
215 // Don't use null propagation here as it could lead to MissingReferenceException
216 NetworkIdentity networkIdentity = reader.ReadNetworkIdentity();
217 return networkIdentity != null ? networkIdentity.transform : null;
218 }

◆ ReadUri()

static Uri Mirror.NetworkReaderExtensions.ReadUri ( this NetworkReader  reader)
static

Definition at line 266 of file NetworkReaderExtensions.cs.

267 {
268 string uriString = reader.ReadString();
269 return (string.IsNullOrWhiteSpace(uriString) ? null : new Uri(uriString));
270 }