public void ShowOneMatchRecord(int recordIndex) { var dirIndex = recordIndex; recordIndex--; //从1开始,变成从0开始 m_allRecords[recordIndex].Init(); m_allRecords[recordIndex].dirStr = PlayerPrefs.GetString(string.Format("RecordFile{0}", dirIndex)); //存档文件夹 DirectoryInfo dirInfo = new DirectoryInfo(m_allRecords[recordIndex].dirStr); var fileInfo = dirInfo.GetFiles(); int replyCount = fileInfo.Length; #if UNITY_IOS || UNITY_ANDROID replyCount -= 5; #else replyCount -= 1; #endif FileStream fInfo = new FileStream(string.Format("{0}/recordInfo.save", m_allRecords[recordIndex].dirStr), FileMode.Open); var bwReader = new BinaryReader(fInfo); m_allRecords[recordIndex].Init(); m_allRecords[recordIndex].roomID = bwReader.ReadInt32(); m_allRecords[recordIndex].createUserID = bwReader.ReadUInt32(); m_allRecords[recordIndex].totalCount = bwReader.ReadInt32(); m_allRecords[recordIndex].localUserID = bwReader.ReadInt32(); Debug.Log("roomID " + m_allRecords[recordIndex].roomID + "createuserID " + m_allRecords[recordIndex].createUserID + " count " + m_allRecords[recordIndex].totalCount + " local user is " + m_allRecords[recordIndex].localUserID); var typeValue = typeof(UserInfoStruct); var typeSize = Marshal.SizeOf(typeValue); for (int i = 0; i < 4; i++) { var buf = bwReader.ReadBytes(typeSize); m_allRecords[recordIndex].userInfo[i] = (UserInfoStruct)StructConverterByteArray.BytesToStruct(buf, typeValue); } m_allRecords[recordIndex].ScorePerMatch = new long[replyCount, 4]; m_allRecords[recordIndex].matchStartTime = new string[replyCount]; for (int i = 0; i < replyCount; i++) { m_allRecords[recordIndex].matchStartTime[i] = bwReader.ReadString(); for (int j = 0; j < 4; j++) { m_allRecords[recordIndex].ScorePerMatch[i, j] = bwReader.ReadInt64(); m_allRecords[recordIndex].ScoreTotal[j] += m_allRecords[recordIndex].ScorePerMatch[i, j]; } } bwReader.Close(); fInfo.Close(); }
public void StartRecord(HNGameManager hnGameManager) { Debug.LogError("StartRecord"); return; #if UNITY_STANDALONE if (HNGameManager.m_iLocalChairID != 0) { return; } #endif var kernel = (GameScene)CServerItem.get().GetClientKernelSink(); CurrentRecordCount = PlayerPrefs.GetInt("RecordNum", 0); var dirStr = ""; var trueRecord = CurrentRecordCount; if (kernel.getPlayCount() == 1)//新的一场游戏 { CurrentRecordCount++; PlayerPrefs.SetInt("RecordNum", CurrentRecordCount); DirectoryInfo dInfo = new DirectoryInfo(Application.persistentDataPath + string.Format("/{0}", DateTime.Now.ToString("yyyyMMddhhmmss"))); dInfo.Create(); Debug.Log("creating saved Dir :" + dInfo.FullName); if (CurrentRecordCount > MaxRecordCount) { trueRecord = (CurrentRecordCount % (MaxRecordCount + 1)) + 1; //删除之前的文件夹 dirStr = PlayerPrefs.GetString(string.Format("RecordFile{0}", trueRecord)); Debug.Log("deleting saved dir :" + dirStr); var oldDirInfo = new DirectoryInfo(dirStr); oldDirInfo.Delete(true); } else { trueRecord = CurrentRecordCount; } Debug.Log("dir full name " + dInfo.FullName); PlayerPrefs.SetString(string.Format("RecordFile{0}", trueRecord), dInfo.FullName); dirStr = dInfo.FullName; FileStream fInfo = new FileStream(string.Format("{0}/recordInfo.save", dInfo.FullName), FileMode.OpenOrCreate); bwWriter = new BinaryWriter(fInfo); bwWriter.Write(hnGameManager.TempRoomId); bwWriter.Write(hnGameManager.CreateUserID); bwWriter.Write(hnGameManager.totalcount); bwWriter.Write(HNGameManager.m_iLocalChairID); userInfoStorages = new UserInfoStorage[4]; hnGameManager.SaveUserInfo(ref userInfoStorages); savedUserInfoData = new UserInfoStruct[4]; for (int i = 0; i < userInfoStorages.Length; i++) { savedUserInfoData[i] = new UserInfoStruct { BGender = userInfoStorages[i].BGender, IUserId = userInfoStorages[i].IUserId, NickName = new byte[SocketDefines.LEN_NICKNAME] }; var nickBuf = Encoding.UTF8.GetBytes(userInfoStorages[i].NickName); Buffer.BlockCopy(nickBuf, 0, savedUserInfoData[i].NickName, 0, SocketDefines.LEN_NICKNAME); var infoBuf = StructConverterByteArray.StructToBytes(savedUserInfoData[i]); bwWriter.Write(infoBuf, 0, infoBuf.Length); } bwWriter.Flush(); bwWriter.Close(); fInfo.Close(); } else { if (CurrentRecordCount > MaxRecordCount) { trueRecord = (CurrentRecordCount % (MaxRecordCount + 1)) + 1; } Debug.Log("True record is " + trueRecord); dirStr = PlayerPrefs.GetString(string.Format("RecordFile{0}", trueRecord)); } fs = new FileStream(string.Format("{0}/recordInfo.save", dirStr), FileMode.Append); bwWriter = new BinaryWriter(fs); bwWriter.Write(DateTime.Now.ToString("yyyyMMddhhmmss")); bwWriter.Flush(); bwWriter.Close(); fs.Close(); Debug.Log("dirStr path: " + dirStr); //DirectoryInfo dInfo = new DirectoryInfo(Application.persistentDataPath); //var files = dInfo.GetFiles() fs = new FileStream(string.Format("{0}/{1}.save", dirStr, hnGameManager.nowcount), FileMode.OpenOrCreate); Debug.Log("writing saved file :" + fs.Name); bwWriter = new BinaryWriter(fs); bRecording = true; msgQueue.Clear(); }
//gameend 消息,存储当局分数等信息到recordInfo.save文件内 //解散时data为空,当前局所有玩家得分为0 public void StopRecord(byte[] data = null, int datasize = 0) { Debug.LogError("StopRecord"); return; if (!bRecording) { return; } #if UNITY_STANDALONE if (HNGameManager.m_iLocalChairID != 0) { return; } #endif for (int i = 0; i < msgQueue.Count; i++) { msgQueue[i].WriteData(bwWriter); } bwWriter.Flush(); bwWriter.Close(); fs.Close(); var trueRecord = CurrentRecordCount; if (CurrentRecordCount > MaxRecordCount) { trueRecord = (CurrentRecordCount % (MaxRecordCount + 1)) + 1; } var dirStr = PlayerPrefs.GetString(string.Format("RecordFile{0}", trueRecord));//存档文件夹 FileStream fInfo = new FileStream(string.Format("{0}/recordInfo.save", dirStr), FileMode.Append); bwWriter = new BinaryWriter(fInfo); var kernel = (GameScene)CServerItem.get().GetClientKernelSink(); for (int i = 0; i < savedUserInfoData.Length; i++) { //#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR // string filename = string.Format("{0}/{1}.png", dirStr, savedUserInfoData[i].IUserId); // if (File.Exists(filename) == false) // { // if (HNGameManager.userId2HeadImageDirectory.ContainsKey(savedUserInfoData[i].IUserId)) // { // var sprite = HNGameManager.userId2HeadImageDirectory[savedUserInfoData[i].IUserId]; // Loom.QueueOnMainThread(() => // { // var buf = sprite.texture.EncodeToPNG(); // File.WriteAllBytes(filename, buf); // }); // } // } //#endif } if (data == null) { Debug.Log("----------write 0 to file1111"); for (int i = 0; i < 4; i++) { bwWriter.Write((long)(0)); } } else { var typeValue = typeof(CMD_S_GameEnd); CMD_S_GameEnd pGameEnd = (CMD_S_GameEnd)StructConverterByteArray.BytesToStruct(data, typeValue); //bwWriter.Write(pGameEnd.lGameScore[HNGameManager.m_iLocalChairID]); //Debug.Log("-------------Write score to file: " + pGameEnd.lGameScore[HNGameManager.m_iLocalChairID]); //var curPlayerChairID = HNGameManager.getNextPlayerChairID(HNGameManager.m_iLocalChairID); //int iIndex = 1; //while (curPlayerChairID != HNGameManager.m_iLocalChairID) //{ // bwWriter.Write(pGameEnd.lGameScore[curPlayerChairID]); // Debug.Log("-------------Write score to file: " + pGameEnd.lGameScore[curPlayerChairID]); // curPlayerChairID = HNGameManager.getNextPlayerChairID(curPlayerChairID); //} } bwWriter.Flush(); bwWriter.Close(); fInfo.Close(); bRecording = false; msgQueue.Clear(); userInfoStorages = null; }