2014-07-25 07:06:10 +04:00
|
|
|
//! \file GarExtract.cs
|
|
|
|
//! \date Fri Jul 25 05:52:27 2014
|
|
|
|
//! \brief Extract archive frontend.
|
|
|
|
//
|
2017-02-03 08:53:34 +04:00
|
|
|
// Copyright (C) 2014-2017 by morkt
|
2014-07-25 07:06:10 +04: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-15 21:19:15 +04:00
|
|
|
using System.ComponentModel;
|
2017-02-01 16:47:48 +04:00
|
|
|
using System.Runtime.InteropServices;
|
2014-07-25 07:06:10 +04:00
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Input;
|
2017-02-01 16:47:48 +04:00
|
|
|
using System.Windows.Interop;
|
2015-06-12 22:45:50 +04:00
|
|
|
using System.Windows.Media.Imaging;
|
2014-07-25 07:06:10 +04:00
|
|
|
using GameRes;
|
|
|
|
using GARbro.GUI.Strings;
|
2014-07-30 01:13:21 +04:00
|
|
|
using GARbro.GUI.Properties;
|
2014-07-25 07:06:10 +04: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 06:57:35 +04:00
|
|
|
if (null == entry && !ViewModel.IsArchive)
|
2014-07-30 09:00:26 +04:00
|
|
|
{
|
|
|
|
SetStatusText (guiStrings.MsgChooseFiles);
|
2014-07-25 07:06:10 +04:00
|
|
|
return;
|
2014-07-30 09:00:26 +04:00
|
|
|
}
|
2015-05-16 00:33:31 +04:00
|
|
|
GarExtract extractor = null;
|
2014-07-25 07:06:10 +04:00
|
|
|
try
|
|
|
|
{
|
2015-08-01 00:19:59 +04:00
|
|
|
string destination = Settings.Default.appLastDestination;
|
2015-08-01 18:30:09 +04:00
|
|
|
if (!Directory.Exists (destination))
|
|
|
|
destination = "";
|
2015-08-31 10:48:27 +04:00
|
|
|
var vm = ViewModel;
|
2015-09-17 04:25:36 +04:00
|
|
|
if (vm.IsArchive)
|
2014-07-25 07:06:10 +04:00
|
|
|
{
|
2015-08-01 00:19:59 +04:00
|
|
|
if (string.IsNullOrEmpty (destination))
|
2015-08-31 10:48:27 +04:00
|
|
|
destination = Path.GetDirectoryName (vm.Path.First());
|
2015-09-01 08:52:47 +04:00
|
|
|
var archive_name = vm.Path[vm.Path.Count-2];
|
2015-09-06 05:33:56 +04:00
|
|
|
extractor = new GarExtract (this, archive_name, VFS.Top as ArchiveFileSystem);
|
2015-08-31 10:48:27 +04:00
|
|
|
if (null == entry || (entry.Name == ".." && string.IsNullOrEmpty (vm.Path.Last()))) // root entry
|
2015-05-16 00:33:31 +04:00
|
|
|
extractor.ExtractAll (destination);
|
|
|
|
else
|
|
|
|
extractor.Extract (entry, destination);
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
2015-09-17 04:25:36 +04:00
|
|
|
else if (!entry.IsDirectory)
|
|
|
|
{
|
|
|
|
var source = entry.Source.Name;
|
|
|
|
SetBusyState();
|
|
|
|
if (string.IsNullOrEmpty (destination))
|
|
|
|
{
|
|
|
|
// extract into directory named after archive
|
|
|
|
if (!string.IsNullOrEmpty (Path.GetExtension (entry.Name)))
|
|
|
|
destination = Path.GetFileNameWithoutExtension (source);
|
|
|
|
else
|
|
|
|
destination = vm.Path.First();
|
|
|
|
}
|
|
|
|
extractor = new GarExtract (this, source);
|
|
|
|
extractor.ExtractAll (destination);
|
|
|
|
}
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
2015-05-15 21:19:15 +04:00
|
|
|
catch (OperationCanceledException X)
|
|
|
|
{
|
|
|
|
SetStatusText (X.Message);
|
|
|
|
}
|
2014-07-25 07:06:10 +04:00
|
|
|
catch (Exception X)
|
|
|
|
{
|
|
|
|
PopupError (X.Message, guiStrings.MsgErrorExtracting);
|
|
|
|
}
|
2015-09-06 05:33:56 +04:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (null != extractor && !extractor.IsActive)
|
|
|
|
extractor.Dispose();
|
|
|
|
}
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
2015-05-15 21:19:15 +04:00
|
|
|
}
|
2014-07-25 07:06:10 +04:00
|
|
|
|
2017-02-03 21:56:04 +04:00
|
|
|
sealed internal class GarExtract : GarOperation, IDisposable
|
2015-05-15 21:19:15 +04:00
|
|
|
{
|
|
|
|
private string m_arc_name;
|
2015-09-06 05:33:56 +04:00
|
|
|
private ArchiveFileSystem m_fs;
|
2015-09-16 23:56:18 +04:00
|
|
|
private readonly bool m_should_ascend;
|
2015-05-15 21:19:15 +04:00
|
|
|
private bool m_skip_images = false;
|
|
|
|
private bool m_skip_script = false;
|
|
|
|
private bool m_skip_audio = false;
|
2015-06-12 22:45:50 +04:00
|
|
|
private bool m_adjust_image_offset = false;
|
2015-05-15 21:19:15 +04:00
|
|
|
private bool m_convert_audio;
|
|
|
|
private ImageFormat m_image_format;
|
|
|
|
private int m_extract_count;
|
2017-02-01 16:47:48 +04:00
|
|
|
private int m_skip_count;
|
2015-05-15 21:19:15 +04:00
|
|
|
private bool m_extract_in_progress = false;
|
|
|
|
|
2015-05-16 00:33:31 +04:00
|
|
|
public bool IsActive { get { return m_extract_in_progress; } }
|
|
|
|
|
2017-02-03 21:56:04 +04:00
|
|
|
public GarExtract (MainWindow parent, string source) : base (parent, guiStrings.TextExtractionError)
|
2015-05-15 21:19:15 +04:00
|
|
|
{
|
|
|
|
m_arc_name = Path.GetFileName (source);
|
2015-09-16 23:56:18 +04:00
|
|
|
try
|
2015-09-06 05:33:56 +04:00
|
|
|
{
|
2015-09-16 23:56:18 +04:00
|
|
|
VFS.ChDir (source);
|
|
|
|
m_should_ascend = true;
|
2015-09-06 05:33:56 +04:00
|
|
|
}
|
2015-09-16 23:56:18 +04:00
|
|
|
catch (Exception X)
|
|
|
|
{
|
|
|
|
throw new OperationCanceledException (string.Format ("{1}: {0}", X.Message, m_arc_name));
|
|
|
|
}
|
|
|
|
m_fs = VFS.Top as ArchiveFileSystem;
|
2015-09-06 05:33:56 +04:00
|
|
|
}
|
|
|
|
|
2017-02-03 21:56:04 +04:00
|
|
|
public GarExtract (MainWindow parent, string source, ArchiveFileSystem fs) : base (parent, guiStrings.TextExtractionError)
|
2015-09-06 05:33:56 +04:00
|
|
|
{
|
|
|
|
if (null == fs)
|
|
|
|
throw new UnknownFormatException();
|
|
|
|
m_fs = fs;
|
|
|
|
m_arc_name = Path.GetFileName (source);
|
2015-09-16 23:56:18 +04:00
|
|
|
m_should_ascend = false;
|
2015-05-15 21:19:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 00:19:59 +04:00
|
|
|
Settings.Default.appLastDestination = destination;
|
2015-05-15 21:19:15 +04:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (stop_watch)
|
|
|
|
m_main.ResumeWatchDirectoryChanges();
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:19:15 +04:00
|
|
|
public void ExtractAll (string destination)
|
2014-07-25 07:06:10 +04:00
|
|
|
{
|
2015-09-06 05:33:56 +04:00
|
|
|
var file_list = m_fs.GetFilesRecursive();
|
|
|
|
if (!file_list.Any())
|
2014-07-25 07:06:10 +04:00
|
|
|
{
|
2015-05-15 21:19:15 +04:00
|
|
|
m_main.SetStatusText (string.Format ("{1}: {0}", guiStrings.MsgEmptyArchive, m_arc_name));
|
2014-07-25 07:06:10 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-05-15 21:19:15 +04:00
|
|
|
var extractDialog = new ExtractArchiveDialog (m_arc_name, destination);
|
|
|
|
extractDialog.Owner = m_main;
|
2014-07-25 07:06:10 +04:00
|
|
|
var result = extractDialog.ShowDialog();
|
|
|
|
if (!result.Value)
|
|
|
|
return;
|
|
|
|
|
2014-07-30 01:13:21 +04:00
|
|
|
destination = extractDialog.Destination;
|
2014-07-25 07:06:10 +04:00
|
|
|
if (!string.IsNullOrEmpty (destination))
|
|
|
|
{
|
|
|
|
destination = Path.GetFullPath (destination);
|
2015-05-15 21:19:15 +04:00
|
|
|
PrepareDestination (destination);
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
2015-05-15 21:19:15 +04: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 07:06:10 +04:00
|
|
|
|
2015-05-15 21:19:15 +04:00
|
|
|
m_main.SetStatusText (string.Format(guiStrings.MsgExtractingTo, m_arc_name, destination));
|
2015-09-06 05:33:56 +04:00
|
|
|
ExtractFilesFromArchive (string.Format (guiStrings.MsgExtractingArchive, m_arc_name), file_list);
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
|
|
|
|
2015-05-15 21:19:15 +04:00
|
|
|
public void Extract (EntryViewModel entry, string destination)
|
2014-07-25 07:06:10 +04:00
|
|
|
{
|
2015-05-15 21:19:15 +04:00
|
|
|
var view_model = m_main.ViewModel;
|
|
|
|
var selected = m_main.CurrentDirectory.SelectedItems.Cast<EntryViewModel>();
|
2015-09-05 10:06:12 +04:00
|
|
|
if (!selected.Any() && entry.Name == "..")
|
|
|
|
selected = view_model;
|
|
|
|
|
|
|
|
IEnumerable<Entry> file_list = selected.Select (e => e.Source);
|
2015-09-06 05:33:56 +04:00
|
|
|
if (m_fs is TreeArchiveFileSystem)
|
|
|
|
file_list = (m_fs as TreeArchiveFileSystem).GetFilesRecursive (file_list);
|
2015-09-05 10:06:12 +04:00
|
|
|
|
2014-07-30 09:00:26 +04:00
|
|
|
if (!file_list.Any())
|
2014-07-30 01:13:21 +04:00
|
|
|
{
|
2015-05-15 21:19:15 +04:00
|
|
|
m_main.SetStatusText (guiStrings.MsgChooseFiles);
|
2014-07-30 09:00:26 +04:00
|
|
|
return;
|
2014-07-30 01:13:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ExtractDialog extractDialog;
|
2015-05-15 21:19:15 +04:00
|
|
|
bool multiple_files = file_list.Skip (1).Any();
|
|
|
|
if (multiple_files)
|
|
|
|
extractDialog = new ExtractArchiveDialog (m_arc_name, destination);
|
2014-07-30 01:13:21 +04:00
|
|
|
else
|
|
|
|
extractDialog = new ExtractFile (entry, destination);
|
2015-05-15 21:19:15 +04:00
|
|
|
extractDialog.Owner = m_main;
|
2014-07-25 07:06:10 +04:00
|
|
|
var result = extractDialog.ShowDialog();
|
|
|
|
if (!result.Value)
|
|
|
|
return;
|
2015-05-15 21:19:15 +04: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 01:13:21 +04:00
|
|
|
destination = extractDialog.Destination;
|
2014-07-25 07:06:10 +04:00
|
|
|
if (!string.IsNullOrEmpty (destination))
|
|
|
|
{
|
|
|
|
destination = Path.GetFullPath (destination);
|
2015-05-15 21:19:15 +04:00
|
|
|
PrepareDestination (destination);
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
2015-05-15 21:19:15 +04:00
|
|
|
if (!m_skip_images)
|
|
|
|
m_image_format = FormatCatalog.Instance.ImageFormats.FirstOrDefault (f => f.Tag.Equals (Settings.Default.appImageFormat));
|
2014-07-25 07:06:10 +04:00
|
|
|
|
2015-05-15 21:19:15 +04:00
|
|
|
ExtractFilesFromArchive (string.Format (guiStrings.MsgExtractingFile, m_arc_name), file_list);
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
|
|
|
|
2015-05-15 21:19:15 +04:00
|
|
|
private void ExtractFilesFromArchive (string text, IEnumerable<Entry> file_list)
|
2014-07-25 07:06:10 +04:00
|
|
|
{
|
2017-02-04 13:26:51 +04:00
|
|
|
file_list = file_list.Where (e => e.Offset >= 0);
|
2015-05-15 21:19:15 +04: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-02 22:56:37 +04:00
|
|
|
file_list = file_list.OrderBy (e => e.Offset);
|
2015-05-15 21:19:15 +04:00
|
|
|
m_progress_dialog = new ProgressDialog ()
|
2014-07-25 07:06:10 +04:00
|
|
|
{
|
|
|
|
WindowTitle = guiStrings.TextTitle,
|
|
|
|
Text = text,
|
|
|
|
Description = "",
|
|
|
|
MinimizeBox = true,
|
|
|
|
};
|
2015-06-02 22:56:37 +04:00
|
|
|
if (!file_list.Skip (1).Any()) // 1 == file_list.Count()
|
2015-05-15 21:19:15 +04:00
|
|
|
{
|
2015-06-02 22:56:37 +04:00
|
|
|
m_progress_dialog.Description = file_list.First().Name;
|
2015-05-15 21:19:15 +04:00
|
|
|
m_progress_dialog.ProgressBarStyle = ProgressBarStyle.MarqueeProgressBar;
|
|
|
|
}
|
|
|
|
m_convert_audio = !m_skip_audio && Settings.Default.appConvertAudio;
|
2015-06-02 22:56:37 +04:00
|
|
|
m_progress_dialog.DoWork += (s, e) => ExtractWorker (file_list);
|
2015-05-15 21:19:15 +04:00
|
|
|
m_progress_dialog.RunWorkerCompleted += OnExtractComplete;
|
2017-04-14 04:17:33 +04:00
|
|
|
m_main.IsEnabled = false;
|
2015-05-15 21:19:15 +04:00
|
|
|
m_progress_dialog.ShowDialog (m_main);
|
|
|
|
m_extract_in_progress = true;
|
|
|
|
}
|
|
|
|
|
2015-06-02 22:56:37 +04:00
|
|
|
void ExtractWorker (IEnumerable<Entry> file_list)
|
2015-05-15 21:19:15 +04:00
|
|
|
{
|
2017-02-01 16:47:48 +04:00
|
|
|
m_extract_count = 0;
|
|
|
|
m_skip_count = 0;
|
|
|
|
var arc = m_fs.Source;
|
|
|
|
int total = file_list.Count();
|
|
|
|
int progress_count = 0;
|
2017-02-03 21:56:04 +04:00
|
|
|
bool ignore_errors = false;
|
2017-02-01 16:47:48 +04:00
|
|
|
foreach (var entry in file_list)
|
2014-07-25 07:06:10 +04:00
|
|
|
{
|
2017-02-01 16:47:48 +04:00
|
|
|
if (m_progress_dialog.CancellationPending)
|
|
|
|
break;
|
|
|
|
if (total > 1)
|
|
|
|
m_progress_dialog.ReportProgress (progress_count++*100/total, null, entry.Name);
|
|
|
|
try
|
2015-05-15 21:19:15 +04:00
|
|
|
{
|
|
|
|
if (null != m_image_format && entry.Type == "image")
|
2015-09-06 05:33:56 +04:00
|
|
|
ExtractImage (arc, entry, m_image_format);
|
2015-05-15 21:19:15 +04:00
|
|
|
else if (m_convert_audio && entry.Type == "audio")
|
2015-09-06 05:33:56 +04:00
|
|
|
ExtractAudio (arc, entry);
|
2015-05-15 21:19:15 +04:00
|
|
|
else
|
2017-02-03 23:43:38 +04:00
|
|
|
ExtractEntryAsIs (arc, entry);
|
2015-05-15 21:19:15 +04:00
|
|
|
++m_extract_count;
|
|
|
|
}
|
2017-02-03 21:56:04 +04:00
|
|
|
catch (SkipExistingFileException)
|
|
|
|
{
|
|
|
|
++m_skip_count;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-02-01 16:47:48 +04:00
|
|
|
catch (Exception X)
|
|
|
|
{
|
2017-02-03 21:56:04 +04:00
|
|
|
if (!ignore_errors)
|
2017-02-01 16:47:48 +04:00
|
|
|
{
|
2017-02-03 08:53:34 +04:00
|
|
|
var error_text = string.Format (guiStrings.TextErrorExtracting, entry.Name, X.Message);
|
2017-02-03 21:56:04 +04:00
|
|
|
var result = ShowErrorDialog (error_text);
|
2017-02-03 18:04:35 +04:00
|
|
|
if (!result.Continue)
|
2017-02-02 20:27:13 +04:00
|
|
|
break;
|
2017-02-03 21:56:04 +04:00
|
|
|
ignore_errors = result.IgnoreErrors;
|
2017-02-01 16:47:48 +04:00
|
|
|
}
|
|
|
|
++m_skip_count;
|
|
|
|
}
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
2017-02-01 16:47:48 +04:00
|
|
|
}
|
|
|
|
|
2017-02-03 23:43:38 +04:00
|
|
|
void ExtractEntryAsIs (ArcFile arc, Entry entry)
|
|
|
|
{
|
|
|
|
using (var input = arc.OpenEntry (entry))
|
2017-02-05 01:38:54 +04:00
|
|
|
using (var output = CreateNewFile (entry.Name, true))
|
2017-02-03 23:43:38 +04:00
|
|
|
input.CopyTo (output);
|
|
|
|
}
|
|
|
|
|
2015-06-12 22:45:50 +04:00
|
|
|
void ExtractImage (ArcFile arc, Entry entry, ImageFormat target_format)
|
2015-05-15 21:19:15 +04:00
|
|
|
{
|
2017-02-01 16:47:48 +04:00
|
|
|
using (var decoder = arc.OpenImage (entry))
|
2015-05-15 21:19:15 +04:00
|
|
|
{
|
2017-02-01 16:47:48 +04:00
|
|
|
var src_format = decoder.SourceFormat; // could be null
|
|
|
|
string target_ext = target_format.Extensions.FirstOrDefault() ?? "";
|
2017-02-03 21:56:04 +04:00
|
|
|
string outname = Path.ChangeExtension (entry.Name, target_ext);
|
2017-02-01 16:47:48 +04:00
|
|
|
if (src_format == target_format)
|
2015-06-12 22:45:50 +04:00
|
|
|
{
|
2017-02-01 16:47:48 +04:00
|
|
|
// source format is the same as a target, copy file as is
|
2017-02-05 01:38:54 +04:00
|
|
|
using (var output = CreateNewFile (outname, true))
|
2017-02-01 16:47:48 +04:00
|
|
|
decoder.Source.CopyTo (output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ImageData image = decoder.Image;
|
|
|
|
if (m_adjust_image_offset)
|
|
|
|
{
|
|
|
|
image = AdjustImageOffset (image);
|
|
|
|
}
|
2017-02-05 01:38:54 +04:00
|
|
|
using (var outfile = CreateNewFile (outname, true))
|
2017-02-01 16:47:48 +04:00
|
|
|
{
|
|
|
|
target_format.Write (outfile, image);
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
2016-10-25 18:18:51 +04:00
|
|
|
}
|
2015-05-15 21:19:15 +04:00
|
|
|
}
|
|
|
|
|
2015-06-12 22:45:50 +04: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);
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:56:04 +04:00
|
|
|
void ExtractAudio (ArcFile arc, Entry entry)
|
2015-05-15 21:19:15 +04:00
|
|
|
{
|
2016-10-15 09:34:46 +04:00
|
|
|
using (var file = arc.OpenBinaryEntry (entry))
|
2015-05-15 21:19:15 +04:00
|
|
|
using (var sound = AudioFormat.Read (file))
|
|
|
|
{
|
|
|
|
if (null == sound)
|
2017-02-01 16:47:48 +04:00
|
|
|
throw new InvalidFormatException (guiStrings.MsgUnableInterpretAudio);
|
2015-05-15 21:19:15 +04:00
|
|
|
ConvertAudio (entry.Name, sound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:56:04 +04:00
|
|
|
public void ConvertAudio (string filename, SoundInput input)
|
2015-05-15 21:19:15 +04:00
|
|
|
{
|
|
|
|
string source_format = input.SourceFormat;
|
2015-06-14 09:05:09 +04:00
|
|
|
if (GarConvertMedia.CommonAudioFormats.Contains (source_format))
|
2015-05-15 21:19:15 +04:00
|
|
|
{
|
2017-02-03 21:56:04 +04:00
|
|
|
var output_name = Path.ChangeExtension (filename, source_format);
|
2017-02-05 01:38:54 +04:00
|
|
|
using (var output = CreateNewFile (output_name, true))
|
2014-07-25 07:06:10 +04:00
|
|
|
{
|
2015-05-15 21:19:15 +04:00
|
|
|
input.Source.Position = 0;
|
|
|
|
input.Source.CopyTo (output);
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
2015-05-15 21:19:15 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-03 21:56:04 +04:00
|
|
|
var output_name = Path.ChangeExtension (filename, "wav");
|
2017-02-05 01:38:54 +04:00
|
|
|
using (var output = CreateNewFile (output_name, true))
|
2015-08-14 06:05:50 +04:00
|
|
|
AudioFormat.Wav.Write (input, output);
|
2015-05-15 21:19:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnExtractComplete (object sender, RunWorkerCompletedEventArgs e)
|
|
|
|
{
|
2017-04-14 04:17:33 +04:00
|
|
|
m_main.IsEnabled = true;
|
2015-05-15 21:19:15 +04:00
|
|
|
m_extract_in_progress = false;
|
|
|
|
m_progress_dialog.Dispose();
|
|
|
|
m_main.Activate();
|
2018-01-12 19:57:56 +04:00
|
|
|
m_main.ListViewFocus();
|
2015-05-15 21:19:15 +04:00
|
|
|
if (!m_main.ViewModel.IsArchive)
|
|
|
|
{
|
|
|
|
m_main.Dispatcher.Invoke (m_main.RefreshView);
|
|
|
|
}
|
|
|
|
m_main.SetStatusText (Localization.Format ("MsgExtractedFiles", m_extract_count));
|
2015-09-06 05:33:56 +04:00
|
|
|
this.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
bool disposed = false;
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
if (!disposed)
|
|
|
|
{
|
2015-09-16 23:56:18 +04:00
|
|
|
if (m_should_ascend)
|
|
|
|
{
|
|
|
|
VFS.ChDir ("..");
|
|
|
|
}
|
2015-09-06 05:33:56 +04:00
|
|
|
disposed = true;
|
|
|
|
}
|
|
|
|
GC.SuppressFinalize (this);
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
2015-09-06 05:33:56 +04:00
|
|
|
#endregion
|
2014-07-25 07:06:10 +04:00
|
|
|
}
|
|
|
|
}
|