void Instantiate(NetStream stream) { PlayerName = stream.ReadString(); Hp = stream.ReadInt(); Vector3 pos = stream.ReadVector3(); inventory.SetAllFromStream(stream); // Prevemt jumpiness during handoff by ignoring position data if similar enough: if (transform.position != Vector3.zero && Vector3.Distance(transform.position, pos) < 5) return; transform.position = pos; }
public static object ItemDeserializer(NetStream stream) { uint dbId = stream.ReadUInt(); int quantity = stream.ReadInt(); IInvItem item; if (!Inventory.TryCloneFromDb(dbId, quantity, stream, out item)) { Debug.LogError("Failed to deserialize IInvItem. Item with given ID not found in database: " + dbId); } return item; }
private static object ReadParam(NetStream stream, Type type) { if (type == typeof(bool)) { return(stream.ReadBool()); } if (type == typeof(byte)) { return(stream.ReadByte()); } if (type == typeof(short)) { return(stream.ReadShort()); } if (type == typeof(ushort)) { return(stream.ReadUShort()); } if (type == typeof(int)) { return(stream.ReadInt()); } if (type == typeof(uint)) { return(stream.ReadUInt()); } if (type == typeof(float)) { return(stream.ReadFloat()); } if (type == typeof(long)) { return(stream.ReadLong()); } if (type == typeof(ulong)) { return(stream.ReadULong()); } if (type == typeof(double)) { return(stream.ReadDouble()); } if (type == typeof(string)) { return(stream.ReadString()); } if (type == typeof(Vector2)) { return(stream.ReadVector2()); } if (type == typeof(Vector3)) { return(stream.ReadVector3()); } if (type == typeof(Quaternion)) { return(stream.ReadQuaternion()); } if (type == typeof(bool[])) { ushort length = stream.ReadUShort(); bool[] arr = new bool[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadBool(); } return(arr); } if (type == typeof(byte[])) { ushort length = stream.ReadUShort(); byte[] arr = new byte[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadByte(); } return(arr); } if (type == typeof(ushort[])) { ushort length = stream.ReadUShort(); ushort[] arr = new ushort[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadUShort(); } return(arr); } if (type == typeof(int[])) { ushort length = stream.ReadUShort(); int[] arr = new int[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadInt(); } return(arr); } if (type == typeof(uint[])) { ushort length = stream.ReadUShort(); uint[] arr = new uint[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadUInt(); } return(arr); } if (type == typeof(float[])) { ushort length = stream.ReadUShort(); float[] arr = new float[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadFloat(); } return(arr); } if (type == typeof(string[])) { ushort length = stream.ReadUShort(); string[] arr = new string[length]; for (int i = 0; i < arr.Length; i++) { arr[i] = stream.ReadString(); } return(arr); } if (type == typeof(char[])) { ushort length = stream.ReadUShort(); char[] arr = new char[length]; for (int i = 0; i < arr.Length; i++) { arr[i] = stream.ReadChar(); } return(arr); } if (type == typeof(NetMessage)) { return(ReadNetMessage(stream)); } if (type == typeof(NetMessage[])) { byte length = stream.ReadByte(); NetMessage[] arr = new NetMessage[length]; for (int i = 0; i < length; i++) { arr[i] = ReadNetMessage(stream); } return(arr); } if (type == typeof(NetConnection)) { return(stream.Connection); } if (type == typeof(IPAddress)) { byte[] array = new byte[4]; stream.ReadByteArray(array); return(new IPAddress(array)); } if (type == typeof(IPEndPoint)) { if (!stream.ReadBool()) { return(null); } return(new IPEndPoint((IPAddress)ReadParam(stream, typeof(IPAddress)), stream.ReadUShort())); } if (type == typeof(NetStream)) { return(stream.ReadNetStream()); } if (type == typeof(NetZone)) { return(new NetZone { Id = stream.ReadUInt(), ServerEndpoint = stream.ReadBool() ? (IPEndPoint)ReadParam(stream, typeof(IPEndPoint)) : null, Position = stream.ReadVector3(), ViewIdMin = stream.ReadInt(), ViewIdMax = stream.ReadInt() }); } if (HasType(type)) { return(Read(stream, type)); } // We don't know how to deserialize this type throw new Exception("Deserializer not implemented for type: " + type); }
void ReadInstantiateData(NetStream stream) { PlayerName = stream.ReadString(); currentHp = stream.ReadInt(); transform.position = stream.ReadVector3(); inventory.SetAllFromStream(stream); if (!Players.ContainsKey(PlayerName)) Players.Add(PlayerName, this); }