Delete renderer-local console authority
This commit is contained in:
parent
38ff448d4c
commit
d678f3210f
9 changed files with 61 additions and 421 deletions
|
|
@ -1,124 +0,0 @@
|
|||
/// <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 System.Text.RegularExpressions;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
public class ConsoleController
|
||||
{
|
||||
public static Component MessageReceiver;
|
||||
private static ConsoleController _instance;
|
||||
private static Regex _permittedCharacters = new Regex("[^a-zA-Z0-9 -]");
|
||||
private static Dictionary<string, Action<string[]>> _commands = new Dictionary<string, Action<string[]>>();
|
||||
|
||||
private ConsoleController()
|
||||
{
|
||||
}
|
||||
|
||||
public static ConsoleController Instance
|
||||
{
|
||||
get => _instance ?? (_instance = new ConsoleController());
|
||||
}
|
||||
|
||||
public static void ClearCommands()
|
||||
{
|
||||
_commands.Clear();
|
||||
}
|
||||
|
||||
public static void AddCommand(string command, Action<string[]> action)
|
||||
{
|
||||
var lower = command.ToLowerInvariant();
|
||||
if(!_commands.ContainsKey(lower))
|
||||
_commands.Add(lower, action);
|
||||
}
|
||||
|
||||
#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>
|
||||
private const int ScrollbackSize = 100;
|
||||
|
||||
private readonly Queue<string> _scrollback = new Queue<string>(ScrollbackSize);
|
||||
private readonly 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) {
|
||||
if (_scrollback.Count >= ScrollbackSize) {
|
||||
_scrollback.Dequeue();
|
||||
}
|
||||
_scrollback.Enqueue(line);
|
||||
|
||||
Log = _scrollback.ToArray();
|
||||
LogChanged?.Invoke(Log);
|
||||
}
|
||||
|
||||
public void RunCommandString(string commandString) {
|
||||
AppendLogLine($"$ {commandString}");
|
||||
|
||||
string[] commandSplit = ParseArguments(commandString);
|
||||
string[] args = {"",""};
|
||||
if (commandSplit.Length < 1) {
|
||||
AppendLogLine($"Unable to process command '{commandString}'");
|
||||
return;
|
||||
|
||||
}
|
||||
if (commandSplit.Length >= 2) {
|
||||
int numArgs = commandSplit.Length - 1;
|
||||
args = new string[numArgs];
|
||||
Array.Copy(commandSplit, 1, args, 0, numArgs);
|
||||
}
|
||||
|
||||
var cmd = commandSplit[0].ToLower();
|
||||
if(_commands.ContainsKey(cmd))
|
||||
_commands[cmd].Invoke(args);
|
||||
else AppendLogLine("Invalid command!");
|
||||
//MessageReceiver.SendMessage(commandSplit[0].ToLower(), args, SendMessageOptions.DontRequireReceiver);
|
||||
_commandHistory.Add(commandString);
|
||||
}
|
||||
|
||||
static string[] ParseArguments(string commandString)
|
||||
{
|
||||
var parmChars = new LinkedList<char>(commandString.ToCharArray());
|
||||
var 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;
|
||||
}
|
||||
var parmCharsArr = new char[parmChars.Count];
|
||||
parmChars.CopyTo(parmCharsArr, 0);
|
||||
var args = new string(parmCharsArr).Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
|
||||
for (var i = 0; i < args.Length; i++)
|
||||
{
|
||||
args[i] = _permittedCharacters.Replace(args[i], "");
|
||||
}
|
||||
return args;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 426b2d2dbea4d4d40b069c1ba2f6efb0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,149 +0,0 @@
|
|||
/// <summary>
|
||||
/// Marshals events and data between ConsoleController and uGUI.
|
||||
/// </summary>
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using TMPro;
|
||||
using UniRx;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class ConsoleView : MonoBehaviour {
|
||||
bool _didShow = false;
|
||||
|
||||
public ActionGameManager GameManager;
|
||||
public GameObject ViewContainer; //Container for console view, should be a child of this GameObject
|
||||
public TextMeshProUGUI LogTextArea;
|
||||
public TextMeshProUGUI InputArea;
|
||||
public ScrollRect Scroll;
|
||||
|
||||
private string _inputString = "";
|
||||
private CursorLockMode _previousCursorLockMode;
|
||||
private bool _playerInputPreviouslyEnabled;
|
||||
|
||||
public string InputString
|
||||
{
|
||||
get => _inputString;
|
||||
set
|
||||
{
|
||||
_inputString = value;
|
||||
InputArea.text = $"> {_inputString}";
|
||||
}
|
||||
}
|
||||
|
||||
//public bool AcceptingInput { get; private set; }
|
||||
|
||||
public bool Visible { get; private set; }
|
||||
|
||||
void Start()
|
||||
{
|
||||
//AcceptingInput = false;
|
||||
//_console = new ConsoleController();
|
||||
Application.logMessageReceived += OnLogCallback;
|
||||
|
||||
SetVisibility(false);
|
||||
|
||||
ConsoleController.Instance.VisibilityChanged += OnVisibilityChanged;
|
||||
ConsoleController.Instance.LogChanged += OnLogChanged;
|
||||
|
||||
Keyboard.current.onTextInput += c =>
|
||||
{
|
||||
if (!Visible || c == '`') return;
|
||||
InputString += c;
|
||||
};
|
||||
InputString = "";
|
||||
|
||||
ConsoleController.AddCommand("echo", Echo);
|
||||
|
||||
// var sizeFitter = GetComponentInChildren<ContentSizeFitter>().on
|
||||
}
|
||||
|
||||
private void OnLogCallback(string condition, string trace, LogType type)
|
||||
{
|
||||
ConsoleController.Instance.AppendLogLine("<color=" + (type == LogType.Error ? "red" : type == LogType.Warning ? "yellow" : "white") + ">" + condition + "</color>");
|
||||
}
|
||||
|
||||
private void OnDestroy() {
|
||||
Application.logMessageReceived -= OnLogCallback;
|
||||
ConsoleController.Instance.VisibilityChanged -= OnVisibilityChanged;
|
||||
ConsoleController.Instance.LogChanged -= OnLogChanged;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
var keyboard = Keyboard.current;
|
||||
//Toggle visibility when tilde key pressed
|
||||
if (keyboard.backquoteKey.wasPressedThisFrame) {
|
||||
ToggleVisibility();
|
||||
}
|
||||
|
||||
if (!Visible) return;
|
||||
|
||||
if(keyboard.deleteKey.wasPressedThisFrame && InputString.Length>0)
|
||||
{
|
||||
InputString = InputString.Substring(0, InputString.Length - 1);
|
||||
}
|
||||
|
||||
if (keyboard.enterKey.wasPressedThisFrame)
|
||||
{
|
||||
if (/*AcceptingInput && */InputString.Length > 0)
|
||||
{
|
||||
//if(_inputString.StartsWith("!") || _inputString.StartsWith("/"))
|
||||
ConsoleController.Instance.RunCommandString(InputString);//.Substring(1));
|
||||
//else ConsoleController.Instance.RunCommandString($"say \"{_inputString}\"");
|
||||
InputString = "";
|
||||
}
|
||||
//AcceptingInput = !AcceptingInput;
|
||||
}
|
||||
}
|
||||
|
||||
void Echo(string[] args)
|
||||
{
|
||||
if(args.Length>0)
|
||||
Debug.Log(args.Aggregate((a,b) => a + " " + b));
|
||||
}
|
||||
|
||||
void ToggleVisibility() {
|
||||
SetVisibility(!ViewContainer.activeSelf);
|
||||
}
|
||||
|
||||
void SetVisibility(bool visible) {
|
||||
ViewContainer.SetActive(visible);
|
||||
Visible = visible;
|
||||
if(visible)
|
||||
{
|
||||
_playerInputPreviouslyEnabled = GameManager.Input.Player.enabled;
|
||||
_previousCursorLockMode = Cursor.lockState;
|
||||
GameManager.Input.Global.Disable();
|
||||
GameManager.DisablePlayerInput();
|
||||
GameManager.Input.UI.Disable();
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_playerInputPreviouslyEnabled)
|
||||
GameManager.EnablePlayerInput();
|
||||
else GameManager.Input.UI.Enable();
|
||||
GameManager.Input.Global.Enable();
|
||||
Cursor.lockState = _previousCursorLockMode;
|
||||
}
|
||||
}
|
||||
|
||||
void OnVisibilityChanged(bool visible) {
|
||||
SetVisibility(visible);
|
||||
}
|
||||
|
||||
void OnLogChanged(string[] newLog) {
|
||||
LogTextArea.text = string.Join("\n", newLog);
|
||||
// if (AcceptingInput)
|
||||
// LogTextArea.text += "\n\n> " + _inputString;
|
||||
Observable.NextFrame().Subscribe(_ =>
|
||||
{
|
||||
if(Scroll)
|
||||
Scroll.verticalNormalizedPosition = 0;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 60214997723e68547b9c784ebda51d9a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue