Mirror Networking
Mirror.NetworkWriterExtensions Class Reference

Classes

struct  UIntDouble
 

Static Public Member Functions

static void WriteByte (this NetworkWriter writer, byte value)
 
static void WriteByteNullable (this NetworkWriter writer, byte? value)
 
static void WriteSByte (this NetworkWriter writer, sbyte value)
 
static void WriteSByteNullable (this NetworkWriter writer, sbyte? value)
 
static void WriteChar (this NetworkWriter writer, char value)
 
static void WriteCharNullable (this NetworkWriter writer, char? value)
 
static void WriteBool (this NetworkWriter writer, bool value)
 
static void WriteBoolNullable (this NetworkWriter writer, bool? value)
 
static void WriteShort (this NetworkWriter writer, short value)
 
static void WriteShortNullable (this NetworkWriter writer, short? value)
 
static void WriteUShort (this NetworkWriter writer, ushort value)
 
static void WriteUShortNullable (this NetworkWriter writer, ushort? value)
 
static void WriteInt (this NetworkWriter writer, int value)
 
static void WriteIntNullable (this NetworkWriter writer, int? value)
 
static void WriteUInt (this NetworkWriter writer, uint value)
 
static void WriteUIntNullable (this NetworkWriter writer, uint? value)
 
static void WriteLong (this NetworkWriter writer, long value)
 
static void WriteLongNullable (this NetworkWriter writer, long? value)
 
static void WriteULong (this NetworkWriter writer, ulong value)
 
static void WriteULongNullable (this NetworkWriter writer, ulong? value)
 
static void WriteFloat (this NetworkWriter writer, float value)
 
static void WriteFloatNullable (this NetworkWriter writer, float? value)
 
static void WriteDouble (this NetworkWriter writer, double value)
 
static void WriteDoubleNullable (this NetworkWriter writer, double? value)
 
static void WriteDecimal (this NetworkWriter writer, decimal value)
 
static void WriteDecimalNullable (this NetworkWriter writer, decimal? value)
 
static void WriteString (this NetworkWriter writer, string value)
 
static void WriteBytesAndSizeSegment (this NetworkWriter writer, ArraySegment< byte > buffer)
 
static void WriteBytesAndSize (this NetworkWriter writer, byte[] buffer)
 
static void WriteBytesAndSize (this NetworkWriter writer, byte[] buffer, int offset, int count)
 
static void WriteArraySegment< T > (this NetworkWriter writer, ArraySegment< T > segment)
 
static void WriteVector2 (this NetworkWriter writer, Vector2 value)
 
static void WriteVector2Nullable (this NetworkWriter writer, Vector2? value)
 
static void WriteVector3 (this NetworkWriter writer, Vector3 value)
 
static void WriteVector3Nullable (this NetworkWriter writer, Vector3? value)
 
static void WriteVector4 (this NetworkWriter writer, Vector4 value)
 
static void WriteVector4Nullable (this NetworkWriter writer, Vector4? value)
 
static void WriteVector2Int (this NetworkWriter writer, Vector2Int value)
 
static void WriteVector2IntNullable (this NetworkWriter writer, Vector2Int? value)
 
static void WriteVector3Int (this NetworkWriter writer, Vector3Int value)
 
static void WriteVector3IntNullable (this NetworkWriter writer, Vector3Int? value)
 
static void WriteColor (this NetworkWriter writer, Color value)
 
static void WriteColorNullable (this NetworkWriter writer, Color? value)
 
static void WriteColor32 (this NetworkWriter writer, Color32 value)
 
static void WriteColor32Nullable (this NetworkWriter writer, Color32? value)
 
static void WriteQuaternion (this NetworkWriter writer, Quaternion value)
 
static void WriteQuaternionNullable (this NetworkWriter writer, Quaternion? value)
 
static void WriteRect (this NetworkWriter writer, Rect value)
 
static void WriteRectNullable (this NetworkWriter writer, Rect? value)
 
