3
3
using System . Collections . Generic ;
4
4
using UnityEngine ;
5
5
using System . Reflection ;
6
+ using System . Text ;
6
7
7
8
namespace Wanderer . GameFramework
8
9
{
@@ -15,6 +16,10 @@ public class ConsoleWindow : IDebuggerWindow
15
16
private Dictionary < string , MethodInfo > _commandMethod = new Dictionary < string , MethodInfo > ( ) ;
16
17
//action执行函数
17
18
private Dictionary < string , Action > _commandAction = new Dictionary < string , Action > ( ) ;
19
+ //命令帮助
20
+ private Dictionary < string , string > _commandHelp = new Dictionary < string , string > ( ) ;
21
+ //命令反馈
22
+ private Dictionary < string , string > _commandFeedback = new Dictionary < string , string > ( ) ;
18
23
//支持反射
19
24
private bool _reflectionSupported = true ;
20
25
//支持消息发送
@@ -28,22 +33,10 @@ public class ConsoleWindow : IDebuggerWindow
28
33
29
34
public void OnInit ( params object [ ] args )
30
35
{
31
- _commandAction . Add ( "d1" , ( ) =>
32
- {
33
- Debug . unityLogger . logEnabled = true ;
34
- } ) ;
35
- _commandAction . Add ( "d0" , ( ) =>
36
- {
37
- Debug . unityLogger . logEnabled = false ;
38
- } ) ;
39
- _commandAction . Add ( "f1" , ( ) =>
40
- {
41
- GameFrameworkMode . GetModule < DebuggerManager > ( ) . SetLogFileEnable ( true ) ;
42
- } ) ;
43
- _commandAction . Add ( "f0" , ( ) =>
44
- {
45
- GameFrameworkMode . GetModule < DebuggerManager > ( ) . SetLogFileEnable ( false ) ;
46
- } ) ;
36
+ AddCommand ( "d0" , ( ) => { Debug . unityLogger . logEnabled = false ; } , "关闭Debug.Log" , "Log disabled." ) ;
37
+ AddCommand ( "d1" , ( ) => { Debug . unityLogger . logEnabled = true ; } , "开启Debug.Log" , "Log enabled." ) ;
38
+ AddCommand ( "f0" , ( ) => { GameFrameworkMode . GetModule < DebuggerManager > ( ) . SetLogFileEnable ( false ) ; } , "关闭日志文件" , "Log file disabled." ) ;
39
+ AddCommand ( "f1" , ( ) => { GameFrameworkMode . GetModule < DebuggerManager > ( ) . SetLogFileEnable ( true ) ; } , "开启日志文件" , "Log file enabled." ) ;
47
40
}
48
41
49
42
public void OnEnter ( )
@@ -106,24 +99,52 @@ public void OnDraw()
106
99
/// </summary>
107
100
/// <param name="command"></param>
108
101
/// <param name="callAction"></param>
109
- public void AddCommand ( string command , Action callAction )
102
+ public void AddCommand ( string command , Action callAction , string help = null , string feedback = null )
110
103
{
111
104
if ( ! _commandAction . ContainsKey ( command ) )
112
105
{
113
106
_commandAction . Add ( command , callAction ) ;
114
107
}
108
+ if ( ! string . IsNullOrEmpty ( help ) )
109
+ {
110
+ if ( ! _commandHelp . ContainsKey ( command ) )
111
+ {
112
+ _commandHelp . Add ( command , help ) ;
113
+ }
114
+ }
115
+ if ( ! string . IsNullOrEmpty ( feedback ) )
116
+ {
117
+ if ( ! _commandFeedback . ContainsKey ( command ) )
118
+ {
119
+ _commandFeedback . Add ( command , feedback ) ;
120
+ }
121
+ }
115
122
}
116
123
/// <summary>
117
124
/// 添加命令
118
125
/// </summary>
119
126
/// <param name="command"></param>
120
127
/// <param name="callMethod"></param>
121
- public void AddCommand ( string command , MethodInfo callMethod )
128
+ public void AddCommand ( string command , MethodInfo callMethod , string help = null , string feedback = null )
122
129
{
123
130
if ( ! _commandMethod . ContainsKey ( command ) )
124
131
{
125
132
_commandMethod . Add ( command , callMethod ) ;
126
133
}
134
+ if ( ! string . IsNullOrEmpty ( help ) )
135
+ {
136
+ if ( ! _commandHelp . ContainsKey ( command ) )
137
+ {
138
+ _commandHelp . Add ( command , help ) ;
139
+ }
140
+ }
141
+ if ( ! string . IsNullOrEmpty ( feedback ) )
142
+ {
143
+ if ( ! _commandFeedback . ContainsKey ( command ) )
144
+ {
145
+ _commandFeedback . Add ( command , feedback ) ;
146
+ }
147
+ }
127
148
}
128
149
/// <summary>
129
150
/// 移除命令
@@ -139,6 +160,14 @@ public void RemoveCommand(string command)
139
160
{
140
161
_commandMethod . Remove ( command ) ;
141
162
}
163
+ if ( _commandHelp . ContainsKey ( command ) )
164
+ {
165
+ _commandHelp . Remove ( command ) ;
166
+ }
167
+ if ( _commandFeedback . ContainsKey ( command ) )
168
+ {
169
+ _commandFeedback . Remove ( command ) ;
170
+ }
142
171
}
143
172
#endregion
144
173
@@ -179,6 +208,10 @@ private void ExecuteCommand(string command)
179
208
if ( _commandAction . TryGetValue ( command , out Action callAction ) )
180
209
{
181
210
callAction . Invoke ( ) ;
211
+ if ( _commandFeedback . TryGetValue ( command , out string feedback ) )
212
+ {
213
+ AddLine ( feedback ) ;
214
+ }
182
215
return ;
183
216
}
184
217
@@ -195,7 +228,6 @@ private void ExecuteCommand(string command)
195
228
MethodInfo callMethod ;
196
229
if ( ! _commandMethod . TryGetValue ( command , out callMethod ) )
197
230
{
198
-
199
231
int index = command . LastIndexOf ( '.' ) ;
200
232
if ( index > 0 && index < command . Length - 1 )
201
233
{
@@ -255,6 +287,15 @@ private void ExecuteCommand(string command)
255
287
}
256
288
}
257
289
}
290
+ else
291
+ {
292
+ callMethod . Invoke ( null , null ) ;
293
+ if ( _commandFeedback . TryGetValue ( command , out string feedback ) )
294
+ {
295
+ AddLine ( feedback ) ;
296
+ }
297
+ return ;
298
+ }
258
299
259
300
260
301
//添加反馈
@@ -264,11 +305,22 @@ private void ExecuteCommand(string command)
264
305
//执行默认的命令
265
306
private int ExecuteDefaultCommand ( string command )
266
307
{
308
+ command = command . ToLower ( ) ;
267
309
switch ( command )
268
310
{
269
311
case "clear" :
270
312
ClearLines ( ) ;
271
313
return 0 ;
314
+ case "help" :
315
+ StringBuilder helpBuilder = new StringBuilder ( ) ;
316
+ helpBuilder . AppendLine ( "help\t See all the commands and the corresponding help." ) ;
317
+ helpBuilder . AppendLine ( "clear\t Clean up all lines." ) ;
318
+ foreach ( var item in _commandHelp )
319
+ {
320
+ helpBuilder . AppendLine ( $ "{ item . Key } \t { item . Value } .") ;
321
+ }
322
+ AddLine ( helpBuilder . ToString ( ) ) ;
323
+ return 0 ;
272
324
}
273
325
return - 1 ;
274
326
}
0 commit comments