This commit is contained in:
CortexCore
2025-01-22 16:50:46 +08:00
28 changed files with 474 additions and 411 deletions

View File

@@ -176,12 +176,7 @@ namespace BITKit
/// <summary>
/// 依赖服务提供接口
/// </summary>
public static ServiceProvider ServiceProvider
{
get=>_serviceProvider??=ServiceCollection.BuildServiceProvider();
set => _serviceProvider = value;
}
private static ServiceProvider _serviceProvider;
public static ServiceProvider ServiceProvider { get; set; }
/// <summary>
/// 主线程
/// </summary>

View File

@@ -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;

View File

@@ -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<string> OnLog;
public static event Action<string,Type> OnLogCallback;
public static event Action<Exception> OnException;
public static event Action<Exception,Type> OnExceptionCallback;
public static event Action<string> OnWarning;
public static event Action<string,Type> OnWarningCallback;
public static event Action<ConsoleColor> 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<string> OnLog;
public static event Action<Exception> OnException;
public static event Action<string> OnWarning;
public static event Action OnNextLine;
#if UNITY_5_3_OR_NEWER
[HideInCallstack]
[HideInCallstack]
#endif
public static void Log<T>(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($"<color=#add8e6ff><b>{typeof(T).Name}</b></color>:{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<T>(object x)
{
if (OnLog is not null)
{
OnLog.Invoke(x?.ToString());
return;
}
#if UNITY_5_3_OR_NEWER
[HideInCallstack]
UnityEngine.Debug.Log($"<b>{typeof(T).Name}</b>" + x);
#endif
public static void Warning<T>(object x)
{
Warning($"<color=#ffa500ff><b>{typeof(T).Name}</b></color>:{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<T>(object x)
{
if (OnWarning is not null)
{
OnWarning.Invoke(x.ToString());
return;
}
#if UNITY_5_3_OR_NEWER
UnityEngine.Debug.LogWarning($"<b>{typeof(T).Name}</b>" + x);
#endif
}
}
}
}

View File

@@ -126,7 +126,12 @@ namespace BITKit
}
catch (Exception e)
{
BIT4Log.LogException(e);
#if UNITY_EDITOR
UnityEngine.Debug.LogException(e);
#else
BIT4Log.LogException(e);
#endif
}
}

View File

@@ -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<KcpNetClient>("正在连接中");
_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<byte> 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<INetClient>($"请求返回失败:{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<KcpNetClient>(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<KcpNetClient>(reportBuilder);
_logger.LogWarning(reportBuilder.ToString());
throw;
}
catch (Exception e)
{
BIT4Log.Warning<KcpNetClient>(rpcName);
_logger.LogWarning(rpcName);
throw;
}
@@ -439,6 +444,7 @@ namespace BITKit.Net
if (eventDelegate is null)
{
//BIT4Log.Warning<KcpNetClient>($"未找到对应的事件:{rpcName}");
}
else
@@ -451,14 +457,13 @@ namespace BITKit.Net
}
else
{
BIT4Log.Warning<KcpNetClient>($"未找到对应的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<KcpNetClient>("心跳超时,自动断开");
_logger.LogWarning("心跳超时,自动断开");
DisconnectInternal();
_commandQueue.Clear();
return;
@@ -728,7 +731,7 @@ namespace BITKit.Net
}
catch (Exception e)
{
BIT4Log.LogException(e);
_logger.LogCritical(e,e.Message);
}
}

View File

@@ -62,7 +62,7 @@ namespace BITKit.Net
KCPNet.Config
);
_timer.Elapsed += Tick;
BIT4Log.Log<KCPNetServer>("已创建KCP服务器");
//BIT4Log.Log<KCPNetServer>("已创建KCP服务器");
AddCommandListener<SimplePing>(F);

View File

@@ -130,10 +130,6 @@ namespace BITKit.Mod
{
public static async UniTask<ModPackage[]> SearchPackages()
{
//Todo
IUXWaiting waiting = null;
var handle = waiting?.Get();
handle?.SetMessage("正在搜索Mod包");
var list=new List<ModPackage>();
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<IMod>();
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<ModPackage> OnPackageLoad;
public static event Action<ModPackage> OnPackageLoaded;
@@ -207,179 +193,72 @@ namespace BITKit.Mod
public static event Action OnReload;
public static event Action OnReloaded;
public static event Action<bool> OnLocked;
public static event Func<IMod,UniTask> OnModLoadAsync;
public static event Func<IMod,UniTask> OnModUnloadAsync;
private static CancellationTokenSource _CancellationTokenSource;
private static readonly ConcurrentQueue<IMod> _RegisterQueue=new();
private static readonly ConcurrentQueue<IMod> _UnRegisterQueue = new();
private static readonly List<IMod> _CacheMods = new();
private static readonly ConcurrentDictionary<string,IMod> _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<ModService>("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<ModService>("自动加载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<ModService>($"卸载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<ModService>($"加载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<ModService>("Mod服务已停止");
}
catch (Exception e)
{
BIT4Log.Warning<ModService>("自动加载Mod失败");
BIT4Log.LogException(e);
BIT4Log.Warning<ModService>("Mod服务遇到了错误,已停止");
}
}
catch (Exception e)
{
BIT4Log.LogException(e);
BIT4Log.Warning<ModService>("Mod服务遇到了错误,已停止");
}
}
public static void Dispose()
{_IsRunning = false;
{
_CancellationTokenSource.Cancel();
try
{
_Thread.Join(100);
_RegisterQueue.Clear();
_UnRegisterQueue.Clear();
Mods = Array.Empty<IMod>();
_InstalledMods.Clear();
}
@@ -387,8 +266,9 @@ namespace BITKit.Mod
{
BIT4Log.LogException(e);
}
}
public static UniTask Load(Assembly assembly,string folderPath=null)
{
BIT4Log.Log<ModService>($"加载程序集:{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<ModService>($"加载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<ModService>($"卸载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();

View File

@@ -66,12 +66,6 @@ namespace BITKit
{
var outputPath = PathHelper.GetTempFilePath();
//DI.TryGet<IUXWaiting>(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<BITSharp>(reportBuilder);
waiting?.Release(handle);
return Assembly.Load(bytes);
}
catch (Exception e)
{
waiting?.Release(handle);
throw new Exception($"编译失败:{e}");
}

View File

@@ -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<float, UniTask> OnTickAsync;

View File

@@ -8,6 +8,7 @@ namespace BITKit.UX
/// </summary>
public interface IUXService:IDisposable
{
public string SettingsPath { get; set; }
object Root { get; }
/// <summary>
/// 初始化

View File

@@ -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<object> disableObjs = new List<object>();
private bool tempEnable;
private Action<bool> EventOnEnableChanged;
private readonly Queue<UniTaskCompletionSource> _completionSources = new();
private readonly ConcurrentQueue<UniTaskCompletionSource> _completionSources = new();
private bool _isDisposed;
public void AddElement(object obj)