mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
implemented archive creation frontend.
This commit is contained in:
@@ -36,7 +36,6 @@ using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
using System.Threading;
|
||||
using Microsoft.VisualBasic.FileIO;
|
||||
using Ookii.Dialogs.Wpf;
|
||||
using GARbro.GUI.Properties;
|
||||
using GARbro.GUI.Strings;
|
||||
using GameRes;
|
||||
@@ -772,220 +771,6 @@ namespace GARbro.GUI
|
||||
*/
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle "Extract item" command.
|
||||
/// </summary>
|
||||
private void ExtractItemExec (object sender, ExecutedRoutedEventArgs e)
|
||||
{
|
||||
var entry = CurrentDirectory.SelectedItem as EntryViewModel;
|
||||
if (null == entry)
|
||||
return;
|
||||
try
|
||||
{
|
||||
if (!ViewModel.IsArchive)
|
||||
{
|
||||
if (!entry.IsDirectory)
|
||||
{
|
||||
var arc_dir = CurrentPath;
|
||||
var source = Path.Combine (arc_dir, entry.Name);
|
||||
string destination = arc_dir;
|
||||
// extract into directory named after archive
|
||||
if (!string.IsNullOrEmpty (Path.GetExtension (entry.Name)))
|
||||
destination = Path.GetFileNameWithoutExtension (source);
|
||||
ExtractArchive (source, destination);
|
||||
}
|
||||
}
|
||||
else if (null != m_app.CurrentArchive)
|
||||
{
|
||||
var vm = ViewModel as ArchiveViewModel;
|
||||
string destination = Path.GetDirectoryName (vm.Path);
|
||||
string arc_name = Path.GetFileName (vm.Path);
|
||||
if (entry.Name == ".." && vm.SubDir == "") // root entry
|
||||
{
|
||||
ExtractArchive (m_app.CurrentArchive, arc_name, destination);
|
||||
}
|
||||
else
|
||||
{
|
||||
ExtractFileFromArchive (entry, destination);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception X)
|
||||
{
|
||||
PopupError (X.Message, guiStrings.MsgErrorExtracting);
|
||||
}
|
||||
}
|
||||
|
||||
private void ExtractArchive (string path, string destination)
|
||||
{
|
||||
string arc_name = Path.GetFileName (path);
|
||||
FormatCatalog.Instance.LastError = null;
|
||||
var arc = ArcFile.TryOpen (path);
|
||||
if (null != arc)
|
||||
{
|
||||
ExtractArchive (arc, arc_name, destination);
|
||||
}
|
||||
else
|
||||
{
|
||||
string error_message;
|
||||
if (FormatCatalog.Instance.LastError != null)
|
||||
error_message = FormatCatalog.Instance.LastError.Message;
|
||||
else
|
||||
error_message = guiStrings.MsgUnknownFormat;
|
||||
SetStatusText (string.Format ("{1}: {0}", error_message, arc_name));
|
||||
}
|
||||
}
|
||||
|
||||
private void ExtractArchive (ArcFile arc, string arc_name, string destination)
|
||||
{
|
||||
if (0 == arc.Dir.Count)
|
||||
{
|
||||
SetStatusText (string.Format ("{1}: {0}", guiStrings.MsgEmptyArchive, arc_name));
|
||||
return;
|
||||
}
|
||||
var extractDialog = new ExtractArchiveDialog (arc_name, destination);
|
||||
extractDialog.Owner = this;
|
||||
var result = extractDialog.ShowDialog();
|
||||
if (!result.Value)
|
||||
return;
|
||||
|
||||
destination = extractDialog.DestinationDir.Text;
|
||||
if (!string.IsNullOrEmpty (destination))
|
||||
{
|
||||
destination = Path.GetFullPath (destination);
|
||||
Trace.WriteLine (destination, "Extract destination");
|
||||
StopWatchDirectoryChanges();
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory (destination);
|
||||
Directory.SetCurrentDirectory (destination);
|
||||
}
|
||||
finally
|
||||
{
|
||||
m_watcher.EnableRaisingEvents = true;
|
||||
}
|
||||
}
|
||||
IEnumerable<Entry> file_list = arc.Dir;
|
||||
bool skip_images = !extractDialog.ExtractImages.IsChecked.Value;
|
||||
bool skip_script = !extractDialog.ExtractText.IsChecked.Value;
|
||||
if (skip_images || skip_script)
|
||||
file_list = file_list.Where (f => !(skip_images && f.Type == "image") && !(skip_script && f.Type == "script"));
|
||||
|
||||
if (!file_list.Any())
|
||||
{
|
||||
SetStatusText (string.Format ("{1}: {0}", guiStrings.MsgNoFiles, arc_name));
|
||||
return;
|
||||
}
|
||||
ImageFormat image_format = null;
|
||||
if (!skip_images)
|
||||
image_format = extractDialog.GetImageFormat (extractDialog.ImageConversionFormat);
|
||||
|
||||
SetStatusText (string.Format(guiStrings.MsgExtractingTo, arc_name, destination));
|
||||
ExtractFilesFromArchive (string.Format (guiStrings.MsgExtractingArchive, arc_name),
|
||||
arc, file_list, image_format);
|
||||
}
|
||||
|
||||
private void ExtractFileFromArchive (EntryViewModel entry, string destination)
|
||||
{
|
||||
var extractDialog = new ExtractFile (entry, destination);
|
||||
extractDialog.Owner = this;
|
||||
var result = extractDialog.ShowDialog();
|
||||
if (!result.Value)
|
||||
return;
|
||||
|
||||
var file_list = (ViewModel as ArchiveViewModel).GetFiles (entry);
|
||||
|
||||
destination = extractDialog.DestinationDir.Text;
|
||||
if (!string.IsNullOrEmpty (destination))
|
||||
{
|
||||
destination = Path.GetFullPath (destination);
|
||||
Directory.CreateDirectory (destination);
|
||||
Directory.SetCurrentDirectory (destination);
|
||||
}
|
||||
ImageFormat format = null;
|
||||
if (entry.Type == "image")
|
||||
format = extractDialog.GetImageFormat (extractDialog.ImageConversionFormat);
|
||||
|
||||
string arc_name = Path.GetFileName (CurrentPath);
|
||||
ExtractFilesFromArchive (string.Format (guiStrings.MsgExtractingFile, arc_name),
|
||||
m_app.CurrentArchive, file_list, format);
|
||||
}
|
||||
|
||||
private void ExtractFilesFromArchive (string text, ArcFile arc, IEnumerable<Entry> file_list,
|
||||
ImageFormat image_format = null)
|
||||
{
|
||||
file_list = file_list.OrderBy (e => e.Offset);
|
||||
var extractProgressDialog = new ProgressDialog ()
|
||||
{
|
||||
WindowTitle = guiStrings.TextTitle,
|
||||
Text = text,
|
||||
Description = "",
|
||||
MinimizeBox = true,
|
||||
};
|
||||
if (!file_list.Skip (1).Any()) // 1 == file_list.Count()
|
||||
{
|
||||
extractProgressDialog.Description = file_list.First().Name;
|
||||
extractProgressDialog.ProgressBarStyle = ProgressBarStyle.MarqueeProgressBar;
|
||||
}
|
||||
extractProgressDialog.DoWork += (s, e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
int total = file_list.Count();
|
||||
int i = 0;
|
||||
foreach (var entry in file_list)
|
||||
{
|
||||
if (extractProgressDialog.CancellationPending)
|
||||
break;
|
||||
if (total > 1)
|
||||
extractProgressDialog.ReportProgress (i*100/total, null, entry.Name);
|
||||
if (null != image_format && entry.Type == "image")
|
||||
ExtractImage (arc, entry, image_format);
|
||||
else
|
||||
arc.Extract (entry);
|
||||
++i;
|
||||
}
|
||||
SetStatusText (string.Format (guiStrings.MsgExtractCompletePlural, i,
|
||||
Localization.Plural (i, "file")));
|
||||
}
|
||||
catch (Exception X)
|
||||
{
|
||||
SetStatusText (X.Message);
|
||||
}
|
||||
};
|
||||
extractProgressDialog.RunWorkerCompleted += (s, e) => {
|
||||
extractProgressDialog.Dispose();
|
||||
if (!ViewModel.IsArchive)
|
||||
{
|
||||
arc.Dispose();
|
||||
Dispatcher.Invoke (RefreshView);
|
||||
}
|
||||
};
|
||||
extractProgressDialog.ShowDialog (this);
|
||||
}
|
||||
|
||||
private void CreateArchiveExec (object sender, ExecutedRoutedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dialog = new CreateArchiveDialog();
|
||||
dialog.Owner = this;
|
||||
if (!dialog.ShowDialog().Value || string.IsNullOrEmpty (dialog.ArchiveName.Text))
|
||||
return;
|
||||
var format = dialog.ArchiveFormat.SelectedItem as ArchiveFormat;
|
||||
if (null == format)
|
||||
return;
|
||||
|
||||
string arc_name = Path.GetFullPath (dialog.ArchiveName.Text);
|
||||
var items = CurrentDirectory.SelectedItems.Cast<EntryViewModel>();
|
||||
format.Create (arc_name, items.Select (entry => entry.Source), dialog.ArchiveOptions);
|
||||
}
|
||||
catch (Exception X)
|
||||
{
|
||||
PopupError (X.Message, guiStrings.TextCreateArchiveError);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle "Exit" command.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user