download formats db.

This commit is contained in:
morkt 2017-02-14 13:11:57 +04:00
parent ccd4424b1b
commit 1d5d74ce93
10 changed files with 199 additions and 50 deletions

View File

@ -41,6 +41,7 @@ namespace GARbro.GUI
const StringComparison StringIgnoreCase = StringComparison.OrdinalIgnoreCase;
public static string Name { get { return "GARbro"; } }
public static string FormatsDat { get { return "Formats.dat"; } }
/// <summary>
/// Initial browsing directory.
@ -80,12 +81,11 @@ namespace GARbro.GUI
if (string.IsNullOrEmpty (InitPath))
InitPath = Directory.GetCurrentDirectory();
string formats_dat = "Formats.dat";
DeserializeScheme (Path.Combine (FormatCatalog.Instance.DataDirectory, formats_dat));
DeserializeScheme (Path.Combine (GetLocalAppDataFolder(), formats_dat));
DeserializeScheme (Path.Combine (FormatCatalog.Instance.DataDirectory, FormatsDat));
DeserializeScheme (Path.Combine (GetLocalAppDataFolder(), FormatsDat));
}
string GetLocalAppDataFolder ()
public string GetLocalAppDataFolder ()
{
string local_app_data = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData);
var attribs = Assembly.GetExecutingAssembly().GetCustomAttributes (typeof(AssemblyCompanyAttribute), false);
@ -93,7 +93,7 @@ namespace GARbro.GUI
return Path.Combine (local_app_data, company, Name);
}
void DeserializeScheme (string scheme_file)
public void DeserializeScheme (string scheme_file)
{
try
{

View File

@ -34,44 +34,93 @@ using System.Windows.Input;
using System.Xml;
using GameRes;
using GARbro.GUI.Strings;
using System.IO;
namespace GARbro.GUI
{
public partial class MainWindow : Window
{
private readonly BackgroundWorker m_update_checker = new BackgroundWorker();
GarUpdate m_updater;
private void InitUpdatesChecker ()
{
m_update_checker.DoWork += StartUpdatesCheck;
m_update_checker.RunWorkerCompleted += UpdatesCheckComplete;
var update_url = App.Resources["UpdateUrl"] as Uri;
m_updater = new GarUpdate (this, update_url);
}
public void CanExecuteUpdate (object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = m_updater.CanExecute (e.Parameter);
}
/// <summary>
/// Handle "Check for updates" command.
/// </summary>
private void CheckUpdatesExec (object sender, ExecutedRoutedEventArgs e)
{
m_updater.Execute (e.Parameter);
}
}
public class GarUpdateInfo
{
public Version ReleaseVersion { get; set; }
public Uri ReleaseUrl { get; set; }
public string ReleaseNotes { get; set; }
public int FormatsVersion { get; set; }
public Uri FormatsUrl { get; set; }
public IDictionary<string, Version> Assemblies { get; set; }
}
internal sealed class GarUpdate : ICommand, IDisposable
{
private readonly MainWindow m_main;
private readonly BackgroundWorker m_update_checker = new BackgroundWorker();
private readonly Uri m_url;
const int RequestTimeout = 20000; // milliseconds
public GarUpdate (MainWindow main, Uri url)
{
m_main = main;
m_url = url;
m_update_checker.DoWork += StartUpdatesCheck;
m_update_checker.RunWorkerCompleted += UpdatesCheckComplete;
}
public void Execute (object parameter)
{
if (!m_update_checker.IsBusy)
m_update_checker.RunWorkerAsync();
}
public bool CanExecute (object parameter)
{
return !m_update_checker.IsBusy;
}
public event EventHandler CanExecuteChanged;
void OnCanExecuteChanged ()
{
var handler = CanExecuteChanged;
if (handler != null)
handler (this, new EventArgs());
}
private void StartUpdatesCheck (object sender, DoWorkEventArgs e)
{
var url = m_app.Resources["UpdateUrl"] as Uri;
if (null == url)
return;
using (var updater = new GarUpdate (this))
{
e.Result = updater.Check (url);
}
OnCanExecuteChanged();
if (m_url != null)
e.Result = Check (m_url);
}
private void UpdatesCheckComplete (object sender, RunWorkerCompletedEventArgs e)
{
OnCanExecuteChanged();
if (e.Error != null)
{
SetStatusText (string.Format ("{0} {1}", guiStrings.MsgUpdateFailed, e.Error.Message));
m_main.SetStatusText (string.Format ("{0} {1}", guiStrings.MsgUpdateFailed, e.Error.Message));
return;
}
else if (e.Cancelled)
@ -79,7 +128,7 @@ namespace GARbro.GUI
var result = e.Result as GarUpdateInfo;
if (null == result)
{
SetStatusText (guiStrings.MsgNoUpdates);
m_main.SetStatusText (guiStrings.MsgNoUpdates);
return;
}
var app_version = Assembly.GetExecutingAssembly().GetName().Version;
@ -88,17 +137,44 @@ namespace GARbro.GUI
bool has_db_update = db_version < result.FormatsVersion && CheckAssemblies (result.Assemblies);
if (!has_app_update && !has_db_update)
{
SetStatusText (guiStrings.MsgUpToDate);
m_main.SetStatusText (guiStrings.MsgUpToDate);
return;
}
var dialog = new UpdateDialog (result, has_app_update, has_db_update);
dialog.Owner = this;
dialog.FormatsDownload.Click += FormatsDownloadExec;
dialog.Owner = m_main;
dialog.FormatsDownload.Click += (s, a) => StartFormatsDownload (dialog, result);
dialog.ShowDialog();
}
private void FormatsDownloadExec (object sender, RoutedEventArgs e)
private void StartFormatsDownload (UpdateDialog dialog, GarUpdateInfo info)
{
try
{
dialog.FormatsDownload.IsEnabled = false;
var app_data_folder = m_main.App.GetLocalAppDataFolder();
Directory.CreateDirectory (app_data_folder);
var local_formats_dat = Path.Combine (app_data_folder, App.FormatsDat);
using (var client = new WebClientEx())
using (var tmp_file = new GARbro.Shell.TemporaryFile (app_data_folder, Path.GetRandomFileName()))
{
client.Timeout = RequestTimeout;
// FIXME download blocks GUI thread.
client.DownloadFile (info.FormatsUrl, tmp_file.Name);
m_main.App.DeserializeScheme (tmp_file.Name);
if (!GARbro.Shell.File.Rename (tmp_file.Name, local_formats_dat))
throw new Win32Exception (GARbro.Shell.File.GetLastError());
}
dialog.FormatsUpdateText.Text = guiStrings.MsgUpdateComplete;
}
catch (Exception X)
{
dialog.FormatsUpdateText.Text = string.Format ("{0}\n{1}", guiStrings.MsgDownloadFailed, X.Message);
}
finally
{
dialog.FormatsDownload.Visibility = Visibility.Collapsed;
}
}
/// <summary>
@ -117,30 +193,8 @@ namespace GARbro.GUI
}
return true;
}
}
public class GarUpdateInfo
{
public Version ReleaseVersion { get; set; }
public Uri ReleaseUrl { get; set; }
public string ReleaseNotes { get; set; }
public int FormatsVersion { get; set; }
public Uri FormatsUrl { get; set; }
public IDictionary<string, Version> Assemblies { get; set; }
}
internal sealed class GarUpdate : IDisposable
{
Window m_main;
const int RequestTimeout = 20000; // milliseconds
public GarUpdate (Window main)
{
m_main = main;
}
public GarUpdateInfo Check (Uri version_url)
GarUpdateInfo Check (Uri version_url)
{
var request = WebRequest.Create (version_url);
request.Timeout = RequestTimeout;
@ -190,9 +244,33 @@ namespace GARbro.GUI
{
if (!m_disposed)
{
m_update_checker.Dispose();
m_disposed = true;
}
GC.SuppressFinalize (this);
}
}
/// <summary>
/// WebClient with timeout setting.
/// </summary>
internal class WebClientEx : WebClient
{
/// <summary>
/// Request timeout, in milliseconds.
/// </summary>
public int Timeout { get; set; }
public WebClientEx ()
{
Timeout = 60000;
}
protected override WebRequest GetWebRequest (Uri uri)
{
var request = base.GetWebRequest (uri);
request.Timeout = Timeout;
return request;
}
}
}

View File

@ -403,7 +403,7 @@
<CommandBinding Command="{x:Static local:Commands.HideMenuBar}" Executed="HideMenuBarExec" CanExecute="CanExecuteAlways"/>
<CommandBinding Command="{x:Static local:Commands.HideToolBar}" Executed="HideToolBarExec" CanExecute="CanExecuteAlways"/>
<CommandBinding Command="{x:Static local:Commands.About}" Executed="AboutExec" CanExecute="CanExecuteAlways"/>
<CommandBinding Command="{x:Static local:Commands.CheckUpdates}" Executed="CheckUpdatesExec" CanExecute="CanExecuteAlways"/>
<CommandBinding Command="{x:Static local:Commands.CheckUpdates}" Executed="CheckUpdatesExec" CanExecute="CanExecuteUpdate"/>
<CommandBinding Command="{x:Static local:Commands.Exit}" Executed="ExitExec" CanExecute="CanExecuteAlways"/>
</Window.CommandBindings>
</Window>

View File

@ -54,6 +54,8 @@ namespace GARbro.GUI
{
private App m_app;
public App App { get { return m_app; } }
const StringComparison StringIgnoreCase = StringComparison.CurrentCultureIgnoreCase;
public MainWindow()

View File

@ -663,6 +663,15 @@ namespace GARbro.GUI.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Update download failed..
/// </summary>
public static string MsgDownloadFailed {
get {
return ResourceManager.GetString("MsgDownloadFailed", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to archive is empty.
/// </summary>
@ -882,6 +891,15 @@ namespace GARbro.GUI.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Formats database updated..
/// </summary>
public static string MsgUpdateComplete {
get {
return ResourceManager.GetString("MsgUpdateComplete", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Update check failed..
/// </summary>
@ -1210,6 +1228,15 @@ namespace GARbro.GUI.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Application update.
/// </summary>
public static string TextUpdateTitle {
get {
return ResourceManager.GetString("TextUpdateTitle", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Visit download page.
/// </summary>

View File

@ -505,6 +505,10 @@
<value>Check for updates...</value>
<comment>translation pending</comment>
</data>
<data name="MsgDownloadFailed" xml:space="preserve">
<value>Update download failed.</value>
<comment>translation pending</comment>
</data>
<data name="MsgNoUpdates" xml:space="preserve">
<value>No updates currently available.</value>
<comment>translation pending</comment>
@ -513,6 +517,10 @@
<value>Formats database update available.</value>
<comment>translation pending</comment>
</data>
<data name="MsgUpdateComplete" xml:space="preserve">
<value>Formats database updated.</value>
<comment>translation pending</comment>
</data>
<data name="MsgUpdateFailed" xml:space="preserve">
<value>Update check failed.</value>
<comment>translation pending</comment>
@ -525,6 +533,10 @@
<value>New version available:</value>
<comment>translation pending</comment>
</data>
<data name="TextUpdateTitle" xml:space="preserve">
<value>Application update</value>
<comment>translation pending</comment>
</data>
<data name="TextVisitPage" xml:space="preserve">
<value>Visit download page</value>
<comment>translation pending</comment>

View File

@ -512,6 +512,9 @@ Overwrite?</value>
<data name="MsgUpdateAvailable" xml:space="preserve">
<value>Formats database update available.</value>
</data>
<data name="MsgUpdateComplete" xml:space="preserve">
<value>Formats database updated.</value>
</data>
<data name="MsgUpdateFailed" xml:space="preserve">
<value>Update check failed.</value>
</data>
@ -521,7 +524,13 @@ Overwrite?</value>
<data name="TextNewVersion" xml:space="preserve">
<value>New version available:</value>
</data>
<data name="TextUpdateTitle" xml:space="preserve">
<value>Application update</value>
</data>
<data name="TextVisitPage" xml:space="preserve">
<value>Visit download page</value>
</data>
<data name="MsgDownloadFailed" xml:space="preserve">
<value>Update download failed.</value>
</data>
</root>

View File

@ -527,12 +527,18 @@
<data name="MenuCheckUpdates" xml:space="preserve">
<value>Проверить обновления...</value>
</data>
<data name="MsgDownloadFailed" xml:space="preserve">
<value>Не удалось обновить базу форматов.</value>
</data>
<data name="MsgNoUpdates" xml:space="preserve">
<value>Обновления недоступны.</value>
</data>
<data name="MsgUpdateAvailable" xml:space="preserve">
<value>Доступна обновлённая база форматов.</value>
</data>
<data name="MsgUpdateComplete" xml:space="preserve">
<value>База форматов успешно обновлена.</value>
</data>
<data name="MsgUpdateFailed" xml:space="preserve">
<value>Сбой проверки обновлений.</value>
</data>
@ -542,6 +548,9 @@
<data name="TextNewVersion" xml:space="preserve">
<value>Доступна новая версия:</value>
</data>
<data name="TextUpdateTitle" xml:space="preserve">
<value>Обновление программы</value>
</data>
<data name="TextVisitPage" xml:space="preserve">
<value>Перейти на страницу загрузки</value>
</data>

View File

@ -517,6 +517,10 @@
<value>Check for updates...</value>
<comment>translation pending</comment>
</data>
<data name="MsgDownloadFailed" xml:space="preserve">
<value>Update download failed.</value>
<comment>translation pending</comment>
</data>
<data name="MsgNoUpdates" xml:space="preserve">
<value>No updates currently available.</value>
<comment>translation pending</comment>
@ -525,6 +529,10 @@
<value>Formats database update available.</value>
<comment>translation pending</comment>
</data>
<data name="MsgUpdateComplete" xml:space="preserve">
<value>Formats database updated.</value>
<comment>translation pending</comment>
</data>
<data name="MsgUpdateFailed" xml:space="preserve">
<value>Update check failed.</value>
<comment>translation pending</comment>
@ -537,6 +545,10 @@
<value>New version available:</value>
<comment>translation pending</comment>
</data>
<data name="TextUpdateTitle" xml:space="preserve">
<value>Application update</value>
<comment>translation pending</comment>
</data>
<data name="TextVisitPage" xml:space="preserve">
<value>Visit download page</value>
<comment>translation pending</comment>

View File

@ -24,7 +24,7 @@ IN THE SOFTWARE.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:GARbro.GUI.Strings"
Title="Application update" ShowInTaskbar="False" WindowStartupLocation="CenterOwner"
Title="{x:Static s:guiStrings.TextUpdateTitle}" ShowInTaskbar="False" WindowStartupLocation="CenterOwner"
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
SizeToContent="WidthAndHeight" ResizeMode="NoResize">
<DockPanel>
@ -34,7 +34,7 @@ IN THE SOFTWARE.
Margin="10" Width="75" Height="25" Click="Button_Click" IsCancel="True"/>
</Border>
</DockPanel>
<Image DockPanel.Dock="Left" Source="Images/64x64/actions.png" Width="32" Height="32" Margin="10"
<Image DockPanel.Dock="Left" Source="Images/64x64/actions.png" Width="32" Height="32" Margin="10,20"
SnapsToDevicePixels="True" VerticalAlignment="Top" RenderOptions.BitmapScalingMode="HighQuality"/>
<StackPanel DockPanel.Dock="Right" Orientation="Vertical">
<StackPanel x:Name="ReleasePane" Orientation="Vertical">
@ -53,8 +53,8 @@ IN THE SOFTWARE.
</StackPanel>
<StackPanel x:Name="FormatsPane" Orientation="Vertical">
<Separator Visibility="{Binding ElementName=ReleasePane, Path=Visibility}"/>
<TextBlock Text="{x:Static s:guiStrings.MsgUpdateAvailable}" Margin="0,10,10,0"/>
<Button x:Name="FormatsDownload" Content="{x:Static s:guiStrings.ButtonDownload}" Width="75" Height="25" Margin="0,10,10,10" HorizontalAlignment="Left"/>
<TextBlock x:Name="FormatsUpdateText" Text="{x:Static s:guiStrings.MsgUpdateAvailable}" Margin="0,10,10,10"/>
<Button x:Name="FormatsDownload" Content="{x:Static s:guiStrings.ButtonDownload}" Width="75" Height="25" Margin="0,0,10,10" HorizontalAlignment="Left"/>
</StackPanel>
</StackPanel>
</DockPanel>