diff --git a/Src/Core/Applation/BITApp.cs b/Src/Core/Applation/BITApp.cs index 7fcd6f0..9f8d4ac 100644 --- a/Src/Core/Applation/BITApp.cs +++ b/Src/Core/Applation/BITApp.cs @@ -176,12 +176,7 @@ namespace BITKit /// /// 依赖服务提供接口 /// - public static ServiceProvider ServiceProvider - { - get=>_serviceProvider??=ServiceCollection.BuildServiceProvider(); - set => _serviceProvider = value; - } - private static ServiceProvider _serviceProvider; + public static ServiceProvider ServiceProvider { get; set; } /// /// 主线程 /// diff --git a/Src/Core/Applation/BITAppForNet.cs b/Src/Core/Applation/BITAppForNet.cs index 0cd644e..664e211 100644 --- a/Src/Core/Applation/BITAppForNet.cs +++ b/Src/Core/Applation/BITAppForNet.cs @@ -8,19 +8,15 @@ namespace BITKit; public class BITAppForNet { - [Obsolete("Use InitializeAsync instead")] - public static UniTask Init(string name)=>UniTask.CompletedTask; + private static readonly Timer _timer = new(); - private static Timer _timer = new(); - - private static DateTime _startTime = DateTime.UtcNow; + private static readonly DateTime _startTime = DateTime.UtcNow; public static async UniTask InitializeAsync(string name) { BIT4Log.OnLog += Console.WriteLine; BIT4Log.OnWarning += Console.WriteLine; BIT4Log.OnException += e => Console.WriteLine(e.ToString()); - BIT4Log.OnSetConsoleColor += color => Console.ForegroundColor = color; BIT4Log.OnNextLine += Console.WriteLine; diff --git a/Src/Core/BITLog/BIT4Log.cs b/Src/Core/BITLog/BIT4Log.cs index 6934f2a..aa26fe7 100644 --- a/Src/Core/BITLog/BIT4Log.cs +++ b/Src/Core/BITLog/BIT4Log.cs @@ -1,79 +1,99 @@ using System; using System.Diagnostics; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; #if UNITY_5_3_OR_NEWER using UnityEngine; #endif namespace BITKit { - public static class BIT4Log - { - #if UNITY_EDITOR && UNITY_5_3_OR_NEWER - [RuntimeInitializeOnLoadMethod] - private static void Reload() + public static class BIT4Log { - OnLog = null; - OnException = null; - OnWarning = null; - OnSetConsoleColor = null; - OnNextLine = null; - } - #endif - public static event Action OnLog; - public static event Action OnLogCallback; - public static event Action OnException; - public static event Action OnExceptionCallback; - public static event Action OnWarning; - public static event Action OnWarningCallback; - public static event Action OnSetConsoleColor; - public static event Action OnNextLine; - private static Type currentType; -#if UNITY_5_3_OR_NEWER - //[HideInCallstack] +#if UNITY_EDITOR && UNITY_5_3_OR_NEWER + [RuntimeInitializeOnLoadMethod] + private static void Reload() + { + OnLog = null; + OnException = null; + OnWarning = null; + OnNextLine = null; + } #endif - public static void Log(object x, ConsoleColor color = ConsoleColor.White) - { - OnSetConsoleColor?.Invoke(color); - OnLog?.Invoke(x?.ToString()); - OnLogCallback?.Invoke(x?.ToString(),currentType); - } + public static event Action OnLog; + public static event Action OnException; + public static event Action OnWarning; + public static event Action OnNextLine; #if UNITY_5_3_OR_NEWER - [HideInCallstack] + [HideInCallstack] #endif - public static void Log(object x, ConsoleColor color = ConsoleColor.White) - { - if (currentType != typeof(T)) - { - OnNextLine?.Invoke(); - } - #if NET5_0_OR_GREATER - Log($"[{DateTime.Now}]{typeof(T).Name}:{x}"); - #else - Log($"{typeof(T).Name}:{x}"); - #endif - - currentType = typeof(T); - } + public static void Log(object x) + { + if (OnLog is not null) + { + OnLog.Invoke(x?.ToString()); + return; + } #if UNITY_5_3_OR_NEWER - [HideInCallstack] + UnityEngine.Debug.Log(x); #endif - public static void LogException(Exception e) - { - OnException?.Invoke(e); - } + } #if UNITY_5_3_OR_NEWER - [HideInCallstack] + [HideInCallstack] #endif - public static void Warning(object x) - { - OnWarning?.Invoke(x.ToString()); - } + public static void Log(object x) + { + if (OnLog is not null) + { + OnLog.Invoke(x?.ToString()); + return; + } #if UNITY_5_3_OR_NEWER - [HideInCallstack] + UnityEngine.Debug.Log($"{typeof(T).Name}" + x); #endif - public static void Warning(object x) - { - Warning($"{typeof(T).Name}:{x}"); + } +#if UNITY_5_3_OR_NEWER + [HideInCallstack] +#endif + public static void LogException(Exception e) + { + if (OnException is not null) + { + OnException.Invoke(e); + return; + } +#if UNITY_5_3_OR_NEWER + UnityEngine.Debug.LogException(e); +#endif + + } +#if UNITY_5_3_OR_NEWER + [HideInCallstack] +#endif + public static void Warning(object x) + { + if (OnWarning is not null) + { + OnWarning.Invoke(x.ToString()); + return; + } +#if UNITY_5_3_OR_NEWER + UnityEngine.Debug.LogWarning(x); +#endif + } +#if UNITY_5_3_OR_NEWER + [HideInCallstack] +#endif + public static void Warning(object x) + { + if (OnWarning is not null) + { + OnWarning.Invoke(x.ToString()); + return; + } +#if UNITY_5_3_OR_NEWER + UnityEngine.Debug.LogWarning($"{typeof(T).Name}" + x); +#endif + } } - } } diff --git a/Src/Core/Group/ActivableGroup.cs b/Src/Core/Group/ActivableGroup.cs index 368bfe0..e01105b 100644 --- a/Src/Core/Group/ActivableGroup.cs +++ b/Src/Core/Group/ActivableGroup.cs @@ -126,7 +126,12 @@ namespace BITKit } catch (Exception e) { - BIT4Log.LogException(e); + #if UNITY_EDITOR + UnityEngine.Debug.LogException(e); + #else + BIT4Log.LogException(e); + #endif + } } diff --git a/Src/Core/Kcp/KcpNetClient.cs b/Src/Core/Kcp/KcpNetClient.cs index 1663058..c37b468 100644 --- a/Src/Core/Kcp/KcpNetClient.cs +++ b/Src/Core/Kcp/KcpNetClient.cs @@ -17,6 +17,7 @@ using BITKit.Net.Examples; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Unity.Mathematics; +using UnityEngine; namespace BITKit.Net { @@ -132,7 +133,7 @@ namespace BITKit.Net { if (IsConnecting) { - BIT4Log.Warning("正在连接中"); + _logger.LogWarning("正在连接中"); return false; } _userConnected = true; @@ -203,7 +204,7 @@ namespace BITKit.Net } catch (Exception e) { - BIT4Log.LogException(e); + _logger.LogCritical(e,e.Message); if (BITApp.SynchronizationContext is not null) await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext); OnConnectedFailed?.Invoke(); @@ -223,7 +224,7 @@ namespace BITKit.Net } catch (Exception e) { - BIT4Log.LogException(e); + _logger.LogCritical(e,e.Message); } } private async void OnDataInternel(ArraySegment bytes, KcpChannel channel) @@ -255,7 +256,7 @@ namespace BITKit.Net } catch (Exception e) { - BIT4Log.LogException(e); + _logger.LogCritical(e,e.Message); } break; @@ -270,7 +271,11 @@ namespace BITKit.Net Ping = (int)(DateTime.Now - _lastPingTime).TotalMilliseconds; break; case NetCommandType.ReturnToClient: + var id = reader.ReadInt32(); + + + try { if (reader.ReadBoolean()) @@ -281,13 +286,14 @@ namespace BITKit.Net else { var message = reader.ReadString(); + _p2p.TryAdd(id,new Exception(message)); } } catch (Exception e) { - BIT4Log.Warning($"请求返回失败:{id}"); - BIT4Log.LogException(e); + _logger.LogWarning($"请求返回失败:{id}"); + _logger.LogCritical(e,e.Message); } break; case NetCommandType.GetFromClient: @@ -327,9 +333,9 @@ namespace BITKit.Net value = methodInfo.Invoke(handle, pars); } } - catch (Exception e) + catch (Exception) { - BIT4Log.Warning(path); + _logger.LogWarning(path); throw; } @@ -369,7 +375,7 @@ namespace BITKit.Net } catch (Exception e) { - BIT4Log.LogException(e); + _logger.LogCritical(e,e.Message); returnWriter.Write(false); returnWriter.Write(e.Message); } @@ -379,7 +385,7 @@ namespace BITKit.Net } catch(Exception e) { - BIT4Log.LogException(e); + _logger.LogCritical(e,e.Message); } break; case NetCommandType.AllRpc: @@ -419,13 +425,12 @@ namespace BITKit.Net reportBuilder.AppendLine($"参数{parameter.GetType()}类型匹配:{parameterInfo.ParameterType}"); } } - - BIT4Log.Warning(reportBuilder); + _logger.LogWarning(reportBuilder.ToString()); throw; } catch (Exception e) { - BIT4Log.Warning(rpcName); + _logger.LogWarning(rpcName); throw; } @@ -439,6 +444,7 @@ namespace BITKit.Net if (eventDelegate is null) { + //BIT4Log.Warning($"未找到对应的事件:{rpcName}"); } else @@ -451,14 +457,13 @@ namespace BITKit.Net } else { - - - BIT4Log.Warning($"未找到对应的Rpc方法:{rpcName}"); + + _logger.LogWarning($"未找到对应的Rpc方法:{rpcName}"); } } break; default: - _logger.LogInformation($"未知消息类型:{type},字节:{(byte)type}"); + _logger.LogWarning($"未知消息类型:{type},字节:{(byte)type}"); if (bytes.Array != null) _logger.LogInformation( $"已收到:({Id}, {BitConverter.ToString(bytes.Array, bytes.Offset, bytes.Count)} @ {channel})"); @@ -553,7 +558,6 @@ namespace BITKit.Net } if ((_now - startTime).TotalSeconds > RpcTimeOut) { - await BITApp.SwitchToMainThread(); if (string.IsNullOrEmpty(path)) { throw new TimeoutException("请求超时或已断开连接"); @@ -561,7 +565,6 @@ namespace BITKit.Net } throw new TimeoutException($"请求超时或已断开连接,请求为{path}"); } - if (_p2p.TryRemove(id, out var value)) { @@ -696,7 +699,7 @@ namespace BITKit.Net { if (DateTime.Now - _lastHeartbeat > TimeSpan.FromSeconds(5)) { - BIT4Log.Warning("心跳超时,自动断开"); + _logger.LogWarning("心跳超时,自动断开"); DisconnectInternal(); _commandQueue.Clear(); return; @@ -728,7 +731,7 @@ namespace BITKit.Net } catch (Exception e) { - BIT4Log.LogException(e); + _logger.LogCritical(e,e.Message); } } diff --git a/Src/Core/Kcp/KcpNetServer.cs b/Src/Core/Kcp/KcpNetServer.cs index 01c13d0..246938c 100644 --- a/Src/Core/Kcp/KcpNetServer.cs +++ b/Src/Core/Kcp/KcpNetServer.cs @@ -62,7 +62,7 @@ namespace BITKit.Net KCPNet.Config ); _timer.Elapsed += Tick; - BIT4Log.Log("已创建KCP服务器"); + //BIT4Log.Log("已创建KCP服务器"); AddCommandListener(F); diff --git a/Src/Core/Mod/ModService.cs b/Src/Core/Mod/ModService.cs index ec23a06..7aa0f43 100644 --- a/Src/Core/Mod/ModService.cs +++ b/Src/Core/Mod/ModService.cs @@ -130,10 +130,6 @@ namespace BITKit.Mod { public static async UniTask SearchPackages() { - //Todo - IUXWaiting waiting = null; - var handle = waiting?.Get(); - handle?.SetMessage("正在搜索Mod包"); var list=new List(); var path = Path.Combine(Environment.CurrentDirectory, "Mods"); @@ -149,7 +145,6 @@ namespace BITKit.Mod list.Add(package); } - waiting?.Release(handle); return list.ToArray(); } public static async UniTask Reload() @@ -184,16 +179,7 @@ namespace BITKit.Mod public static IMod[] Mods { get; private set; }=Array.Empty(); - public static bool IsLocked - { - get => _IsLocked; - set - { - if (_IsLocked == value) return; - _IsLocked = value; - OnLocked?.Invoke(value); - } - } + public static readonly ValidHandle IsBusy = new(); public static event Action OnPackageLoad; public static event Action OnPackageLoaded; @@ -207,179 +193,72 @@ namespace BITKit.Mod public static event Action OnReload; public static event Action OnReloaded; - - public static event Action OnLocked; public static event Func OnModLoadAsync; public static event Func OnModUnloadAsync; private static CancellationTokenSource _CancellationTokenSource; - private static readonly ConcurrentQueue _RegisterQueue=new(); - private static readonly ConcurrentQueue _UnRegisterQueue = new(); - private static readonly List _CacheMods = new(); private static readonly ConcurrentDictionary _InstalledMods=new(); - private static Thread _Thread; - private static bool _IsRunning; - private static bool _IsLocked; - public static void Initialize() + public static async UniTask Initialize() { BIT4Log.Log("Mod服务已启动"); - _IsRunning = true; _CancellationTokenSource = new CancellationTokenSource(); - _Thread = new Thread(InternalInitialize); - _Thread.Start(); - return; - - async void InternalInitialize() + try { + + + try { - - - - try + var modPath = Path.Combine(Environment.CurrentDirectory, "Mods\\"); + PathHelper.EnsureDirectoryCreated(modPath); + var directoryInfo = new DirectoryInfo(modPath); + foreach (var fileInfo in directoryInfo.GetFiles()) { - var modPath = Path.Combine(Environment.CurrentDirectory, "Mods\\"); - PathHelper.EnsureDirectoryCreated(modPath); - var directoryInfo = new DirectoryInfo(modPath); - foreach (var fileInfo in directoryInfo.GetFiles()) + switch (fileInfo.Extension) { - switch (fileInfo.Extension) + case ".dll": { - case ".dll": - { - var assembly = Assembly.LoadFile(fileInfo.FullName); - await Load(assembly); - continue; - } + var assembly = Assembly.LoadFile(fileInfo.FullName); + await Load(assembly); + continue; + } #if UNITY_5_3_OR_NEWER - case ".cs": - { - var code = await File.ReadAllTextAsync(fileInfo.FullName); - var assembly = BITSharp.Compile(code); - await Load(assembly, fileInfo.DirectoryName); - continue; - } -#endif - } - } - - } - catch (Exception e) - { - BIT4Log.Warning("自动加载Mod失败"); - BIT4Log.LogException(e); - } - - - while (_IsRunning) - { - //todo - IUXWaiting waiting = null; - - _CacheMods.Clear(); - - while (_UnRegisterQueue.TryDequeue(out var mod)) - { - var handle = waiting?.Get(); - handle?.SetMessage($":正在卸载{mod.PackageName}"); - - mod.OnDispose(); - _CacheMods.Add(mod); - OnModUnLoad?.Invoke(mod); - - waiting?.Release(handle); - } - - foreach (var mod in _CacheMods) - { - await mod.OnDisposeAsync(_CancellationTokenSource.Token); - foreach (var x in OnModUnloadAsync.CastAsFunc()) + case ".cs": { - await x.Invoke(mod); + var code = await File.ReadAllTextAsync(fileInfo.FullName); + var assembly = BITSharp.Compile(code); + await Load(assembly, fileInfo.DirectoryName); + continue; } - } - - foreach (var mod in _CacheMods) - { - mod.OnDisposed(); - OnModUnLoaded?.Invoke(mod); - BIT4Log.Log($"卸载Mod:{mod.GetType().FullName}"); - } - - _CacheMods.Clear(); - - while (_RegisterQueue.TryDequeue(out var mod)) - { - var handle = waiting?.Get(); - - handle?.SetMessage($"正在加载:{mod.PackageName}"); - - _CacheMods.Add(mod); - mod.OnInitialize(); - OnModLoad?.Invoke(mod); - BIT4Log.Log($"加载Mod:{mod.GetType().FullName}"); - - waiting?.Release(handle); - } - - foreach (var mod in _CacheMods) - { - var handle = waiting?.Get(); - handle?.SetMessage($"正在初始化:{mod.PackageName}"); - - await mod.OnInitializedAsync(_CancellationTokenSource.Token); - foreach (var x in OnModLoadAsync.CastAsFunc()) - { - await x.Invoke(mod); - } - - waiting?.Release(handle); - } - - foreach (var mod in _CacheMods) - { - var handle = waiting?.Get(); - handle?.SetMessage($":正在完成初始化中{mod.PackageName}"); - - mod.OnInitialized(); - OnModLoaded?.Invoke(mod); - - waiting?.Release(handle); - } - - _CacheMods.Clear(); - - - //Thread.Sleep(1000); -#if UNITY_5_3_OR_NEWER - await UniTask.Delay(1000); -#else - await Task.Delay(1000); #endif - IsLocked = false; + } } - BIT4Log.Log("Mod服务已停止"); } catch (Exception e) { + BIT4Log.Warning("自动加载Mod失败"); BIT4Log.LogException(e); - BIT4Log.Warning("Mod服务遇到了错误,已停止"); } + } + catch (Exception e) + { + BIT4Log.LogException(e); + BIT4Log.Warning("Mod服务遇到了错误,已停止"); + } + + } public static void Dispose() - {_IsRunning = false; + { _CancellationTokenSource.Cancel(); try { - _Thread.Join(100); - _RegisterQueue.Clear(); - _UnRegisterQueue.Clear(); Mods = Array.Empty(); _InstalledMods.Clear(); } @@ -387,8 +266,9 @@ namespace BITKit.Mod { BIT4Log.LogException(e); } - + } + public static UniTask Load(Assembly assembly,string folderPath=null) { BIT4Log.Log($"加载程序集:{assembly.FullName}"); @@ -480,20 +360,35 @@ namespace BITKit.Mod } OnPackageLoaded?.Invoke(package); } - public static void Load(IMod mod) + public static async UniTask Load(IMod mod) { - IsLocked = true; - _RegisterQueue.Enqueue(mod); + mod.OnInitialize(); + OnModLoad?.Invoke(mod); + BIT4Log.Log($"加载Mod:{mod.GetType().FullName}"); } - public static void UnLoad(IMod mod) + public static async UniTask UnLoad(IMod mod) { - IsLocked = true; - _UnRegisterQueue.Enqueue(mod); + mod.OnDispose(); + OnModUnLoad?.Invoke(mod); + + await mod.OnDisposeAsync(_CancellationTokenSource.Token); + foreach (var x in OnModUnloadAsync.CastAsFunc()) + { + await x.Invoke(mod); + } + + mod.OnDisposed(); + OnModUnLoaded?.Invoke(mod); + BIT4Log.Log($"卸载Mod:{mod.GetType().FullName}"); } - public static void Install(IMod mod) + public static async void Install(IMod mod) { + await IsBusy; + + using var _ = IsBusy.GetHandle(); + if (_InstalledMods.ContainsKey(mod.PackageName)) { throw new ArgumentException("Mod已安装"); @@ -501,9 +396,21 @@ namespace BITKit.Mod _InstalledMods.TryAdd(mod.PackageName,mod); Mods = _InstalledMods.Values.ToArray(); OnModInstalled?.Invoke(mod); + + await mod.OnInitializedAsync(_CancellationTokenSource.Token); + foreach (var x in OnModLoadAsync.CastAsFunc()) + { + await x.Invoke(mod); + } + + mod.OnInitialized(); + OnModLoaded?.Invoke(mod); } public static void UnInstall(IMod mod) { + using var _ = IsBusy.GetHandle(); + + if(_InstalledMods.ContainsKey(mod.PackageName) is false) return; _InstalledMods.TryRemove(mod.PackageName); Mods = _InstalledMods.Values.ToArray(); diff --git a/Src/Core/Sharp/BITSharp.cs b/Src/Core/Sharp/BITSharp.cs index 96a70af..3161fa2 100644 --- a/Src/Core/Sharp/BITSharp.cs +++ b/Src/Core/Sharp/BITSharp.cs @@ -66,12 +66,6 @@ namespace BITKit { var outputPath = PathHelper.GetTempFilePath(); - //DI.TryGet(out var waiting); - //todo - IUXWaiting waiting = null; - - var handle = waiting?.Get(); - handle?.SetMessage("正在编译"); try { if (Installed is false) @@ -125,7 +119,7 @@ namespace BITKit // CreateNoWindow = false, // }; - var StartInfo = new ProcessStartInfo + var startInfo = new ProcessStartInfo { //FileName = MCSPath, FileName = "dotnet", @@ -142,7 +136,7 @@ namespace BITKit var process = new Process() { - StartInfo = StartInfo, + StartInfo = startInfo, }; var reportBuilder = new StringBuilder(); process.OutputDataReceived += (sender, args) => @@ -184,14 +178,10 @@ namespace BITKit BIT4Log.Log(reportBuilder); - waiting?.Release(handle); - return Assembly.Load(bytes); } catch (Exception e) { - waiting?.Release(handle); - throw new Exception($"编译失败:{e}"); } diff --git a/Src/Core/Ticker/ITicker.cs b/Src/Core/Ticker/ITicker.cs index f6820aa..aeb78e1 100644 --- a/Src/Core/Ticker/ITicker.cs +++ b/Src/Core/Ticker/ITicker.cs @@ -103,10 +103,11 @@ namespace BITKit if (IsSyncContext) { await BITApp.SwitchToMainThread(); - #if UNITY_EDITOR + await BITApp.SwitchToMainThread(); if (UnityEditor.EditorApplication.isPaused) { + _timer.Interval = 1000d / TickRate; _timer.Start(); return; } @@ -164,12 +165,19 @@ namespace BITKit TickCount++; if(_isDisposed)return; + _timer.Interval = 1000d / TickRate; _timer.Start(); } public bool IsSyncContext { get; set; } = true; public ulong TickCount { get; set; } - public int TickRate { get; set; } + + public int TickRate + { + get => _tickRate; + set => _tickRate = Math.Clamp(value, 1, int.MaxValue); + } + private int _tickRate; public bool IsConcurrent { get; set; } public event Func OnTickAsync; diff --git a/Src/Core/UX/IUXService.cs b/Src/Core/UX/IUXService.cs index 74fcaa9..b2c4724 100644 --- a/Src/Core/UX/IUXService.cs +++ b/Src/Core/UX/IUXService.cs @@ -8,6 +8,7 @@ namespace BITKit.UX /// public interface IUXService:IDisposable { + public string SettingsPath { get; set; } object Root { get; } /// /// 初始化 diff --git a/Src/Core/Utility/ValidHandle.cs b/Src/Core/Utility/ValidHandle.cs index 6703cb8..be4f52c 100644 --- a/Src/Core/Utility/ValidHandle.cs +++ b/Src/Core/Utility/ValidHandle.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using Cysharp.Threading.Tasks; @@ -87,7 +88,7 @@ namespace BITKit public readonly List disableObjs = new List(); private bool tempEnable; private Action EventOnEnableChanged; - private readonly Queue _completionSources = new(); + private readonly ConcurrentQueue _completionSources = new(); private bool _isDisposed; public void AddElement(object obj) diff --git a/Src/Unity/Scripts/Console/BITConsole.cs b/Src/Unity/Scripts/Console/BITConsole.cs index da5ab5b..a375717 100644 --- a/Src/Unity/Scripts/Console/BITConsole.cs +++ b/Src/Unity/Scripts/Console/BITConsole.cs @@ -228,9 +228,24 @@ namespace BITKit.Console _commandSelector.SetMethods(null); } } - private async void OnKeyDown(KeyDownEvent keyDownEvent) + private bool _stopNextFrame; + private void OnKeyDown(KeyDownEvent keyDownEvent) { - var nextStop=true; + if (_stopNextFrame) + { + keyDownEvent.StopPropagation(); + keyDownEvent.PreventDefault(); + _stopNextFrame = false; + } + + + if (keyDownEvent.keyCode is KeyCode.BackQuote) + { + keyDownEvent.StopPropagation(); + keyDownEvent.PreventDefault(); + _stopNextFrame = true; + return; + } switch (keyDownEvent.keyCode) { case KeyCode.Return: @@ -240,35 +255,30 @@ namespace BITKit.Console _textField.SetValueWithoutNotify(string.Empty); - await UniTask.NextFrame(); - - _textField.Blur(); - - _textField.Focus(); - BITCommands.Excute(cmd); _commandSelector.SetMethods(null); + keyDownEvent.StopPropagation(); + keyDownEvent.PreventDefault(); + _stopNextFrame = true; break; case KeyCode.Tab: + keyDownEvent.StopPropagation(); + keyDownEvent.PreventDefault(); break; case KeyCode.DownArrow when string.IsNullOrEmpty(_textField.text) is false: _commandSelector.Index-=1; + keyDownEvent.StopPropagation(); + keyDownEvent.PreventDefault(); break; case KeyCode.UpArrow when string.IsNullOrEmpty(_textField.text) is false: _commandSelector.Index+=1; - break; - default: - nextStop = false; + keyDownEvent.StopPropagation(); + keyDownEvent.PreventDefault(); break; } - if (nextStop) - { - keyDownEvent.StopPropagation(); - keyDownEvent.PreventDefault(); - } } private async void LogCallback(string condition, string stackTrace, LogType type) { diff --git a/Src/Unity/Scripts/LoggerForUnity.cs b/Src/Unity/Scripts/LoggerForUnity.cs index 8b5e567..cd56af4 100644 --- a/Src/Unity/Scripts/LoggerForUnity.cs +++ b/Src/Unity/Scripts/LoggerForUnity.cs @@ -40,6 +40,7 @@ namespace BITKit } public sealed class UnityLogger:ILogger { + [HideInCallstack] public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { switch (logLevel) @@ -56,7 +57,7 @@ namespace BITKit } break; default: - Debug.Log($"{typeof(T).Name}:{state.ToString()}"); + Debug.Log($"{typeof(T).Name}:{state.ToString()}"); break; } } diff --git a/Src/Unity/Scripts/Tick/UnityTickService.cs b/Src/Unity/Scripts/Tick/UnityTickService.cs index 2d4849d..b534c1e 100644 --- a/Src/Unity/Scripts/Tick/UnityTickService.cs +++ b/Src/Unity/Scripts/Tick/UnityTickService.cs @@ -75,7 +75,7 @@ namespace BITKit private void LateUpdate() { - LateUpdateTick?.Invoke(Time.deltaTime); + LateUpdateTick?.Invoke(Time.deltaTime); } } diff --git a/Src/Unity/Scripts/UX/ContextMenu/UXContextMenu.cs b/Src/Unity/Scripts/UX/ContextMenu/UXContextMenu.cs index e754af9..a329cf4 100644 --- a/Src/Unity/Scripts/UX/ContextMenu/UXContextMenu.cs +++ b/Src/Unity/Scripts/UX/ContextMenu/UXContextMenu.cs @@ -5,6 +5,7 @@ using UnityEngine; using UnityEngine.UIElements; using UnityEngine.InputSystem; using BITKit.Mod; +using Cysharp.Threading.Tasks; using Object = UnityEngine.Object; namespace BITKit.UX.Internal @@ -111,7 +112,7 @@ namespace BITKit.UX private readonly IUXService _uxService; private VisualElement _root; private VisualElement _container; - private bool _isInitialized = false; + private readonly UniTaskCompletionSource _waitUntilInitialized = new(); public UXContextMenu(IUXService uxService) { _uxService = uxService; @@ -144,11 +145,14 @@ namespace BITKit.UX { Close(); }); - _isInitialized = true; + + _waitUntilInitialized.TrySetResult(); + Close(); } - public void Create(ContextMenuBuilder builder) + public async void Create(ContextMenuBuilder builder) { + await _waitUntilInitialized.Task; var pos = Mouse.current.position.ReadValue(); pos.y = Screen.height - pos.y; pos = RuntimePanelUtils.ScreenToPanel(_root.panel, pos); @@ -166,9 +170,9 @@ namespace BITKit.UX } builder.OnExecuted += Close; } - private void Close() + private async void Close() { - if(_isInitialized is false)return; + await _waitUntilInitialized.Task; _container.Clear(); _root.SetActive(false); } diff --git a/Src/Unity/Scripts/UX/Library/TabBar.cs b/Src/Unity/Scripts/UX/Library/TabBar.cs index e00a7f6..5605702 100644 --- a/Src/Unity/Scripts/UX/Library/TabBar.cs +++ b/Src/Unity/Scripts/UX/Library/TabBar.cs @@ -106,6 +106,7 @@ namespace BITKit.UX var tabName = split[i]; var index = i; var button = _buttons[i] = this.Create