1
This commit is contained in:
@@ -13,25 +13,82 @@ using System.Text;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using BITKit.Mod;
|
||||
using UnityEngine.InputSystem.Interactions;
|
||||
using Label = UnityEngine.UIElements.Label;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
// ReSharper disable PossibleMultipleEnumeration
|
||||
|
||||
namespace BITKit.Console
|
||||
{
|
||||
public class BITConsole : MonoBehaviour
|
||||
public class UXConsole:IDisposable
|
||||
{
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
private static void Reload()
|
||||
{
|
||||
Application.logMessageReceivedThreaded += EnqueueLog;
|
||||
}
|
||||
[BITCommand]
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
public static void Console_Exception_Print_StackTrace(int allow)
|
||||
{
|
||||
exceptionPrintStackTrace = allow is 1;
|
||||
_exceptionPrintStackTrace = allow is 1;
|
||||
}
|
||||
private static bool exceptionPrintStackTrace = false;
|
||||
|
||||
private readonly IMainTicker _ticker;
|
||||
public UXConsole(IMainTicker ticker)
|
||||
{
|
||||
_ticker = ticker;
|
||||
_singleton = this;
|
||||
|
||||
_ticker.Add(OnTick);
|
||||
|
||||
InitializeAsync();
|
||||
|
||||
Application.logMessageReceivedThreaded += EnqueueLog;
|
||||
}
|
||||
|
||||
private async void InitializeAsync()
|
||||
{
|
||||
var go = new GameObject("UXConsole");
|
||||
Object.DontDestroyOnLoad(go);
|
||||
var document = go.AddComponent<UIDocument>();
|
||||
document.sortingOrder = 1;
|
||||
try
|
||||
{
|
||||
var panelSettings =await ModService.LoadAsset<PanelSettings>("ux_panel_settings");
|
||||
document.panelSettings = panelSettings;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.Warning<UXService>("未找到ux_panel_settings");
|
||||
throw;
|
||||
}
|
||||
document.visualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>("ux_console");
|
||||
|
||||
_rootVisualElement = document.rootVisualElement;
|
||||
|
||||
UXUtils.Inject(this,_rootVisualElement);
|
||||
|
||||
_commandSelector = new()
|
||||
{
|
||||
Container = _commandContainer,
|
||||
};
|
||||
_commandSelector.OnSelected += x =>
|
||||
{
|
||||
_textField.SetValueWithoutNotify(x.Name);
|
||||
_textField.Blur();
|
||||
_textField.Focus();
|
||||
};
|
||||
_singleton = this;
|
||||
|
||||
_textField.RegisterValueChangedCallback(OnTextFieldValueChanged);
|
||||
_textField.RegisterCallback<KeyDownEvent>(OnKeyDown);
|
||||
|
||||
_text.text = string.Empty;
|
||||
|
||||
Toggle(false);
|
||||
}
|
||||
|
||||
private VisualElement _rootVisualElement;
|
||||
|
||||
private static bool _exceptionPrintStackTrace;
|
||||
private class CommandSelector
|
||||
{
|
||||
public VisualElement Container { get; set; }
|
||||
@@ -100,111 +157,58 @@ namespace BITKit.Console
|
||||
public static async void Clear()
|
||||
{
|
||||
await BITApp.SwitchToMainThread();
|
||||
singleton.outputString.Clear();
|
||||
singleton.text.text = string.Empty;
|
||||
_singleton._outputString.Clear();
|
||||
_singleton._text.text = string.Empty;
|
||||
}
|
||||
private static BITConsole singleton;
|
||||
[SerializeField] private UIDocument document;
|
||||
[SerializeReference] private InputActionReference toggleAction;
|
||||
private static UXConsole _singleton;
|
||||
|
||||
private static ConcurrentQueue<(string condition, string stackTrace, LogType type)> logQueue = new();
|
||||
private static readonly ConcurrentQueue<(string condition, string stackTrace, LogType type)> LOGQueue = new();
|
||||
private static void EnqueueLog(string condition, string stackTrace, LogType type)
|
||||
{
|
||||
logQueue.Enqueue((condition, stackTrace, type));
|
||||
LOGQueue.Enqueue((condition, stackTrace, type));
|
||||
}
|
||||
|
||||
private readonly InputActionGroup _inputActionGroup=new()
|
||||
{
|
||||
allowGlobalActivation = false
|
||||
};
|
||||
public int logLineLimit = 64;
|
||||
private const int LOGLineLimit = 64;
|
||||
|
||||
[UXBindPath("commands-container")]
|
||||
private VisualElement commandContainer;
|
||||
private VisualElement _commandContainer;
|
||||
[UXBindPath("TextField")]
|
||||
private TextField textField;
|
||||
private TextField _textField;
|
||||
[UXBindPath("Text")]
|
||||
private Label text;
|
||||
private Label _text;
|
||||
[UXBindPath( "context-scrollview")]
|
||||
private ScrollView scrollView;
|
||||
private ScrollView _scrollView;
|
||||
private bool _isRunning;
|
||||
private List<string> outputString = new();
|
||||
private List<string> _outputString = new();
|
||||
|
||||
private CommandSelector _commandSelector;
|
||||
private void Start()
|
||||
{
|
||||
UXUtils.Inject(this);
|
||||
|
||||
_commandSelector = new()
|
||||
{
|
||||
Container = commandContainer,
|
||||
};
|
||||
_commandSelector.OnSelected += x =>
|
||||
{
|
||||
textField.SetValueWithoutNotify(x.Name);
|
||||
textField.Blur();
|
||||
textField.Focus();
|
||||
};
|
||||
|
||||
|
||||
singleton = this;
|
||||
|
||||
|
||||
|
||||
textField.RegisterValueChangedCallback(OnTextFieldValueChanged);
|
||||
textField.RegisterCallback<KeyDownEvent>(OnKeyDown);
|
||||
|
||||
|
||||
text.text = string.Empty;
|
||||
|
||||
|
||||
_inputActionGroup.RegisterCallback(toggleAction,Toggle);
|
||||
//_inputActionGroup.RegisterCallback(nextOrPreviousAction, OnNextCommand);
|
||||
|
||||
_inputActionGroup.allowInput.AddElement(this);
|
||||
|
||||
Toggle(false);
|
||||
|
||||
BIT4Log.OnNextLine += OnNextLine;
|
||||
|
||||
destroyCancellationToken.Register(() =>
|
||||
{
|
||||
BIT4Log.OnNextLine -= OnNextLine;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void OnNextLine()
|
||||
{
|
||||
if (outputString.Count is not 0 && outputString.Last() != string.Empty)
|
||||
outputString.Add(string.Empty);
|
||||
if (_outputString.Count is not 0 && _outputString.Last() != string.Empty)
|
||||
_outputString.Add(string.Empty);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_inputActionGroup.allowInput.RemoveElement(this);
|
||||
}
|
||||
public async void Toggle(bool active)
|
||||
private void Toggle(bool active)
|
||||
{
|
||||
_commandSelector.SetMethods(null);
|
||||
|
||||
document.rootVisualElement.SetActive(active);
|
||||
_rootVisualElement.SetActive(active);
|
||||
_isRunning = active;
|
||||
|
||||
BITAppForUnity.AllowCursor.SetElements(this,active);
|
||||
BITInputSystem.AllowInput.SetDisableElements(this,active);
|
||||
|
||||
await UniTask.WaitForEndOfFrame(this);
|
||||
if (active)
|
||||
{
|
||||
textField.SetValueWithoutNotify(string.Empty);
|
||||
_textField.SetValueWithoutNotify(string.Empty);
|
||||
|
||||
textField.Focus();
|
||||
_textField.Focus();
|
||||
|
||||
scrollView.ScrollToBottom();
|
||||
_scrollView.ScrollToBottom();
|
||||
}
|
||||
else
|
||||
{
|
||||
text.Blur();
|
||||
_text.Blur();
|
||||
}
|
||||
}
|
||||
private void OnTextFieldValueChanged(ChangeEvent<string> callback)
|
||||
@@ -215,11 +219,11 @@ namespace BITKit.Console
|
||||
|
||||
_commandSelector.SetMethods(commands);
|
||||
|
||||
commandContainer.SetActive(commands.Length is not 0);
|
||||
_commandContainer.SetActive(commands.Length is not 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
commandContainer.SetActive(false);
|
||||
_commandContainer.SetActive(false);
|
||||
|
||||
_commandSelector.SetMethods(null);
|
||||
}
|
||||
@@ -230,20 +234,17 @@ namespace BITKit.Console
|
||||
switch (keyDownEvent.keyCode)
|
||||
{
|
||||
case KeyCode.Return:
|
||||
var cmd = textField.text;
|
||||
var cmd = _textField.text;
|
||||
|
||||
LogCallback($">{cmd}", string.Empty, LogType.Log);
|
||||
|
||||
textField.SetValueWithoutNotify(string.Empty);
|
||||
_textField.SetValueWithoutNotify(string.Empty);
|
||||
|
||||
await UniTask.NextFrame();
|
||||
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
|
||||
_textField.Blur();
|
||||
|
||||
textField.Blur();
|
||||
|
||||
textField.Focus();
|
||||
_textField.Focus();
|
||||
|
||||
BITCommands.Excute(cmd);
|
||||
|
||||
@@ -252,10 +253,10 @@ namespace BITKit.Console
|
||||
break;
|
||||
case KeyCode.Tab:
|
||||
break;
|
||||
case KeyCode.DownArrow when string.IsNullOrEmpty(textField.text) is false:
|
||||
case KeyCode.DownArrow when string.IsNullOrEmpty(_textField.text) is false:
|
||||
_commandSelector.Index-=1;
|
||||
break;
|
||||
case KeyCode.UpArrow when string.IsNullOrEmpty(textField.text) is false:
|
||||
case KeyCode.UpArrow when string.IsNullOrEmpty(_textField.text) is false:
|
||||
_commandSelector.Index+=1;
|
||||
break;
|
||||
default:
|
||||
@@ -269,16 +270,6 @@ namespace BITKit.Console
|
||||
keyDownEvent.PreventDefault();
|
||||
}
|
||||
}
|
||||
private void Toggle(InputAction.CallbackContext context)
|
||||
{
|
||||
switch (context)
|
||||
{
|
||||
case { interaction: PressInteraction, performed: true }:
|
||||
Toggle(!_isRunning);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async void LogCallback(string condition, string stackTrace, LogType type)
|
||||
{
|
||||
try
|
||||
@@ -286,33 +277,33 @@ namespace BITKit.Console
|
||||
switch (type)
|
||||
{
|
||||
case LogType.Error:
|
||||
outputString.Add($"<color=red>{condition}</color>");
|
||||
_outputString.Add($"<color=red>{condition}</color>");
|
||||
break;
|
||||
case LogType.Warning:
|
||||
outputString.Add($"<color=yellow>{condition}</color>");
|
||||
_outputString.Add($"<color=yellow>{condition}</color>");
|
||||
break;
|
||||
case LogType.Exception:
|
||||
outputString.Add($"<color=red>{condition}</color>");
|
||||
if (exceptionPrintStackTrace)
|
||||
outputString.Add($"<color=red>{stackTrace}</color>");
|
||||
_outputString.Add($"<color=red>{condition}</color>");
|
||||
if (_exceptionPrintStackTrace)
|
||||
_outputString.Add($"<color=red>{stackTrace}</color>");
|
||||
break;
|
||||
default:
|
||||
outputString.Add(condition);
|
||||
_outputString.Add(condition);
|
||||
break;
|
||||
}
|
||||
|
||||
var length = outputString.Count;
|
||||
if (length > logLineLimit)
|
||||
var length = _outputString.Count;
|
||||
if (length > LOGLineLimit)
|
||||
{
|
||||
outputString = outputString.GetRange(length - logLineLimit, logLineLimit);
|
||||
_outputString = _outputString.GetRange(length - LOGLineLimit, LOGLineLimit);
|
||||
}
|
||||
StringBuilder stringBuilder = new();
|
||||
outputString.ForEach(x => stringBuilder.AppendLine(x));
|
||||
_outputString.ForEach(x => stringBuilder.AppendLine(x));
|
||||
try
|
||||
{
|
||||
await BITApp.SwitchToMainThread();
|
||||
scrollView.ScrollToBottomAutomatic();
|
||||
text.text = stringBuilder.ToString();
|
||||
_scrollView.ScrollToBottomAutomatic();
|
||||
_text.text = stringBuilder.ToString();
|
||||
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
@@ -325,25 +316,39 @@ namespace BITKit.Console
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
private void OnTick(float delta)
|
||||
{
|
||||
while (logQueue.TryDequeue(out var log))
|
||||
if (_rootVisualElement is null) return;
|
||||
|
||||
if (Keyboard.current is { backquoteKey: { wasPressedThisFrame: true } })
|
||||
{
|
||||
Toggle(_isRunning=!_isRunning);
|
||||
return;
|
||||
}
|
||||
|
||||
if(LOGQueue.TryDequeue(out var log))
|
||||
{
|
||||
LogCallback(log.condition, log.stackTrace, log.type);
|
||||
}
|
||||
|
||||
if (_isRunning is false) return;
|
||||
|
||||
var pos = textField.worldTransform.GetPosition();
|
||||
var size = textField.layout.size;
|
||||
var pos = _textField.worldTransform.GetPosition();
|
||||
var size = _textField.layout.size;
|
||||
|
||||
commandContainer.style.left = 0;
|
||||
commandContainer.style.top = 0;
|
||||
_commandContainer.style.left = 0;
|
||||
_commandContainer.style.top = 0;
|
||||
|
||||
pos.y += size.y;
|
||||
commandContainer.transform.position = pos;
|
||||
_commandContainer.transform.position = pos;
|
||||
|
||||
commandContainer.style.width = size.x;
|
||||
_commandContainer.style.width = size.x;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_ticker.Remove(OnTick);
|
||||
Application.logMessageReceivedThreaded -= EnqueueLog;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user