///// <summary> ///// 读文件 ///// </summary> ///// <param name="filePath"> 文件完整路径 </param> ///// <param name="read"> 自定义的读取方法, 使用 BinaryReader 的 Read 方法来完成 </param> //public static void ReadFile(string filePath, Action<BinaryReader> read, Encoding encoding = null) //{ // using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) // { // using (BinaryReader reader = encoding == null ? new BinaryReader(fs) : new BinaryReader(fs, encoding)) // { // read(reader); // } // } //} ///// <summary> ///// 写文件 ///// </summary> ///// <param name="filePath"> 文件完整路径 </param> ///// <param name="write"> 自定义的写入方法, 使用 BinaryWriter 的 Write 方法来完成 </param> //public static void WriteFile(string filePath, Action<BinaryWriter> write, Encoding encoding = null) //{ // using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) // { // using (BinaryWriter writer = encoding == null ? new BinaryWriter(fs) : new BinaryWriter(fs, encoding)) // { // write(writer); // } // } //} ///// <summary> ///// 将一个可序列化的对象写入文件 ///// </summary> ///// <param name="obj"> 需要写入的对象 </param> ///// <param name="filePath"> 文件完整路径 </param> ///// <param name="encrypt"> 是否需要加密 </param> ///// <returns> 如果操作成功返回 true,否则返回 false </returns> //public static bool WriteObjectToFile(object obj, string filePath, bool encrypt = false) //{ // try // { // WriteFile(filePath, writer => // { // byte[] array; // int count; // Serialize(obj, out array, out count); // if (encrypt) // { // uint key = new Random().seed; // int code = EncryptDecrypt(key, array, 0, count); // writer.Write(key); // writer.Write(array, 0, count); // writer.Write(code); // } // else writer.Write(array, 0, count); // }); // return true; // } // catch { return false; } //} ///// <summary> ///// 从文件中读取一个可序列化的对象 ///// </summary> ///// <param name="filePath"> 文件完整路径 </param> ///// <param name="decrypt"> 是否需要解密 </param> ///// <returns> 如果操作成功返回对象,否则返回 null </returns> //public static object ReadObjectFromFile(string filePath, bool decrypt = false) //{ // try // { // byte[] array = null; // ReadFile(filePath, reader => // { // if (decrypt) // { // uint key = reader.ReadUInt32(); // array = reader.ReadBytes((int)reader.BaseStream.Length - 8); // int code = reader.ReadInt32(); // if (EncryptDecrypt(key, array) != code) array = null; // } // else array = reader.ReadBytes((int)reader.BaseStream.Length); // }); // return Deserialize(array); // } // catch { return null; } //} /// <summary> /// 将 Vector3 值写入字节数组 /// </summary> /// <param name="buffer"> 字节数组 </param> /// <param name="offset"> 写入字节数组的开始下标, 操作完成后增加 12 </param> /// <param name="value"> 被写入的值 </param> public static void WriteToBuffer(byte[] buffer, ref int offset, Vector3 value) { UnionValue union = new UnionValue(); union.floatValue = value.x; union.WriteFloatTo(buffer, ref offset); union.floatValue = value.y; union.WriteFloatTo(buffer, ref offset); union.floatValue = value.z; union.WriteFloatTo(buffer, ref offset); }
/// <summary> /// 从字节数组里读取 Vector3 /// </summary> /// <param name="buffer"> 字节数组 </param> /// <param name="offset"> 从字节数组里开始读取的下标, 操作完成后增加 12 </param> /// <returns> 读取的 Vector3 值 </returns> public static Vector3 ReadVector3FromBuffer(byte[] buffer, ref int offset) { Vector3 value = new Vector3(); UnionValue union = new UnionValue(); union.ReadFloatFrom(buffer, ref offset); value.x = union.floatValue; union.ReadFloatFrom(buffer, ref offset); value.y = union.floatValue; union.ReadFloatFrom(buffer, ref offset); value.z = union.floatValue; return value; }
/// <summary> /// 从字节数组里读取 Quaternion /// </summary> /// <param name="buffer"> 字节数组 </param> /// <param name="offset"> 从字节数组里开始读取的下标, 操作完成后增加 16 </param> /// <returns> 读取的 Quaternion 值 </returns> public static Quaternion ReadQuaternionFromBuffer(byte[] buffer, ref int offset) { Quaternion value = new Quaternion(); UnionValue union = new UnionValue(); union.ReadFloatFrom(buffer, ref offset); value.x = union.floatValue; union.ReadFloatFrom(buffer, ref offset); value.y = union.floatValue; union.ReadFloatFrom(buffer, ref offset); value.z = union.floatValue; union.ReadFloatFrom(buffer, ref offset); value.w = union.floatValue; return value; }
/// <summary> /// 非主控端接收数据 /// </summary> public void S2C_SetData(byte[] data) { #if UNITY_EDITOR //_syncCount ++; //_syncData = data; #endif int offset = 0; UnionValue union = new UnionValue(); // needSync union.ReadByteFrom(data, ref offset); byte needSync = union.byteValue; // position byte mask = 1; if ((needSync & mask) != 0) { _netPosition.SetData(Kit.ReadVector3FromBuffer(data, ref offset)); _isFreezed = (byte)(_isFreezed & (~mask)); } // rotation mask <<= 1; if ((needSync & mask) != 0) { _netRotation.SetData(Kit.ReadQuaternionFromBuffer(data, ref offset)); _isFreezed = (byte)(_isFreezed & (~mask)); } // velocity mask <<= 1; if ((needSync & mask) != 0) { _netVelocity.SetData(Kit.ReadVector3FromBuffer(data, ref offset)); _isFreezed = (byte)(_isFreezed & (~mask)); } // angularVelocity mask <<= 1; if ((needSync & mask) != 0) { _netAngularVelocity.SetData(Kit.ReadVector3FromBuffer(data, ref offset)); _isFreezed = (byte)(_isFreezed & (~mask)); } // aimPoint mask <<= 1; if ((needSync & mask) != 0) { _netAimPoint.SetData(Kit.ReadVector3FromBuffer(data, ref offset)); _isFreezed = (byte)(_isFreezed & (~mask)); } // input mask <<= 1; if ((needSync & mask) != 0) { union.ReadUShortFrom(data, ref offset); _netInput.SetData(union.ushortValue); _isFreezed = (byte)(_isFreezed & (~mask)); } // freezeCmd mask <<= 1; if ((needSync & mask) != 0) { union.ReadByteFrom(data, ref offset); byte freezeCmd = union.byteValue; mask = 1; for (int i = 0; i < 8; i++) { if ((freezeCmd & mask) != 0) { _isFreezed |= mask; } mask <<= 1; } } OnNetworkSync(); }
/// <summary> /// 主控端发送数据 /// </summary> /// <returns> 如果不需要发送,返回 null </returns> public byte[] C2S_GetData() { byte needSync = 0; byte freezeCmd = 0; int byteCount = 1; // position byte mask = 1; SyncAction action = _netPosition.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfVector3; } // rotation mask <<= 1; action = _netRotation.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfQuaternion; } // velocity mask <<= 1; action = _netVelocity.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfVector3; } // angularVelocity mask <<= 1; action = _netAngularVelocity.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfVector3; } // aimPoint mask <<= 1; action = _netAimPoint.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfVector3; } // input mask <<= 1; action = _netInput.GetSyncAction(); if (action != SyncAction.none) { needSync |= mask; if (action == SyncAction.freeze) { freezeCmd |= mask; } byteCount += Kit.sizeOfUshort; } // freezeCmd mask <<= 1; if (freezeCmd != 0) { needSync |= mask; byteCount += 1; } byte[] buffer = null; if (needSync != 0) { buffer = GetShortestBuffer(byteCount); int offset = 0; UnionValue union = new UnionValue(needSync); union.WriteByteTo(buffer, ref offset); // position mask = 1; if ((needSync & mask) != 0) { Kit.WriteToBuffer(buffer, ref offset, _netPosition.GetData()); } // rotation mask <<= 1; if ((needSync & mask) != 0) { Kit.WriteToBuffer(buffer, ref offset, _netRotation.GetData()); } // velocity mask <<= 1; if ((needSync & mask) != 0) { Kit.WriteToBuffer(buffer, ref offset, _netVelocity.GetData()); } // angularVelocity mask <<= 1; if ((needSync & mask) != 0) { Kit.WriteToBuffer(buffer, ref offset, _netAngularVelocity.GetData()); } // aimPoint mask <<= 1; if ((needSync & mask) != 0) { Kit.WriteToBuffer(buffer, ref offset, _netAimPoint.GetData()); } // input mask <<= 1; if ((needSync & mask) != 0) { union.ushortValue = _netInput.GetData(); union.WriteUShortTo(buffer, ref offset); } // freezeCmd mask <<= 1; if ((needSync & mask) != 0) { union.byteValue = freezeCmd; union.WriteByteTo(buffer, ref offset); } } return(buffer); }