static void WritePlane (this NetworkWriter writer, Plane value)
 
static void WritePlaneNullable (this NetworkWriter writer, Plane? value)
 
static void WriteRay (this NetworkWriter writer, Ray value)
 
static void WriteRayNullable (this NetworkWriter writer, Ray? value)
 
static void WriteMatrix4x4 (this NetworkWriter writer, Matrix4x4 value)
 
static void WriteMatrix4x4Nullable (this NetworkWriter writer, Matrix4x4? value)
 
static void WriteGuid (this NetworkWriter writer, Guid value)
 
static void WriteGuidNullable (this NetworkWriter writer, Guid? value)
 
static void WriteNetworkIdentity (this NetworkWriter writer, NetworkIdentity value)
 
static void WriteNetworkBehaviour (this NetworkWriter writer, NetworkBehaviour value)
 
static void WriteTransform (this NetworkWriter writer, Transform value)
 
static void WriteGameObject (this NetworkWriter writer, GameObject value)
 
static void WriteList< T > (this NetworkWriter writer, List< T > list)
 
static void WriteArray< T > (this NetworkWriter writer, T[] array)
 
static void WriteUri (this NetworkWriter writer, Uri uri)
 
static void WriteTexture2D (this NetworkWriter writer, Texture2D texture2D)
 
static void WriteSprite (this NetworkWriter writer, Sprite sprite)
 

Detailed Description

Definition at line 11 of file NetworkWriterExtensions.cs.

Member Function Documentation

◆ WriteArray< T >()

static void Mirror.NetworkWriterExtensions.WriteArray< T > ( this NetworkWriter  writer,
T[]  array 
)
static

Definition at line 275 of file NetworkWriterExtensions.cs.

276 {
277 if (array is null)
278 {
279 writer.WriteInt(-1);
280 return;
281 }
282 writer.WriteInt(array.Length);
283 for (int i = 0; i < array.Length; i++)
284 writer.Write(array[i]);
285 }

◆ WriteArraySegment< T >()

static void Mirror.NetworkWriterExtensions.WriteArraySegment< T > ( this NetworkWriter  writer,
ArraySegment< T >  segment 
)
static

Definition at line 136 of file NetworkWriterExtensions.cs.

137 {
138 int length = segment.Count;
139 writer.WriteInt(length);
140 for (int i = 0; i < length; i++)
141 {
142 writer.Write(segment.Array[segment.Offset + i]);
143 }
144 }

◆ WriteBytesAndSize() [1/2]

static void Mirror.NetworkWriterExtensions.WriteBytesAndSize ( this NetworkWriter  writer,
byte[]  buffer 
)
static

Definition at line 113 of file NetworkWriterExtensions.cs.

114 {
115 // buffer might be null, so we can't use .Length in that case
116 writer.WriteBytesAndSize(buffer, 0, buffer != null ? buffer.Length : 0);
117 }

◆ WriteBytesAndSize() [2/2]

static void Mirror.NetworkWriterExtensions.WriteBytesAndSize ( this NetworkWriter  writer,
byte[]  buffer,
int  offset,
int  count 
)
static

Definition at line 122 of file NetworkWriterExtensions.cs.

123 {
124 // null is supported because [SyncVar]s might be structs with null byte[] arrays
125 // write 0 for null array, increment normal size by 1 to save bandwidth
126 // (using size=-1 for null would limit max size to 32kb instead of 64kb)
127 if (buffer == null)
128 {
129 writer.WriteUInt(0u);
130 return;
131 }
132 writer.WriteUInt(checked((uint)count) + 1u);
133 writer.WriteBytes(buffer, offset, count);
134 }

◆ WriteBytesAndSizeSegment()

static void Mirror.NetworkWriterExtensions.WriteBytesAndSizeSegment ( this NetworkWriter  writer,
ArraySegment< byte >  buffer 
)
static

Definition at line 105 of file NetworkWriterExtensions.cs.

