Added Console
Debug commands can now be executed using an in-game console
This commit is contained in:
parent
b589a6422c
commit
75eb534983
305 changed files with 2975 additions and 17480 deletions
110
Assets/Scripts/ConsoleController.cs
Normal file
110
Assets/Scripts/ConsoleController.cs
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/// <summary>
|
||||
/// Handles parsing and execution of console commands, as well as collecting log output.
|
||||
/// </summary>
|
||||
using UnityEngine;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
public class ConsoleController
|
||||
{
|
||||
private static ConsoleController instance;
|
||||
|
||||
private ConsoleController()
|
||||
{
|
||||
}
|
||||
|
||||
public static ConsoleController Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new ConsoleController();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
#region Event declarations
|
||||
// Used to communicate with ConsoleView
|
||||
public delegate void LogChangedHandler(string[] log);
|
||||
public event LogChangedHandler LogChanged;
|
||||
|
||||
public delegate void VisibilityChangedHandler(bool visible);
|
||||
public event VisibilityChangedHandler VisibilityChanged;
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// How many log lines should be retained?
|
||||
/// Note that strings submitted to appendLogLine with embedded newlines will be counted as a single line.
|
||||
/// </summary>
|
||||
const int ScrollbackSize = 100;
|
||||
|
||||
Queue<string> _scrollback = new Queue<string>(ScrollbackSize);
|
||||
List<string> _commandHistory = new List<string>();
|
||||
|
||||
public string[] Log { get; private set; } //Copy of scrollback as an array for easier use by ConsoleView
|
||||
|
||||
public void AppendLogLine(string line) {
|
||||
//Debug.Log(line);
|
||||
|
||||
if (_scrollback.Count >= ConsoleController.ScrollbackSize) {
|
||||
_scrollback.Dequeue();
|
||||
}
|
||||
_scrollback.Enqueue(line);
|
||||
|
||||
Log = _scrollback.ToArray();
|
||||
if (LogChanged != null) {
|
||||
LogChanged(Log);
|
||||
}
|
||||
}
|
||||
|
||||
public void RunCommandString(string commandString) {
|
||||
AppendLogLine("$ " + commandString);
|
||||
|
||||
string[] commandSplit = ParseArguments(commandString);
|
||||
string[] args = {"",""};
|
||||
if (commandSplit.Length < 1) {
|
||||
AppendLogLine(string.Format("Unable to process command '{0}'", commandString));
|
||||
return;
|
||||
|
||||
}
|
||||
if (commandSplit.Length >= 2) {
|
||||
int numArgs = commandSplit.Length - 1;
|
||||
args = new string[numArgs];
|
||||
Array.Copy(commandSplit, 1, args, 0, numArgs);
|
||||
}
|
||||
foreach(var go in Object.FindObjectsOfType<Transform>().Where(t=>t.parent==null))
|
||||
go.gameObject.BroadcastMessageExt<MonoBehaviour>(commandSplit[0].ToLower(), args, SendMessageOptions.DontRequireReceiver);
|
||||
_commandHistory.Add(commandString);
|
||||
}
|
||||
|
||||
static string[] ParseArguments(string commandString)
|
||||
{
|
||||
LinkedList<char> parmChars = new LinkedList<char>(commandString.ToCharArray());
|
||||
bool inQuote = false;
|
||||
var node = parmChars.First;
|
||||
while (node != null)
|
||||
{
|
||||
var next = node.Next;
|
||||
if (node.Value == '"') {
|
||||
inQuote = !inQuote;
|
||||
parmChars.Remove(node);
|
||||
}
|
||||
if (!inQuote && node.Value == ' ') {
|
||||
node.Value = '\n';
|
||||
}
|
||||
node = next;
|
||||
}
|
||||
char[] parmCharsArr = new char[parmChars.Count];
|
||||
parmChars.CopyTo(parmCharsArr, 0);
|
||||
return (new string(parmCharsArr)).Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue