Skip to content

Commit

Permalink
🎨 ASF 插件 Release 版进程残留问题修复,启动优化,打开文件夹提示优化
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzaz committed Sep 27, 2024
1 parent 2883405 commit 95c10da
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 37 deletions.
18 changes: 18 additions & 0 deletions src/BD.WTTS.Client.Plugins.ArchiSteamFarmPlus/Plugins/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,22 @@ public override void OnAddAutoMapper(IMapperConfigurationExpression cfg)
{
yield return GetConfiguration<ASFSettings_>(directoryExists);
}

public override async ValueTask OnExit()
{
if (ArchiSteamFarmServiceImpl.ASFProcessId.HasValue)
{
try
{
var process = Process.GetProcessById(ArchiSteamFarmServiceImpl.ASFProcessId.Value);
process.Kill();
process.Dispose();
}
catch (ArgumentException)
{
// 进程已经退出
}
}
await base.OnExit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public ArchiSteamFarmServiceImpl()
{
}

public static int? ASFProcessId { get; private set; }

public Process? ASFProcess { get; set; }

public event Action<string>? OnConsoleWirteLine;
Expand Down Expand Up @@ -89,26 +91,48 @@ public async Task ShellMessageInput(string data)
{
var isStartSuccess = false;
var ipcUrl = string.Empty;
try

if (ASFProcess != null || string.IsNullOrEmpty(SharedInfo.ASFExecuteFilePath))
{
if (ASFProcess != null || string.IsNullOrEmpty(SharedInfo.ASFExecuteFilePath))
{
Toast.Show(ToastIcon.Error, BDStrings.ASF_SelectASFExePath);
return (isStartSuccess, ipcUrl);
}
Toast.Show(ToastIcon.Error, BDStrings.ASF_SelectASFExePath);
return (isStartSuccess, ipcUrl);
}

if (ASFSettings.CheckArchiSteamFarmExe && !await CheckFileConsistence()) // 检查文件是否被篡改
{
Toast.Show(ToastIcon.Error, BDStrings.ASF_ExecuteFileUnsafe);
return (isStartSuccess, ipcUrl);
}
if (ASFSettings.CheckArchiSteamFarmExe && !await CheckFileConsistence()) // 检查文件是否被篡改
{
Toast.Show(ToastIcon.Error, BDStrings.ASF_ExecuteFileUnsafe);
return (isStartSuccess, ipcUrl);
}

ipcUrl = GetIPCUrl();
webApiService.SetIPCUrl(ipcUrl); // 设置 IPC 接口地址

KillASFProcess(); // 杀死未关闭的 ASF 进程

ASFProcess = StartAsync(SharedInfo.ASFExecuteFilePath);
ASFProcessId = ASFProcess?.Id;

ipcUrl = GetIPCUrl();
webApiService.SetIPCUrl(ipcUrl); // 设置 IPC 接口地址
ASFService.Current.ConsoleLogText = string.Empty;
Task2.InBackground(ReadOutPutData, true);
AppDomain.CurrentDomain.ProcessExit += ExitHandler;
AppDomain.CurrentDomain.UnhandledException += ExitHandler;

KillASFProcess(); // 杀死未关闭的 ASF 进程
ASFProcess!.ErrorDataReceived += new DataReceivedEventHandler(ExitHandler);
ASFProcess.BeginErrorReadLine();

var options = new ProcessStartInfo(SharedInfo.ASFExecuteFilePath);
using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(60));
while (
!(isStartSuccess = SocketHelper.IsUsePort(CurrentIPCPortValue)) &&
!cancellationTokenSource.IsCancellationRequested)
{
await Task.Delay(1000);
continue;
}
return (isStartSuccess, ipcUrl);

Process? StartAsync(string fileName)
{
var options = new ProcessStartInfo(fileName);
options.CreateNoWindow = true;
options.UseShellExecute = false;
options.RedirectStandardOutput = true;
Expand All @@ -120,26 +144,8 @@ public async Task ShellMessageInput(string data)
options.ArgumentList.Add("--CRYPTKEY");
options.ArgumentList.Add(EncryptionKey);
}
ASFProcess = Process.Start(options);

ASFService.Current.ConsoleLogText = string.Empty;
Task2.InBackground(ReadOutPutData, true);
AppDomain.CurrentDomain.ProcessExit += ExitHandler;
AppDomain.CurrentDomain.UnhandledException += ExitHandler;

ASFProcess!.ErrorDataReceived += new DataReceivedEventHandler(ExitHandler);
ASFProcess.BeginErrorReadLine();
while (!SocketHelper.IsUsePort(CurrentIPCPortValue))
{
Thread.Sleep(1000);
continue;
}
isStartSuccess = true;
return Process.Start(options);
}
catch (Exception)
{
}
return (isStartSuccess, ipcUrl);
}

private void KillASFProcess()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,20 @@ public async void RefreshConfig()

public void OpenFolder(string tag)
{
if (!Enum.TryParse<EPathFolder>(tag, true, out var folderASFPath)) return;
var folderASFPathValue = folderASFPath.GetFolderPath();
if (string.IsNullOrEmpty(ASFSettings.ArchiSteamFarmExePath.Value))
{
Toast.Show(ToastIcon.Error, BDStrings.ASF_SetExePathFirst);
return;
}

string folderASFPathValue = string.Empty;
if (!Enum.TryParse<EPathFolder>(tag, true, out var folderASFPath) ||
!Path.Exists(folderASFPathValue = folderASFPath.GetFolderPath()))
{
Toast.Show(ToastIcon.Error, BDStrings.ASF_SelectASFExePath);
return;
}

IPlatformService.Instance.OpenFolder(folderASFPathValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ internal static string HomeDirectory
{
get
{
return Path.GetDirectoryName(ASFSettingsExecuteFilePath) ?? string.Empty;
return !string.IsNullOrWhiteSpace(ASFSettingsExecuteFilePath)
? Path.GetDirectoryName(ASFSettingsExecuteFilePath) ?? string.Empty
: string.Empty;
}
}

Expand Down

0 comments on commit 95c10da

Please sign in to comment.