1 using System.Collections;
2 using System.Collections.Generic;
21 private static List<string> output_history =
new List<string>();
23 private static List<string> command_history =
new List<string>();
24 private static int command_history_limit = 10;
25 private static List<AVR_ConsoleCommand> commands =
new List<AVR_ConsoleCommand>();
27 private static bool initialized =
false;
29 private static int repeat_counter = 1;
31 public static event System.Action<
string> OnPrint = delegate { };
32 public static event System.Action<
string> OnCommand = delegate { };
36 [InitializeOnLoadMethod]
38 [RuntimeInitializeOnLoadMethod]
39 public static void init() {
40 if(initialized)
return;
42 Application.logMessageReceived += HandleLog;
45 register_all_attribute_commands();
51 public static void HandleLog(
string logString,
string stackTrace, LogType type) {
53 case LogType.Log : { cprint(logString,
"Debug.Log");
break; }
54 case LogType.Warning: { cwarn(logString,
"Debug.Log");
break; }
55 case LogType.Error: { cerror(logString, stackTrace);
break; }
56 case LogType.Exception: { cerror(logString, stackTrace);
break; }
57 default : { cprint(logString,
"Debug.Log");
break; }
65 return output_s + (repeat_counter > 1 ?
" [x"+repeat_counter+
"]" :
"");
74 public static void cprint(
string s, MonoBehaviour obj)
76 print(obj.name +
"::" + obj.GetType().ToString() +
">> " + s);
80 public static void cprint(
string s, GameObject obj)
82 print(obj.name +
">> " + s);
86 public static void cprint(
string s,
string obj)
88 print(obj +
">> " + s);
92 public static void csuccess(
string s, MonoBehaviour obj)
94 success(obj.name +
"::" + obj.GetType().ToString() +
">> " + s);
98 public static void csuccess(
string s, GameObject obj) {
99 success(obj.name +
">> " + s);
105 success(obj +
">> " + s);
109 public static void cwarn(
string s, MonoBehaviour obj)
111 warn(obj.name +
"::" + obj.GetType().ToString() +
">> " + s);
115 public static void cwarn(
string s, GameObject obj)
117 warn(obj.name +
">> " + s);
121 public static void cwarn(
string s,
string obj)
123 warn(obj +
">> " + s);
127 public static void cerror(
string s, MonoBehaviour obj)
129 error(obj.name +
"::" + obj.GetType().ToString() +
">> " + s);
133 public static void cerror(
string s, GameObject obj)
135 error(obj.name +
">> " + s);
139 public static void cerror(
string s,
string obj)
141 error(obj +
">> " + s);
146 if(output_history.Count>0 && s==output_history.Last()) {
151 if(repeat_counter>1) output_s +=
" [x"+repeat_counter+
"]";
155 output_history.Add(s);
166 raw_print(
"\n# " + s);
174 raw_print(
"\n# <color=green><b>" + s +
"</b></color>");
182 raw_print(
"\n! <color=red><b>ERR:</b> " + s +
"</color>");
188 public static void warn(
string s)
190 raw_print(
"\n! <color=yellow><b>WRN:</b> " + s +
"</color>");
195 raw_print(
"\n> <color=grey> " + s +
"</color>");
203 if(command_history.Count<1)
return "";
204 t = Mathf.Clamp(t, 0, command_history.Count-1);
205 return command_history[t];
211 public static void command(
string s,
bool addToHistory=
true) {
213 command_history.Insert(0, s);
214 while(command_history.Count>command_history_limit) command_history.RemoveAt(0);
220 string[] c = s.Split(
' ');
223 List<AVR_ConsoleCommand> cmds = commands.Where((cmd) => cmd.name == c[0]).ToList();
224 AVR_ConsoleCommand cmd = cmds.Where((cmd) => cmd.min_args <= c.Length - 1).OrderBy((cmd) => -cmd.min_args).FirstOrDefault();
226 if(cmd != null && c.Length > cmd.min_args) {
227 cmd.func(c.Skip(1).ToArray());
229 else if(cmd == null && cmds.Count > 0) {
230 error(
"Command "+c[0]+
" requires more arguments.");
234 error(
"Could not recognize command \"" + c[0] +
"\"");
250 public AVR_ConsoleCommand(
string name, System.Action<
string[]> func,
int min_args = 0,
string desc =
"No description given.") {
253 this.min_args = min_args;
257 public System.Action<
string[]>
func;
277 public static void register_command(
string name, System.Action<
string[]> func,
int min_args=0,
string desc =
"No description given.") {
288 public static void register_command(
string name, System.Action<
string[]> func,
string desc) {
294 var methodsMarkedAsCommand =
295 from a in System.AppDomain.CurrentDomain.GetAssemblies().AsParallel()
296 from t in a.GetTypes()
297 from m in t.GetMethods(System.Reflection.BindingFlags.Static | (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic))
298 let attributes = m.GetCustomAttributes(typeof(AVR.Core.Attributes.ConsoleCommand),
true)
299 where attributes != null && attributes.Length > 0
300 select
new { method = m, attributes = attributes.Cast<AVR.Core.Attributes.ConsoleCommand>() };
303 foreach(var result
in methodsMarkedAsCommand) {
304 foreach(var att
in result.attributes) {
307 if (result.method.GetParameters().Count() == 1 && result.method.GetParameters()[0].ParameterType == typeof(System.String))
309 System.Action<
string> action = (System.Action<
string>)System.Delegate.CreateDelegate(typeof(System.Action<
string>), result.method);
310 register_command(att.getCommandString(), (args) => action(args[0]), att.getMinArgs(), att.getDescription());
313 else if(result.method.GetParameters().Count()>0) {
314 System.Action<
string[]> action = (System.Action<
string[]>)System.Delegate.CreateDelegate(typeof(System.Action<
string[]>), result.method);
315 register_command(att.getCommandString(), action, att.getMinArgs(), att.getDescription());
319 System.Action action = (System.Action)System.Delegate.CreateDelegate(typeof(System.Action), result.method);
320 register_command(att.getCommandString(), (args) => action(), att.getMinArgs(), att.getDescription());
323 catch (System.Exception) {
324 error(
"Following method is marked as ConsoleCommand, but doesnt follow the necessary requirements: " + result.method +
"\n It needs to be static and void. Parameters have to be either none, a single string or a single array of strings.");
335 if(cmd.
name == arg) {
336 print(
"COMMAND: " + cmd.
name);
338 print(
"DESCRIPTION: " + cmd.
desc);
343 if(!found) print(
"Command "+arg+
" does not exist. Type \"commands\" to see a list of all available commands.");
348 print(
"COMMAND \t\t MIN ARGS");
356 Debug.Log(
"This is a sample command.");
static void cerror(string s, string obj)
Print error message with a caller-context variable.
static void command(string s, bool addToHistory=true)
Execute a given console command.
Represents a command for the AVR_Devconsole
static void csuccess(string s, MonoBehaviour obj)
Print success message with a caller-context variable.
AVR_ConsoleCommand(string name, System.Action< string[]> func, int min_args=0, string desc="No description given.")
Represents a command for the AVR_Devconsole Identifier-string for the command. This is the token a us...
static void success(string s)
Print a success message string on the console.
static void raw_print(string s)
static void init()
Initializes the DevConsole. Only the first execution will have an effect.
static void cwarn(string s, MonoBehaviour obj)
Print warning message with a caller-context variable.
System.Action< string[]> func
static void error(string s)
Print a given error string on the console.
static void register_all_attribute_commands()
static void register_command(string name, System.Action< string[]> func, string desc)
Registers a new command to the console.
static void csuccess(string s, string obj)
Print success message with a caller-context variable.
Class for the arc-vr developer's console. Functions as a singleton with only static members...
Contains constan values, settings etc. which can all be set through *.avr files. Duplicate settings w...
static void cprint(string s, string obj)
Print with a caller-context variable.
static string history(int t)
Returns any command executed in the past. Index of the past command with t=0 being the most recent on...
static void HandleLog(string logString, string stackTrace, LogType type)
Executes a respective output based on logString, stackTrace and LogType.
static void cwarn(string s, GameObject obj)
Print warning message with a caller-context variable.
static string get_text()
Get the complete output of this console as text.
static void echo_command(string s)
static void cerror(string s, MonoBehaviour obj)
Print error message with a caller-context variable.
static void register_command(AVR_ConsoleCommand cmd)
Registers a new command to the console.
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
static void cprint(string s, GameObject obj)
Print with a caller-context variable.
static void cwarn(string s, string obj)
Print warning message with a caller-context variable.
This attribute can be used to easily make a function into a command runnable through the AVR_DevConso...
static bool get_bool(string token)
Get a registered setting of the type bool.
static void register_command(string name, System.Action< string[]> func, int min_args=0, string desc="No description given.")
Registers a new command to the console.
static void print_all_available_commands()
static void print(string s)
Print a given string on the console.
static void sample_command(string[] args)
static void help_command(string arg)
static void csuccess(string s, GameObject obj)
Print success message with a caller-context variable.
static void cprint(string s, MonoBehaviour obj)
Print with a caller-context variable.
static void cerror(string s, GameObject obj)
Print error message with a caller-context variable.
static void warn(string s)
Print a given warning string on the console.
static void clear()
Clear console output entirely.