123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 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;
- }
- }
|