1
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
|
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
using UnityEngine;
|
||||
#endif
|
||||
@@ -15,44 +17,40 @@ namespace BITKit
|
||||
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]
|
||||
[HideInCallstack]
|
||||
#endif
|
||||
public static void Log(object x, ConsoleColor color = ConsoleColor.White)
|
||||
public static void Log(object x)
|
||||
{
|
||||
OnSetConsoleColor?.Invoke(color);
|
||||
OnLog?.Invoke(x?.ToString());
|
||||
OnLogCallback?.Invoke(x?.ToString(),currentType);
|
||||
if (OnLog is null)
|
||||
{
|
||||
BITApp.ServiceProvider.GetRequiredService<ILogger<BITApp>>().LogInformation(x.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
OnLog?.Invoke(x?.ToString());
|
||||
}
|
||||
}
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
[HideInCallstack]
|
||||
#endif
|
||||
public static void Log<T>(object x, ConsoleColor color = ConsoleColor.White)
|
||||
public static void Log<T>(object x)
|
||||
{
|
||||
if (currentType != typeof(T))
|
||||
if (OnLog is null)
|
||||
{
|
||||
OnNextLine?.Invoke();
|
||||
BITApp.ServiceProvider.GetRequiredService<ILogger<T>>().LogInformation(x.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
OnLog?.Invoke(x?.ToString());
|
||||
}
|
||||
#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);
|
||||
}
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
[HideInCallstack]
|
||||
|
@@ -126,7 +126,12 @@ namespace BITKit
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
#if UNITY_EDITOR
|
||||
UnityEngine.Debug.LogException(e);
|
||||
#else
|
||||
BIT4Log.LogException(e);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -553,7 +553,6 @@ namespace BITKit.Net
|
||||
}
|
||||
if ((_now - startTime).TotalSeconds > RpcTimeOut)
|
||||
{
|
||||
await BITApp.SwitchToMainThread();
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
throw new TimeoutException("请求超时或已断开连接");
|
||||
@@ -561,7 +560,6 @@ namespace BITKit.Net
|
||||
}
|
||||
throw new TimeoutException($"请求超时或已断开连接,请求为{path}");
|
||||
}
|
||||
|
||||
|
||||
if (_p2p.TryRemove(id, out var value))
|
||||
{
|
||||
|
@@ -62,7 +62,7 @@ namespace BITKit.Net
|
||||
KCPNet.Config
|
||||
);
|
||||
_timer.Elapsed += Tick;
|
||||
BIT4Log.Log<KCPNetServer>("已创建KCP服务器");
|
||||
//BIT4Log.Log<KCPNetServer>("已创建KCP服务器");
|
||||
|
||||
AddCommandListener<SimplePing>(F);
|
||||
|
||||
|
@@ -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();
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -8,6 +8,7 @@ namespace BITKit.UX
|
||||
/// </summary>
|
||||
public interface IUXService:IDisposable
|
||||
{
|
||||
public string SettingsPath { get; set; }
|
||||
object Root { get; }
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user