Skip to content

Commit

Permalink
feat: 支持选择是否强制使用winpty
Browse files Browse the repository at this point in the history
fix: 删除未使用的虚方法
fix: 部分变量无法替换
  • Loading branch information
Zaitonn committed Dec 13, 2024
1 parent 061e05d commit b87acba
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 50 deletions.
10 changes: 2 additions & 8 deletions src/Serein.Core/Models/Plugins/Net/PluginBase.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ protected virtual Task<bool> OnServerRawOutput(ServerBase server, string line) =

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

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

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

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

protected virtual Task<bool> OnGroupMessageReceived(MessagePacket packet) =>
Task.FromResult(true);

Expand All @@ -42,7 +36,7 @@ protected virtual Task<bool> OnPrivateMessageReceived(MessagePacket packet) =>

protected virtual Task<bool> OnWsDataReceived(string data) => Task.FromResult(true);

protected virtual Task<bool> OnPacketReceived(JsonNode packet) => Task.FromResult(true);
protected virtual Task<bool> OnPacketReceived(JsonObject packet) => Task.FromResult(true);

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

Expand Down Expand Up @@ -75,7 +69,7 @@ internal Task Invoke(Event @event, params object[] args)
return OnWsDataReceived(args.First().OfType<string>());

case Event.PacketReceived:
return OnPacketReceived(args.First().OfType<JsonNode>());
return OnPacketReceived(args.First().OfType<JsonObject>());

case Event.ServerOutput:
if (args.Length != 2)
Expand Down
19 changes: 14 additions & 5 deletions src/Serein.Core/Models/Server/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System.Collections.Generic;
using Serein.Core.Utils;

namespace Serein.Core.Models.Server;
Expand All @@ -11,6 +11,8 @@ public class Configuration : NotifyPropertyChangedModelBase

public string Argument { get; set; } = string.Empty;

public Dictionary<string, string> Environment { get; set; } = [];

public bool AutoStopWhenCrashing { get; set; } = true;

public bool AutoRestart { get; set; }
Expand All @@ -19,7 +21,7 @@ public class Configuration : NotifyPropertyChangedModelBase

public bool SaveLog { get; set; }

public string LineTerminator { get; set; } = Environment.NewLine;
public string LineTerminator { get; set; } = System.Environment.NewLine;

public EncodingMap.EncodingType InputEncoding { get; set; }

Expand All @@ -35,9 +37,16 @@ public class Configuration : NotifyPropertyChangedModelBase

public bool UseUnicodeChars { get; set; }

public bool UsePty { get; set; }
public PtyOptions Pty { get; set; } = new();

public sealed class PtyOptions : NotifyPropertyChangedModelBase
{
public bool IsEnabled { get; set; }

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

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

public int? TerminalHeight { get; set; } = 80;
public bool ForceWinPty { get; set; } = true;
}
}
5 changes: 3 additions & 2 deletions src/Serein.Core/Services/Commands/CommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ HardwareInfoProvider hardwareInfoProvider
[GeneratedRegex(@"^\[(?<name>[a-zA-Z]+)(:(?<argument>[\w\-\s]+))?\](?<body>.+)$")]
private static partial Regex GetGeneralCommandRegex();

[GeneratedRegex(@"\{([a-zA-Z]+(\.[a-zA-Z]+)?(@\w+)?)\}")]
[GeneratedRegex(@"\{([a-zA-Z][a-zA-Z0-9\.]+?(@\w+)?)\}")]
private static partial Regex GetVariableRegex();

public static readonly Regex Variable = GetVariableRegex();
Expand Down Expand Up @@ -295,7 +295,6 @@ public string ApplyVariables(

return text;
}
#pragma warning restore IDE0046

