ReplayManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Sfs2X.Entities.Data;
  5. using Sfs2X.Util;
  6. using System.IO;
  7. using LitJson;
  8. public class ReplayManager
  9. {
  10. public const string LABEL_MSG = "m";
  11. public const string LABEL_MSG_TIME = "t";
  12. public const string LABEL_DATETIME = "d";
  13. public const string LABEL_VERSION = "v";
  14. public const string LABEL_RECORD_ARRAY = "a";
  15. public static string LOCAL_REPLAY_FOLDER = Application.persistentDataPath + "/Replay/";
  16. private bool m_IsRecording;
  17. private bool m_IsReplaying;
  18. private float startRecordTime;
  19. private float startReplayTime;
  20. private int recordIndex;
  21. private ISFSArray recordArr;
  22. public void StartRecord()
  23. {
  24. if (IsReplaying ())
  25. return;
  26. m_IsRecording = true;
  27. startRecordTime = GameTime.realtimeSinceStartup;
  28. recordArr = new SFSArray ();
  29. }
  30. public void StopRecord()
  31. {
  32. if (IsReplaying ())
  33. return;
  34. m_IsRecording = false;
  35. }
  36. public bool IsRecording()
  37. {
  38. return m_IsRecording;
  39. }
  40. public void CheckRecord(ISFSObject msg)
  41. {
  42. if (IsReplaying ())
  43. return;
  44. if(IsRecording())
  45. {
  46. Record (msg);
  47. }
  48. }
  49. private void Record(ISFSObject msg)
  50. {
  51. float currentTime = GameTime.realtimeSinceStartup - startRecordTime;
  52. ISFSObject recordObj = new SFSObject ();
  53. recordObj.PutSFSObject (LABEL_MSG, msg);
  54. recordObj.PutFloat (LABEL_MSG_TIME, currentTime);
  55. recordArr.AddSFSObject (recordObj);
  56. Debuger.Log ("Record : "+recordObj.GetDump());
  57. }
  58. public void Save(BattleSession battleSession)
  59. {
  60. if (IsReplaying ())
  61. return;
  62. if(!IsRecording() && recordArr != null && recordArr.Size() > 0)
  63. {
  64. System.DateTime now = System.DateTime.Now;
  65. string fileName = now.ToString ("yyyyMMddHHmmss") + "_" + Session.myUserId;
  66. ISFSObject obj = new SFSObject ();
  67. obj.PutUtfString (LABEL_DATETIME, now.ToString());
  68. obj.PutUtfString (LABEL_VERSION, Config.VERSION);
  69. obj.PutSFSArray (LABEL_RECORD_ARRAY, recordArr);
  70. // Debuger.Log ("Save replay : "+obj.GetDump());
  71. ByteArray arr = obj.ToBinary ();
  72. arr.Compress ();
  73. DGFileUtil.SaveFile (arr.Bytes, LOCAL_REPLAY_FOLDER + fileName + ".rep");
  74. string infoStr = ReplayInfo.ToJson (battleSession);
  75. ChatManager.GetInstance ().AddReplayMsg (infoStr, fileName);
  76. }
  77. }
  78. public void Load(string fileName)
  79. {
  80. byte[] bytes = GetLocalReplayFile (fileName);
  81. ByteArray arr = new ByteArray (bytes);
  82. // if(arr.Compressed)
  83. arr.Uncompress();
  84. SFSObject obj = SFSObject.NewFromBinaryData(arr);
  85. Debuger.LogWarning("Load Replay");
  86. Debuger.Log(obj.GetDump());
  87. recordArr = obj.GetSFSArray(LABEL_RECORD_ARRAY);
  88. StartReplay();
  89. }
  90. public void StartReplay()
  91. {
  92. recordIndex = 0;
  93. m_IsReplaying = true;
  94. startReplayTime = GameTime.time;
  95. }
  96. public void StopReplay()
  97. {
  98. m_IsReplaying = false;
  99. }
  100. public ISFSObject GetRecord()
  101. {
  102. if(!IsReplaying() || recordIndex >= recordArr.Size())
  103. {
  104. return null;
  105. }
  106. ISFSObject obj = recordArr.GetSFSObject (recordIndex);
  107. float msgTime = obj.GetFloat (LABEL_MSG_TIME);
  108. float currentTime = GameTime.time - startReplayTime;
  109. if (msgTime > currentTime)
  110. return null;
  111. ISFSObject msg = obj.GetSFSObject (LABEL_MSG);
  112. recordIndex++;
  113. return msg;
  114. }
  115. public bool IsReplaying()
  116. {
  117. return m_IsReplaying;
  118. }
  119. private static ReplayManager instance;
  120. public static ReplayManager GetInstance()
  121. {
  122. if (instance == null)
  123. instance = new ReplayManager ();
  124. return instance;
  125. }
  126. public static byte[] GetLocalReplayFile(string fileName)
  127. {
  128. string path = LOCAL_REPLAY_FOLDER + fileName + ".rep";
  129. byte[] bytes = File.ReadAllBytes(path);
  130. return bytes;
  131. }
  132. }