/// <summary> /// 获取录制字节数据 /// </summary> /// <returns></returns> public byte[] GetClipData() { if (m_MicroPhoneSource.clip == null) { Debug.Log("GetClipData audio.clip is null"); return(null); } int sample = m_MicroPhoneSource.clip.frequency * Mathf.CeilToInt(m_CurrentRecordDuration); float[] samples = new float[sample]; m_MicroPhoneSource.clip.GetData(samples, 0); byte[] ret = null; using (MemoryStreamExt ms = new MemoryStreamExt()) { for (int i = 0; i < samples.Length; ++i) { ms.WriteFloat(samples[i]); } ret = ms.ToArray(); } ret = GZipCompress.Compress(ret); return(ret); }
public void OnUpdate() { while (true) { if (m_ReceiveCount < 5) { ++m_ReceiveCount; lock (m_ReceiveQueue) { if (m_ReceiveQueue.Count > 0) { byte[] buffer = m_ReceiveQueue.Dequeue(); byte[] protocodeBuffer = new byte[4]; byte[] protoContent = new byte[buffer.Length - 5]; bool isCompress = false; using (MemoryStreamExt ms = new MemoryStreamExt(buffer)) { isCompress = ms.ReadBool(); ms.Read(protocodeBuffer, 0, protocodeBuffer.Length); Array.Reverse(protocodeBuffer); int protoCode = BitConverter.ToInt32(protocodeBuffer, 0); //LogSystem.Log("=============================================================收到消息" + protoCode); ms.Read(protoContent, 0, protoContent.Length); if (isCompress) { //protoContent = GZipCompress.DeCompress(protoContent); } // if (protoCode == OP_PLAYER_CLOSE.CODE) { // LogSystem.LogSpecial("服务器返回关闭消息,网络连接断开"); Close(true); } //else if (protoCode == OP_SYS_HEART.CODE) { // lastHeart = OP_SYS_HEART.decode(protoContent); } // else { //return; // NetDispatcher.Instance.Dispatch(protoCode, protoContent); } } } else { break; } } } else { m_ReceiveCount = 0; break; } } m_SocketStateDic[m_CurrentState].OnUpdate(); }
private byte[] xorScale = new byte[] { 45, 66, 38, 55, 23, 254, 9, 165, 90, 19, 41, 45, 201, 58, 55, 37, 254, 185, 165, 169, 19, 171 };//.data文件的xor加解密因子 #endregion #region GameDataTableParser 构造函数 /// <summary> /// 构造函数 /// </summary> /// <param name="path"></param> public GameDataTableParser(string path) { m_FieldNameDic = new Dictionary <string, int>(); //------------------ //读取文件 //------------------ //NewFunction byte[] buffer = LocalFileManager.Instance.GetBuffer(path); //------------------ //解压缩 //------------------ buffer = ZlibHelper.DeCompressBytes(buffer); //------------------ //xor解密 //------------------ int iScaleLen = xorScale.Length; for (int i = 0; i < buffer.Length; i++) { buffer[i] = (byte)(buffer[i] ^ xorScale[i % iScaleLen]); } //------------------ //解析数据到数组 //------------------ using (MemoryStreamExt ms = new MemoryStreamExt(buffer)) { m_Row = ms.ReadInt(); m_Column = ms.ReadInt(); m_GameData = new string[m_Row, m_Column]; m_FieldName = new string[m_Column]; for (int i = 0; i < m_Row; i++) { for (int j = 0; j < m_Column; j++) { string str = ms.ReadUTF8String(); if (i == 2) { //表示读取的是字段 m_FieldName[j] = str; m_FieldNameDic[str] = j; } else if (i > 2) { //表示读取的是内容 m_GameData[i, j] = str; } } } } }
/// <summary> /// 播放外部音频 /// </summary> /// <param name="bytes"></param> public void PlayExternalAudio(byte[] bytes) { bytes = GZipCompress.DeCompress(bytes); float[] fl = new float[bytes.Length / 4]; using (MemoryStreamExt ms = new MemoryStreamExt(bytes)) { for (int i = 0; i < fl.Length; ++i) { fl[i] = ms.ReadFloat(); } } AudioClip clip = AudioClip.Create("MicroAudio", fl.Length, 1, FREQUENCY, false); clip.SetData(fl, 0); m_MicroQueue.Enqueue(clip); }
/// <summary> /// 封装数据包 /// </summary> /// <param name="data">要发送的数据</param> /// <returns>封装好的数据</returns> private byte[] MakeDatas(byte[] data, int protoCode) { int leng = 0; bool isCompress; if (data == null) { leng = 5; isCompress = false; } else { //data = EncryptUtil.NetEncrypt(data, SystemProxy.Instance.NetKey, SystemProxy.Instance.NetCorrected); isCompress = data.Length >= COMPRESS_LENGTH; if (isCompress) { //data = GZipCompress.Compress(data); } leng = data.Length + 5; } byte[] head = new byte[4]; head[0] = (byte)((leng & 0xFF000000) >> 24); head[1] = (byte)((leng & 0x00FF0000) >> 16); head[2] = (byte)((leng & 0x0000FF00) >> 8); head[3] = (byte)((leng & 0x000000FF)); byte[] protoCodeBuffer = BitConverter.GetBytes(protoCode); Array.Reverse(protoCodeBuffer); using (MemoryStreamExt ms = new MemoryStreamExt()) { ms.Write(head, 0, 4); ms.WriteBool(isCompress); ms.Write(protoCodeBuffer, 0, protoCodeBuffer.Length); if (data != null) { ms.Write(data, 0, data.Length); } return(ms.ToArray()); } return(null); }