106 {
107 writer.WriteBytesAndSize(buffer.Array, buffer.Offset, buffer.Count);
108 }

◆ WriteDouble()

static void Mirror.NetworkWriterExtensions.WriteDouble ( this NetworkWriter  writer,
double  value 
)
static

Definition at line 64 of file NetworkWriterExtensions.cs.

65 {
66 // DEBUG: try to find the exact value that fails.
67 //UIntDouble convert = new UIntDouble{doubleValue = value};
68 //Debug.Log($"=> NetworkWriter.WriteDouble: {value} => 0x{convert.longValue:X8}");
69
70
71 writer.WriteBlittable(value);
72 }

◆ WriteGameObject()

static void Mirror.NetworkWriterExtensions.WriteGameObject ( this NetworkWriter  writer,
GameObject  value 
)
static

Definition at line 245 of file NetworkWriterExtensions.cs.

246 {
247 if (value == null)
248 {
249 writer.WriteUInt(0);
250 return;
251 }
252
253 // warn if the GameObject doesn't have a NetworkIdentity,
254 NetworkIdentity identity = value.GetComponent<NetworkIdentity>();
255 if (identity == null)
256 Debug.LogWarning($"NetworkWriter {value} has no NetworkIdentity");
257
258 // serialize the correct amount of data in any case to make sure
259 // that the other end can read the expected amount of data too.
260 writer.WriteNetworkIdentity(identity);
261 }

◆ WriteGuid()

static void Mirror.NetworkWriterExtensions.WriteGuid ( this NetworkWriter  writer,
Guid  value 
)
static

Definition at line 182 of file NetworkWriterExtensions.cs.

183 {
184 byte[] data = value.ToByteArray();
185 writer.WriteBytes(data, 0, data.Length);
186 }

◆ WriteGuidNullable()

static void Mirror.NetworkWriterExtensions.WriteGuidNullable ( this NetworkWriter  writer,
Guid?  value 
)
static

Definition at line 187 of file NetworkWriterExtensions.cs.

188 {
189 writer.WriteBool(value.HasValue);
190 if (value.HasValue)
191 writer.WriteGuid(value.Value);
192 }

◆ WriteList< T >()

static void Mirror.NetworkWriterExtensions.WriteList< T > ( this NetworkWriter  writer,
List< T >  list 
)
static

Definition at line 263 of file NetworkWriterExtensions.cs.

264 {
265 if (list is null)
266 {
267 writer.WriteInt(-1);
268 return;
269 }
270 writer.WriteInt(list.Count);
271 for (int i = 0; i < list.Count; i++)
272 writer.Write(list[i]);
273 }

◆ WriteNetworkBehaviour()

static void Mirror.NetworkWriterExtensions.WriteNetworkBehaviour ( this NetworkWriter  writer,
NetworkBehaviour  value 
)
static

Definition at line 215 of file NetworkWriterExtensions.cs.

216 {
217 if (value == null)
218 {
219 writer.WriteUInt(0);
220 return;
221 }
222 writer.WriteUInt(value.netId);
223 writer.WriteByte((byte)value.ComponentIndex);
224 }

◆ WriteNetworkIdentity()

static void Mirror.NetworkWriterExtensions.WriteNetworkIdentity ( this NetworkWriter  writer,
NetworkIdentity  value 
)
static

Definition at line 194 of file NetworkWriterExtensions.cs.

195 {
196 if (value == null)
197 {
198 writer.WriteUInt(0);
199 return;
200 }
201
202 // users might try to use unspawned / prefab GameObjects in
203 // rpcs/cmds/syncvars/messages. they would be null on the other
204 // end, and it might not be obvious why. let's make it obvious.
205 // https://github.com/vis2k/Mirror/issues/2060
206 //
207 // => warning (instead of exception) because we also use a warning
208 // if a GameObject doesn't have a NetworkIdentity component etc.
209 if (value.netId == 0)
210 Debug.LogWarning($"Attempted to serialize unspawned GameObject: {value.name}. Prefabs and unspawned GameObjects would always be null on the other side. Please spawn it before using it in [SyncVar]s/Rpcs/Cmds/NetworkMessages etc.");
211
212 writer.WriteUInt(value.netId);
213 }

