Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
New class OpenFolderDialog
Browse files Browse the repository at this point in the history
- Fix import
- Use the custom class OpenFolderDialog instead of OpenFileDialog for selecting the Bundles2 folder to import.
  • Loading branch information
aianlinb committed Oct 11, 2020
1 parent 3c304fc commit 5afb38d
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

*.pfx
*.zip

# User-specific files
*.rsuser
Expand Down
2 changes: 1 addition & 1 deletion VisualBundle/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Window x:Class="VisualBundle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="VisualBundle v2.0.0" Height="480" Width="800" Loaded="OnLoaded" Closing="OnWindowClosing" DragEnter="OnDragEnter" Drop="OnDragDrop" AllowDrop="True">
Title="VisualBundle v2.1.0" Height="480" Width="800" Loaded="OnLoaded" Closing="OnWindowClosing" DragEnter="OnDragEnter" Drop="OnDragDrop" AllowDrop="True">
<Window.Resources>
<HierarchicalDataTemplate x:Key="FileViewItemTemplate" DataType="x:Type VisualBundle:ItemModel" ItemsSource="{Binding ChildItems}">
<StackPanel Orientation="Horizontal">
Expand Down
30 changes: 7 additions & 23 deletions VisualBundle/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,26 +464,23 @@ private void OnButtonAddClick(object sender, RoutedEventArgs e)
"Import Confirm",
MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.Cancel) == MessageBoxResult.OK)
{
var fbd = OpenBundles2Dialog();
var fbd = new OpenFolderDialog();
if (fbd.ShowDialog() == true)
{
var Bundles2_path = Path.GetDirectoryName(fbd.FileName);
var fileNames = Directory.GetFiles(Bundles2_path, "*", SearchOption.AllDirectories);
var checkedPaths = new List<string>(fileNames.Length);
var fileNames = Directory.GetFiles(fbd.DirectoryPath, "*", SearchOption.AllDirectories);
int l = loadedBundles[0].UncompressedSize;
BundleRecord bundleToSave = loadedBundles[0];
RunBackground(() =>
{
Dispatcher.Invoke(() => { CurrentBackground.Message.Text = "Checking files . . ."; });
foreach (var f in fileNames)
{
var path = f.Remove(0, Bundles2_path.Length + 1).Replace("\\", "/");
var path = f.Remove(0, fbd.DirectoryPath.Length + 1).Replace("\\", "/");
if (!paths.Contains(path))
{
MessageBox.Show("The index didn't define the file:" + Environment.NewLine + path, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
checkedPaths.Add(path);
}
foreach (var b in loadedBundles)
Expand All @@ -494,11 +491,11 @@ private void OnButtonAddClick(object sender, RoutedEventArgs e)
bundleToSave = b;
}
}
string str = "Imported {0}/" + checkedPaths.Count.ToString() + " Files";
string str = "Imported {0}/" + fileNames.Length.ToString() + " Files";
int count = 0;
foreach (var f in checkedPaths)
foreach (var f in fileNames)
{
var path = f.Remove(0, Bundles2_path.Length + 1).Replace("\\", "/");
var path = f.Remove(0, fbd.DirectoryPath.Length + 1).Replace("\\", "/");
var fr = ic.FindFiles[IndexContainer.FNV1a64Hash(path)];
fr.Write(File.ReadAllBytes(f));
fr.Move(bundleToSave);
Expand All @@ -509,7 +506,7 @@ private void OnButtonAddClick(object sender, RoutedEventArgs e)
changed.Add(bundleToSave);
});
ButtonSave.IsEnabled = true;
MessageBox.Show("Imported " + checkedPaths.Count.ToString() + " files into " + bundleToSave.Name, "Done");
MessageBox.Show("Imported " + fileNames.Length.ToString() + " files into " + bundleToSave.Name, "Done");
}
}
}
Expand Down Expand Up @@ -758,19 +755,6 @@ public void RunBackground(Action action)
CurrentBackground.ShowDialog();
}

public OpenFileDialog OpenBundles2Dialog()
{
var ofd = new OpenFileDialog()
{
ValidateNames = false,
CheckFileExists = false,
CheckPathExists = true,
Title = "Go Into Bundles2 Folder And Click Open",
FileName = "(Go Into Bundles2 Folder And Click Open)"
};
return ofd;
}

private void OnShowAllCheckedChanged(object sender, RoutedEventArgs e)
{
UpdateBundleList();
Expand Down
154 changes: 154 additions & 0 deletions VisualBundle/OpenFolderDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace VisualBundle
{

#region FolderBrowserDialog Base

/// <summary>
/// Vista 樣式的 FolderBrowserDialog
/// </summary>
public class OpenFolderDialog
{
#region Public Property
/// <summary>
/// 获取在 FolderBrowser 中选择的文件夹路径
/// </summary>
public string DirectoryPath { get; set; }
/// <summary>
/// 向用户显示 FolderBrowser 的对话框
/// </summary>
/// <param name="owner">任何实现 System.Windows.Forms.IWin32Window(表示将拥有模式对话框的顶级窗口)的对象。</param>
/// <returns></returns>
public bool? ShowDialog(Window owner = null)
{
;
IntPtr hwndOwner = owner != null ? new WindowInteropHelper(owner).Handle : Process.GetCurrentProcess().MainWindowHandle;
IFileOpenDialog dialog = (IFileOpenDialog)new FileOpenDialog();
try
{
IShellItem item;
if (!string.IsNullOrEmpty(DirectoryPath))
{
uint atts = 0;
if (SHILCreateFromPath(DirectoryPath, out IntPtr idl, ref atts) == 0)
if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0)
dialog.SetFolder(item);
}
dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM);
uint hr = dialog.Show(hwndOwner);
if (hr == ERROR_CANCELLED)
return false;
if (hr != 0)
return null;
dialog.GetResult(out item);
item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out string path);
DirectoryPath = path;
return true;
}
finally
{
Marshal.ReleaseComObject(dialog);
}
}
#endregion

