| | 1 | | // Copyright (c) 2020-2022 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Text; |
| | 8 | | using GDX.Collections.Generic; |
| | 9 | | using GDX.Developer.ConsoleCommands; |
| | 10 | | using UnityEditor; |
| | 11 | | using UnityEngine; |
| | 12 | | using UnityEngine.Networking.PlayerConnection; |
| | 13 | |
|
| | 14 | | namespace GDX.Developer |
| | 15 | | { |
| | 16 | | #if UNITY_2022_2_OR_NEWER |
| | 17 | | public static class Console |
| | 18 | | { |
| | 19 | | public enum ConsoleAccessLevel |
| | 20 | | { |
| | 21 | | Anonymous = -1, |
| | 22 | | User = 0, |
| | 23 | | Superuser = 1, |
| | 24 | | Developer = 2 |
| | 25 | | } |
| | 26 | |
|
| 2 | 27 | | static ConsoleAccessLevel s_AccessLevel = ConsoleAccessLevel.Developer; |
| 2 | 28 | | static CircularBuffer<string> s_CommandHistory = new CircularBuffer<string>(50); |
| 2 | 29 | | static readonly Queue<ConsoleCommandBase> k_CommandBuffer = new Queue<ConsoleCommandBase>(50); |
| 2 | 30 | | static StringKeyDictionary<ConsoleCommandBase> |
| | 31 | | s_KnownCommands = new StringKeyDictionary<ConsoleCommandBase>(50); |
| 2 | 32 | | static StringKeyDictionary<ConsoleVariableBase> s_ConsoleVariables = |
| | 33 | | new StringKeyDictionary<ConsoleVariableBase>( |
| | 34 | | 50); |
| | 35 | |
|
| 2 | 36 | | static readonly List<string> k_KnownCommandsList = new List<string>(50); |
| 2 | 37 | | static readonly List<string> k_KnownVariablesList = new List<string>(50); |
| | 38 | | public static ConsoleLog Log; |
| | 39 | |
|
| 2 | 40 | | static int s_HintCacheLength = 0; |
| | 41 | |
|
| 0 | 42 | | public static int PreviousCommandCount => s_CommandHistory.Count; |
| 0 | 43 | | public static int CommandBufferCount => k_CommandBuffer.Count; |
| | 44 | |
|
| | 45 | |
|
| 2 | 46 | | static SimpleWatch s_BufferCountWatch = new SimpleWatch("console.buffer", "Buffered Commands", |
| 0 | 47 | | () => CommandBufferCount.ToString(), false); |
| | 48 | |
|
| | 49 | | public static void RegisterCommand(ConsoleCommandBase command) |
| 139 | 50 | | { |
| 139 | 51 | | string keyword = command.GetKeyword(); |
| 139 | 52 | | if (!s_KnownCommands.ContainsKey(keyword)) |
| 44 | 53 | | { |
| 44 | 54 | | s_KnownCommands.AddWithExpandCheck(keyword, command); |
| 44 | 55 | | k_KnownCommandsList.Add(keyword); |
| 44 | 56 | | s_HintCacheLength++; |
| 44 | 57 | | } |
| 139 | 58 | | } |
| | 59 | |
|
| | 60 | | public static void RegisterVariable(ConsoleVariableBase variable) |
| 0 | 61 | | { |
| 0 | 62 | | string name = variable.GetName(); |
| 0 | 63 | | if (s_ConsoleVariables.ContainsKey(name)) |
| 0 | 64 | | { |
| 0 | 65 | | return; |
| | 66 | | } |
| | 67 | |
|
| 0 | 68 | | s_ConsoleVariables.AddWithExpandCheck(name, variable); |
| 0 | 69 | | k_KnownVariablesList.Add(name); |
| 0 | 70 | | s_HintCacheLength++; |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public static void UnregisterVariable(ConsoleVariableBase variable) |
| 0 | 74 | | { |
| 0 | 75 | | UnregisterVariable(variable.GetName()); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public static void UnregisterVariable(string name) |
| 0 | 79 | | { |
| 0 | 80 | | k_KnownVariablesList.Remove(name); |
| 0 | 81 | | s_ConsoleVariables.TryRemove(name); |
| 0 | 82 | | s_HintCacheLength--; |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public static bool HasVariable(string name) |
| 0 | 86 | | { |
| 0 | 87 | | return k_KnownVariablesList.Contains(name); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public static int GetVariableCount() |
| 0 | 91 | | { |
| 0 | 92 | | return k_KnownVariablesList.Count; |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | public static void UnregisterCommand(ConsoleCommandBase command) |
| 0 | 96 | | { |
| 0 | 97 | | UnregisterCommand(command.GetKeyword()); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | public static void UnregisterCommand(string keyword) |
| 0 | 101 | | { |
| 0 | 102 | | k_KnownCommandsList.Remove(keyword); |
| 0 | 103 | | s_KnownCommands.TryRemove(keyword); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public static ConsoleAccessLevel GetAccessLevel() |
| 0 | 107 | | { |
| 0 | 108 | | return s_AccessLevel; |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | |
|
| | 112 | | static string[] FilterStartsWith(string filter, string[] set) |
| 0 | 113 | | { |
| 0 | 114 | | int count = set.Length; |
| 0 | 115 | | SimpleList<string> possible = new SimpleList<string>(count); |
| 0 | 116 | | for (int i = 0; i < count; i++) |
| 0 | 117 | | { |
| 0 | 118 | | string evalString = set[i]; |
| 0 | 119 | | if (evalString.StartsWith(filter)) |
| 0 | 120 | | { |
| 0 | 121 | | possible.AddUnchecked(evalString); |
| 0 | 122 | | } |
| 0 | 123 | | } |
| 0 | 124 | | possible.Compact(); |
| 0 | 125 | | return possible.Array; |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | static string[] FilterContains(string filter, string[] set) |
| 0 | 129 | | { |
| 0 | 130 | | int count = set.Length; |
| 0 | 131 | | SimpleList<string> possible = new SimpleList<string>(count); |
| 0 | 132 | | for (int i = 0; i < count; i++) |
| 0 | 133 | | { |
| 0 | 134 | | string evalString = set[i]; |
| 0 | 135 | | if (evalString.Contains(filter)) |
| 0 | 136 | | { |
| 0 | 137 | | possible.AddUnchecked(evalString); |
| 0 | 138 | | } |
| 0 | 139 | | } |
| 0 | 140 | | possible.Compact(); |
| 0 | 141 | | return possible.Array; |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | |
|
| | 145 | | public static string[] GetCommandAutoCompleteSuggestions(string hint, string[] existingSet = null) |
| 0 | 146 | | { |
| 0 | 147 | | if (existingSet != null) |
| 0 | 148 | | { |
| 0 | 149 | | return FilterStartsWith(hint, existingSet); |
| | 150 | | } |
| | 151 | |
|
| 0 | 152 | | SimpleList<string> possibleCommands = new SimpleList<string>(s_HintCacheLength); |
| 0 | 153 | | int commandCount = k_KnownCommandsList.Count; |
| 0 | 154 | | for (int i = 0; i < commandCount; i++) |
| 0 | 155 | | { |
| 0 | 156 | | string evalString = k_KnownCommandsList[i]; |
| 0 | 157 | | if (evalString.StartsWith(hint)) |
| 0 | 158 | | { |
| 0 | 159 | | possibleCommands.AddUnchecked(evalString); |
| 0 | 160 | | } |
| 0 | 161 | | } |
| | 162 | |
|
| 0 | 163 | | int variableCount = k_KnownVariablesList.Count; |
| 0 | 164 | | for (int i = 0; i < variableCount; i++) |
| 0 | 165 | | { |
| 0 | 166 | | string evalString = k_KnownVariablesList[i]; |
| 0 | 167 | | if (evalString.StartsWith(hint)) |
| 0 | 168 | | { |
| 0 | 169 | | possibleCommands.AddUnchecked(evalString); |
| 0 | 170 | | } |
| 0 | 171 | | } |
| 0 | 172 | | possibleCommands.Compact(); |
| 0 | 173 | | return possibleCommands.Array; |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | public static ConsoleCommandBase GetCommand(string keyword) |
| 0 | 177 | | { |
| 0 | 178 | | return s_KnownCommands.ContainsKey(keyword) ? s_KnownCommands[keyword] : null; |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | public static ConsoleVariableBase GetVariable(string name) |
| 0 | 182 | | { |
| 0 | 183 | | return s_ConsoleVariables.ContainsKey(name) ? s_ConsoleVariables[name] : null; |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | public static string[] GetCommandKeywordsCopy() |
| 0 | 187 | | { |
| 0 | 188 | | return k_KnownCommandsList.ToArray(); |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | public static string[] GetVariableNamesCopy() |
| 0 | 192 | | { |
| 0 | 193 | | return k_KnownVariablesList.ToArray(); |
| 0 | 194 | | } |
| | 195 | |
|
| | 196 | |
|
| | 197 | |
|
| | 198 | | public static List<string>.Enumerator GetCommandKeywordsEnumerator() |
| 0 | 199 | | { |
| 0 | 200 | | return k_KnownCommandsList.GetEnumerator(); |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | public static string GetPreviousCommand(int offset = 0) |
| 0 | 204 | | { |
| 0 | 205 | | int count = s_CommandHistory.Count; |
| 0 | 206 | | if (count == 0) |
| 0 | 207 | | { |
| 0 | 208 | | return string.Empty; |
| | 209 | | } |
| | 210 | |
|
| 0 | 211 | | if (offset >= count) |
| 0 | 212 | | { |
| 0 | 213 | | return s_CommandHistory[count - 1]; |
| | 214 | | } |
| | 215 | |
|
| 0 | 216 | | return s_CommandHistory[count - 1 - offset]; |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | public static bool QueueCommand(string commandString) |
| 0 | 220 | | { |
| 0 | 221 | | string safeCommand = commandString.Trim(); |
| 0 | 222 | | if (string.IsNullOrEmpty(safeCommand)) |
| 0 | 223 | | { |
| 0 | 224 | | return false; |
| | 225 | | } |
| | 226 | |
|
| | 227 | | // Add to history even if bad |
| 0 | 228 | | s_CommandHistory.Add(safeCommand); |
| | 229 | |
|
| | 230 | | // We do not split passed the command as to ensure the execution cost is passed down to the actual command |
| | 231 | | // if subsequent args need to be processed. |
| 0 | 232 | | string[] split = safeCommand.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries); |
| | 233 | |
|
| | 234 | | // Check if its a valid command |
| 0 | 235 | | ConsoleCommandBase command = GetCommand(split[0]); |
| 0 | 236 | | if (command != null) |
| 0 | 237 | | { |
| 0 | 238 | | if (command.GetAccessLevel() > s_AccessLevel) |
| 0 | 239 | | { |
| 0 | 240 | | Debug.LogWarning($"Invalid Command: {split[0]} - A higher access level is required."); |
| 0 | 241 | | return false; |
| | 242 | | } |
| | 243 | |
|
| 0 | 244 | | ConsoleCommandBase workUnit = command.GetInstance(split.Length > 1 ? split[1].Trim() : null); |
| 0 | 245 | | if (workUnit == null) |
| 0 | 246 | | { |
| 0 | 247 | | Debug.LogWarning($"Invalid Command: {split[0]} - Unable to generate work unit."); |
| 0 | 248 | | return false; |
| | 249 | | } |
| | 250 | |
|
| 0 | 251 | | k_CommandBuffer.Enqueue(workUnit); |
| 0 | 252 | | return true; |
| | 253 | | } |
| | 254 | |
|
| | 255 | | // Check for console variables |
| 0 | 256 | | ConsoleVariableBase variable = GetVariable(split[0]); |
| 0 | 257 | | if (variable != null) |
| 0 | 258 | | { |
| 0 | 259 | | if (split.Length > 1) // Set Value |
| 0 | 260 | | { |
| 0 | 261 | | if (variable.GetAccessLevel() > s_AccessLevel) |
| 0 | 262 | | { |
| 0 | 263 | | Debug.LogWarning($"Unable to set {split[0]} - A higher access level is required."); |
| 0 | 264 | | return false; |
| | 265 | | } |
| | 266 | |
|
| 0 | 267 | | variable.SetValueFromString(split[1].Trim()); |
| 0 | 268 | | Debug.Log($"{variable.GetName()} is now '{variable.GetCurrentValueAsString()}'"); |
| 0 | 269 | | } |
| | 270 | | else // Echo |
| 0 | 271 | | { |
| 0 | 272 | | Debug.Log($"{variable.GetName()} is '{variable.GetCurrentValueAsString()}' [{variable.GetDefaultValu |
| 0 | 273 | | } |
| | 274 | |
|
| 0 | 275 | | return true; |
| | 276 | | } |
| | 277 | |
|
| 0 | 278 | | Debug.LogWarning($"Invalid Command: {split[0]}"); |
| 0 | 279 | | return false; |
| 0 | 280 | | } |
| | 281 | |
|
| | 282 | | public static void SetAccessLevel(ConsoleAccessLevel level) |
| 0 | 283 | | { |
| 0 | 284 | | s_AccessLevel = level; |
| 0 | 285 | | } |
| | 286 | |
|
| | 287 | | public static void Tick(float deltaTime) |
| 4405 | 288 | | { |
| 4405 | 289 | | while (k_CommandBuffer.Count > 0) |
| 0 | 290 | | { |
| 0 | 291 | | ConsoleCommandBase command = k_CommandBuffer.Peek(); |
| | 292 | |
|
| | 293 | | // We evaluate inside of a try-catch to capture the exception and flush the command buffer. |
| | 294 | | try |
| 0 | 295 | | { |
| 0 | 296 | | if (!command.Evaluate(deltaTime)) |
| 0 | 297 | | { |
| 0 | 298 | | break; |
| | 299 | | } |
| 0 | 300 | | } |
| 0 | 301 | | catch (Exception e) |
| 0 | 302 | | { |
| 0 | 303 | | Debug.LogException(e); |
| 0 | 304 | | k_CommandBuffer.Clear(); |
| 0 | 305 | | break; |
| | 306 | | } |
| | 307 | |
|
| 0 | 308 | | k_CommandBuffer.Dequeue(); |
| 0 | 309 | | } |
| 4405 | 310 | | } |
| | 311 | |
|
| | 312 | | #if UNITY_EDITOR |
| | 313 | | [InitializeOnLoadMethod] |
| | 314 | | #endif |
| | 315 | | [RuntimeInitializeOnLoadMethod] |
| | 316 | | static void Initialize() |
| 7 | 317 | | { |
| 7 | 318 | | if (!Config.EnvironmentDeveloperConsole) return; |
| | 319 | |
|
| 7 | 320 | | Log = new ConsoleLog(); |
| | 321 | |
|
| | 322 | | // We preregister a bunch of commands to be optimal |
| 7 | 323 | | RegisterCommand(new HelpConsoleCommand()); |
| 7 | 324 | | RegisterCommand(new ShowConsoleCommand()); |
| 7 | 325 | | RegisterCommand(new HideConsoleCommand()); |
| 7 | 326 | | RegisterCommand(new QuitConsoleCommand()); |
| 7 | 327 | | RegisterCommand(new ExecConsoleCommand()); |
| 7 | 328 | | RegisterCommand(new PlayerLoopConsoleCommand()); |
| 7 | 329 | | RegisterCommand(new VersionConsoleCommand()); |
| 7 | 330 | | RegisterCommand(new WaitConsoleCommand()); |
| 7 | 331 | | RegisterCommand(new SceneListConsoleCommand()); |
| 7 | 332 | | RegisterCommand(new SceneLoadConsoleCommand()); |
| 7 | 333 | | RegisterCommand(new SceneWaitConsoleCommand()); |
| 7 | 334 | | RegisterCommand(new InputKeyPressConsoleCommand()); |
| 7 | 335 | | RegisterCommand(new GarbageCollectionConsoleCommand()); |
| 7 | 336 | | RegisterCommand(new BuildVerificationTestConsoleCommand()); |
| 7 | 337 | | RegisterCommand(new ConsoleVariablesConsoleCommand()); |
| 7 | 338 | | RegisterCommand(new WatchConsoleCommand()); |
| 7 | 339 | | RegisterCommand(new WatchAllConsoleCommand()); |
| 7 | 340 | | RegisterCommand(new WatchListConsoleCommand()); |
| 7 | 341 | | RegisterCommand(new ScreenCaptureConsoleCommand()); |
| | 342 | |
|
| | 343 | | // We are going to look at the arguments |
| 7 | 344 | | if (CommandLineParser.Arguments.ContainsKey("exec")) |
| 0 | 345 | | { |
| | 346 | | // We want to make sure that we have everything loading and ready before we actually execute a script |
| 0 | 347 | | QueueCommand("scene.wait"); |
| 0 | 348 | | QueueCommand($"exec {CommandLineParser.Arguments["exec"]}"); |
| 0 | 349 | | } |
| | 350 | |
|
| | 351 | | // Register for remote connection from editor response |
| 7 | 352 | | PlayerConnection.instance.Register(ConsoleCommandBase.PlayerConnectionGuid, |
| 0 | 353 | | args => QueueCommand(Encoding.UTF8.GetString(args.data))); |
| | 354 | |
|
| 7 | 355 | | ConsoleVariableSettings.UpdateFromFile(); |
| 7 | 356 | | WatchSettings.UpdateFromFile(); |
| 7 | 357 | | } |
| | 358 | | } |
| | 359 | | #endif // UNITY_2022_2_OR_NEWER |
| | 360 | | } |