2014-07-25 11:06:10 +08:00
|
|
|
//! \file GarExtract.cs
|
|
|
|
//! \date Fri Jul 25 05:52:27 2014
|
|
|
|
//! \brief Extract archive frontend.
|
|
|
|
//
|
2015-05-14 19:58:16 +08:00
|
|
|
// Copyright (C) 2014-2015 by morkt
|
2014-07-25 11:06:10 +08:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Collections.Generic;
|
2015-05-16 01:19:15 +08:00
|
|
|
using System.ComponentModel;
|
2014-07-25 11:06:10 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Input;
|
2015-06-13 02:45:50 +08:00
|
|
|
using System.Windows.Media.Imaging;
|
2014-07-25 11:06:10 +08:00
|
|
|
using Ookii.Dialogs.Wpf;
|
|
|
|
using GameRes;
|
|
|
|
using GARbro.GUI.Strings;
|
2014-07-30 05:13:21 +08:00
|
|
|
using GARbro.GUI.Properties;
|
2014-07-25 11:06:10 +08:00
|
|
|
|
|
|
|
namespace GARbro.GUI
|
|
|
|
{
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Handle "Extract item" command.
|
|
|
|
/// </summary>
|
|
|
|
private void ExtractItemExec (object sender, ExecutedRoutedEventArgs e)
|
|
|
|
{
|
|
|
|
var entry = CurrentDirectory.SelectedItem as EntryViewModel;
|
2014-07-29 10:57:35 +08:00
|
|
|
if (null == entry && !ViewModel.IsArchive)
|
2014-07-30 13:00:26 +08:00
|
|
|
{
|
|
|
|
SetStatusText (guiStrings.MsgChooseFiles);
|
2014-07-25 11:06:10 +08:00
|
|
|
return;
|
2014-07-30 13:00:26 +08:00
|
|
|
}
|
2015-05-16 04:33:31 +08:00
|
|
|
GarExtract extractor = null;
|
2014-07-25 11:06:10 +08:00
|
|
|
try
|
|
|
|
{
|
2015-08-01 04:19:59 +08:00
|
|
|
string destination = Settings.Default.appLastDestination;
|
2015-08-01 22:30:09 +08:00
|
|
|
if (!Directory.Exists (destination))
|
|
|
|
destination = "";
|
2014-07-25 11:06:10 +08:00
|
|
|
if (!ViewModel.IsArchive)
|
|
|
|
{
|
|
|
|
if (!entry.IsDirectory)
|
|
|
|
{
|
|
|
|
var arc_dir = CurrentPath;
|
|
|
|
var source = Path.Combine (arc_dir, entry.Name);
|
2015-08-01 04:19:59 +08:00
|
|
|
if (string.IsNullOrEmpty (destination))
|
|
|
|
destination = arc_dir;
|
2014-07-25 11:06:10 +08:00
|
|
|
// extract into directory named after archive
|
|
|
|
if (!string.IsNullOrEmpty (Path.GetExtension (entry.Name)))
|
|
|
|
destination = Path.GetFileNameWithoutExtension (source);
|
2015-05-16 04:33:31 +08:00
|
|
|
extractor = new GarExtract (this, source);
|
|
|
|
extractor.ExtractAll (destination);
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (null != m_app.CurrentArchive)
|
|
|
|
{
|
|
|
|
var vm = ViewModel as ArchiveViewModel;
|
2015-08-01 04:19:59 +08:00
|
|
|
if (string.IsNullOrEmpty (destination))
|
|
|
|
destination = Path.GetDirectoryName (vm.Path);
|
2015-05-16 04:33:31 +08:00
|
|
|
extractor = new GarExtract (this, vm.Path, m_app.CurrentArchive);
|
|
|
|
if (null == entry || (entry.Name == ".." && vm.SubDir == "")) // root entry
|
|
|
|
extractor.ExtractAll (destination);
|
|
|
|
else
|
|
|
|
extractor.Extract (entry, destination);
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-16 01:19:15 +08:00
|
|
|
catch (OperationCanceledException X)
|
|
|
|
{
|
|
|
|
SetStatusText (X.Message);
|
|
|
|
}
|
2014-07-25 11:06:10 +08:00
|
|
|
catch (Exception X)
|
|
|
|
{
|
|
|
|
PopupError (X.Message, guiStrings.MsgErrorExtracting);
|
|
|
|
}
|
2015-05-16 04:33:31 +08:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (null != extractor && !extractor.IsActive)
|
|
|
|
extractor.Dispose();
|
|
|
|
}
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
2015-05-16 01:19:15 +08:00
|
|
|
}
|
2014-07-25 11:06:10 +08:00
|
|
|
|
2015-05-16 01:19:15 +08:00
|
|
|
sealed internal class GarExtract : IDisposable
|
|
|
|
{
|
|
|
|
private MainWindow m_main;
|
|
|
|
private string m_arc_name;
|
|
|
|
private ArcFile m_arc;
|
|
|
|
private readonly bool m_should_dispose;
|
|
|
|
private bool m_skip_images = false;
|
|
|
|
private bool m_skip_script = false;
|
|
|
|
private bool m_skip_audio = false;
|
2015-06-13 02:45:50 +08:00
|
|
|
private bool m_adjust_image_offset = false;
|
2015-05-16 01:19:15 +08:00
|
|
|
private bool m_convert_audio;
|
|
|
|
private ImageFormat m_image_format;
|
|
|
|
private int m_extract_count;
|
|
|
|
private bool m_extract_in_progress = false;
|
|
|
|
private ProgressDialog m_progress_dialog;
|
|
|
|
private Exception m_pending_error;
|
|
|
|
|
2015-05-16 04:33:31 +08:00
|
|
|
public bool IsActive { get { return m_extract_in_progress; } }
|
|
|
|
|
2015-05-16 01:19:15 +08:00
|
|
|
public GarExtract (MainWindow parent, string source)
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
2015-05-16 01:19:15 +08:00
|
|
|
m_main = parent;
|
|
|
|
m_arc_name = Path.GetFileName (source);
|
2014-07-25 11:06:10 +08:00
|
|
|
FormatCatalog.Instance.LastError = null;
|
2015-05-16 01:19:15 +08:00
|
|
|
m_arc = ArcFile.TryOpen (source);
|
|
|
|
if (null == m_arc)
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
|
|
|
string error_message;
|
|
|
|
if (FormatCatalog.Instance.LastError != null)
|
|
|
|
error_message = FormatCatalog.Instance.LastError.Message;
|
|
|
|
else
|
|
|
|
error_message = guiStrings.MsgUnknownFormat;
|
2015-05-16 01:19:15 +08:00
|
|
|
throw new OperationCanceledException (string.Format ("{1}: {0}", error_message, m_arc_name));
|
|
|
|
}
|
|
|
|
m_should_dispose = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GarExtract (MainWindow parent, string source, ArcFile arc)
|
|
|
|
{
|
|
|
|
m_main = parent;
|
|
|
|
m_arc_name = Path.GetFileName (source);
|
|
|
|
m_arc = arc;
|
|
|
|
m_should_dispose = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void PrepareDestination (string destination)
|
|
|
|
{
|
|
|
|
bool stop_watch = !m_main.ViewModel.IsArchive;
|
|
|
|
if (stop_watch)
|
|
|
|
m_main.StopWatchDirectoryChanges();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory (destination);
|
|
|
|
Directory.SetCurrentDirectory (destination);
|
2015-08-01 04:19:59 +08:00
|
|
|
Settings.Default.appLastDestination = destination;
|
2015-05-16 01:19:15 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (stop_watch)
|
|
|
|
m_main.ResumeWatchDirectoryChanges();
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 01:19:15 +08:00
|
|
|
public void ExtractAll (string destination)
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
2015-05-16 01:19:15 +08:00
|
|
|
if (0 == m_arc.Dir.Count)
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
2015-05-16 01:19:15 +08:00
|
|
|
m_main.SetStatusText (string.Format ("{1}: {0}", guiStrings.MsgEmptyArchive, m_arc_name));
|
2014-07-25 11:06:10 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-05-16 01:19:15 +08:00
|
|
|
var extractDialog = new ExtractArchiveDialog (m_arc_name, destination);
|
|
|
|
extractDialog.Owner = m_main;
|
2014-07-25 11:06:10 +08:00
|
|
|
var result = extractDialog.ShowDialog();
|
|
|
|
if (!result.Value)
|
|
|
|
return;
|
|
|
|
|
2014-07-30 05:13:21 +08:00
|
|
|
destination = extractDialog.Destination;
|
2014-07-25 11:06:10 +08:00
|
|
|
if (!string.IsNullOrEmpty (destination))
|
|
|
|
{
|
|
|
|
destination = Path.GetFullPath (destination);
|
2015-05-16 01:19:15 +08:00
|
|
|
PrepareDestination (destination);
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
2015-05-16 01:19:15 +08:00
|
|
|
else
|
|
|
|
destination = ".";
|
|
|
|
m_skip_images = !extractDialog.ExtractImages.IsChecked.Value;
|
|
|
|
m_skip_script = !extractDialog.ExtractText.IsChecked.Value;
|
|
|
|
m_skip_audio = !extractDialog.ExtractAudio.IsChecked.Value;
|
|
|
|
if (!m_skip_images)
|
|
|
|
m_image_format = extractDialog.GetImageFormat (extractDialog.ImageConversionFormat);
|
2014-07-25 11:06:10 +08:00
|
|
|
|
2015-05-16 01:19:15 +08:00
|
|
|
m_main.SetStatusText (string.Format(guiStrings.MsgExtractingTo, m_arc_name, destination));
|
|
|
|
ExtractFilesFromArchive (string.Format (guiStrings.MsgExtractingArchive, m_arc_name), m_arc.Dir);
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
|
|
|
|
2015-05-16 01:19:15 +08:00
|
|
|
public void Extract (EntryViewModel entry, string destination)
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
2015-05-16 01:19:15 +08:00
|
|
|
var view_model = m_main.ViewModel;
|
|
|
|
var selected = m_main.CurrentDirectory.SelectedItems.Cast<EntryViewModel>();
|
2014-07-30 13:00:26 +08:00
|
|
|
IEnumerable<Entry> file_list = view_model.GetFiles (selected);
|
|
|
|
if (!file_list.Any() && entry.Name == "..")
|
|
|
|
file_list = view_model.GetFiles (view_model);
|
|
|
|
if (!file_list.Any())
|
2014-07-30 05:13:21 +08:00
|
|
|
{
|
2015-05-16 01:19:15 +08:00
|
|
|
m_main.SetStatusText (guiStrings.MsgChooseFiles);
|
2014-07-30 13:00:26 +08:00
|
|
|
return;
|
2014-07-30 05:13:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ExtractDialog extractDialog;
|
2015-05-16 01:19:15 +08:00
|
|
|
bool multiple_files = file_list.Skip (1).Any();
|
|
|
|
if (multiple_files)
|
|
|
|
extractDialog = new ExtractArchiveDialog (m_arc_name, destination);
|
2014-07-30 05:13:21 +08:00
|
|
|
else
|
|
|
|
extractDialog = new ExtractFile (entry, destination);
|
2015-05-16 01:19:15 +08:00
|
|
|
extractDialog.Owner = m_main;
|
2014-07-25 11:06:10 +08:00
|
|
|
var result = extractDialog.ShowDialog();
|
|
|
|
if (!result.Value)
|
|
|
|
return;
|
2015-05-16 01:19:15 +08:00
|
|
|
if (multiple_files)
|
|
|
|
{
|
|
|
|
m_skip_images = !Settings.Default.appExtractImages;
|
|
|
|
m_skip_script = !Settings.Default.appExtractText;
|
|
|
|
m_skip_audio = !Settings.Default.appExtractAudio;
|
|
|
|
}
|
2014-07-30 05:13:21 +08:00
|
|
|
destination = extractDialog.Destination;
|
2014-07-25 11:06:10 +08:00
|
|
|
if (!string.IsNullOrEmpty (destination))
|
|
|
|
{
|
|
|
|
destination = Path.GetFullPath (destination);
|
2015-05-16 01:19:15 +08:00
|
|
|
PrepareDestination (destination);
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
2015-05-16 01:19:15 +08:00
|
|
|
if (!m_skip_images)
|
|
|
|
m_image_format = FormatCatalog.Instance.ImageFormats.FirstOrDefault (f => f.Tag.Equals (Settings.Default.appImageFormat));
|
2014-07-25 11:06:10 +08:00
|
|
|
|
2015-05-16 01:19:15 +08:00
|
|
|
ExtractFilesFromArchive (string.Format (guiStrings.MsgExtractingFile, m_arc_name), file_list);
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
|
|
|
|
2015-05-16 01:19:15 +08:00
|
|
|
private void ExtractFilesFromArchive (string text, IEnumerable<Entry> file_list)
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
2015-05-16 01:19:15 +08:00
|
|
|
if (file_list.Skip (1).Any() // file_list.Count() > 1
|
|
|
|
&& (m_skip_images || m_skip_script || m_skip_audio))
|
|
|
|
file_list = file_list.Where (f => !(m_skip_images && f.Type == "image") &&
|
|
|
|
!(m_skip_script && f.Type == "script") &&
|
|
|
|
!(m_skip_audio && f.Type == "audio"));
|
|
|
|
if (!file_list.Any())
|
|
|
|
{
|
|
|
|
m_main.SetStatusText (string.Format ("{1}: {0}", guiStrings.MsgNoFiles, m_arc_name));
|
|
|
|
return;
|
|
|
|
}
|
2015-06-03 02:56:37 +08:00
|
|
|
file_list = file_list.OrderBy (e => e.Offset);
|
2015-05-16 01:19:15 +08:00
|
|
|
m_progress_dialog = new ProgressDialog ()
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
|
|
|
WindowTitle = guiStrings.TextTitle,
|
|
|
|
Text = text,
|
|
|
|
Description = "",
|
|
|
|
MinimizeBox = true,
|
|
|
|
};
|
2015-06-03 02:56:37 +08:00
|
|
|
if (!file_list.Skip (1).Any()) // 1 == file_list.Count()
|
2015-05-16 01:19:15 +08:00
|
|
|
{
|
2015-06-03 02:56:37 +08:00
|
|
|
m_progress_dialog.Description = file_list.First().Name;
|
2015-05-16 01:19:15 +08:00
|
|
|
m_progress_dialog.ProgressBarStyle = ProgressBarStyle.MarqueeProgressBar;
|
|
|
|
}
|
|
|
|
m_convert_audio = !m_skip_audio && Settings.Default.appConvertAudio;
|
|
|
|
m_extract_count = 0;
|
|
|
|
m_pending_error = null;
|
2015-06-03 02:56:37 +08:00
|
|
|
m_progress_dialog.DoWork += (s, e) => ExtractWorker (file_list);
|
2015-05-16 01:19:15 +08:00
|
|
|
m_progress_dialog.RunWorkerCompleted += OnExtractComplete;
|
|
|
|
m_progress_dialog.ShowDialog (m_main);
|
|
|
|
m_extract_in_progress = true;
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:56:37 +08:00
|
|
|
void ExtractWorker (IEnumerable<Entry> file_list)
|
2015-05-16 01:19:15 +08:00
|
|
|
{
|
|
|
|
try
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
2015-06-03 02:56:37 +08:00
|
|
|
int total = file_list.Count();
|
|
|
|
foreach (var entry in file_list)
|
2015-05-16 01:19:15 +08:00
|
|
|
{
|
|
|
|
if (m_progress_dialog.CancellationPending)
|
|
|
|
break;
|
|
|
|
if (total > 1)
|
|
|
|
m_progress_dialog.ReportProgress (m_extract_count*100/total, null, entry.Name);
|
|
|
|
if (null != m_image_format && entry.Type == "image")
|
|
|
|
ExtractImage (m_arc, entry, m_image_format);
|
|
|
|
else if (m_convert_audio && entry.Type == "audio")
|
|
|
|
ExtractAudio (m_arc, entry);
|
|
|
|
else
|
|
|
|
m_arc.Extract (entry);
|
|
|
|
++m_extract_count;
|
|
|
|
}
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
2015-05-16 01:19:15 +08:00
|
|
|
catch (Exception X)
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
2015-05-16 01:19:15 +08:00
|
|
|
m_pending_error = X;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-13 02:45:50 +08:00
|
|
|
void ExtractImage (ArcFile arc, Entry entry, ImageFormat target_format)
|
2015-05-16 01:19:15 +08:00
|
|
|
{
|
2015-06-03 02:56:37 +08:00
|
|
|
using (var file = arc.OpenSeekableEntry (entry))
|
2015-05-16 01:19:15 +08:00
|
|
|
{
|
2015-07-25 14:14:49 +08:00
|
|
|
var src_format = ImageFormat.FindFormat (file, entry.Name);
|
2015-06-03 02:56:37 +08:00
|
|
|
if (null == src_format)
|
2015-06-13 02:45:50 +08:00
|
|
|
throw new InvalidFormatException (string.Format ("{1}: {0}", guiStrings.MsgUnableInterpretImage, entry.Name));
|
2015-06-03 02:56:37 +08:00
|
|
|
file.Position = 0;
|
|
|
|
string target_ext = target_format.Extensions.First();
|
2015-08-04 02:33:00 +08:00
|
|
|
string outname = FindUniqueFileName (entry.Name, target_ext);
|
2015-06-03 02:56:37 +08:00
|
|
|
if (src_format.Item1 == target_format)
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
2015-06-03 02:56:37 +08:00
|
|
|
// source format is the same as a target, copy file as is
|
|
|
|
using (var output = ArchiveFormat.CreateFile (outname))
|
2015-05-16 01:19:15 +08:00
|
|
|
file.CopyTo (output);
|
|
|
|
return;
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
2015-06-03 02:56:37 +08:00
|
|
|
ImageData image = src_format.Item1.Read (file, src_format.Item2);
|
2015-05-16 01:19:15 +08:00
|
|
|
Trace.WriteLine (string.Format ("{0} => {1}", entry.Name, outname), "ExtractImage");
|
2015-06-13 02:45:50 +08:00
|
|
|
if (m_adjust_image_offset)
|
|
|
|
{
|
|
|
|
image = AdjustImageOffset (image);
|
|
|
|
}
|
2015-05-16 01:19:15 +08:00
|
|
|
using (var outfile = ArchiveFormat.CreateFile (outname))
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
2015-05-16 01:19:15 +08:00
|
|
|
target_format.Write (outfile, image);
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
2015-05-16 01:19:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-13 02:45:50 +08:00
|
|
|
static ImageData AdjustImageOffset (ImageData image)
|
|
|
|
{
|
|
|
|
if (0 == image.OffsetX && 0 == image.OffsetY)
|
|
|
|
return image;
|
|
|
|
int width = (int)image.Width + image.OffsetX;
|
|
|
|
int height = (int)image.Height + image.OffsetY;
|
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
return image;
|
|
|
|
|
|
|
|
int x = Math.Max (image.OffsetX, 0);
|
|
|
|
int y = Math.Max (image.OffsetY, 0);
|
|
|
|
int src_x = image.OffsetX < 0 ? Math.Abs (image.OffsetX) : 0;
|
|
|
|
int src_y = image.OffsetY < 0 ? Math.Abs (image.OffsetY) : 0;
|
|
|
|
int src_stride = (int)image.Width * (image.BPP+7) / 8;
|
|
|
|
int dst_stride = width * (image.BPP+7) / 8;
|
|
|
|
var pixels = new byte[height*dst_stride];
|
|
|
|
int offset = y * dst_stride + x * image.BPP / 8;
|
|
|
|
Int32Rect rect = new Int32Rect (src_x, src_y, (int)image.Width - src_x, 1);
|
|
|
|
for (int row = src_y; row < image.Height; ++row)
|
|
|
|
{
|
|
|
|
rect.Y = row;
|
|
|
|
image.Bitmap.CopyPixels (rect, pixels, src_stride, offset);
|
|
|
|
offset += dst_stride;
|
|
|
|
}
|
|
|
|
var bitmap = BitmapSource.Create (width, height, image.Bitmap.DpiX, image.Bitmap.DpiY,
|
|
|
|
image.Bitmap.Format, image.Bitmap.Palette, pixels, dst_stride);
|
|
|
|
return new ImageData (bitmap);
|
|
|
|
}
|
|
|
|
|
2015-05-16 01:19:15 +08:00
|
|
|
static void ExtractAudio (ArcFile arc, Entry entry)
|
|
|
|
{
|
|
|
|
using (var file = arc.OpenEntry (entry))
|
|
|
|
using (var sound = AudioFormat.Read (file))
|
|
|
|
{
|
|
|
|
if (null == sound)
|
2015-06-13 02:45:50 +08:00
|
|
|
throw new InvalidFormatException (string.Format ("{1}: {0}", guiStrings.MsgUnableInterpretAudio, entry.Name));
|
2015-05-16 01:19:15 +08:00
|
|
|
ConvertAudio (entry.Name, sound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void ConvertAudio (string entry_name, SoundInput input)
|
|
|
|
{
|
|
|
|
string source_format = input.SourceFormat;
|
2015-06-14 13:05:09 +08:00
|
|
|
if (GarConvertMedia.CommonAudioFormats.Contains (source_format))
|
2015-05-16 01:19:15 +08:00
|
|
|
{
|
2015-08-04 02:33:00 +08:00
|
|
|
string output_name = FindUniqueFileName (entry_name, source_format);
|
2015-05-16 01:19:15 +08:00
|
|
|
using (var output = ArchiveFormat.CreateFile (output_name))
|
2014-07-25 11:06:10 +08:00
|
|
|
{
|
2015-05-16 01:19:15 +08:00
|
|
|
input.Source.Position = 0;
|
|
|
|
input.Source.CopyTo (output);
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
2015-05-16 01:19:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-04 02:33:00 +08:00
|
|
|
string output_name = FindUniqueFileName (entry_name, "wav");
|
2015-05-16 01:19:15 +08:00
|
|
|
using (var output = ArchiveFormat.CreateFile (output_name))
|
2015-06-14 13:05:09 +08:00
|
|
|
GarConvertMedia.WavFormat.Write (input, output);
|
2015-05-16 01:19:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 02:33:00 +08:00
|
|
|
public static string FindUniqueFileName (string source_filename, string target_ext)
|
|
|
|
{
|
|
|
|
string ext = target_ext;
|
|
|
|
for (int attempt = 1; attempt < 100; ++attempt)
|
|
|
|
{
|
|
|
|
string filename = Path.ChangeExtension (source_filename, ext);
|
|
|
|
if (!File.Exists (filename))
|
|
|
|
return filename;
|
|
|
|
ext = string.Format ("{0}.{1}", attempt, target_ext);
|
|
|
|
}
|
|
|
|
throw new IOException ("File aready exists");
|
|
|
|
}
|
|
|
|
|
2015-05-16 01:19:15 +08:00
|
|
|
void OnExtractComplete (object sender, RunWorkerCompletedEventArgs e)
|
|
|
|
{
|
|
|
|
m_extract_in_progress = false;
|
|
|
|
m_progress_dialog.Dispose();
|
|
|
|
m_main.Activate();
|
|
|
|
if (!m_main.ViewModel.IsArchive)
|
|
|
|
{
|
|
|
|
m_main.Dispatcher.Invoke (m_main.RefreshView);
|
|
|
|
}
|
|
|
|
m_main.SetStatusText (Localization.Format ("MsgExtractedFiles", m_extract_count));
|
|
|
|
if (null != m_pending_error)
|
|
|
|
{
|
|
|
|
if (m_pending_error is OperationCanceledException)
|
|
|
|
m_main.SetStatusText (m_pending_error.Message);
|
|
|
|
else
|
|
|
|
m_main.PopupError (m_pending_error.Message, guiStrings.MsgErrorExtracting);
|
|
|
|
}
|
|
|
|
this.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
bool disposed = false;
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
2015-05-16 04:33:31 +08:00
|
|
|
if (!disposed)
|
2015-05-16 01:19:15 +08:00
|
|
|
{
|
2015-05-16 04:33:31 +08:00
|
|
|
if (m_should_dispose)
|
|
|
|
m_arc.Dispose();
|
|
|
|
disposed = true;
|
2015-05-16 01:19:15 +08:00
|
|
|
}
|
2015-05-16 04:33:31 +08:00
|
|
|
GC.SuppressFinalize (this);
|
2015-05-16 01:19:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~GarExtract ()
|
|
|
|
{
|
|
|
|
Dispose();
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
2015-05-16 01:19:15 +08:00
|
|
|
#endregion
|
2014-07-25 11:06:10 +08:00
|
|
|
}
|
|
|
|
}
|