Skip to content

Commit

Permalink
アップデーター改良
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtska committed Jun 10, 2018
1 parent ced92c3 commit 54a82f9
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 324 deletions.
31 changes: 3 additions & 28 deletions SRNicoNico/Models/NicoNicoViewer/UpdateChecker.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Codeplex.Data;
using System;
using System.Threading.Tasks;
using Livet;
using System.Net;

using Codeplex.Data;

namespace SRNicoNico.Models.NicoNicoViewer {
public class UpdateChecker {

//アップデート確認するURL
#if DEBUG
private const string CheckUrl = "https://mrtska.net/niconicowrapper/debugupdate";
#else
private const string CheckUrl = "https://mrtska.net/niconicoviewer/latest";
#endif

public static async Task<string> GetUpdaterBinaryUrl() {

try {

var a = await App.ViewModelRoot.CurrentUser.Session.GetAsync(CheckUrl);

var json = DynamicJson.Parse(a);

return json.updater;
} catch(Exception) {

return "";
}
}

public static async Task<bool> IsUpdateAvailable() {

Expand All @@ -40,7 +15,7 @@ public static async Task<bool> IsUpdateAvailable() {
var a = await App.ViewModelRoot.CurrentUser.Session.GetAsync(CheckUrl);

var json = DynamicJson.Parse(a);
return App.ViewModelRoot.CurrentVersion < json.version;
return App.ViewModelRoot.CurrentVersion < json.version;
} catch(Exception) {

return false;
Expand Down
10 changes: 5 additions & 5 deletions SRNicoNico/ViewModels/Misc/UpdaterViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class UpdaterViewModel : ViewModel {

public string AssemblyDirectory {
get {
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Expand All @@ -41,12 +41,12 @@ public async void Update() {

using(var wc = new WebClient()) {

await wc.DownloadFileTaskAsync(await UpdateChecker.GetUpdaterBinaryUrl(), updater);
await wc.DownloadFileTaskAsync("https://mrtska.net/download/niconicoviewer/NicoNicoViewerUpdater.exe", updater);
}

var process = new Process();
process.StartInfo.FileName = updater;
process.StartInfo.Arguments = "iris \"" + AssemblyDirectory + "\"";
process.StartInfo.Arguments = "iris \"" + AssemblyDirectory + "\" " + Process.GetCurrentProcess().Id;
process.Start();
Environment.Exit(0);

Expand Down
25 changes: 1 addition & 24 deletions Updater/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using Livet;
using System.Windows;

using Livet;

namespace Updater {
/// <summary>
/// App.xaml の相互作用ロジック
/// </summary>
public partial class App : Application {
private void Application_Startup(object sender, StartupEventArgs e) {
DispatcherHelper.UIDispatcher = Dispatcher;
//AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}

//集約エラーハンドラ
//private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
//{
// //TODO:ロギング処理など
// MessageBox.Show(
// "不明なエラーが発生しました。アプリケーションを終了します。",
// "エラー",
// MessageBoxButton.OK,
// MessageBoxImage.Error);
//
// Environment.Exit(1);
//}
}
}
81 changes: 81 additions & 0 deletions Updater/Models/InstallProcess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Updater.ViewModels;
using Microsoft.Win32;
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Threading.Tasks;
using System.Threading;

namespace Updater.Models {
public class InstallProcess {

private readonly MainWindowViewModel Owner;

public InstallProcess(MainWindowViewModel vm) {

Owner = vm;
}

public async Task InstallAsync() {

Owner.Status = "レジストリ登録中";
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", "SRNicoNico.exe", 0x00002AF8, RegistryValueKind.DWord);
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_GPU_RENDERING", "SRNicoNico.exe", 0x00000001, RegistryValueKind.DWord);

var location = Owner.InstallLocation;

var settings = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SRNicoNico");

if (Directory.Exists(location)) {

Owner.Status = "元ファイルバックアップ中";
if (File.Exists(Path.Combine(settings, "backup.zip"))) {

Owner.Status = "古いバックアップファイルを削除中";
File.Delete(Path.Combine(settings, "backup.zip"));
}

ZipFile.CreateFromDirectory(location, Path.Combine(settings, "backup.zip"), CompressionLevel.Optimal, true);
}

Owner.Status = "ディレクトリ作成中";
Directory.CreateDirectory(location);
Directory.CreateDirectory(Path.Combine(location, "tmp"));
Directory.CreateDirectory(settings);

using (var wc = new WebClient()) {

Owner.Status = "本体ダウンロード中";
await wc.DownloadFileTaskAsync(await UpdateChecker.GetLatestBinaryUrl(wc), Path.Combine(location, @"tmp\data"));
}

Owner.Status = "解凍中";
using (var archive = ZipFile.OpenRead(Path.Combine(location, @"tmp\data"))) {

retry:
try {
foreach (var entry in archive.Entries) {

var fullname = Path.Combine(location, entry.FullName);
var dir = Path.GetDirectoryName(fullname);
if (!Directory.Exists(fullname)) {

Directory.CreateDirectory(dir);
}
if (!string.IsNullOrEmpty(Path.GetFileName(fullname))) {

entry.ExtractToFile(fullname, true);
}
}
} catch(Exception) {

Thread.Sleep(100);
goto retry;
}

}
Owner.Status = "OKOK";
}
}
}
10 changes: 3 additions & 7 deletions Updater/Models/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ namespace Updater.Models {
public class UpdateChecker {

//アップデート確認するURL
#if DEBUG
private static string CheckUrl = "https://mrtska.net/niconicowrapper/debugupdate";
#else
private static string CheckUrl = "http://download.mrtska.net/DownloadCounter/Download?file=NicoNicoViewer/releaseupdate";
#endif
private const string CheckUrl = "https://mrtska.net/niconicoviewer/latest";

public static async Task<string> GetLatestBinaryUrl(WebClient wc) {

Expand All @@ -26,8 +22,8 @@ public static async Task<string> GetLatestBinaryUrl(WebClient wc) {

var json = DynamicJson.Parse(a);

return json.url;
} catch(Exception) {
return "https://mrtska.net/niconicoviewer/download/" + json.version;
} catch (Exception) {

return null;
}
Expand Down
12 changes: 5 additions & 7 deletions Updater/Updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,15 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>InfrastructureAssemblies\Livet.dll</HintPath>
</Reference>
<Reference Include="MetroRadiance, Version=2.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MetroRadiance.2.3.0\lib\net46\MetroRadiance.dll</HintPath>
<Private>True</Private>
<Reference Include="MetroRadiance, Version=2.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MetroRadiance.2.4.0\lib\net46\MetroRadiance.dll</HintPath>
</Reference>
<Reference Include="MetroRadiance.Chrome, Version=2.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MetroRadiance.Chrome.2.2.0\lib\net46\MetroRadiance.Chrome.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MetroRadiance.Core, Version=2.2.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MetroRadiance.Core.2.2.1\lib\net46\MetroRadiance.Core.dll</HintPath>
<Private>True</Private>
<Reference Include="MetroRadiance.Core, Version=2.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MetroRadiance.Core.2.4.0\lib\net46\MetroRadiance.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Expression.Interactions">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -96,6 +94,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Models\InstallProcess.cs" />
<Compile Include="Models\UpdateChecker.cs" />
<Compile Include="Views\ProgressView.xaml.cs">
<DependentUpon>ProgressView.xaml</DependentUpon>
Expand All @@ -111,7 +110,6 @@
<Compile Include="DynamicJson.cs" />
<Compile Include="EarlyStartUp.cs" />
<Compile Include="ViewModels\MainWindowViewModel.cs" />
<Compile Include="ViewModels\ProgressViewModel.cs" />
<Compile Include="Views\MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down
58 changes: 30 additions & 28 deletions Updater/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using Livet;
using Livet.Commands;
using Livet.Messaging;
using Livet.Messaging.IO;
using Livet.EventListeners;
using Livet.Messaging.Windows;

using Livet;
using System;
using System.Diagnostics;
using System.IO;
using Updater.Models;
using System.Windows;

namespace Updater.ViewModels {
public class MainWindowViewModel : ViewModel {
Expand All @@ -31,7 +21,6 @@ public int Index {
}
#endregion


#region PrevButtonAvailable変更通知プロパティ
private bool _PrevButtonAvailable = false;

Expand All @@ -46,7 +35,6 @@ public bool PrevButtonAvailable {
}
#endregion


#region NextButtonAvailable変更通知プロパティ
private bool _NextButtonAvailable = true;

Expand All @@ -61,24 +49,24 @@ public bool NextButtonAvailable {
}
#endregion

#region Status変更通知プロパティ
private string _Status = string.Empty;

#region ProgressViewModel変更通知プロパティ
private ProgressViewModel _ProgressViewModel;

public ProgressViewModel ProgressViewModel {
get { return _ProgressViewModel; }
public string Status {
get { return _Status; }
set {
if(_ProgressViewModel == value)
if (_Status == value)
return;
_ProgressViewModel = value;
_Status = value;
RaisePropertyChanged();
}
}
#endregion


internal string InstallLocation = "";

private readonly InstallProcess InstallProcess;

public MainWindowViewModel() {

var args = Environment.GetCommandLineArgs();
Expand All @@ -87,14 +75,26 @@ public MainWindowViewModel() {

Environment.Exit(0);
}
if(args.Length == 4) {

int pid = int.Parse(args[3]);
Process.GetProcessById(pid).WaitForExit();
}

InstallLocation = args[2] + @"\";
InstallProcess = new InstallProcess(this);
}

public void EndOfLife() {

ProgressViewModel = new ProgressViewModel(this);
Environment.Exit(0);
}

public async void Initialize() {

public void Initialize() {
await InstallProcess.InstallAsync();
Next();
Status = "アップデート完了\n手動で起動してください";
}

public void Prev() {
Expand All @@ -105,8 +105,10 @@ public void Prev() {
public void Next() {

Index++;
}

if(Index == 2) {

EndOfLife();
}
}
}
}
Loading

0 comments on commit 54a82f9

Please sign in to comment.