1 #pragma warning disable 0649 6 using System.Collections.Generic;
8 namespace HKI.Core.Debuging
24 public delegate
void ConsoleCommandFunction(
string[] parameters);
27 public const string NoHelpString =
"(no description)";
30 ConsoleCommandFunction callback = null;
32 string paramsExample = null;
36 public ConsoleCommandFunction Callback {
get {
return callback; } }
37 public string Name {
get {
return name; } }
38 public string ParamsExample {
get {
return paramsExample; } }
39 public string Help {
get {
return help; } }
42 public Command(
string name, ConsoleCommandFunction callback,
string help,
string paramsExample)
44 this.callback = callback;
46 this.paramsExample = paramsExample;
56 [SerializeField] Scrollbar Scrollbar = null;
57 [SerializeField] InputField CommandInputField = null;
59 [SerializeField] Color EchoColor =
new Color(0.2f, 0.2f, 0.2f, 1.0f);
60 [SerializeField] Color LogColor = Color.white;
61 [SerializeField] Color WarningColor =
new Color(1.0f, 0.5f, 0.0f, 1.0f);
62 [SerializeField] Color ErrorColor = Color.red;
63 [SerializeField] Color CommandColor = Color.cyan;
64 [SerializeField] Color InvalidCommand = Color.magenta;
69 Queue<Text> textStack =
new Queue<Text>();
71 const string sizeStartString =
"<size=10>";
72 const string sizeEndString =
"</size>";
74 Dictionary<string, Command> commands =
new Dictionary<string, Command>();
76 string[] seperators =
new string[] {
"| ",
" | ",
"|" };
88 for(
int i = 0; i < Texts.Length; i++)
90 Texts[i].gameObject.SetActive(
false);
91 textStack.Enqueue(Texts[i]);
95 StartCoroutine(SetScrollPosition());
97 Application.logMessageReceived += OnLogReceived;
99 InitConsoleCommands();
110 Application.logMessageReceived -= OnLogReceived;
116 if(!
string.IsNullOrEmpty(CommandInputField.text))
118 string command = CommandInputField.text;
120 CommandInputField.text =
"";
121 ValidateAndCallCommand(command);
124 DebugSystem.StartCoroutine(SetScrollPosition());
129 if(
string.IsNullOrEmpty(name))
131 Debug.LogError(
"You are trying to register a command but this command doesn't have a name.");
137 Debug.LogError(
"You are trying to register a command (" + name +
") but this commands callback function is null.");
141 string nameLower = name.ToLower();
143 if (!commands.ContainsKey(nameLower))
144 commands.Add(nameLower,
new Command(nameLower, callback, help, paramsExample));
146 Debug.LogError(
"You are trying to register a command (" + name +
") but this command already exists.");
151 if (
string.IsNullOrEmpty(name))
153 Debug.LogError(
"You are trying to deregister a command but this command doesn't have a name.");
157 string nameLower = name.ToLower();
159 if(commands.ContainsKey(nameLower))
160 commands.Remove(nameLower);
162 Debug.LogError(
"You are trying to deregister a command (" + name +
") but this command doesn't exists.");
168 StringBuilder sb =
new StringBuilder();
169 sb.AppendLine(logString);
170 sb.Append(sizeStartString);
171 sb.Append(stackTrace.Remove(0, stackTrace.IndexOf(
'\n') + 1));
172 sb.Remove(sb.Length - 1, 1);
173 sb.Append(sizeEndString);
178 Print(LogColor, sb.ToString());
182 case LogType.Exception:
184 Print(ErrorColor, sb.ToString());
187 case LogType.Warning:
188 Print(WarningColor, sb.ToString());
192 DebugSystem.StartCoroutine(SetScrollPosition());
197 Text text = textStack.Dequeue();
200 text.transform.SetAsLastSibling();
201 text.gameObject.SetActive(
true);
202 textStack.Enqueue(text);
207 int openParenteciesIndex = fullCommand.IndexOf(
'(');
208 if(openParenteciesIndex >= 0 && fullCommand[fullCommand.Length - 1] !=
')')
210 Print(InvalidCommand, fullCommand);
214 string command = openParenteciesIndex >= 0 ? fullCommand.Remove(openParenteciesIndex).ToLower() : fullCommand.ToLower();
216 if(
string.IsNullOrEmpty(command) || !commands.ContainsKey(command))
218 Print(InvalidCommand, fullCommand);
222 string[] parameters = openParenteciesIndex >= 0 ? fullCommand.Substring(openParenteciesIndex + 1, fullCommand.Length - openParenteciesIndex - 2).Split(seperators,
System.StringSplitOptions.None) : null;
224 Print(CommandColor, fullCommand);
226 commands[command].Callback(parameters);
231 RegisterCommand(
"Help", Help,
"Displays all commands and help information for console commands when they are available.");
232 RegisterCommand(
"Clear", Clear,
"Clears the console history.");
233 RegisterCommand(
"Cls", Clear,
"Clears the console history (alias for Clear)");
234 RegisterCommand(
"Echo", Echo,
"Writes <string> message to the console",
"<string> message");
235 RegisterCommand(
"Print", Echo,
"Writes <string> message to the console (alias for echo)",
"<string> message");
236 RegisterCommand(
"QuitGame", Quit,
"Quit the game.");
237 RegisterCommand(
"Quit Game", Quit,
"Quit the game.");
239 RegisterCommand(
"Log", Debug.Log,
"Log a msg to the console.",
"string");
246 for (
int i = 0; i < Texts.Length; i++)
247 Texts[i].gameObject.SetActive(
false);
253 StringBuilder sb =
new StringBuilder(
"<color=#ffffff>Commands:</color>");
254 foreach(
Command cmd
in commands.Values)
256 sb.Append(
"\n<color=#ffffff>> ");
260 sb.Append(
")</color>\n ");
264 Print(EchoColor, sb.ToString());
269 if(parameters != null && parameters.Length == 1)
270 Print(EchoColor,
"Echo: " + parameters[0]);
282 if (Scrollbar != null)
283 Scrollbar.value = 0.0f;
void Clear(string[] parameters)
void DeregisterCommand(string name)
void ValidateAndCallCommand(string fullCommand)
delegate void ConsoleCommandFunction(string[] parameters)
void OnLogReceived(string logString, string stackTrace, LogType type)
void Help(string[] parameters)
const string NoHelpString
void InitConsoleCommands()
void Print(Color color, string msg)
void Quit(string[] parameters)
MonoBehaviour for the debugging console
IEnumerator SetScrollPosition()
void Echo(string[] parameters)
Command(string name, ConsoleCommandFunction callback, string help, string paramsExample)
The DebugSystem controlls the pannel which includes the DebuConsole, SystemInfo, MemoryInfo and FPSIn...
void RegisterCommand(string name, Command.ConsoleCommandFunction callback, string help=Command.NoHelpString, string paramsExample="")
This command class binds all data related so it can be executed by the DebugConsole ...