151 lines
4.8 KiB
C#
151 lines
4.8 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Reflection;
|
|||
|
using Cysharp.Threading.Tasks;
|
|||
|
using System.Text;
|
|||
|
#if UNITY_5_3_OR_NEWER
|
|||
|
using UnityEngine;
|
|||
|
#endif
|
|||
|
namespace BITKit
|
|||
|
{
|
|||
|
public static class BITCommands
|
|||
|
{
|
|||
|
[BITCommand]
|
|||
|
public static void Help()
|
|||
|
{
|
|||
|
|
|||
|
StringBuilder sb = new StringBuilder();
|
|||
|
sb.AppendLine("----------Commands--------");
|
|||
|
methodInfos.ForEach(x =>
|
|||
|
{
|
|||
|
StringBuilder stringBuilder = new();
|
|||
|
StringBuilder samplesBuilder=new StringBuilder();
|
|||
|
stringBuilder.Append(x.Value.Name);
|
|||
|
stringBuilder.Append('(');
|
|||
|
foreach (var parmeter in x.Value.GetParameters())
|
|||
|
{
|
|||
|
stringBuilder.Append($"{parmeter.ParameterType.Name} {parmeter.Name}");
|
|||
|
samplesBuilder.Append(parmeter.Name);
|
|||
|
}
|
|||
|
stringBuilder.Append(");");
|
|||
|
|
|||
|
sb.AppendLine($"{stringBuilder} \t// {x.Value.Name} {samplesBuilder}");
|
|||
|
});
|
|||
|
sb.AppendLine("----------End--------");
|
|||
|
BIT4Log.Log(sb);
|
|||
|
}
|
|||
|
[BITCommand]
|
|||
|
public static void Error(string value)
|
|||
|
{
|
|||
|
BIT4Log.Log(value);
|
|||
|
}
|
|||
|
[BITCommand]
|
|||
|
public static void Warning(string value)
|
|||
|
{
|
|||
|
BIT4Log.Warning(value);
|
|||
|
}
|
|||
|
|
|||
|
[BITCommand]
|
|||
|
public static void Quit()
|
|||
|
{
|
|||
|
BITApp.Stop();
|
|||
|
}
|
|||
|
public static IEnumerable<MethodInfo> GetMethodInfos(string keyWord)
|
|||
|
{
|
|||
|
return methodInfos.Where(x => x.Key.Contains(keyWord)).Select(x => x.Value);
|
|||
|
}
|
|||
|
public static async void Excute(string cmd)
|
|||
|
{
|
|||
|
var cmdSplit = cmd.Split("|");
|
|||
|
if (cmdSplit.Length is 1 or 0)
|
|||
|
{
|
|||
|
cmdSplit = cmd.Split("\n");
|
|||
|
}
|
|||
|
if (cmdSplit.Length > 1)
|
|||
|
{
|
|||
|
foreach (var x in cmdSplit)
|
|||
|
{
|
|||
|
Excute(x);
|
|||
|
}
|
|||
|
return;
|
|||
|
}
|
|||
|
await UniTask.SwitchToThreadPool();
|
|||
|
await TaskHelper.WaitUntil(() => state is InitializationState.Initialized);
|
|||
|
var split = cmd.Split(" ").ToList();
|
|||
|
var args = split.GetRange(1, split.Count - 1);
|
|||
|
cmd = split.First().ToLower();
|
|||
|
if (methodInfos.TryGetValue(cmd, out var methodInfo))
|
|||
|
{
|
|||
|
var parameterInfos = methodInfo.GetParameters();
|
|||
|
var parameters = new List<object>();
|
|||
|
parameterInfos.ForEach((i, x) =>
|
|||
|
{
|
|||
|
var arg = args[i];
|
|||
|
object parameter = arg;
|
|||
|
if (x.ParameterType == typeof(int))
|
|||
|
{
|
|||
|
parameter = int.Parse(arg);
|
|||
|
}
|
|||
|
else if (x.ParameterType == typeof(float))
|
|||
|
{
|
|||
|
parameter = float.Parse(arg);
|
|||
|
}
|
|||
|
parameters.Add(parameter);
|
|||
|
});
|
|||
|
//await UniTask.SwitchToMainThread();
|
|||
|
methodInfo.Invoke(null, parameters.ToArray());
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
BIT4Log.Log($"没有找到命令:{cmd}");
|
|||
|
}
|
|||
|
}
|
|||
|
static Dictionary<string, MethodInfo> methodInfos = new();
|
|||
|
static InitializationState state;
|
|||
|
public static async UniTask InitializeAsync()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
await Init();
|
|||
|
}
|
|||
|
catch (System.Exception e)
|
|||
|
{
|
|||
|
BIT4Log.LogException(e);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
public static void Dispose()
|
|||
|
{
|
|||
|
state = 0;
|
|||
|
methodInfos.Clear();
|
|||
|
}
|
|||
|
private static async UniTask Init()
|
|||
|
{
|
|||
|
state = InitializationState.Initializing;
|
|||
|
await UniTask.SwitchToThreadPool();
|
|||
|
Data.AddListener<string>("Cmd", Excute);
|
|||
|
Data.AddListener<string>("cmd", Excute);
|
|||
|
Data.AddListener<string>("command", Excute);
|
|||
|
Data.AddListener<string>("Command", Excute);
|
|||
|
methodInfos.Clear();
|
|||
|
|
|||
|
var commands = (await ReflectionHelper.GetMethods<BITCommandAttribute>())
|
|||
|
.Where(x => x.IsStatic)
|
|||
|
.Where(x => x.GetCustomAttribute<BITCommandAttribute>() is not null);
|
|||
|
foreach (var command in commands)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
methodInfos.Add(command.Name.ToLower(), command);
|
|||
|
}
|
|||
|
catch (System.Exception e)
|
|||
|
{
|
|||
|
BIT4Log.LogException(e);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
state = InitializationState.Initialized;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|