private string? GetGameId(long? userId)
{
Expand All @@ -309,6 +308,8 @@ public string ApplyVariables(
: null;
}

#pragma warning restore IDE0046

private object? GetServerVariables(string input, string? id = null)
{
var i = input.IndexOf('@');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal ServerProperty(ServerManager servers)

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

public void Add(string id, Configuration configuration) =>
public ServerBase Add(string id, Configuration configuration) =>
_serverManager.Add(id, configuration);

public bool Remove(string id) => _serverManager.Remove(id);
Expand Down
34 changes: 19 additions & 15 deletions src/Serein.Core/Services/Servers/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,25 @@ ReactionTrigger reactionManager

protected override void StartProcess()
{
_process = Process.Start(
new ProcessStartInfo
{
FileName = Configuration.FileName,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
StandardOutputEncoding = EncodingMap.GetEncoding(Configuration.OutputEncoding),
StandardErrorEncoding = EncodingMap.GetEncoding(Configuration.OutputEncoding),
WorkingDirectory = Path.GetDirectoryName(Configuration.FileName),
Arguments = Configuration.Argument,
}
);
var psi = new ProcessStartInfo
{
FileName = Configuration.FileName,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
StandardOutputEncoding = EncodingMap.GetEncoding(Configuration.OutputEncoding),
StandardErrorEncoding = EncodingMap.GetEncoding(Configuration.OutputEncoding),
WorkingDirectory = Path.GetDirectoryName(Configuration.FileName),
Arguments = Configuration.Argument,
};
foreach (var (key, value) in Configuration.Environment)
{
psi.Environment[key] = value;
}

_process = Process.Start(psi);
_process!.EnableRaisingEvents = true;

_inputWriter = new(_process.StandardInput.BaseStream);
Expand Down
2 changes: 1 addition & 1 deletion src/Serein.Core/Services/Servers/ServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public ServerBase Add(string id, Configuration configuration)
{
ValidateId(id);

ServerBase server = configuration.UsePty
ServerBase server = configuration.Pty.IsEnabled
? new ServerWithPty(
id,
_matcher,
Expand Down
8 changes: 4 additions & 4 deletions src/Serein.Core/Services/Servers/ServerWithPty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ protected override void StartProcess()
Rows =
SereinApp.Type == AppType.Cli
&& Environment.OSVersion.Platform == PlatformID.Win32NT
&& Configuration.TerminalHeight is null
&& Configuration.Pty.TerminalHeight is null
? Console.WindowHeight
: Configuration.TerminalHeight ?? 80,
: Configuration.Pty.TerminalHeight ?? 80,
Cols =
SereinApp.Type == AppType.Cli
&& Environment.OSVersion.Platform == PlatformID.Win32NT
&& Configuration.TerminalHeight is null
&& Configuration.Pty.TerminalHeight is null
? Console.WindowHeight
: Configuration.TerminalWidth ?? 150,
: Configuration.Pty.TerminalWidth ?? 150,
},
_cancellationTokenSource.Token
)
Expand Down
35 changes: 25 additions & 10 deletions src/Serein.Lite/Ui/Servers/ConfigurationEditor.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/Serein.Lite/Ui/Servers/ConfigurationEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private void SyncData()
InputEncondingComboBox.SelectedIndex = (int)_configuration.InputEncoding;
OutputEncondingComboBox.SelectedIndex = (int)_configuration.OutputEncoding;
UseUnicodeCharsCheckBox.Checked = _configuration.UseUnicodeChars;
UsePtyCheckBox.Checked = _configuration.UsePty;
UsePtyCheckBox.Checked = ForceWinPtyCheckBox.Enabled = _configuration.Pty.IsEnabled;
ForceWinPtyCheckBox.Checked = _configuration.Pty.ForceWinPty;
}

private void ConfirmButton_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -95,7 +96,8 @@ private void ConfirmButton_Click(object sender, EventArgs e)
|| string.IsNullOrWhiteSpace(NameTextBox.Text)
? "未命名"
: NameTextBox.Text;
_configuration.UsePty = UsePtyCheckBox.Checked;
_configuration.Pty.IsEnabled = UsePtyCheckBox.Checked;
_configuration.Pty.ForceWinPty = ForceWinPtyCheckBox.Checked;
_configuration.FileName = FileNameTextBox.Text;
_configuration.Argument = ArgumentTextBox.Text;
_configuration.AutoRestart = AutoRestartCheckBox.Checked;
Expand Down Expand Up @@ -158,4 +160,9 @@ private void OpenFileButton_Click(object sender, EventArgs e)
FileNameTextBox.Text = openFileDialog.FileName;
}
}

private void UsePtyCheckBox_CheckedChanged(object sender, EventArgs e)
{
ForceWinPtyCheckBox.Enabled = UsePtyCheckBox.Checked;
}
}
14 changes: 12 additions & 2 deletions src/Serein.Plus/Windows/ServerConfigurationEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@
</CheckBox.ToolTip>
</CheckBox>
<CheckBox
Margin="0,20"
Margin="0,15,0,5"
Content="使用虚拟终端"
IsChecked="{Binding Configuration.UsePty}">
IsChecked="{Binding Configuration.Pty.IsEnabled}">
<CheckBox.ToolTip>
<sys:String xml:space="preserve">使用虚拟终端输入和输出
· 用于解决一些控制台无输入或输出的问题
Expand All @@ -169,6 +169,16 @@
· 这是一个实验性选项,后续版本中可能会发生变化</sys:String>
</CheckBox.ToolTip>
</CheckBox>
<CheckBox
Content="强制使用WinPty"
IsChecked="{Binding Configuration.Pty.ForceWinPty}"
IsEnabled="{Binding Configuration.Pty.IsEnabled}">
<CheckBox.ToolTip>
<sys:String xml:space="preserve">· 仅在Windows平台下生效
· 若不勾选此项,你需要手动补全相应的动态链接库
· 不推荐修改此项,除非你知道你在做什么!</sys:String>
</CheckBox.ToolTip>
</CheckBox>
</StackPanel>
</Grid>
<TextBox ui:ControlHelper.Header="行终止符" Text="{Binding Configuration.LineTerminator, Converter={StaticResource lineTerminatorConverter}}">
Expand Down

0 comments on commit b87acba

Please sign in to comment.