2023-06-05 19:57:17 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2023-06-29 14:57:11 +08:00
|
|
|
|
using System.IO;
|
2023-06-05 19:57:17 +08:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2023-06-29 14:57:11 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
#if NET5_0_OR_GREATER
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
|
namespace BITKit
|
|
|
|
|
{
|
2023-06-07 18:38:07 +08:00
|
|
|
|
public class AppIsNotPlayingException : Exception
|
|
|
|
|
{
|
|
|
|
|
public override string Message => "Application Is Not Playing";
|
|
|
|
|
}
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public class BITApp
|
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
public static class Time
|
|
|
|
|
{
|
|
|
|
|
public static float DeltaTime { get; internal set; }
|
|
|
|
|
public static double TimeAsDouble { get; internal set; }
|
|
|
|
|
|
|
|
|
|
}
|
2023-06-05 19:57:17 +08:00
|
|
|
|
[System.Serializable]
|
|
|
|
|
public record AppSettings
|
|
|
|
|
{
|
2023-07-02 02:20:40 +08:00
|
|
|
|
public bool AllowInitialize = true;
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public List<string> whiteList = new();
|
2023-06-29 14:57:11 +08:00
|
|
|
|
public List<string> blackList = new()
|
|
|
|
|
{
|
|
|
|
|
"System",
|
|
|
|
|
"Unity",
|
|
|
|
|
"UnityEngine",
|
|
|
|
|
"Microsoft",
|
|
|
|
|
"Godot",
|
|
|
|
|
"Internal",
|
|
|
|
|
"GodotPlugins",
|
|
|
|
|
// ReSharper disable once StringLiteralTypo
|
|
|
|
|
"Cysharp",
|
|
|
|
|
"kcp2k",
|
|
|
|
|
};
|
2023-06-05 19:57:17 +08:00
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
#if NET5_0_OR_GREATER
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 依赖服务集合
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static ServiceCollection ServiceCollection { get; internal set; } = new();
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 依赖服务提供接口
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static ServiceProvider ServiceProvider { get; private set; }
|
2023-06-05 19:57:17 +08:00
|
|
|
|
|
2023-06-29 14:57:11 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 服务创建后的回调
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Action<ServiceProvider> OnServiceProviderBuilded;
|
|
|
|
|
#endif
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 主线程
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static SynchronizationContext SynchronizationContext { get; private set; }
|
|
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
|
[System.Serializable]
|
|
|
|
|
public class OpenPath : IAction, IDisposable
|
|
|
|
|
{
|
|
|
|
|
public string path;
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
path = null;
|
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
public void Execute()
|
2023-06-05 19:57:17 +08:00
|
|
|
|
{
|
|
|
|
|
path = path.Replace(@"/", @"\");
|
|
|
|
|
BIT4Log.Log<OpenPath>($"正在打开文件夹:{path}");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var process = Process.Start("explorer.exe", path);
|
|
|
|
|
SwitchToThisWindow(process.MainWindowHandle, true);
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
BIT4Log.LogException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public class OpenAPP : IAction, IDisposable
|
|
|
|
|
{
|
|
|
|
|
public string path;
|
|
|
|
|
public string WorkingDirectory;
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
path = WorkingDirectory = null;
|
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
public void Execute()
|
2023-06-05 19:57:17 +08:00
|
|
|
|
{
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
ProcessStartInfo startInfo = new(path)
|
|
|
|
|
{
|
|
|
|
|
UseShellExecute = true
|
|
|
|
|
};
|
|
|
|
|
if (string.IsNullOrEmpty(WorkingDirectory) is false)
|
|
|
|
|
{
|
|
|
|
|
startInfo.WorkingDirectory = WorkingDirectory;
|
|
|
|
|
}
|
|
|
|
|
var process = Process.Start(startInfo);
|
|
|
|
|
SwitchToThisWindow(process.MainWindowHandle, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
|
|
|
|
public static extern bool SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
|
|
|
|
|
public static string AppName { get; private set; } = nameof(BITApp);
|
|
|
|
|
public static CancellationTokenSource CancellationTokenSource = new();
|
|
|
|
|
public static InitializationState State;
|
|
|
|
|
public static Assembly[] Assemblies;
|
|
|
|
|
public static AppSettings Settings { get; protected set; }
|
|
|
|
|
|
|
|
|
|
static IEnumerable<MethodInfo> excuteOnAwake;
|
|
|
|
|
static IEnumerable<MethodInfo> excuteOnStart;
|
|
|
|
|
static IEnumerable<MethodInfo> excuteOnStops;
|
|
|
|
|
public static async void Start(string appName = nameof(BITApp),AppSettings settings=default)
|
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
Time.TimeAsDouble = 0;
|
|
|
|
|
Time.DeltaTime = 1 / 60f;
|
|
|
|
|
SynchronizationContext=SynchronizationContext.Current;
|
|
|
|
|
Settings = settings??new AppSettings();
|
|
|
|
|
CancellationTokenSource = new CancellationTokenSource();
|
2023-06-05 19:57:17 +08:00
|
|
|
|
AppName = appName;
|
|
|
|
|
ThreadHelper.LogCurrentThread();
|
2023-07-02 02:20:40 +08:00
|
|
|
|
|
|
|
|
|
if (settings is not null && settings.AllowInitialize)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-05 19:57:17 +08:00
|
|
|
|
await Init();
|
|
|
|
|
}
|
2023-07-02 02:20:40 +08:00
|
|
|
|
private static async Task Init()
|
2023-06-05 19:57:17 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (State is not InitializationState.None)
|
|
|
|
|
{
|
|
|
|
|
BIT4Log.Warnning("警告:上次启动可能未正确退出");
|
|
|
|
|
State = InitializationState.None;
|
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
//加载app-settings.json
|
|
|
|
|
var appSettingsPath = Path.Combine(Environment.CurrentDirectory, "appsettings.json");
|
|
|
|
|
if (PathHelper.TryGetText(appSettingsPath,out var json))
|
|
|
|
|
{
|
|
|
|
|
DataParser.Set(json);
|
|
|
|
|
BIT4Log.Log<BITApp>("已加载全局appsettings");
|
|
|
|
|
}
|
|
|
|
|
//内部反射初始化
|
2023-06-05 19:57:17 +08:00
|
|
|
|
ThreadHelper.InitAPP();
|
|
|
|
|
await UniTask.SwitchToThreadPool();
|
|
|
|
|
ThreadHelper.LogCurrentThread();
|
|
|
|
|
Stopwatch stopwatch = new();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
Stopwatch reflectionHelperWatch = new();
|
|
|
|
|
reflectionHelperWatch.Start();
|
|
|
|
|
ReflectionHelper.Init();
|
|
|
|
|
reflectionHelperWatch.Stop();
|
|
|
|
|
excuteOnAwake = await ReflectionHelper.GetMethods<ExcuteOnAwakeAttribute>();
|
|
|
|
|
excuteOnStart = await ReflectionHelper.GetMethods<ExcuteOnStartAttribute>();
|
|
|
|
|
excuteOnStops = await ReflectionHelper.GetMethods<ExcuteOnStopAttribute>();
|
|
|
|
|
foreach (var x in excuteOnAwake)
|
|
|
|
|
{
|
|
|
|
|
x.Invoke(null, null);
|
|
|
|
|
if (CancellationTokenSource.IsCancellationRequested)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
foreach (var x in excuteOnStart)
|
|
|
|
|
{
|
|
|
|
|
x.Invoke(null, null);
|
|
|
|
|
if (CancellationTokenSource.IsCancellationRequested)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
State = InitializationState.Initialized;
|
|
|
|
|
BIT4Log.Log<BITApp>($"初始化耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
BIT4Log.Log<BITApp>($"已完成初始化");
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
BIT4Log.Warnning<BITApp>($"{nameof(BITApp)}初始化错误:");
|
|
|
|
|
BIT4Log.LogException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public static async Task Stop()
|
|
|
|
|
{
|
|
|
|
|
BIT4Log.Log<BITApp>($"正在停止{nameof(BITApp)}");
|
|
|
|
|
CancellationTokenSource.Cancel();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var x in excuteOnStops)
|
|
|
|
|
{
|
|
|
|
|
x.Invoke(null, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
if (e is not NullReferenceException)
|
|
|
|
|
BIT4Log.LogException(e);
|
|
|
|
|
}
|
|
|
|
|
await UniTask.Yield();
|
|
|
|
|
CancellationTokenSource = default;
|
|
|
|
|
State = InitializationState.None;
|
|
|
|
|
BIT4Log.Log<BITApp>($"已停止{nameof(BITApp)}");
|
|
|
|
|
}
|
|
|
|
|
public static void Run(string path, string WorkingDirectory = "")
|
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
using var open = new OpenAPP() { path = path, WorkingDirectory = WorkingDirectory };
|
|
|
|
|
open.Execute();
|
2023-06-05 19:57:17 +08:00
|
|
|
|
}
|
|
|
|
|
public static void Open(string path)
|
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
using var open = new OpenPath() { path = path };
|
|
|
|
|
open.Execute();
|
|
|
|
|
}
|
|
|
|
|
#if NET5_0_OR_GREATER
|
|
|
|
|
public static void BuildService()
|
|
|
|
|
{
|
|
|
|
|
ServiceProvider = ServiceCollection.BuildServiceProvider();
|
|
|
|
|
OnServiceProviderBuilded?.Invoke(ServiceProvider);
|
2023-06-05 19:57:17 +08:00
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
#endif
|
2023-06-05 19:57:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|