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

Commit

Permalink
add Replace All file function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan8489 committed Sep 25, 2020
1 parent 705611f commit b4064cf
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
5 changes: 3 additions & 2 deletions VisualBundle/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
<Button x:Name="ButtonReplace" Content="Replace" IsEnabled="False" HorizontalAlignment="Right" Margin="0,210,25,0" VerticalAlignment="Top" Width="75" Click="OnButtonReplaceClick"/>
<Button x:Name="ButtonMove" Content="MoveTo" IsEnabled="False" HorizontalAlignment="Right" Margin="0,240,25,0" VerticalAlignment="Top" Width="75" Click="OnButtonMoveClick"/>
<Button x:Name="ButtonAdd" Content="AddFiles" IsEnabled="False" HorizontalAlignment="Right" Margin="0,270,25,0" VerticalAlignment="Top" Width="75" Click="OnButtonAddClick"/>
<Button x:Name="ButtonOpen" Content="Open" IsEnabled="False" HorizontalAlignment="Right" Margin="0,300,25,0" VerticalAlignment="Top" Width="75" Click="OnButtonOpenClick"/>
<Button x:Name="ButtonSave" Content="Save" IsEnabled="False" HorizontalAlignment="Right" Margin="0,330,25,0" VerticalAlignment="Top" Width="75" Click="OnButtonSaveClick"/>
<Button x:Name="ButtonReplaceAll" Content="ReplaceAll" IsEnabled="False" HorizontalAlignment="Right" Margin="0,300,25,0" VerticalAlignment="Top" Width="75" Click="ButtonReplaceAllClick"/>
<Button x:Name="ButtonOpen" Content="Open" IsEnabled="False" HorizontalAlignment="Right" Margin="0,330,25,0" VerticalAlignment="Top" Width="75" Click="OnButtonOpenClick"/>
<Button x:Name="ButtonSave" Content="Save" IsEnabled="False" HorizontalAlignment="Right" Margin="0,360,25,0" VerticalAlignment="Top" Width="75" Click="OnButtonSaveClick"/>
<TextBlock x:Name="MessageLabel" HorizontalAlignment="Right" TextWrapping="Wrap" Text="" Foreground="Red" VerticalAlignment="Bottom" Margin="0,0,10,30"/>
<TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="Copyright © 2020 aianlinb." VerticalAlignment="Bottom" Margin="0,0,10,5"/>
</Grid>
Expand Down
89 changes: 85 additions & 4 deletions VisualBundle/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class MainWindow : Window
private FileRecord moveF;
private ItemModel moveD;
private HashSet<BundleRecord> changed = new HashSet<BundleRecord>();

public MainWindow()
{
InitializeComponent();
Expand Down Expand Up @@ -89,6 +89,7 @@ private void OnLoaded(object sender, RoutedEventArgs e)
BuildTree(root, b.Name, b);
foreach (var tvi in root.ChildItems)
View1.Items.Add(tvi);
ButtonReplaceAll.IsEnabled = true;
}

private void OnTreeViewItemExpanded(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -145,7 +146,7 @@ private void OnTreeView1SelectedChanged(object sender, RoutedPropertyChangedEven

private void OnTreeView2SelectedChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{

var tvi = GetSelectedFile();
if (tvi == null) //No Selected
{
Expand Down Expand Up @@ -173,7 +174,7 @@ private void OnTreeView2SelectedChanged(object sender, RoutedPropertyChangedEven
ButtonMove.IsEnabled = true;
ButtonOpen.IsEnabled = true;
}

}

public StackPanel TreeItem(string path, ImageSource icon)
Expand Down Expand Up @@ -318,7 +319,8 @@ private void OnButtonAddClick(object sender, RoutedEventArgs e)
var br = tvi.Record as BundleRecord;
if (br != null) //Selected Bundle File
{
var fbd = new OpenFileDialog() {
var fbd = new OpenFileDialog()
{
ValidateNames = false,
CheckFileExists = false,
CheckPathExists = true,
Expand Down Expand Up @@ -437,5 +439,84 @@ private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArg
if (MessageBox.Show("There are unsaved changes" + Environment.NewLine + "Are you sure you want to leave?", "Closing", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel) == MessageBoxResult.Cancel)
e.Cancel = true;
}

private void ButtonReplaceAllClick(object sender, RoutedEventArgs e)
{
var fbd = OpenBundle2Dialog();
if (fbd.ShowDialog() == true)
{
if (MessageBox.Show(
"This function will replace all files to every loaded bundles" + Environment.NewLine
+ "Are you sure you want to do this?",
"Replace all files to every loaded bundles",
MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel) == MessageBoxResult.OK)
{
var Bundle2_path = Path.GetDirectoryName(fbd.FileName);
var fs = Directory.GetFiles(Bundle2_path, "*", SearchOption.AllDirectories);
var paths = ic.Hashes.Values;
var loadedbundles = GetLoadedBundleRecordAll();
var count = 0;
var size = 0;
foreach (var f in fs)
{
var path = f.Remove(0, Bundle2_path.Length + 1).Replace("\\", "/");
if (paths.Contains(path))
{
var fr = ic.FindFiles[IndexContainer.FNV1a64Hash(path)];
var br = fr.bundleRecord;
if (loadedbundles.Contains(br))
{
fr.Write(File.ReadAllBytes(f));
changed.Add(br);
++count;
size += fr.Size;
}
}
}
ButtonSave.IsEnabled = true;
MessageBox.Show("Imported " + count.ToString() + " Files." + Environment.NewLine
+ "Total " + size.ToString() + " Bytes.", "Done");
}
}
}
private HashSet<BundleRecord> GetLoadedBundleRecordAll()
{
var bundles = new HashSet<BundleRecord>();
var queue = new Queue<ItemModel>();
foreach (ItemModel item in View1.Items)
{
queue.Enqueue(item);
}
while (queue.Count > 0)
{
ItemModel item = queue.Dequeue();
if (item as FolderModel != null)
{
foreach (var childitem in item.ChildItems)
{
queue.Enqueue(childitem);
}
continue;
}
if (item.Record as BundleRecord != null)
{
bundles.Add(item.Record as BundleRecord);
continue;
}
}
return bundles;

}
private Microsoft.Win32.OpenFileDialog OpenBundle2Dialog()
{
var ofd = new OpenFileDialog()
{
ValidateNames = false,
CheckFileExists = false,
CheckPathExists = true,
FileName = "(In Bundle2 Folder)"
};
return ofd;
}
}
}

0 comments on commit b4064cf

Please sign in to comment.