Skip to content

Commit

Permalink
🎨 ASF fixs
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzaz committed Nov 14, 2023
1 parent aa63e3a commit a69d97f
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ref/SteamClient
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public bool IsReadPasswordLine

public Version CurrentVersion => SharedInfo.Version;

public void ShellMessageInput(string data)
{
using (var sw = ASFProcess.StandardInput)
{
sw.WriteLine(data);
sw.Flush();
}
}

public async Task<bool> StartAsync(string[]? args = null)
{
try
Expand Down Expand Up @@ -111,16 +120,10 @@ private async Task<bool> StartProcess()
options.ArgumentList.Add(EncryptionKey);
}
ASFProcess = Process.Start(options);
ThreadPool.QueueUserWorkItem(ReadOutPutData);
AppDomain.CurrentDomain.ProcessExit += ExitHandler;
AppDomain.CurrentDomain.UnhandledException += ExitHandler;
ASFProcess!.ErrorDataReceived += new DataReceivedEventHandler(ExitHandler);
ASFProcess.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
OnConsoleWirteLine?.Invoke(e.Data);
});

ASFProcess.BeginOutputReadLine();
ASFProcess.BeginErrorReadLine();
while (!SocketHelper.IsUsePort(CurrentIPCPortValue))
{
Expand Down Expand Up @@ -217,6 +220,64 @@ private void ExitHandler(object? sender, EventArgs eventArgs)
{
ASFService.Current.StopASFAsync().GetAwaiter().GetResult();
}

private async void ReadOutPutData(object? obj)
{
using (StreamReader sr = ASFProcess.StandardOutput)
{
int readResult;
char ch;
var len = string.Empty;
StringBuilder sb = new();
while (true)
{
Task<int> readAsync = Task.Run(sr.Read);

await Task.WhenAny(readAsync, Task.Delay(TimeSpan.FromSeconds(3)));

if (!readAsync.IsCompleted)
{
if (sb.Length > 0)
{
len = sb.ToString();
OnConsoleWirteLine?.Invoke(len);
sb.Clear();
}
}

if ((readResult = await readAsync) != -1)
{
ch = (char)readResult;
sb.Append(ch);

// Note the following common line feed chars:
// \n - UNIX \r\n - DOS \r - Mac
if (ch == '\r' || ch == '\n')
{
readResult = sr.Read();
if (readResult != -1)
{
ch = (char)readResult;
if (ch == '\n')
{
sb.Append(ch);
len = sb.ToString();
OnConsoleWirteLine?.Invoke(len);
sb.Clear();
}
else
{
len = sb.ToString();
OnConsoleWirteLine?.Invoke(len);
sb.Clear();
sb.Append(ch);
}
}
}
}
}
}
}
#endregion

public async Task StopAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public partial interface IArchiSteamFarmService

Version CurrentVersion { get; }

/// <summary>
/// 控制台输入
/// </summary>
/// <param name="data"></param>
void ShellMessageInput(string data);

/// <summary>
/// 启动 ArchiSteamFarm
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public string? ConsoleLogText
set => this.RaiseAndSetIfChanged(ref _ConsoleLogText, value);
}

string? _ConsoleLogInput;

public string? ConsoleLogInput
{
get => _ConsoleLogInput;
set => this.RaiseAndSetIfChanged(ref _ConsoleLogInput, value);
}

public IConsoleBuilder ConsoleLogBuilder { get; } = new ConsoleBuilder();

public SourceCache<Bot, string> SteamBotsSourceList;
Expand Down Expand Up @@ -74,6 +82,15 @@ void OnConsoleWirteLine(string message)
});
}

public void ShellMessageInput()
{
if (string.IsNullOrEmpty(ConsoleLogInput))
{
return;
}
archiSteamFarmService.ShellMessageInput(ConsoleLogInput);
}

/// <summary>
/// 是否正在启动或停止中
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public ArchiSteamFarmPlusPageViewModel()

OpenASFBrowser = ReactiveCommand.Create<string>(OpenBrowser);

ShellMessageInput = ReactiveCommand.Create(ShellMessageInput_Click);

RunOrStop = ReactiveCommand.Create(RunOrStopASF);

AddBot = ReactiveCommand.Create(ShowAddBotWindow);
Expand Down Expand Up @@ -284,6 +286,8 @@ public void OpenBrowser(string? tag)

public async void SetEncryptionKey_Click() => await asfService.SetEncryptionKeyAsync();

public void ShellMessageInput_Click() => ASFService.Current.ShellMessageInput();

public string IPCUrl => asfService.GetIPCUrl();
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public sealed partial class ArchiSteamFarmPlusPageViewModel
/// </summary>
public ICommand OpenASFBrowser { get; }

public ICommand ShellMessageInput { get; }

private bool _IsRedeemKeyDialogOpen;

public bool IsRedeemKeyDialogOpen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,19 @@
CommandParameter="www"
Content="WEBUI 文件夹" />
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="2">
<Label
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="控制台输入" />
<TextBox Width="200" Text="{Binding Source={x:Static s:ASFService.Current}, Path=ConsoleLogInput, Mode=TwoWay}" />
<Button
HorizontalAlignment="Center"
VerticalAlignment="Center"
Command="{Binding ShellMessageInput}"
Content="发送" />
</StackPanel>

<TextBlock Text="{Binding Source={x:Static s:ASFService.Current}, Path=ConsoleLogText, Mode=TwoWay}" />
</StackPanel>
</spp:ContentLoader>
Expand Down

0 comments on commit a69d97f

Please sign in to comment.