Skip to content

Commit

Permalink
Fixed offering to close processes of hf patch and kk manager itself
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Jan 22, 2020
1 parent 53ea3a3 commit 2b59625
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion KKManager.Core/Util/ProcessTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static bool IsAdministrator()
public static Process FixPermissions(string path)
{
// false for cancel
if (ProcessWaiter.ProcessWaiter.CheckForRunningProcesses(new[] {path}).Result == false)
if (ProcessWaiter.ProcessWaiter.CheckForProcessesBlockingKoiDir().Result == false)
return null;

path = path.Trim(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar, ' ');
Expand Down
11 changes: 6 additions & 5 deletions KKManager.Core/Util/ProcessWaiter/ProcessWaiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Apache License Version 2.0
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

Expand Down Expand Up @@ -56,7 +57,7 @@ public static async Task<bool> ShowDialog(Form owner, int[] processIDs, bool pro
/// <summary>
/// true if user accepted, false if user cancelled, null if no applications were found so dialog was not shown.
/// </summary>
public static async Task<bool?> CheckForRunningProcesses(string[] filters, Form parentForm = null)
public static async Task<bool?> CheckForRunningProcesses(string[] filters, string[] processNameExcludeRegexes, Form parentForm = null)
{
if (!IsAdministrator)
{
Expand All @@ -65,7 +66,7 @@ public static async Task<bool> ShowDialog(Form owner, int[] processIDs, bool pro
}

int[] idsToCheck = { };
await Task.Run(() => idsToCheck = GetRelatedProcessIds(filters));
await Task.Run(() => idsToCheck = GetRelatedProcessIds(filters, processNameExcludeRegexes));

if (idsToCheck.Length == 0) return null;

Expand All @@ -89,23 +90,23 @@ private static bool IsAdministrator
}
}

private static int[] GetRelatedProcessIds(string[] filters)
private static int[] GetRelatedProcessIds(string[] filenamesToSearch, string[] processNameExcludeRegexes)
{
var myProcessId = Process.GetCurrentProcess().Id;
var idsToCheck = new List<int>();
foreach (var pr in Process.GetProcesses())
{
try
{
if (pr.Id == myProcessId || pr.HasExited)
if (pr.Id == myProcessId || pr.HasExited || processNameExcludeRegexes.Any(x => Regex.IsMatch(pr.ProcessName, x)))
continue;

var filenames = pr.Modules.Cast<ProcessModule>()
.Select(x => x.FileName)
.Where(s => !string.IsNullOrEmpty(s))
.Distinct();

if (filenames.Any(filename => filters.Any(filter =>
if (filenames.Any(filename => filenamesToSearch.Any(filter =>
{
if (string.IsNullOrEmpty(filename))
return false;
Expand Down
23 changes: 17 additions & 6 deletions KKManager.Core/Util/ProcessWaiter/ProcessWaiter.designer.cs

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

4 changes: 2 additions & 2 deletions KKManager.Updater/Data/UpdateItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static async Task<FileInfo> GetTempDownloadFilename()
}
catch (IOException ex)
{
if (await ProcessWaiter.CheckForRunningProcesses(new[] { InstallDirectoryHelper.KoikatuDirectory.FullName }) != true)
if (await ProcessWaiter.CheckForProcessesBlockingKoiDir() != true)
throw new IOException($"Failed to create file in directory {tempPath} because of an IO issue - {ex.Message}", ex);

goto retryCreate;
Expand Down Expand Up @@ -89,7 +89,7 @@ public async Task Update(Progress<double> progressCallback, CancellationToken ca
}
catch (IOException ex)
{
if (await ProcessWaiter.CheckForRunningProcesses(new[] { InstallDirectoryHelper.KoikatuDirectory.FullName }) != true)
if (await ProcessWaiter.CheckForProcessesBlockingKoiDir() != true)
throw new IOException($"Failed to apply update {TargetPath.FullName} because of an IO issue - {ex.Message}", ex);

goto retryDelete;
Expand Down
2 changes: 1 addition & 1 deletion KKManager.Updater/Windows/ModUpdateProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private async void ModUpdateProgress_Shown(object sender, EventArgs e)
labelPercent.Text = "";

SetStatus("Preparing...");
if (await ProcessWaiter.CheckForRunningProcesses(new[] { InstallDirectoryHelper.KoikatuDirectory.FullName }) == false)
if (await ProcessWaiter.CheckForProcessesBlockingKoiDir() == false)
throw new OperationCanceledException();

SetStatus("Searching for mod updates...");
Expand Down

0 comments on commit 2b59625

Please sign in to comment.