◆ WriteSprite()

static void Mirror.NetworkWriterExtensions.WriteSprite ( this NetworkWriter  writer,
Sprite  sprite 
)
static

Definition at line 313 of file NetworkWriterExtensions.cs.

314 {
315 // support 'null' textures for [SyncVar]s etc.
316 // https://github.com/vis2k/Mirror/issues/3144
317 // simply send a 'null' for texture content.
318 if (sprite == null)
319 {
320 writer.WriteTexture2D(null);
321 return;
322 }
323
324 writer.WriteTexture2D(sprite.texture);
325 writer.WriteRect(sprite.rect);
326 writer.WriteVector2(sprite.pivot);
327 }

◆ WriteString()

static void Mirror.NetworkWriterExtensions.WriteString ( this NetworkWriter  writer,
string  value 
)
static

Definition at line 78 of file NetworkWriterExtensions.cs.

79 {
80 // write 0 for null support, increment real size by 1
81 // (note: original HLAPI would write "" for null strings, but if a
82 // string is null on the server then it should also be null
83 // on the client)
84 if (value == null)
85 {
86 writer.WriteUShort(0);
87 return;
88 }
89
90 // write string with same method as NetworkReader
91 // convert to byte[]
92 int size = encoding.GetBytes(value, 0, value.Length, stringBuffer, 0);
93
94 // check if within max size
95 if (size >= NetworkWriter.MaxStringLength)
96 {
97 throw new IndexOutOfRangeException($"NetworkWriter.Write(string) too long: {size}. Limit: {NetworkWriter.MaxStringLength}");
98 }
99
100 // write size and bytes
101 writer.WriteUShort(checked((ushort)(size + 1)));
102 writer.WriteBytes(stringBuffer, 0, size);
103 }

◆ WriteTexture2D()

static void Mirror.NetworkWriterExtensions.WriteTexture2D ( this NetworkWriter  writer,
Texture2D  texture2D 
)
static

Definition at line 292 of file NetworkWriterExtensions.cs.

293 {
294 // TODO allocation protection when sending textures to server.
295 // currently can allocate 32k x 32k x 4 byte = 3.8 GB
296
297 // support 'null' textures for [SyncVar]s etc.
298 // https://github.com/vis2k/Mirror/issues/3144
299 // simply send -1 for width.
300 if (texture2D == null)
301 {
302 writer.WriteShort(-1);
303 return;
304 }
305
306 // write dimensions first so reader can create the texture with size
307 // 32k x 32k short is more than enough
308 writer.WriteShort((short)texture2D.width);
309 writer.WriteShort((short)texture2D.height);
310 writer.WriteArray(texture2D.GetPixels32());
311 }

◆ WriteTransform()

static void Mirror.NetworkWriterExtensions.WriteTransform ( this NetworkWriter  writer,
Transform  value 
)
static

Definition at line 226 of file NetworkWriterExtensions.cs.

227 {
228 if (value == null)
229 {
230 writer.WriteUInt(0);
231 return;
232 }
233 NetworkIdentity identity = value.GetComponent<NetworkIdentity>();
234 if (identity != null)
235 {
236 writer.WriteUInt(identity.netId);
237 }
238 else
239 {
240 Debug.LogWarning($"NetworkWriter {value} has no NetworkIdentity");
241 writer.WriteUInt(0);
242 }
243 }

◆ WriteUri()

static void Mirror.NetworkWriterExtensions.WriteUri ( this NetworkWriter  writer,
Uri  uri 
)
static

Definition at line 287 of file NetworkWriterExtensions.cs.

288 {
289 writer.WriteString(uri?.ToString());
290 }