CTT/Server/Hotfix/Game/System/Other/ConsoleComponentSystem.cs

182 lines
7.9 KiB
C#

using System;
using System.Threading;
namespace ET
{
public class ConsoleComponentAwakeSystem: StartSystem<ConsoleComponent>
{
public override void Start(ConsoleComponent self)
{
self.Start().Coroutine();
}
}
public static class ConsoleComponentSystem
{
public static async ETVoid Start(this ConsoleComponent self)
{
self.CancellationTokenSource = new CancellationTokenSource();
while (true)
{
try
{
string line = await System.Threading.Tasks.Task.Factory.StartNew(() =>
{
Console.Write($"{self.Mode}> ");
return Console.In.ReadLine();
}, self.CancellationTokenSource.Token);
line = line.Trim();
if (self.Mode != "")
{
bool isExited = true;
switch (self.Mode)
{
case ConsoleMode.Repl:
{
ReplComponent replComponent = self.GetComponent<ReplComponent>();
if (replComponent == null)
{
Console.WriteLine($"no command: {line}!");
break;
}
//string name=null;
//if(line.StartsWith('_'))
//{
// name = line.Substring(1);
// while (true)
// {
// string str = Console.ReadLine();
// if (str.Equals("data"))
// {
// line = $"foreach (var item in ET.UnitComponent.Instance.GetAll()){{if(item.GetComponent<ET.User>().NickName=={name}){{" +
// $"var data = item.GetComponent<ET.PlayerData>();" +
// $"foreach(var value in data.GoodsCapacityDic.Values){{ET.Log.Info(\"CapacityDic:\"+value.ToString());}}" +
// $"ET.Log.Info(\"BattleExpSpeed:\"+data.BattleExpSpeed.ToString());" +
// $"ET.Log.Info(\"IdleExpSpeed:\"+data.IdleExpSpeed.ToString());" +
// $"ET.Log.Info(\"MainStoryRecordId:\"+data.MainStoryRecordId.ToString());" +
// $"}}}}";
// }
// if (str.Equals("character"))
// {
// line = $@"foreach (var item in ET.UnitComponent.Instance.GetAll()){{if(item.GetComponent<ET.User>().NickName=={name}){{
// var character = item.GetComponent<ET.NumericComponent>();
// ET.Log.Info(""Hp:""+ character.GetAsInt(ET.NumericType.Hp).ToString());
// ET.Log.Info(""MaxHp:""+ character.GetAsInt(ET.NumericType.MaxHp).ToString());
// ET.Log.Info(""Mp:""+ character.GetAsInt(ET.NumericType.Mp).ToString());
// ET.Log.Info(""MaxMp:""+ character.GetAsInt(ET.NumericType.MaxMp).ToString());
// }}}}";
// }
// if (str.Equals("exit"))
// {
// line = $"exit";
// }
// try
// {
// isExited = await replComponent.Run(line, this.CancellationTokenSource.Token);
// if (isExited) break;
// }
// catch (Exception e)
// {
// Console.WriteLine(e);
// }
// }
//}
break;
}
}
if (isExited)
{
self.Mode = "";
}
continue;
}
switch (line)
{
case "close":
try
{
await Game.EventSystem.Publish(new EventType.CloseServer());
}
catch (Exception e)
{
Console.WriteLine(e);
}
break;
case "reload":
try
{
Game.EventSystem.Add(DllHelper.GetHotfixAssembly());
Console.WriteLine($"@热更完成了");
}
catch (Exception e)
{
Console.WriteLine(e);
}
break;
case "deluser":
try
{
foreach (Scene scene in Game.Scene.Children.Values)
{
if (scene.SceneType == SceneType.UserCache)
{
try
{
scene.GetComponent<PlayerInfoComponent>().ClearAll();
Console.WriteLine($"@删除缓存");
}
catch (Exception e)
{
Console.WriteLine(e);
}
break;
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
break;
case "repl":
try
{
self.Mode = ConsoleMode.Repl;
self.AddComponent<ReplComponent>();
}
catch (Exception e)
{
Console.WriteLine(e);
}
break;
default:
if (line.StartsWith("cmd"))
{
GMTool.Execute(null, line).Coroutine();
}
else
Console.WriteLine($"no such command: {line}");
break;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
}