| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Diagnostics; |
| 4 | using System.IO; |
| 5 | using System.Linq; |
| 6 | using System.Net; |
| 7 | using System.Text; |
| 8 | using System.Threading.Tasks; |
| 9 | using System.Windows; |
| 10 | using System.Windows.Controls; |
| 11 | using System.Windows.Data; |
| 12 | using System.Windows.Documents; |
| 13 | using System.Windows.Input; |
| 14 | using System.Windows.Media; |
| 15 | using System.Windows.Media.Imaging; |
| 16 | using System.Windows.Navigation; |
| 17 | using System.Windows.Shapes; |
| 18 | using static Updater.GithubRelease; |
| 19 | |
| 20 | namespace Updater |
| 21 | { |
| 22 | ///<summary> |
| 23 | /// |
| 24 | ///</summary> |
| 25 | public partial class MainWindow : Window |
| 26 | { |
| 27 | GithubRelease githubRelease; |
| 28 | |
| 29 | ///<summary> |
| 30 | /// |
| 31 | ///</summary> |
| 32 | private string SaveDir = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory); |
| 33 | ///<summary> |
| 34 | /// |
| 35 | ///</summary> |
| 36 | private string SaveName = "update.zip"; |
| 37 | ///<summary> |
| 38 | /// |
| 39 | ///</summary> |
| 40 | private string NewVersionZipURL; |
| 41 | ///<summary> |
| 42 | /// |
| 43 | ///</summary> |
| 44 | private string NewVersionURL; |
| 45 | private MainModel mainModel; |
| 46 | public MainWindow() |
| 47 | { |
| 48 | InitializeComponent(); |
| 49 | |
| 50 | mainModel = new MainModel(); |
| 51 | |
| 52 | DataContext = mainModel; |
| 53 | |
| 54 | mainModel.PropertyChanged += MainModel_PropertyChanged; |
| 55 | } |
| 56 | |
| 57 | private void MainModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) |
| 58 | { |
| 59 | if (e.PropertyName == nameof(mainModel.Version)) |
| 60 | { |
| 61 | githubRelease = new GithubRelease("https://api.github.com/repos/planshit/tai/releases/latest", mainModel.Version); |
| 62 | |
| 63 | Check(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private async void Download() |
| 68 | { |
| 69 | SetStatus("正在下载新版本文件...", false); |
| 70 | |
| 71 | UpdateBtn.Visibility = Visibility.Collapsed; |
| 72 | ReCheckBtn.Visibility = Visibility.Collapsed; |
| 73 | ProgressBar.Visibility = Visibility.Visible; |
| 74 | mainModel.ProcessValue = 0; |
| 75 | |
| 76 | var res = await Task.Run(() => |
| 77 | { |
| 78 | try |
| 79 | { |
| 80 | |
| 81 | if (!Directory.Exists(SaveDir)) |
| 82 | { |
| 83 | Directory.CreateDirectory(SaveDir); |
| 84 | } |
| 85 | |
| 86 | Uri uri = new Uri(NewVersionZipURL); |
| 87 | HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); |
| 88 | httpWebRequest.Timeout = 120 * 1000; |
| 89 | HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); |
| 90 | |
| 91 | |
| 92 | long totalBytes = httpWebResponse.ContentLength; |
| 93 | |
| 94 | Stream st = httpWebResponse.GetResponseStream(); |
| 95 | Stream so = new FileStream(System.IO.Path.Combine(SaveDir, SaveName), FileMode.Create); |
| 96 | |
| 97 | long totalDownloadedByte = 0; |
| 98 | byte[] by = new byte[1024]; |
| 99 | int osize = st.Read(by, 0, (int)by.Length); |
| 100 | while (osize > 0) |
| 101 | { |
| 102 | |
| 103 | totalDownloadedByte = osize + totalDownloadedByte; |
| 104 | so.Write(by, 0, osize); |
| 105 | |
| 106 | osize = st.Read(by, 0, (int)by.Length); |
| 107 | |
| 108 | |
| 109 | double process = double.Parse(String.Format("{0:F}", |
| 110 | ((double)totalDownloadedByte / (double)totalBytes * 100))); |
| 111 | mainModel.ProcessValue = process; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | httpWebResponse.Close(); |
| 116 | so.Close(); |
| 117 | st.Close(); |
| 118 | |
| 119 | return true; |
| 120 | |
| 121 | } |
| 122 | catch (Exception ec) |
| 123 | { |
| 124 | return false; |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | if (res) |
| 129 | { |
| 130 | |
| 131 | var process = Process.GetProcessesByName("Tai"); |
| 132 | if (process != null && process.Length > 0) |
| 133 | { |
| 134 | process[0].Kill(); |
| 135 | } |
| 136 | |
| 137 | SetStatus("下载完成,正在解压请勿关闭此窗口..."); |
| 138 | |
| 139 | string unpath = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName; |
| 140 | |
| 141 | var unresult = await Task.Run(async () => |
| 142 | { |
| 143 | await Task.Delay(3000); |
| 144 | return Unzip.ExtractZipFile(System.IO.Path.Combine(SaveDir, SaveName), unpath); |
| 145 | }); |
| 146 | if (unresult) |
| 147 | { |
| 148 | SetStatus("更新完成!", false); |
| 149 | Process tai = new Process(); |
| 150 | ProcessStartInfo startInfo = new ProcessStartInfo(System.IO.Path.Combine(unpath, "Tai.exe")); |
| 151 | tai.StartInfo = startInfo; |
| 152 | tai.Start(); |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | SetStatus("解压文件时发生异常,请重试!通常情况可能是因为Tai主程序尚未退出。", false); |
| 157 | UpdateBtn.Visibility = Visibility.Visible; |
| 158 | |
| 159 | } |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | |
| 164 | SetStatus("下载时发生异常,请重试。", false); |
| 165 | UpdateBtn.Visibility = Visibility.Visible; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | |
| 170 | private async void Check() |
| 171 | { |
| 172 | NewVersionSP.Visibility = Visibility.Collapsed; |
| 173 | PreTag.Visibility = Visibility.Collapsed; |
| 174 | |
| 175 | SetStatus("正在检查更新"); |
| 176 | UpdateBtn.Visibility = Visibility.Collapsed; |
| 177 | ReCheckBtn.IsEnabled = false; |
| 178 | |
| 179 | var info = await githubRelease.GetRequest(); |
| 180 | |
| 181 | if (info != null) |
| 182 | { |
| 183 | if (githubRelease.IsCanUpdate()) |
| 184 | { |
| 185 | UpdateBtn.Visibility = Visibility.Visible; |
| 186 | |
| 187 | NewVersionSP.Visibility = Visibility.Visible; |
| 188 | Version.Text = info.Version; |
| 189 | VersionTitle.Text = info.Title; |
| 190 | NewVersionZipURL = info.DownloadUrl; |
| 191 | NewVersionURL = info.HtmlUrl; |
| 192 | if (info.IsPre) |
| 193 | { |
| 194 | PreTag.Visibility = Visibility.Visible; |
| 195 | } |
| 196 | SetStatus("检测到新的版本!", false); |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | SetStatus("目前没有可用的更新。", false); |
| 201 | } |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | SetStatus("无法获取版本信息,请检查代理或网络。", false); |
| 206 | } |
| 207 | ReCheckBtn.IsEnabled = true; |
| 208 | |
| 209 | } |
| 210 | |
| 211 | private void SetStatus(string statusText, bool isLoading = true) |
| 212 | { |
| 213 | StatusLabel.Text = statusText; |
| 214 | ProgressBar.IsIndeterminate = isLoading; |
| 215 | if (isLoading) |
| 216 | { |
| 217 | ProgressBar.Visibility = Visibility.Visible; |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | ProgressBar.Visibility = Visibility.Collapsed; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | private void ReCheckBtn_Click(object sender, RoutedEventArgs e) |
| 226 | { |
| 227 | Check(); |
| 228 | } |
| 229 | |
| 230 | private void UpdateBtn_Click(object sender, RoutedEventArgs e) |
| 231 | { |
| 232 | Download(); |
| 233 | } |
| 234 | |
| 235 | private void Hyperlink_Click(object sender, RoutedEventArgs e) |
| 236 | { |
| 237 | Process.Start(new ProcessStartInfo(NewVersionURL)); |
| 238 | } |
| 239 | } |
| 240 | } |