void WriteCSVFile(string filename, List <CSVMsgData> csvMsgData, List <MsgData> msgData) { Vector3 lastPos; int lastToTalkID = -1; Dictionary <int, Vector3> lastPosition = new Dictionary <int, Vector3>(); string delim = "\t"; using (System.IO.StreamWriter file = new System.IO.StreamWriter(filename)) { file.WriteLine("Date" + delim + "Name" + delim + "Moved" + delim + "MouseMoved" + delim + "Talked" + delim + "RelVolume" + delim + "Frequency" + delim + "MinFrequency" + delim + "MaxFrequency" + delim + "TalkedLength" + delim + "NotLastToTalk"); for (int i = 0; i < csvMsgData.Count; ++i) { CSVMsgData csvMsg = csvMsgData[i]; MsgData msg = msgData[csvMsg.msgIdx]; string line = msg.time.ToString(); float distMoved = 0.0f; if (lastPosition.TryGetValue(msg.id, out lastPos)) { distMoved = Vector3.Distance(lastPos, csvMsg.position); } line += delim + msg.name; line += delim + distMoved; line += delim + (csvMsg.mouseMoved ? "1" : ""); line += delim + (csvMsg.talked ? "0.04" : ""); // time of talk sample for october competition was 640/16000 -- Speex_16K line += delim + (csvMsg.talked ? csvMsg.volume.ToString() : ""); line += delim + (csvMsg.talked ? csvMsg.frequency.ToString() : ""); line += delim + (csvMsg.talked ? csvMsg.minFrequency.ToString() : ""); line += delim + (csvMsg.talked ? csvMsg.maxFrequency.ToString() : ""); line += delim + (csvMsg.talkTimeSum > 0.0f ? csvMsg.talkTimeSum.ToString() : ""); line += delim + (csvMsg.talked && lastToTalkID != msg.id ? "1" : ""); file.WriteLine(line); lastPosition[msg.id] = csvMsg.position; if (csvMsg.talked) { lastToTalkID = msg.id; } } } Debug.LogError("Wrote " + filename); }
public List <MsgData> ReadStream(Stream stream, string filename, int msgOffset = 0, int numMsgsToLoad = -1, bool saveCSV = false) { bool savePosData = saveCSV; int msgCount = 0; List <MsgData> msgData = new List <MsgData>(); List <CSVMsgData> csvMsgData = new List <CSVMsgData>(); Dictionary <int, CSVMsgData> lastVoiceMsg = new Dictionary <int, CSVMsgData>(); try{ using (BinaryReader reader = new BinaryReader(stream)) { startFileTime = DateTime.FromBinary(reader.ReadInt64()); // start time fileVersion = reader.ReadInt32(); // file version if (fileVersion >= 5) { level = (int)reader.ReadByte(); } Debug.LogError("FileVersion: " + fileVersion + " Replay Level: " + ((GameManager.Level)level).ToString()); while (reader.BaseStream.Position != reader.BaseStream.Length && (numMsgsToLoad < 0 || msgData.Count < numMsgsToLoad)) { MsgData data = new MsgData(); data.time = DateTime.FromBinary(reader.ReadInt64()); data.msgType = reader.ReadString(); if (data.msgType == SFSEvent.USER_VARIABLES_UPDATE) { ReadUserVariableUpdate(reader, ref data); } else if (data.msgType == SFSEvent.OBJECT_MESSAGE) { ReadObjectMessage(reader, ref data); } else if (data.msgType == SFSEvent.ROOM_VARIABLES_UPDATE) { ReadRoomVariablesUpdate(reader, ref data); } else if (data.msgType == SFSEvent.PUBLIC_MESSAGE || data.msgType == SFSEvent.PRIVATE_MESSAGE || data.msgType == SFSEvent.ADMIN_MESSAGE) { ReadPublicPrivateAdminMessage(reader, ref data); } else if (data.msgType == SFSEvent.USER_ENTER_ROOM || data.msgType == SFSEvent.USER_EXIT_ROOM) { ReadUserEnterExit(reader, ref data); } else { Debug.LogError(reader.BaseStream.Position + " Unknown msg type: " + data.msgType + " assuming data corrupted, searching for next valid message"); reader.BaseStream.Seek(-data.msgType.Length - 8, SeekOrigin.Current); if (msgData.Count > 0) { FindNextValidMessage(reader, msgData[msgData.Count - 1].time); } continue; } msgCount++; if (msgCount >= msgOffset) { msgData.Add(data); if (saveCSV) { float voiceDelayToConsiderTalkingStopped = 1.0f; // seconds -- used for CSV file CSVMsgData newMsg = new CSVMsgData(msgData.Count - 1, data); csvMsgData.Add(newMsg); if (newMsg.talked) { newMsg.talkTimeSum += 0.04f; // 640/16000 -- Speex_16K CSVMsgData lastMsg = null; if (lastVoiceMsg.TryGetValue(data.id, out lastMsg)) { if (data.time.Subtract(msgData[lastMsg.msgIdx].time).TotalSeconds >= voiceDelayToConsiderTalkingStopped) { lastVoiceMsg[data.id] = newMsg; } else { newMsg.talkTimeSum += lastMsg.talkTimeSum; lastMsg.talkTimeSum = 0; lastVoiceMsg[data.id] = newMsg; } } else { lastVoiceMsg.Add(data.id, newMsg); } } } } } } }catch (Exception e) { Debug.LogError("Caught Exception reading replay file: " + e.ToString()); } if (saveCSV) { WriteCSVFile(filename + ".txt", csvMsgData, msgData); } if (savePosData) { WritePosFile(filename + ".pos.txt", msgData); } return(msgData); }