This repository has been archived by the owner on Sep 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.xaml.cs
116 lines (113 loc) · 3.87 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Windows;
namespace BeatmapTool
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
private const string CoreDllFileName = "BeatmapTool.Core.dll";
private const string PakFileName = "BeatmapTool.Core.pak";
private Assembly CoreAssembly { get; set; }
private ICore Core { get; set; }
public App()
{
try
{
var core = File.ReadAllBytes(CoreDllFileName);
this.CoreAssembly = Assembly.Load(core);
this.Core = (ICore)CoreAssembly.CreateInstance("BeatmapTool.Core.Main");
CheckLastVersionAsync();
this.Startup += (sender, e) => Core.Run(null);
}
catch (Exception)
{
MessageBox.Show("error load main!");
this.Shutdown(-1);
}
}
public void CheckLastVersionAsync()
{
WebClient webClient = new WebClient();
webClient.DownloadStringAsync(new Uri("http://beatmaptool.googlecode.com/files/version.json"));
webClient.DownloadStringCompleted += (sender, e) =>
{
if (e.Cancelled || e.Error != null || string.IsNullOrWhiteSpace(e.Result))
{
return;
}
try
{
var lastVersion = Newtonsoft.Json.JsonConvert.DeserializeObject<CoreVersion>(e.Result);
if (Core.Version >= lastVersion.Version)
{
return;
}
MessageBoxResult result = MessageBoxResult.Cancel;
Dispatcher.Invoke(new Action(() =>
{
result = MessageBox.Show(MainWindow, "是否更新到新版本?\n" + lastVersion.Desciption, "提示", MessageBoxButton.YesNo);
}));
switch (result)
{
case MessageBoxResult.Yes:
case MessageBoxResult.OK:
UpdateToVersion(lastVersion);
break;
}
}
catch (Exception)
{
return;
}
finally
{
((WebClient)sender).Dispose();
}
};
}
private void UpdateToVersion(CoreVersion version)
{
using (WebClient webClient = new WebClient())
{
for (int i = 0; i < version.Files.Length; i++)
{
string tmpFile = version.Files[i] + ".tmp";
try
{
webClient.DownloadFile(new Uri(version.Url[i]), tmpFile);
}
catch (Exception)
{
foreach (var file in version.Files)
{
if (File.Exists(file + ".tmp"))
{
File.Delete(file + ".tmp");
}
}
return;
}
}
foreach (var file in version.Files)
{
if (File.Exists(file))
{
File.Delete(file);
}
File.Move(file + ".tmp", file);
}
}
Process.Start(Process.GetCurrentProcess().MainModule.FileName);
Shutdown(0);
}
}
}