-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleManager.cs
More file actions
161 lines (136 loc) · 5.5 KB
/
ConsoleManager.cs
File metadata and controls
161 lines (136 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace Steelbox.Console
{
public class ConsoleCommandMeta
{
public string Description;
public Func<string[], string> Handler;
}
public enum ConsoleLogType
{
User,
System,
Warning,
Error
}
public class ConsoleManager : MonoBehaviour
{
public static ConsoleManager Instance;
public static Action<string, ConsoleLogType> OnLogToConsole;
public static Action OnClear;
public bool LogUnityDebugs { get; set; }
public IEnumerable<string> CommandKeys => _commandMap.Keys;
public IReadOnlyDictionary<string, ConsoleCommandMeta> CommandMetadata => _commandMap;
private Dictionary<string, ConsoleCommandMeta> _commandMap;
private void Awake()
{
transform.SetParent(null);
DontDestroyOnLoad(this);
Instance = this;
Application.logMessageReceived += OnUnityLog;
GetCommands();
}
private void OnDestroy()
{
Application.logMessageReceived -= OnUnityLog;
}
public void ExecuteCommand(string commandName, string[] args)
{
string loweredCommandName = commandName.ToLower();
if (loweredCommandName == "clear")
{
OnClear?.Invoke();
return;
}
LogToConsole(commandName + " " + string.Join(" ", args), ConsoleLogType.User);
if (_commandMap.TryGetValue(loweredCommandName, out var meta))
{
string result = meta.Handler.Invoke(args);
if (!string.IsNullOrEmpty(result))
LogToConsole(result);
}
else
{
LogToConsole($"Command '{loweredCommandName}' does not exist.");
}
}
public void Log(string log, ConsoleLogType logType = ConsoleLogType.System) => LogToConsole(log, logType);
private void LogToConsole(string log, ConsoleLogType logType = ConsoleLogType.System)
{
OnLogToConsole?.Invoke(log, logType);
}
private void OnUnityLog(string condition, string stackTrace, LogType type)
{
if (!LogUnityDebugs) return;
ConsoleLogType logType = type switch
{
LogType.Log => ConsoleLogType.System,
LogType.Warning => ConsoleLogType.Warning,
LogType.Error => ConsoleLogType.Error,
LogType.Exception => ConsoleLogType.Error,
LogType.Assert => ConsoleLogType.Warning,
_ => ConsoleLogType.System
};
string formatted = condition;
if (type is LogType.Exception or LogType.Error)
{
formatted += $"\n{stackTrace}";
}
LogToConsole(formatted, logType);
}
private void GetCommands()
{
_commandMap = new Dictionary<string, ConsoleCommandMeta>();
var methodInfos = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => type.IsClass)
.SelectMany(type => type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
.Where(method =>
method.GetCustomAttributes<ConsoleCommand>().Any() &&
method.GetParameters().Length == 1 &&
method.GetParameters()[0].ParameterType == typeof(string[]));
foreach (var method in methodInfos)
{
var commandAttrs = method.GetCustomAttributes<ConsoleCommand>();
foreach (var attr in commandAttrs)
{
string key = attr.Command.ToLower();
if (_commandMap.ContainsKey(key))
{
Debug.LogWarning($"[Console] Duplicate command '{key}' found in {method.DeclaringType?.FullName}.{method.Name}");
continue;
}
Delegate del;
if (method.ReturnType == typeof(string))
{
del = Delegate.CreateDelegate(typeof(Func<string[], string>), method);
_commandMap[key] = new ConsoleCommandMeta
{
Description = attr.Description,
Handler = (Func<string[], string>)del
};
}
else if (method.ReturnType == typeof(void))
{
del = Delegate.CreateDelegate(typeof(Action<string[]>), method);
_commandMap[key] = new ConsoleCommandMeta
{
Description = attr.Description,
Handler = args =>
{
((Action<string[]>)del)(args);
return string.Empty;
}
};
}
Debug.Log($"[Console] Registered command: '{key}' → {method.DeclaringType?.Name}.{method.Name}");
}
}
}
}
}