Skip to content

Commit

Permalink
feat: 支持使用虚拟终端启动服务器
Browse files Browse the repository at this point in the history
fix: 被禁用的插件仍会调用事件
fix: 部分ANSI控制代码会导致输出异常或程序崩溃
  • Loading branch information
Zaitonn committed Dec 12, 2024
1 parent 80b5d45 commit a24ba39
Show file tree
Hide file tree
Showing 36 changed files with 850 additions and 551 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private IEnumerable<CompletionItem> GetServerCompletionItem()
(kv) => new CompletionItem(kv.Key, getExtendedDescription: (_) => GetDescription(kv))
);

static Task<FormattedString> GetDescription(KeyValuePair<string, Server> kv)
static Task<FormattedString> GetDescription(KeyValuePair<string, ServerBase> kv)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(kv.Value.Configuration.Name);
Expand Down
4 changes: 2 additions & 2 deletions src/Serein.Cli/Services/Interaction/Handlers/ServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void Invoke(IReadOnlyList<string> args)
);
}

if (!_serverManager.Servers.TryGetValue(id, out Server? server))
if (!_serverManager.Servers.TryGetValue(id, out ServerBase? server))
{
throw new InvalidArgumentException("指定的服务器不存在");
}
Expand Down Expand Up @@ -133,7 +133,7 @@ public override void Invoke(IReadOnlyList<string> args)
}
}

private void LogServerInfo(Server server)
private void LogServerInfo(ServerBase server)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"{server.Configuration.Name}({server.Id})");
Expand Down
2 changes: 1 addition & 1 deletion src/Serein.Cli/Services/Interaction/ServerSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void Initialize()

private void LogToConsole(object? sender, ServerOutputEventArgs e)
{
if (sender is not Server server)
if (sender is not ServerBase server)
{
return;
}
Expand Down
37 changes: 16 additions & 21 deletions src/Serein.Core/Models/Plugins/Net/PluginBase.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,29 @@
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Serein.Core.Models.Network.Connection.OneBot.Packets;
using Serein.Core.Services.Servers;
using Serein.Core.Utils.Extensions;

namespace Serein.Core.Models.Plugins.Net;

public abstract partial class PluginBase
{
protected virtual Task<bool> OnServerStarting(Services.Servers.Server server) =>
Task.FromResult(true);
protected virtual Task<bool> OnServerStarting(ServerBase server) => Task.FromResult(true);

protected virtual Task OnServerStarted(Services.Servers.Server server) => Task.CompletedTask;
protected virtual Task OnServerStarted(ServerBase server) => Task.CompletedTask;

protected virtual Task<bool> OnServerStopping(Services.Servers.Server server) =>
Task.FromResult(true);
protected virtual Task<bool> OnServerStopping(ServerBase server) => Task.FromResult(true);

protected virtual Task OnServerExited(
Services.Servers.Server server,
int exitcode,
DateTime exitTime
) => Task.CompletedTask;
protected virtual Task OnServerExited(ServerBase server, int exitcode, DateTime exitTime) =>
Task.CompletedTask;

protected virtual Task<bool> OnServerOutput(Services.Servers.Server server, string line) =>
protected virtual Task<bool> OnServerOutput(ServerBase server, string line) =>
Task.FromResult(true);

protected virtual Task<bool> OnServerRawOutput(Services.Servers.Server server, string line) =>
protected virtual Task<bool> OnServerRawOutput(ServerBase server, string line) =>
Task.FromResult(true);

protected virtual Task OnServerInput(Services.Servers.Server server, string line) =>
Task.CompletedTask;
protected virtual Task OnServerInput(ServerBase server, string line) => Task.CompletedTask;

protected virtual Task OnGroupIncreased() => Task.CompletedTask;

Expand Down Expand Up @@ -62,13 +57,13 @@ internal Task Invoke(Event @event, params object[] args)
switch (@event)
{
case Event.ServerStarted:
return OnServerStarted(args.First().OfType<Services.Servers.Server>());
return OnServerStarted(args.First().OfType<ServerBase>());

case Event.ServerStarting:
return OnServerStarting(args.First().OfType<Services.Servers.Server>());
return OnServerStarting(args.First().OfType<ServerBase>());

case Event.ServerStopping:
return OnServerStopping(args.First().OfType<Services.Servers.Server>());
return OnServerStopping(args.First().OfType<ServerBase>());

case Event.GroupMessageReceived:
return OnGroupMessageReceived(args.First().OfType<MessagePacket>());
Expand All @@ -89,7 +84,7 @@ internal Task Invoke(Event @event, params object[] args)
}

return OnServerOutput(
args.First().OfType<Services.Servers.Server>(),
args.First().OfType<ServerBase>(),
args.Last().OfType<string>()
);

Expand All @@ -100,7 +95,7 @@ internal Task Invoke(Event @event, params object[] args)
}

return OnServerRawOutput(
args.First().OfType<Services.Servers.Server>(),
args.First().OfType<ServerBase>(),
args.Last().OfType<string>()
);

Expand All @@ -111,14 +106,14 @@ internal Task Invoke(Event @event, params object[] args)
}

return OnServerInput(
args.First().OfType<Services.Servers.Server>(),
args.First().OfType<ServerBase>(),
args.Last().OfType<string>()
);

