protected virtual void SerializeKey(NetworkWriter writer, TKey item) { }
// zigzag encoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba public static void WritePackedInt64(this NetworkWriter writer, long i) { ulong zigzagged = (ulong)((i >> 63) ^ (i << 1)); writer.WritePackedUInt64(zigzagged); }
public static void WriteVector3(this NetworkWriter writer, Vector3 value) { writer.WriteSingle(value.x); writer.WriteSingle(value.y); writer.WriteSingle(value.z); }
// Weaver needs a write function with just one byte[] parameter // (we don't name it .Write(byte[]) because it's really a WriteBytesAndSize since we write size / null info too) public static void WriteBytesAndSize(this NetworkWriter writer, byte[] buffer) { // buffer might be null, so we can't use .Length in that case writer.WriteBytesAndSize(buffer, 0, buffer != null ? buffer.Length : 0); }
// zigzag encoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba public static void WritePackedInt32(this NetworkWriter writer, int i) { uint zigzagged = (uint)((i >> 31) ^ (i << 1)); writer.WritePackedUInt32(zigzagged); }
public static void WriteChar(this NetworkWriter writer, char value) => writer.WriteUInt16((ushort)value);
public static void WriteUInt16(this NetworkWriter writer, ushort value) { writer.WriteByte((byte)(value & 0xFF)); writer.WriteByte((byte)(value >> 8)); }
protected void SendTargetRPCInternal(NetworkConnection conn, Type invokeClass, string rpcName, NetworkWriter writer, int channelId) { if (!NetworkServer.active) { Debug.LogError($"TargetRPC {rpcName} called when server not active"); return; } if (!isServer) { Debug.LogWarning($"TargetRpc {rpcName} called on {name} but that object has not been spawned or has been unspawned"); return; } // connection parameter is optional. assign if null. if (conn is null) { conn = connectionToClient; } // if still null if (conn is null) { Debug.LogError($"TargetRPC {rpcName} was given a null connection, make sure the object has an owner or you pass in the target connection"); return; } if (!(conn is NetworkConnectionToClient)) { Debug.LogError($"TargetRPC {rpcName} requires a NetworkConnectionToClient but was given {conn.GetType().Name}"); return; } // construct the message RpcMessage message = new RpcMessage { netId = netId, componentIndex = (byte)ComponentIndex, // type+func so Inventory.RpcUse != Equipment.RpcUse functionHash = RemoteCallHelper.GetMethodHash(invokeClass, rpcName), // segment to avoid reader allocations payload = writer.ToArraySegment() }; conn.Send(message, channelId); }
public override void Serialize(NetworkWriter writer) { writer.WritePackedInt32(value); }
protected virtual void SerializeItem(NetworkWriter writer, T item) { }
protected override void SerializeItem(NetworkWriter writer, string item) => writer.WriteString(item);
protected override void SerializeItem(NetworkWriter writer, bool item) => writer.WriteBoolean(item);
protected override void SerializeItem(NetworkWriter writer, uint item) => writer.WritePackedUInt32(item);
protected override void SerializeItem(NetworkWriter writer, float item) => writer.WriteSingle(item);
public static void WriteByte(this NetworkWriter writer, byte value) => writer.WriteByte(value);
public override void Serialize(NetworkWriter writer) { }
public static void WriteSByte(this NetworkWriter writer, sbyte value) => writer.WriteByte((byte)value);
// Serialize the contents of this message into the writer public virtual void Serialize(NetworkWriter writer) { }
public static void WriteBoolean(this NetworkWriter writer, bool value) => writer.WriteByte((byte)(value ? 1 : 0));
public override void Serialize(NetworkWriter writer) { writer.WritePackedUInt32(netId); writer.Write(authority); }
public static void WriteInt16(this NetworkWriter writer, short value) => writer.WriteUInt16((ushort)value);
public override void Serialize(NetworkWriter writer) { writer.WritePackedUInt32(netId); writer.WriteBytesAndSize(payload); }
public static void WriteBytesAndSizeSegment(this NetworkWriter writer, ArraySegment <byte> buffer) { writer.WriteBytesAndSize(buffer.Array, buffer.Offset, buffer.Count); }
public override void Serialize(NetworkWriter writer) { writer.Write(clientTime); writer.Write(serverTime); }
// http://sqlite.org/src4/doc/trunk/www/varint.wiki public static void WritePackedUInt32(this NetworkWriter writer, uint value) { // for 32 bit values WritePackedUInt64 writes the // same exact thing bit by bit writer.WritePackedUInt64(value); }
public override void Serialize(NetworkWriter writer) { writer.WriteBytesAndSize(value); }
public static void WritePackedUInt64(this NetworkWriter writer, ulong value) { if (value <= 240) { writer.WriteByte((byte)value); return; } if (value <= 2287) { writer.WriteByte((byte)(((value - 240) >> 8) + 241)); writer.WriteByte((byte)((value - 240) & 0xFF)); return; } if (value <= 67823) { writer.WriteByte((byte)249); writer.WriteByte((byte)((value - 2288) >> 8)); writer.WriteByte((byte)((value - 2288) & 0xFF)); return; } if (value <= 16777215) { writer.WriteByte((byte)250); writer.WriteByte((byte)(value & 0xFF)); writer.WriteByte((byte)((value >> 8) & 0xFF)); writer.WriteByte((byte)((value >> 16) & 0xFF)); return; } if (value <= 4294967295) { writer.WriteByte((byte)251); writer.WriteByte((byte)(value & 0xFF)); writer.WriteByte((byte)((value >> 8) & 0xFF)); writer.WriteByte((byte)((value >> 16) & 0xFF)); writer.WriteByte((byte)((value >> 24) & 0xFF)); return; } if (value <= 1099511627775) { writer.WriteByte((byte)252); writer.WriteByte((byte)(value & 0xFF)); writer.WriteByte((byte)((value >> 8) & 0xFF)); writer.WriteByte((byte)((value >> 16) & 0xFF)); writer.WriteByte((byte)((value >> 24) & 0xFF)); writer.WriteByte((byte)((value >> 32) & 0xFF)); return; } if (value <= 281474976710655) { writer.WriteByte((byte)253); writer.WriteByte((byte)(value & 0xFF)); writer.WriteByte((byte)((value >> 8) & 0xFF)); writer.WriteByte((byte)((value >> 16) & 0xFF)); writer.WriteByte((byte)((value >> 24) & 0xFF)); writer.WriteByte((byte)((value >> 32) & 0xFF)); writer.WriteByte((byte)((value >> 40) & 0xFF)); return; } if (value <= 72057594037927935) { writer.WriteByte((byte)254); writer.WriteByte((byte)(value & 0xFF)); writer.WriteByte((byte)((value >> 8) & 0xFF)); writer.WriteByte((byte)((value >> 16) & 0xFF)); writer.WriteByte((byte)((value >> 24) & 0xFF)); writer.WriteByte((byte)((value >> 32) & 0xFF)); writer.WriteByte((byte)((value >> 40) & 0xFF)); writer.WriteByte((byte)((value >> 48) & 0xFF)); return; } // all others { writer.WriteByte((byte)255); writer.WriteByte((byte)(value & 0xFF)); writer.WriteByte((byte)((value >> 8) & 0xFF)); writer.WriteByte((byte)((value >> 16) & 0xFF)); writer.WriteByte((byte)((value >> 24) & 0xFF)); writer.WriteByte((byte)((value >> 32) & 0xFF)); writer.WriteByte((byte)((value >> 40) & 0xFF)); writer.WriteByte((byte)((value >> 48) & 0xFF)); writer.WriteByte((byte)((value >> 56) & 0xFF)); } }
protected void SendTargetRPCInternal(NetworkConnection conn, Type invokeClass, string rpcName, NetworkWriter writer, int channelId) { // this was in Weaver before if (!NetworkServer.active) { logger.LogError("TargetRPC Function " + rpcName + " called on client."); return; } // connection parameter is optional. assign if null. if (conn == null) { conn = connectionToClient; } // this was in Weaver before if (conn is NetworkConnectionToServer) { logger.LogError("TargetRPC Function " + rpcName + " called on connection to server"); return; } // This cannot use NetworkServer.active, as that is not specific to this object. if (!isServer) { logger.LogWarning("TargetRpc " + rpcName + " called on un-spawned object: " + name); return; } // construct the message RpcMessage message = new RpcMessage { netId = netId, componentIndex = ComponentIndex, // type+func so Inventory.RpcUse != Equipment.RpcUse functionHash = RemoteCallHelper.GetMethodHash(invokeClass, rpcName), // segment to avoid reader allocations payload = writer.ToArraySegment() }; conn.Send(message, channelId); }
public static void WriteVector2Int(this NetworkWriter writer, Vector2Int value) { writer.WritePackedInt32(value.x); writer.WritePackedInt32(value.y); }
public override bool OnSerialize(NetworkWriter writer, bool initialState) { // use local position/rotation/scale for VR support SerializeIntoWriter(writer, targetComponent.transform.localPosition, targetComponent.transform.localRotation, compressRotation, targetComponent.transform.localScale); return(true); }