2014-08-22 10:16:11 +04:00
|
|
|
//! \file GarConvert.cs
|
|
|
|
//! \date Fri Aug 22 08:22:47 2014
|
|
|
|
//! \brief Game resources conversion methods.
|
|
|
|
//
|
2015-06-14 09:05:09 +04:00
|
|
|
// Copyright (C) 2014-2015 by morkt
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2014-08-22 10:16:11 +04:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Input;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using GameRes;
|
|
|
|
using GARbro.GUI.Strings;
|
|
|
|
using GARbro.GUI.Properties;
|
|
|
|
using Ookii.Dialogs.Wpf;
|
|
|
|
|
|
|
|
namespace GARbro.GUI
|
|
|
|
{
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Convert selected images to another format.
|
|
|
|
/// </summary>
|
2015-06-14 09:05:09 +04:00
|
|
|
void ConvertMediaExec (object sender, ExecutedRoutedEventArgs e)
|
2014-08-22 10:16:11 +04:00
|
|
|
{
|
|
|
|
if (ViewModel.IsArchive)
|
|
|
|
return;
|
2015-06-17 17:36:31 +04:00
|
|
|
var source = from entry in CurrentDirectory.SelectedItems.Cast<EntryViewModel>()
|
|
|
|
where entry.Type == "image" || entry.Type == "audio"
|
|
|
|
select entry.Source;
|
2015-06-14 09:05:09 +04:00
|
|
|
if (!source.Any())
|
|
|
|
{
|
|
|
|
PopupError (guiStrings.MsgNoMediaFiles, guiStrings.TextMediaConvertError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var convert_dialog = new ConvertMedia();
|
2014-08-22 10:16:11 +04:00
|
|
|
convert_dialog.Owner = this;
|
|
|
|
var result = convert_dialog.ShowDialog() ?? false;
|
|
|
|
if (!result)
|
|
|
|
return;
|
2015-06-14 09:05:09 +04:00
|
|
|
var format = convert_dialog.ImageConversionFormat.SelectedItem as ImageFormat;
|
2014-08-22 10:16:11 +04:00
|
|
|
if (null == format)
|
|
|
|
{
|
2015-06-14 09:05:09 +04:00
|
|
|
Trace.WriteLine ("Format is not selected", "ConvertMediaExec");
|
2014-08-22 10:16:11 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Directory.SetCurrentDirectory (ViewModel.Path);
|
2015-06-14 09:05:09 +04:00
|
|
|
var converter = new GarConvertMedia (this);
|
2015-06-14 13:54:03 +04:00
|
|
|
converter.IgnoreErrors = convert_dialog.IgnoreErrors.IsChecked ?? false;
|
2014-08-22 10:16:11 +04:00
|
|
|
converter.Convert (source, format);
|
|
|
|
}
|
|
|
|
catch (Exception X)
|
|
|
|
{
|
2015-06-14 09:05:09 +04:00
|
|
|
PopupError (X.Message, guiStrings.TextMediaConvertError);
|
2014-08-22 10:16:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-14 09:05:09 +04:00
|
|
|
internal class GarConvertMedia
|
2014-08-22 10:16:11 +04:00
|
|
|
{
|
|
|
|
private MainWindow m_main;
|
|
|
|
private ProgressDialog m_progress_dialog;
|
2015-06-14 09:05:09 +04:00
|
|
|
private IEnumerable<Entry> m_source;
|
2014-08-22 10:16:11 +04:00
|
|
|
private ImageFormat m_image_format;
|
|
|
|
private Exception m_pending_error;
|
2015-06-14 09:05:09 +04:00
|
|
|
private List<Tuple<string,string>> m_failed = new List<Tuple<string,string>>();
|
2014-08-22 10:16:11 +04:00
|
|
|
|
|
|
|
public bool IgnoreErrors { get; set; }
|
2015-06-14 09:05:09 +04:00
|
|
|
public IEnumerable<Tuple<string,string>> FailedFiles { get { return m_failed; } }
|
2014-08-22 10:16:11 +04:00
|
|
|
|
2015-06-14 09:05:09 +04:00
|
|
|
public GarConvertMedia (MainWindow parent)
|
2014-08-22 10:16:11 +04:00
|
|
|
{
|
|
|
|
m_main = parent;
|
|
|
|
}
|
|
|
|
|
2015-06-14 09:05:09 +04:00
|
|
|
public void Convert (IEnumerable<Entry> images, ImageFormat format)
|
2014-08-22 10:16:11 +04:00
|
|
|
{
|
|
|
|
m_main.StopWatchDirectoryChanges();
|
|
|
|
m_source = images;
|
|
|
|
m_image_format = format;
|
|
|
|
m_progress_dialog = new ProgressDialog ()
|
|
|
|
{
|
|
|
|
WindowTitle = guiStrings.TextTitle,
|
|
|
|
Text = "Converting image",
|
|
|
|
Description = "",
|
|
|
|
MinimizeBox = true,
|
|
|
|
};
|
|
|
|
m_progress_dialog.DoWork += ConvertWorker;
|
|
|
|
m_progress_dialog.RunWorkerCompleted += OnConvertComplete;
|
|
|
|
m_progress_dialog.ShowDialog (m_main);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertWorker (object sender, DoWorkEventArgs e)
|
|
|
|
{
|
|
|
|
m_pending_error = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int total = m_source.Count();
|
|
|
|
int i = 0;
|
2015-06-14 09:05:09 +04:00
|
|
|
foreach (var entry in m_source)
|
2014-08-22 10:16:11 +04:00
|
|
|
{
|
|
|
|
if (m_progress_dialog.CancellationPending)
|
|
|
|
throw new OperationCanceledException();
|
2015-06-14 09:05:09 +04:00
|
|
|
var filename = entry.Name;
|
2014-08-22 10:16:11 +04:00
|
|
|
int progress = i++*100/total;
|
2015-06-14 09:05:09 +04:00
|
|
|
m_progress_dialog.ReportProgress (progress, string.Format (guiStrings.MsgConvertingFile,
|
|
|
|
Path.GetFileName (filename)), null);
|
2014-08-22 10:16:11 +04:00
|
|
|
try
|
|
|
|
{
|
2015-06-14 09:05:09 +04:00
|
|
|
if ("image" == entry.Type)
|
|
|
|
ConvertImage (filename);
|
|
|
|
else if ("audio" == entry.Type)
|
|
|
|
ConvertAudio (filename);
|
2014-08-22 10:16:11 +04:00
|
|
|
}
|
2015-06-17 17:36:31 +04:00
|
|
|
catch (NotImplementedException X)
|
|
|
|
{
|
|
|
|
// target format creation not implemented
|
|
|
|
m_pending_error = X;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-22 10:16:11 +04:00
|
|
|
catch (Exception X)
|
|
|
|
{
|
|
|
|
if (!IgnoreErrors)
|
|
|
|
throw;
|
2015-06-14 09:05:09 +04:00
|
|
|
m_failed.Add (Tuple.Create (Path.GetFileName (filename), X.Message));
|
2014-08-22 10:16:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception X)
|
|
|
|
{
|
|
|
|
m_pending_error = X;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-14 09:05:09 +04:00
|
|
|
public static readonly HashSet<string> CommonAudioFormats = new HashSet<string> { "wav", "mp3", "ogg" };
|
|
|
|
public static readonly AudioFormat WavFormat = FormatCatalog.Instance.AudioFormats.First (f => f.Tag == "WAV");
|
|
|
|
|
|
|
|
void ConvertAudio (string filename)
|
|
|
|
{
|
|
|
|
using (var file = File.OpenRead (filename))
|
|
|
|
using (var input = AudioFormat.Read (file))
|
|
|
|
{
|
|
|
|
if (null == input)
|
|
|
|
return;
|
|
|
|
var source_ext = Path.GetExtension (filename).TrimStart ('.').ToLowerInvariant();
|
|
|
|
string source_format = input.SourceFormat;
|
|
|
|
if (CommonAudioFormats.Contains (source_format))
|
|
|
|
{
|
|
|
|
if (source_ext == source_format)
|
|
|
|
return;
|
|
|
|
string output_name = Path.ChangeExtension (filename, source_format);
|
2015-08-03 22:33:00 +04:00
|
|
|
using (var output = CreateNewFile (output_name))
|
2015-06-14 09:05:09 +04:00
|
|
|
{
|
|
|
|
input.Source.Position = 0;
|
|
|
|
input.Source.CopyTo (output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (source_ext == "wav")
|
|
|
|
return;
|
|
|
|
string output_name = Path.ChangeExtension (filename, "wav");
|
2015-08-03 22:33:00 +04:00
|
|
|
using (var output = CreateNewFile (output_name))
|
2015-06-14 09:05:09 +04:00
|
|
|
WavFormat.Write (input, output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertImage (string filename)
|
|
|
|
{
|
|
|
|
string source_ext = Path.GetExtension (filename).TrimStart ('.').ToLowerInvariant();
|
|
|
|
if (m_image_format.Extensions.Any (ext => ext == source_ext))
|
|
|
|
return;
|
2015-06-17 17:36:31 +04:00
|
|
|
string target_ext = m_image_format.Extensions.First();
|
|
|
|
string target_name = Path.ChangeExtension (filename, target_ext);
|
2015-06-14 09:05:09 +04:00
|
|
|
using (var file = File.OpenRead (filename))
|
|
|
|
{
|
2015-07-25 10:14:49 +04:00
|
|
|
var image = ImageFormat.Read (filename, file);
|
2015-06-14 09:05:09 +04:00
|
|
|
if (null == image)
|
|
|
|
return;
|
|
|
|
try
|
|
|
|
{
|
2015-08-03 22:33:00 +04:00
|
|
|
using (var output = CreateNewFile (target_name))
|
2015-06-14 09:05:09 +04:00
|
|
|
m_image_format.Write (output, image);
|
|
|
|
}
|
|
|
|
catch // delete destination file on conversion failure
|
|
|
|
{
|
|
|
|
File.Delete (target_name);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 22:33:00 +04:00
|
|
|
/// <summary>
|
|
|
|
/// Creates new file with specified filename, or, if it's already exists, tries to open
|
|
|
|
/// files named "FILENAME.1.EXT", "FILENAME.2.EXT" and so on.
|
|
|
|
/// <exception cref="System.IOException">Throws exception after 100th failed attempt.</exception>
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
public static Stream CreateNewFile (string filename)
|
|
|
|
{
|
|
|
|
string name = filename;
|
|
|
|
var ext = new Lazy<string> (() => Path.GetExtension (filename));
|
|
|
|
for (int attempt = 1; ; ++attempt)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return File.Open (name, FileMode.CreateNew);
|
|
|
|
}
|
|
|
|
catch (IOException) // file already exists
|
|
|
|
{
|
|
|
|
if (100 == attempt) // limit number of attempts
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
name = Path.ChangeExtension (filename, attempt.ToString()+ext.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 10:16:11 +04:00
|
|
|
void OnConvertComplete (object sender, RunWorkerCompletedEventArgs e)
|
|
|
|
{
|
|
|
|
m_main.ResumeWatchDirectoryChanges();
|
|
|
|
m_progress_dialog.Dispose();
|
|
|
|
if (null != m_pending_error)
|
|
|
|
{
|
|
|
|
if (m_pending_error is OperationCanceledException)
|
|
|
|
m_main.SetStatusText (m_pending_error.Message);
|
|
|
|
else
|
2015-06-14 09:05:09 +04:00
|
|
|
m_main.PopupError (m_pending_error.Message, guiStrings.TextMediaConvertError);
|
2014-08-22 10:16:11 +04:00
|
|
|
}
|
|
|
|
m_main.Activate();
|
|
|
|
m_main.RefreshView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|