using UnityEngine; using System.Collections; using System.Collections.Generic; using Sfs2X.Entities.Data; using Sfs2X.Util; using System.IO; using LitJson; public class ReplayManager { public const string LABEL_MSG = "m"; public const string LABEL_MSG_TIME = "t"; public const string LABEL_DATETIME = "d"; public const string LABEL_VERSION = "v"; public const string LABEL_RECORD_ARRAY = "a"; public static string LOCAL_REPLAY_FOLDER = Application.persistentDataPath + "/Replay/"; private bool m_IsRecording; private bool m_IsReplaying; private float startRecordTime; private float startReplayTime; private int recordIndex; private ISFSArray recordArr; public void StartRecord() { if (IsReplaying ()) return; m_IsRecording = true; startRecordTime = GameTime.realtimeSinceStartup; recordArr = new SFSArray (); } public void StopRecord() { if (IsReplaying ()) return; m_IsRecording = false; } public bool IsRecording() { return m_IsRecording; } public void CheckRecord(ISFSObject msg) { if (IsReplaying ()) return; if(IsRecording()) { Record (msg); } } private void Record(ISFSObject msg) { float currentTime = GameTime.realtimeSinceStartup - startRecordTime; ISFSObject recordObj = new SFSObject (); recordObj.PutSFSObject (LABEL_MSG, msg); recordObj.PutFloat (LABEL_MSG_TIME, currentTime); recordArr.AddSFSObject (recordObj); Debuger.Log ("Record : "+recordObj.GetDump()); } public void Save(BattleSession battleSession) { if (IsReplaying ()) return; if(!IsRecording() && recordArr != null && recordArr.Size() > 0) { System.DateTime now = System.DateTime.Now; string fileName = now.ToString ("yyyyMMddHHmmss") + "_" + Session.myUserId; ISFSObject obj = new SFSObject (); obj.PutUtfString (LABEL_DATETIME, now.ToString()); obj.PutUtfString (LABEL_VERSION, Config.VERSION); obj.PutSFSArray (LABEL_RECORD_ARRAY, recordArr); // Debuger.Log ("Save replay : "+obj.GetDump()); ByteArray arr = obj.ToBinary (); arr.Compress (); DGFileUtil.SaveFile (arr.Bytes, LOCAL_REPLAY_FOLDER + fileName + ".rep"); string infoStr = ReplayInfo.ToJson (battleSession); ChatManager.GetInstance ().AddReplayMsg (infoStr, fileName); } } public void Load(string fileName) { byte[] bytes = GetLocalReplayFile (fileName); ByteArray arr = new ByteArray (bytes); // if(arr.Compressed) arr.Uncompress(); SFSObject obj = SFSObject.NewFromBinaryData(arr); Debuger.LogWarning("Load Replay"); Debuger.Log(obj.GetDump()); recordArr = obj.GetSFSArray(LABEL_RECORD_ARRAY); StartReplay(); } public void StartReplay() { recordIndex = 0; m_IsReplaying = true; startReplayTime = GameTime.time; } public void StopReplay() { m_IsReplaying = false; } public ISFSObject GetRecord() { if(!IsReplaying() || recordIndex >= recordArr.Size()) { return null; } ISFSObject obj = recordArr.GetSFSObject (recordIndex); float msgTime = obj.GetFloat (LABEL_MSG_TIME); float currentTime = GameTime.time - startReplayTime; if (msgTime > currentTime) return null; ISFSObject msg = obj.GetSFSObject (LABEL_MSG); recordIndex++; return msg; } public bool IsReplaying() { return m_IsReplaying; } private static ReplayManager instance; public static ReplayManager GetInstance() { if (instance == null) instance = new ReplayManager (); return instance; } public static byte[] GetLocalReplayFile(string fileName) { string path = LOCAL_REPLAY_FOLDER + fileName + ".rep"; byte[] bytes = File.ReadAllBytes(path); return bytes; } }