case Event.ServerExited:
if (
args.Length != 3
|| args[0] is not Services.Servers.Server server
|| args[0] is not ServerBase server
|| args[1] is not int code
|| args[2] is not DateTime time
)
Expand Down
8 changes: 7 additions & 1 deletion src/Serein.Core/Models/Server/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Configuration : NotifyPropertyChangedModelBase

public EncodingMap.EncodingType OutputEncoding { get; set; }

public OutputStyle OutputStyle { get; set; }
public OutputStyle OutputStyle { get; set; } = OutputStyle.RawText;

public short PortIPv4 { get; set; } = 19132;

Expand All @@ -34,4 +34,10 @@ public class Configuration : NotifyPropertyChangedModelBase
public bool StartWhenSettingUp { get; set; }

public bool UseUnicodeChars { get; set; }

public bool UsePty { get; set; }

public int? TerminalWidth { get; set; } = 150;

public int? TerminalHeight { get; set; } = 80;
}
2 changes: 1 addition & 1 deletion src/Serein.Core/Models/Server/ServerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Serein.Core.Models.Server;

internal class ServerInfo : NotifyPropertyChangedModelBase, IServerInfo
public class ServerInfo : NotifyPropertyChangedModelBase, IServerInfo
{
public string? FileName { get; internal set; }

Expand Down
10 changes: 4 additions & 6 deletions src/Serein.Core/Models/Server/ServersUpdatedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using Serein.Core.Services.Servers;

namespace Serein.Core.Models.Server;

public class ServersUpdatedEventArgs(
ServersUpdatedType type,
string id,
Services.Servers.Server server
) : EventArgs
public class ServersUpdatedEventArgs(ServersUpdatedType type, string id, ServerBase server)
: EventArgs
{
public string Id { get; } = id;
public ServersUpdatedType Type { get; } = type;
public Services.Servers.Server Server { get; } = server;
public ServerBase Server { get; } = server;
}
1 change: 1 addition & 0 deletions src/Serein.Core/Serein.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageReference Include="NCrontab" Version="3.3.3" />
<PackageReference Include="Octokit" Version="13.0.1" />
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" PrivateAssets="all" />
<PackageReference Include="Quick.PtyNet" Version="1.0.3" />
<PackageReference Include="Sentry.Profiling" Version="4.12.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
<PackageReference Include="WebSocket4Net" Version="0.15.2" />
Expand Down
4 changes: 2 additions & 2 deletions src/Serein.Core/Services/Commands/CommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public string ApplyVariables(
private object? GetServerVariables(string input, string? id = null)
{
var i = input.IndexOf('@');
Server? server;
ServerBase? server;

if (i < 0)
{
Expand All @@ -334,7 +334,7 @@ public string ApplyVariables(

return !_serverManager.Servers.TryGetValue(id, out server) ? null : Switch(key, server);

static object? Switch(string key, Server? server)
static object? Switch(string key, ServerBase? server)
{
return server is null
? null
Expand Down
2 changes: 1 addition & 1 deletion src/Serein.Core/Services/Commands/CommandRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task RunAsync(Command command, CommandContext? commandContext = nul
break;

case CommandType.InputServer:
Server? server = null;
ServerBase? server = null;
if (!string.IsNullOrEmpty(argumentStr))
{
_serverManager.Value.Servers.TryGetValue(argumentStr, out server);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private void OnServersUpdate(object? sender, ServersUpdatedEventArgs e)

private void NotifyOutput(object? sender, ServerOutputEventArgs e)
{
if (sender is not Server server)
if (sender is not ServerBase server)
{
return;
}
Expand Down Expand Up @@ -162,7 +162,7 @@ private void NotifyOutput(object? sender, ServerOutputEventArgs e)

private void NotifyStatusChanged(object? sender, EventArgs e)
{
if (sender is not Server server)
if (sender is not ServerBase server)
{
return;
}
Expand Down
33 changes: 22 additions & 11 deletions src/Serein.Core/Services/Plugins/EventDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ params object[] args
{
foreach ((_, var jsPlugin) in _jsPluginLoader.Plugins)
{
tasks.Add(Task.Run(() => jsPlugin.Invoke(@event, cancellationToken, args)));
if (cancellationToken.IsCancellationRequested)
{
break;
}

if (jsPlugin.IsEnabled)
{
tasks.Add(Task.Run(() => jsPlugin.Invoke(@event, cancellationToken, args)));
}
}
}

Expand All @@ -94,17 +102,20 @@ params object[] args
break;
}

try
{
tasks.Add(plugin.Invoke(@event, args));
}
catch (Exception e)
if (plugin.IsEnabled)
{
_pluginLogger.Log(
LogLevel.Error,
name,
$"触发事件{name}时出现异常:\n{e.GetDetailString()}"
);
try
{
tasks.Add(plugin.Invoke(@event, args));
}
catch (Exception e)
{
_pluginLogger.Log(
LogLevel.Error,
name,
$"触发事件{name}时出现异常:\n{e.GetDetailString()}"
);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal ServerProperty(ServerManager servers)
_serverManager = servers;
}

public Server this[string id] => _serverManager.Servers[id];
public ServerBase this[string id] => _serverManager.Servers[id];

public string[] Ids => _serverManager.Servers.Keys.ToArray();

Expand Down
Loading

0 comments on commit a24ba39

Please sign in to comment.