| | 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.IO; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace GDX.Developer.ConsoleCommands |
| | 11 | | { |
| | 12 | | #if UNITY_2022_2_OR_NEWER |
| | 13 | | public class ExecConsoleCommand : ConsoleCommandBase |
| | 14 | | { |
| | 15 | | string[] m_FilePaths; |
| | 16 | | int m_FilePathsCount; |
| | 17 | |
|
| | 18 | | /// <inheritdoc /> |
| | 19 | | public override bool Evaluate(float deltaTime) |
| 0 | 20 | | { |
| 0 | 21 | | for (int i = 0; i < m_FilePathsCount; i++) |
| 0 | 22 | | { |
| 0 | 23 | | string[] lines = File.ReadAllLines(m_FilePaths[i]); |
| 0 | 24 | | Debug.Log($"Queueing commands found in {m_FilePaths[i]} ..."); |
| 0 | 25 | | int lineCount = lines.Length; |
| 0 | 26 | | for (int j = 0; j < lineCount; j++) |
| 0 | 27 | | { |
| 0 | 28 | | string line = lines[j].Trim(); |
| 0 | 29 | | if (line[0] == '#') // Skip comments |
| 0 | 30 | | { |
| 0 | 31 | | continue; |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | if (!Console.QueueCommand(line)) |
| 0 | 35 | | { |
| 0 | 36 | | Debug.LogWarning( |
| | 37 | | $"An error occured adding command '{line}' at line #{j} in {m_FilePaths[i]}."); |
| 0 | 38 | | } |
| 0 | 39 | | } |
| 0 | 40 | | } |
| | 41 | |
|
| 0 | 42 | | return true; |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <inheritdoc /> |
| | 46 | | public override string GetKeyword() |
| 7 | 47 | | { |
| 7 | 48 | | return "exec"; |
| 7 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <inheritdoc /> |
| | 52 | | public override string GetHelpUsage() |
| 0 | 53 | | { |
| 0 | 54 | | return "exec <absolute path>"; |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <inheritdoc /> |
| | 58 | | public override string GetHelpMessage() |
| 0 | 59 | | { |
| 0 | 60 | | return "Execute an arbitrary script file based on the known commands of the console."; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | /// <inheritdoc /> |
| | 64 | | public override ConsoleCommandBase GetInstance(string context) |
| 0 | 65 | | { |
| 0 | 66 | | string[] files = context.Split(',', StringSplitOptions.RemoveEmptyEntries); |
| 0 | 67 | | int fileCount = files.Length; |
| 0 | 68 | | List<string> foundFiles = new List<string>(fileCount); |
| | 69 | |
|
| 0 | 70 | | for (int i = 0; i < fileCount; i++) |
| 0 | 71 | | { |
| 0 | 72 | | string workingFile = files[i]; |
| 0 | 73 | | if (File.Exists(workingFile)) |
| 0 | 74 | | { |
| 0 | 75 | | foundFiles.Add(workingFile); |
| 0 | 76 | | continue; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | string dataPath = System.IO.Path.Combine(Application.dataPath, workingFile); |
| 0 | 80 | | if (File.Exists(dataPath)) |
| 0 | 81 | | { |
| 0 | 82 | | foundFiles.Add(dataPath); |
| 0 | 83 | | continue; |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | string persistentPath = Path.Combine(Application.persistentDataPath, workingFile); |
| 0 | 87 | | if (File.Exists(persistentPath)) |
| 0 | 88 | | { |
| 0 | 89 | | foundFiles.Add(persistentPath); |
| 0 | 90 | | continue; |
| | 91 | | } |
| | 92 | |
|
| 0 | 93 | | Debug.LogWarning($"Unable to find script to execute @ {workingFile}."); |
| 0 | 94 | | } |
| | 95 | |
|
| 0 | 96 | | if (foundFiles.Count <= 0) |
| 0 | 97 | | { |
| 0 | 98 | | return null; |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | ExecConsoleCommand command = new ExecConsoleCommand |
| | 102 | | { |
| | 103 | | m_FilePaths = foundFiles.ToArray(), m_FilePathsCount = foundFiles.Count |
| | 104 | | }; |
| 0 | 105 | | return command; |
| 0 | 106 | | } |
| | 107 | | } |
| | 108 | | #endif // UNITY_2022_2_OR_NEWER |
| | 109 | | } |