Skip to content

Commit

Permalink
Fix deletion of unmanaged files
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jul 23, 2023
1 parent a8e3ce3 commit 2ad7981
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Core/GameInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,9 @@ public IEnumerable<string> UnmanagedFiles(Registry registry)
/// <returns>true if any descendants of given path were installed by CKAN, false otherwise</returns>
public bool HasManagedFiles(Registry registry, string absPath)
=> registry.FileOwner(ToRelativeGameDir(absPath)) != null
|| Directory.EnumerateFileSystemEntries(absPath, "*", SearchOption.AllDirectories)
.Any(f => registry.FileOwner(ToRelativeGameDir(f)) != null);
|| (Directory.Exists(absPath)
&& Directory.EnumerateFileSystemEntries(absPath, "*", SearchOption.AllDirectories)
.Any(f => registry.FileOwner(ToRelativeGameDir(f)) != null));

public override string ToString()
=> string.Format(Properties.Resources.GameInstanceToString, game.ShortName, gameDir);
Expand Down
25 changes: 19 additions & 6 deletions GUI/Controls/UnmanagedFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.IO;
using System.Threading.Tasks;

using log4net;

using CKAN.Games;

namespace CKAN.GUI
Expand All @@ -19,9 +21,10 @@ public UnmanagedFiles()
GameFolderTree.TreeViewNodeSorter = new DirsFirstSorter();
}

public void LoadFiles(GameInstance inst)
public void LoadFiles(GameInstance inst, IUser user)
{
this.inst = inst;
this.user = user;
this.registry = RegistryManager.Instance(inst).registry;
Util.Invoke(this, _UpdateGameFolderTree);
}
Expand Down Expand Up @@ -151,6 +154,7 @@ private void DeleteButton_Click(object sender, EventArgs e)
{
var relPath = GameFolderTree.SelectedNode?.Name;
var absPath = inst.ToAbsoluteGameDir(relPath);
log.DebugFormat("Trying to delete {0}", absPath);
if (inst.HasManagedFiles(registry, absPath))
{
Main.Instance.ErrorDialog(Properties.Resources.FolderContainsManagedFiles, relPath);
Expand All @@ -161,15 +165,22 @@ private void DeleteButton_Click(object sender, EventArgs e)
Properties.Resources.DeleteUnmanagedFileDelete,
Properties.Resources.DeleteUnmanagedFileCancel))
{
if (File.Exists(absPath))
try
{
File.Delete(absPath);
if (File.Exists(absPath))
{
File.Delete(absPath);
}
else if (Directory.Exists(absPath))
{
Directory.Delete(absPath, true);
}
GameFolderTree.Nodes.Remove(GameFolderTree.SelectedNode);
}
else if (Directory.Exists(absPath))
catch (Exception exc)
{
Directory.Delete(absPath, true);
user.RaiseError(exc.Message);
}
GameFolderTree.Nodes.Remove(GameFolderTree.SelectedNode);
}
}

Expand Down Expand Up @@ -213,7 +224,9 @@ private void OpenFileBrowser(TreeNode node)
}

private GameInstance inst;
private IUser user;
private Registry registry;
private static readonly ILog log = LogManager.GetLogger(typeof(UnmanagedFiles));
}

internal class DirsFirstSorter : IComparer, IComparer<TreeNode>
Expand Down
2 changes: 1 addition & 1 deletion GUI/Main/MainUnmanaged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class Main
{
private void viewUnmanagedFilesStripMenuItem_Click(object sender, EventArgs e)
{
UnmanagedFiles.LoadFiles(manager.CurrentInstance);
UnmanagedFiles.LoadFiles(manager.CurrentInstance, currentUser);
tabController.ShowTab("UnmanagedFilesTabPage", 2);
DisableMainWindow();
}
Expand Down

0 comments on commit 2ad7981

Please sign in to comment.