#region BaseType
[DllImport("shell32.dll")]
private static extern int SHILCreateFromPath([MarshalAs(UnmanagedType.LPWStr)] string pszPath, out IntPtr ppIdl, ref uint rgflnOut);
[DllImport("shell32.dll")]
private static extern int SHCreateShellItem(IntPtr pidlParent, IntPtr psfParent, IntPtr pidl, out IShellItem ppsi);
private const uint ERROR_CANCELLED = 0x800704C7;
[ComImport]
[Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")]
private class FileOpenDialog
{
}
[ComImport]
[Guid("42f85136-db7e-439c-85f1-e4075d135fc8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IFileOpenDialog
{
[PreserveSig]
uint Show([In] IntPtr parent); // IModalWindow
void SetFileTypes(); // not fully defined
void SetFileTypeIndex([In] uint iFileType);
void GetFileTypeIndex(out uint piFileType);
void Advise(); // not fully defined
void Unadvise();
void SetOptions([In] FOS fos);
void GetOptions(out FOS pfos);
void SetDefaultFolder(IShellItem psi);
void SetFolder(IShellItem psi);
void GetFolder(out IShellItem ppsi);
void GetCurrentSelection(out IShellItem ppsi);
void SetFileName([In, MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetFileName([MarshalAs(UnmanagedType.LPWStr)] out string pszName);
void SetTitle([In, MarshalAs(UnmanagedType.LPWStr)] string pszTitle);
void SetOkButtonLabel([In, MarshalAs(UnmanagedType.LPWStr)] string pszText);
void SetFileNameLabel([In, MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
void GetResult(out IShellItem ppsi);
void AddPlace(IShellItem psi, int alignment);
void SetDefaultExtension([In, MarshalAs(UnmanagedType.LPWStr)] string pszDefaultExtension);
void Close(int hr);
void SetClientGuid(); // not fully defined
void ClearClientData();
void SetFilter([MarshalAs(UnmanagedType.Interface)] IntPtr pFilter);
void GetResults([MarshalAs(UnmanagedType.Interface)] out IntPtr ppenum); // not fully defined
void GetSelectedItems([MarshalAs(UnmanagedType.Interface)] out IntPtr ppsai); // not fully defined
}
[ComImport]
[Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IShellItem
{
void BindToHandler(); // not fully defined
void GetParent(); // not fully defined
void GetDisplayName([In] SIGDN sigdnName, [MarshalAs(UnmanagedType.LPWStr)] out string ppszName);
void GetAttributes(); // not fully defined
void Compare(); // not fully defined
}
private enum SIGDN : uint
{
SIGDN_DESKTOPABSOLUTEEDITING = 0x8004c000,
SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000,
SIGDN_FILESYSPATH = 0x80058000,
SIGDN_NORMALDISPLAY = 0,
SIGDN_PARENTRELATIVE = 0x80080001,
SIGDN_PARENTRELATIVEEDITING = 0x80031001,
SIGDN_PARENTRELATIVEFORADDRESSBAR = 0x8007c001,
SIGDN_PARENTRELATIVEPARSING = 0x80018001,
SIGDN_URL = 0x80068000
}
[Flags]
private enum FOS
{
FOS_ALLNONSTORAGEITEMS = 0x80,
FOS_ALLOWMULTISELECT = 0x200,
FOS_CREATEPROMPT = 0x2000,
FOS_DEFAULTNOMINIMODE = 0x20000000,
FOS_DONTADDTORECENT = 0x2000000,
FOS_FILEMUSTEXIST = 0x1000,
FOS_FORCEFILESYSTEM = 0x40,
FOS_FORCESHOWHIDDEN = 0x10000000,
FOS_HIDEMRUPLACES = 0x20000,
FOS_HIDEPINNEDPLACES = 0x40000,
FOS_NOCHANGEDIR = 8,
FOS_NODEREFERENCELINKS = 0x100000,
FOS_NOREADONLYRETURN = 0x8000,
FOS_NOTESTFILECREATE = 0x10000,
FOS_NOVALIDATE = 0x100,
FOS_OVERWRITEPROMPT = 2,
FOS_PATHMUSTEXIST = 0x800,
FOS_PICKFOLDERS = 0x20,
FOS_SHAREAWARE = 0x4000,
FOS_STRICTFILETYPES = 4
}
#endregion
}
#endregion
}
4 changes: 2 additions & 2 deletions VisualBundle/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@
// 您可以指定所有的值,也可以使用 '*' 將組建和修訂編號
// 設為預設,如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]

0 comments on commit 5afb38d

Please sign in to comment.