From e30ca825a38bd6aec78c836ad5bc253061fbcf99 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Mon, 12 Jan 2026 17:22:32 +0100 Subject: [PATCH] Add OpenFolderDialog.CheckPath & refactor ShowDialog to return `DialogResult` --- PCK-Studio/Controls/RawAssetsEditor.cs | 4 ++-- PCK-Studio/Forms/Features/CemuPanel.cs | 5 +++-- PckStudio.Core/Misc/OpenFolderDialog.cs | 27 ++++++++++++++----------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/PCK-Studio/Controls/RawAssetsEditor.cs b/PCK-Studio/Controls/RawAssetsEditor.cs index 23cd6307..e4b66f6b 100644 --- a/PCK-Studio/Controls/RawAssetsEditor.cs +++ b/PCK-Studio/Controls/RawAssetsEditor.cs @@ -1325,7 +1325,7 @@ namespace PckStudio.Controls private void importExtractedSkinsFolder(object sender, EventArgs e) { OpenFolderDialog contents = new OpenFolderDialog(); - if (contents.ShowDialog(Handle) == true) + if (contents.ShowDialog(Handle) == DialogResult.OK) { //checks to make sure selected path exist if (!Directory.Exists(contents.ResultPath)) @@ -1503,7 +1503,7 @@ namespace PckStudio.Controls OpenFolderDialog dialog = new OpenFolderDialog(); dialog.Title = @"Select destination folder"; - if (dialog.ShowDialog(Handle) == true) + if (dialog.ShowDialog(Handle) == DialogResult.OK) extractFolder(dialog.ResultPath); } else if (node.TryGetTagData(out PckAsset asset)) diff --git a/PCK-Studio/Forms/Features/CemuPanel.cs b/PCK-Studio/Forms/Features/CemuPanel.cs index 311c1c74..dad6e263 100644 --- a/PCK-Studio/Forms/Features/CemuPanel.cs +++ b/PCK-Studio/Forms/Features/CemuPanel.cs @@ -169,9 +169,10 @@ namespace PckStudio.Forms.Features { OpenFolderDialog openFolderDialog = new OpenFolderDialog { - Title = "Select Cemu mlc01 Directory" + Title = "Select Cemu mlc01 Directory", + CheckPath = true }; - if (openFolderDialog.ShowDialog(Handle) == true && Directory.Exists(openFolderDialog.ResultPath)) + if (openFolderDialog.ShowDialog(Handle) == DialogResult.OK) { GameDirectoryTextBox.Text = openFolderDialog.ResultPath; } diff --git a/PckStudio.Core/Misc/OpenFolderDialog.cs b/PckStudio.Core/Misc/OpenFolderDialog.cs index 90a18310..cb5a9a68 100644 --- a/PckStudio.Core/Misc/OpenFolderDialog.cs +++ b/PckStudio.Core/Misc/OpenFolderDialog.cs @@ -6,6 +6,7 @@ using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; +using System.Windows.Forms; namespace PckStudio.Core.Misc { @@ -18,6 +19,7 @@ namespace PckStudio.Core.Misc public virtual string Title { get; set; } public virtual string OkButtonLabel { get; set; } public virtual string FileNameLabel { get; set; } + public bool CheckPath { get; set; } protected virtual int SetOptions(int options) { @@ -25,17 +27,21 @@ namespace PckStudio.Core.Misc { options |= (int)FOS.FOS_FORCEFILESYSTEM; } + if (CheckPath) + { + options |= (int)FOS.FOS_PATHMUSTEXIST; + } return options; } // for all .NET - public virtual bool? ShowDialog(IntPtr owner, bool throwOnError = false) + public virtual DialogResult ShowDialog(IntPtr owner, bool throwOnError = false) { var dialog = (IFileOpenDialog)new FileOpenDialog(); if (!string.IsNullOrEmpty(InputPath)) { if (CheckHr(SHCreateItemFromParsingName(InputPath, null, typeof(IShellItem).GUID, out IShellItem item), throwOnError) != 0) - return null; + return DialogResult.Abort; dialog.SetFolder(item); } @@ -70,16 +76,16 @@ namespace PckStudio.Core.Misc var hr = dialog.Show(owner); if (hr == ERROR_CANCELLED) - return null; + return DialogResult.Cancel; if (CheckHr(hr, throwOnError) != 0) - return null; + return DialogResult.Abort; if (CheckHr(dialog.GetResult(out IShellItem result), throwOnError) != 0) - return null; + return DialogResult.Abort; if (CheckHr(result.GetDisplayName(SIGDN.SIGDN_DESKTOPABSOLUTEPARSING, out var path), throwOnError) != 0) - return null; + return DialogResult.Abort; ResultPath = path; @@ -87,16 +93,13 @@ namespace PckStudio.Core.Misc { ResultName = path; } - return true; + return DialogResult.OK; } private static int CheckHr(int hr, bool throwOnError) { - if (hr != 0) - { - if (throwOnError) - Marshal.ThrowExceptionForHR(hr); - } + if (hr != 0 && throwOnError) + Marshal.ThrowExceptionForHR(hr); return hr; }