88public class DevelopReplayManager
99{
1010 const string c_directoryName = "DevelopReplay" ;
11- public const string c_expandName = "json" ;
11+ public const string c_eventExpandName = "inputEvent" ;
12+ public const string c_randomExpandName = "random" ;
1213
1314 const string c_recordName = "DevelopReplay" ;
1415 const string c_qucikLunchKey = "qucikLunch" ;
@@ -28,6 +29,9 @@ public class DevelopReplayManager
2829
2930 private static Action s_onLunchCallBack ;
3031
32+ private static StreamWriter m_EventWriter = null ;
33+ private static StreamWriter m_RandomWriter = null ;
34+
3135 public static Action OnLunchCallBack
3236 {
3337 get { return s_onLunchCallBack ; }
@@ -78,14 +82,12 @@ static void ChoseReplayMode(bool isReplay,string replayFileName = null)
7882 {
7983 Log . Init ( true ) ; //日志记录启动
8084
81- s_eventStreamSerialize = new List < Dictionary < string , string > > ( ) ;
82- s_randomList = new List < int > ( ) ;
83-
8485 ApplicationManager . s_OnApplicationUpdate += OnRecordUpdate ;
8586 InputManager . OnEveryEventDispatch += OnEveryEventCallBack ;
8687 GUIConsole . onGUICallback += RecordModeGUI ;
8788 RandomService . OnRandomCreat += OnGetRandomCallBack ;
88- SaveFileName = GetLogFileName ( ) ;
89+
90+ OpenWriteFileStream ( GetLogFileName ( ) ) ;
8991 }
9092
9193 ApplicationManager . s_OnApplicationOnGUI -= ReplayMenuGUI ;
@@ -104,74 +106,79 @@ static void ChoseReplayMode(bool isReplay,string replayFileName = null)
104106
105107 public static void OnEveryEventCallBack ( string eventName , IInputEventBase inputEvent )
106108 {
107- // Dictionary<string, string > tmp = new Dictionary<string, string> ();
109+ Dictionary < string , object > tmp = HeapObjectPool . GetSODict ( ) ;
108110
109- // tmp.Add(c_eventNameKey, inputEvent.GetType().Name);
110- // tmp.Add(c_serializeInfoKey, inputEvent.Serialize());
111+ tmp . Add ( c_eventNameKey , inputEvent . GetType ( ) . Name ) ;
112+ tmp . Add ( c_serializeInfoKey , inputEvent . Serialize ( ) ) ;
111113
112- //s_eventStreamSerialize.Add (tmp);
114+ WriteInputEvent ( Json . Serialize ( tmp ) ) ;
113115 }
114116
115117 public static void OnGetRandomCallBack ( int random )
116118 {
117- s_randomList . Add ( random ) ;
119+ WriteRandomRecord ( random ) ;
118120 }
119121
120-
121-
122122 #region SaveReplayFile
123123
124-
125- public static void SaveReplayFile ( string fileName )
124+ static void OpenWriteFileStream ( string fileName )
126125 {
127- List < Dictionary < string , string > > EventStreamContent = SaveEventStream ( ) ;
128- List < int > randomListContent = SaveRandomList ( ) ;
129126
130- Dictionary < string , object > replayInfo = new Dictionary < string , object > ( ) ;
131-
132- replayInfo . Add ( c_eventStreamKey , EventStreamContent ) ;
133- replayInfo . Add ( c_randomListKey , randomListContent ) ;
134-
135- string content = Json . Serialize ( replayInfo ) ;
136-
137- ResourceIOTool . WriteStringByFile (
138- PathTool . GetAbsolutePath ( ResLoadType . Persistent ,
127+ try
128+ {
129+ string path = PathTool . GetAbsolutePath ( ResLoadLocation . Persistent ,
139130 PathTool . GetRelativelyPath (
140131 c_directoryName ,
141132 fileName ,
142- c_expandName ) )
143- , content ) ;
144- }
133+ c_randomExpandName ) ) ;
145134
146- static List < Dictionary < string , string > > SaveEventStream ( )
147- {
148- //List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
135+ string dirPath = Path . GetDirectoryName ( path ) ;
149136
150- //for (int i = 0; i < s_eventStream.Count; i++)
151- //{
152- // Dictionary<string, string> tmp = new Dictionary<string, string>();
137+ if ( ! Directory . Exists ( dirPath ) )
138+ Directory . CreateDirectory ( dirPath ) ;
153139
154- // tmp.Add(c_eventNameKey, s_eventStream[i].GetType().Name);
155- // tmp.Add(c_serializeInfoKey,s_eventStream[i].Serialize());
156140
157- // list.Add(tmp);
158- //}
141+ //Debug.Log("EventStream Name: " + PathTool.GetAbsolutePath(ResLoadLocation.Persistent,
142+ // PathTool.GetRelativelyPath(
143+ // c_directoryName,
144+ // fileName,
145+ // c_randomExpandName)));
159146
160- //return list;
147+ m_RandomWriter = new StreamWriter ( PathTool . GetAbsolutePath ( ResLoadLocation . Persistent ,
148+ PathTool . GetRelativelyPath (
149+ c_directoryName ,
150+ fileName ,
151+ c_randomExpandName ) ) ) ;
152+ m_RandomWriter . AutoFlush = true ;
161153
162- return s_eventStreamSerialize ;
154+ m_EventWriter = new StreamWriter ( PathTool . GetAbsolutePath ( ResLoadLocation . Persistent ,
155+ PathTool . GetRelativelyPath (
156+ c_directoryName ,
157+ fileName ,
158+ c_eventExpandName ) ) ) ;
159+ m_EventWriter . AutoFlush = true ;
160+
161+ }
162+ catch ( Exception e )
163+ {
164+ Debug . LogError ( e . ToString ( ) ) ;
165+ }
163166 }
164167
165- static List < int > SaveRandomList ( )
168+ static void WriteRandomRecord ( int random )
166169 {
167- List < int > list = new List < int > ( ) ;
168-
169- for ( int i = 0 ; i < s_randomList . Count ; i ++ )
170+ if ( m_RandomWriter != null )
170171 {
171- list . Add ( s_randomList [ i ] ) ;
172+ m_RandomWriter . WriteLine ( random . ToString ( ) ) ;
172173 }
174+ }
173175
174- return list ;
176+ static void WriteInputEvent ( string EventSerializeContent )
177+ {
178+ if ( m_EventWriter != null )
179+ {
180+ m_EventWriter . WriteLine ( EventSerializeContent ) ;
181+ }
175182 }
176183
177184 #endregion
@@ -180,42 +187,48 @@ static List<int> SaveRandomList()
180187
181188 public static void LoadReplayFile ( string fileName )
182189 {
183- string content = ResourceIOTool . ReadStringByFile (
184- PathTool . GetAbsolutePath ( ResLoadType . Persistent ,
190+ string eventContent = ResourceIOTool . ReadStringByFile (
191+ PathTool . GetAbsolutePath ( ResLoadLocation . Persistent ,
185192 PathTool . GetRelativelyPath (
186193 c_directoryName ,
187194 fileName ,
188- c_expandName ) ) ) ;
195+ c_eventExpandName ) ) ) ;
189196
190- Dictionary < string , object > replayInfo = ( Dictionary < string , object > ) Json . Deserialize ( content ) ;
197+ string randomContent = ResourceIOTool . ReadStringByFile (
198+ PathTool . GetAbsolutePath ( ResLoadLocation . Persistent ,
199+ PathTool . GetRelativelyPath (
200+ c_directoryName ,
201+ fileName ,
202+ c_randomExpandName ) ) ) ;
191203
192- LoadEventStream ( ( List < object > ) replayInfo [ c_eventStreamKey ] ) ;
193- LoadRandomList ( ( List < object > ) replayInfo [ c_randomListKey ] ) ;
204+ LoadEventStream ( eventContent . Split ( ' \n ' ) ) ;
205+ LoadRandomList ( randomContent . Split ( ' \n ' ) ) ;
194206 }
195207
196- static void LoadEventStream ( List < object > content )
208+ static void LoadEventStream ( string [ ] content )
197209 {
198210 s_eventStream = new List < IInputEventBase > ( ) ;
199-
200- for ( int i = 0 ; i < content . Count ; i ++ )
211+ for ( int i = 0 ; i < content . Length ; i ++ )
201212 {
202- Dictionary < string , object > info = ( Dictionary < string , object > ) ( content [ i ] ) ;
203-
204- //Debug.Log(info[c_eventNameKey].ToString());
205- //Debug.Log(info[c_eventNameKey].ToString());
206-
207- IInputEventBase eTmp = ( IInputEventBase ) JsonUtility . FromJson ( info [ c_serializeInfoKey ] . ToString ( ) , Type . GetType ( info [ c_eventNameKey ] . ToString ( ) ) ) ;
208- s_eventStream . Add ( eTmp ) ;
213+ if ( content [ i ] != "" )
214+ {
215+ Dictionary < string , object > info = ( Dictionary < string , object > ) ( Json . Deserialize ( content [ i ] ) ) ;
216+ IInputEventBase eTmp = ( IInputEventBase ) JsonUtility . FromJson ( info [ c_serializeInfoKey ] . ToString ( ) , Type . GetType ( info [ c_eventNameKey ] . ToString ( ) ) ) ;
217+ s_eventStream . Add ( eTmp ) ;
218+ }
209219 }
210220 }
211221
212- static void LoadRandomList ( List < object > content )
222+ static void LoadRandomList ( string [ ] content )
213223 {
214224 s_randomList = new List < int > ( ) ;
215225
216- for ( int i = 0 ; i < content . Count ; i ++ )
226+ for ( int i = 0 ; i < content . Length ; i ++ )
217227 {
218- s_randomList . Add ( int . Parse ( content [ i ] . ToString ( ) ) ) ;
228+ if ( content [ i ] != "" )
229+ {
230+ s_randomList . Add ( int . Parse ( content [ i ] . ToString ( ) ) ) ;
231+ }
219232 }
220233 }
221234
@@ -350,8 +363,6 @@ static void ShowLog()
350363
351364 static Rect consoleRect = new Rect ( margin , Screen . height * 0.6f , Screen . width * 0.3f , Screen . height * 0.4f - margin ) ;
352365
353- static string SaveFileName = "" ;
354-
355366 static void RecordModeGUI ( )
356367 {
357368 consoleRect = new Rect ( margin , Screen . height * 0.5f , Screen . width * 0.4f , Screen . height * 0.5f - margin ) ;
@@ -360,13 +371,6 @@ static void RecordModeGUI()
360371 }
361372 static void RecordModeGUIWindow ( int id )
362373 {
363- SaveFileName = GUILayout . TextField ( SaveFileName , GUILayout . ExpandHeight ( true ) ) ;
364-
365- if ( GUILayout . Button ( "保存录像文件" , GUILayout . ExpandHeight ( true ) ) )
366- {
367- SaveReplayFile ( SaveFileName ) ;
368- }
369-
370374 if ( RecordManager . GetData ( c_recordName ) . GetRecord ( c_qucikLunchKey , true ) )
371375 {
372376 if ( GUILayout . Button ( "开启后台" , GUILayout . ExpandHeight ( true ) ) )
@@ -428,13 +432,13 @@ static void OnRecordUpdate()
428432
429433 public static string [ ] GetRelpayFileNames ( )
430434 {
431- FileTool . CreatPath ( PathTool . GetAbsolutePath ( ResLoadType . Persistent , c_directoryName ) ) ;
435+ FileTool . CreatPath ( PathTool . GetAbsolutePath ( ResLoadLocation . Persistent , c_directoryName ) ) ;
432436
433437 List < string > relpayFileNames = new List < string > ( ) ;
434- string [ ] allFileName = Directory . GetFiles ( PathTool . GetAbsolutePath ( ResLoadType . Persistent , c_directoryName ) ) ;
438+ string [ ] allFileName = Directory . GetFiles ( PathTool . GetAbsolutePath ( ResLoadLocation . Persistent , c_directoryName ) ) ;
435439 foreach ( var item in allFileName )
436440 {
437- if ( item . EndsWith ( "." + c_expandName ) )
441+ if ( item . EndsWith ( "." + c_eventExpandName ) )
438442 {
439443 string configName = FileTool . RemoveExpandName ( FileTool . GetFileNameByPath ( item ) ) ;
440444 relpayFileNames . Add ( configName ) ;
0 commit comments