InputRecorder.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using CIS;
  5. using LitJson;
  6. using UnityEngine;
  7. public class InputRecorder : MonoBehaviour, ILevelItem, IInputEventHandler
  8. {
  9. private void Start()
  10. {
  11. if (this.replayTarget != null)
  12. {
  13. this.replayTargetHandler = (IInputEventHandler)this.replayTarget.GetComponent(typeof(IInputEventHandler));
  14. }
  15. SingletonMonoBehaviourClass<InputMgr>.instance.RegisterInputEventHandler(this);
  16. this.demo = PlayerControllerDemo.Instance;
  17. }
  18. private void Update()
  19. {
  20. if (this.recordMode == InputRecorder.RecordMode.Record)
  21. {
  22. if (this.startRecordInput)
  23. {
  24. this.frameCounter++;
  25. }
  26. }
  27. else if (this.recordMode == InputRecorder.RecordMode.Replay && this.startRecordInput)
  28. {
  29. this.frameCounter++;
  30. InputRecorder.InputSlot inputSlot = null;
  31. if (this.inputRecord.slots.Count > 0)
  32. {
  33. inputSlot = this.inputRecord.slots[0];
  34. }
  35. int num = 0;
  36. while (inputSlot != null && this.frameCounter == inputSlot.frame)
  37. {
  38. if (inputSlot.type == 0)
  39. {
  40. this.replayTargetHandler.KeyDown(inputSlot.key);
  41. }
  42. else
  43. {
  44. this.replayTargetHandler.KeyUp(inputSlot.key);
  45. }
  46. this.inputRecord.slots.RemoveAt(0);
  47. if (this.inputRecord.slots.Count > 0)
  48. {
  49. inputSlot = this.inputRecord.slots[0];
  50. }
  51. else
  52. {
  53. inputSlot = null;
  54. }
  55. num++;
  56. }
  57. if (num > 1)
  58. {
  59. }
  60. }
  61. }
  62. public void StartRecord()
  63. {
  64. UnityEngine.Debug.Log("[StartRecord]");
  65. this.startRecordInput = true;
  66. if (this.recordMode == InputRecorder.RecordMode.Record)
  67. {
  68. this.inputRecord = new InputRecorder.InputRecord();
  69. }
  70. else if (this.recordMode == InputRecorder.RecordMode.Replay)
  71. {
  72. TextAsset textAsset = Resources.Load<TextAsset>(this.fileName);
  73. if (textAsset == null)
  74. {
  75. UnityEngine.Debug.LogError("inputRecord asset not found!");
  76. return;
  77. }
  78. this.inputRecord = JsonMapper.ToObject<InputRecorder.InputRecord>(textAsset.text);
  79. }
  80. }
  81. public void EndRecord()
  82. {
  83. UnityEngine.Debug.Log("[EndRecord]");
  84. this.startRecordInput = false;
  85. this.frameCounter = 0;
  86. if (this.recordMode == InputRecorder.RecordMode.Record)
  87. {
  88. string contents = JsonMapper.ToJson(this.inputRecord);
  89. File.WriteAllText(Application.dataPath + "/Resources/" + this.fileName + ".txt", contents);
  90. }
  91. }
  92. public void KeyDown(KeyCode key)
  93. {
  94. if (this.recordMode == InputRecorder.RecordMode.Record && this.startRecordInput)
  95. {
  96. this.inputRecord.slots.Add(new InputRecorder.InputSlot(key, this.frameCounter, 0));
  97. }
  98. }
  99. public void KeyUp(KeyCode key)
  100. {
  101. if (this.recordMode == InputRecorder.RecordMode.Record && this.startRecordInput)
  102. {
  103. this.inputRecord.slots.Add(new InputRecorder.InputSlot(key, this.frameCounter, 1));
  104. }
  105. }
  106. public void OnReset()
  107. {
  108. }
  109. public void OnPause(bool isPause)
  110. {
  111. }
  112. public void OnEntertBlock(ILevelBlock block)
  113. {
  114. UnityEngine.Debug.Log("[OnEntertBlock]");
  115. if (this.startBlock == block)
  116. {
  117. this.startRecordInput = true;
  118. this.fileName = block.GetGo().name + "_test";
  119. this.StartRecord();
  120. }
  121. }
  122. public void OnLeaveBlock(ILevelBlock block)
  123. {
  124. if (this.startRecordInput)
  125. {
  126. this.EndRecord();
  127. }
  128. }
  129. public void OnPickingTime(float time)
  130. {
  131. }
  132. public LevelBlock startBlock;
  133. public InputRecorder.RecordMode recordMode;
  134. public GameObject replayTarget;
  135. private string fileName;
  136. private bool startRecordInput;
  137. private int frameCounter;
  138. private InputRecorder.InputRecord inputRecord = new InputRecorder.InputRecord();
  139. private IInputEventHandler replayTargetHandler;
  140. private PlayerControllerDemo demo;
  141. public class InputSlot
  142. {
  143. public InputSlot(KeyCode key, int frame, int type)
  144. {
  145. this.key = key;
  146. this.frame = frame;
  147. this.type = type;
  148. }
  149. public KeyCode key;
  150. public int type;
  151. public int frame;
  152. }
  153. public class InputRecord
  154. {
  155. public InputRecord()
  156. {
  157. this.slots = new List<InputRecorder.InputSlot>();
  158. }
  159. public List<InputRecorder.InputSlot> slots;
  160. }
  161. public enum RecordMode
  162. {
  163. Record,
  164. Replay
  165. }
  166. }