markus / MarkusAutoUpdate / src / NetSparkle.Samples.NetFramework.WPF / MainWindow.xaml.cs @ 7e810f54
이력 | 보기 | 이력해설 | 다운로드 (12.6 KB)
1 | 8de0cf00 | taeseongkim | using NetSparkle.TestAppWPF.Common; |
---|---|---|---|
2 | 77cdac33 | taeseongkim | using NetSparkle.TestAppWPF.ViewModel; |
3 | d8f5045e | taeseongkim | using NetSparkleUpdater.Events; |
4 | using NetSparkleUpdater.SignatureVerifiers; |
||
5 | using System; |
||
6 | using System.Diagnostics; |
||
7 | using System.Drawing; |
||
8 | using System.IO; |
||
9 | using System.IO.Compression; |
||
10 | using System.Linq; |
||
11 | using System.Security; |
||
12 | using System.Threading.Tasks; |
||
13 | using System.Windows; |
||
14 | |||
15 | |||
16 | namespace NetSparkleUpdater.Samples.NetFramework.WPF |
||
17 | { |
||
18 | /// <summary> |
||
19 | /// Interaction logic for MainWindow.xaml |
||
20 | /// </summary> |
||
21 | public partial class MainWindow : Window |
||
22 | { |
||
23 | private SparkleUpdater _sparkle; |
||
24 | private const string InstallPath = @"C:\Program Files\Doftech\MARKUS"; |
||
25 | private string KcomPath = @"C:\Program Files\Doftech\MARKUS\KCOM.exe"; |
||
26 | |||
27 | private string downloadFile; |
||
28 | private string tempStoragePath; |
||
29 | |||
30 | private NetSparkle.TestAppWPF.ViewModels.MainViewModel viewModel; |
||
31 | |||
32 | public MainWindow() |
||
33 | { |
||
34 | InitializeComponent(); |
||
35 | viewModel = new NetSparkle.TestAppWPF.ViewModels.MainViewModel(); |
||
36 | this.DataContext = viewModel; |
||
37 | |||
38 | //remove the netsparkle key from registry z |
||
39 | //try |
||
40 | //{ |
||
41 | // Microsoft.Win32.Registry.CurrentUser.DeleteSubKeyTree("Software\\Microsoft\\NetSparkle.TestAppNetCoreWPF"); |
||
42 | //} |
||
43 | //catch (Exception ex) |
||
44 | //{ |
||
45 | // System.Diagnostics.Debug.WriteLine(ex); |
||
46 | //} |
||
47 | |||
48 | // set icon in project properties! |
||
49 | try |
||
50 | { |
||
51 | string manifestModuleName = System.Reflection.Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName; |
||
52 | var icon = System.Drawing.Icon.ExtractAssociatedIcon(manifestModuleName); |
||
53 | |||
54 | tempStoragePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "MARKUS", System.IO.Path.GetRandomFileName()); |
||
55 | |||
56 | App.logger.Info($"Temp Strorage Create : {tempStoragePath}"); |
||
57 | |||
58 | if (!Directory.Exists(tempStoragePath)) |
||
59 | { |
||
60 | Directory.CreateDirectory(tempStoragePath); |
||
61 | } |
||
62 | |||
63 | 8f86cf42 | taeseongkim | if (!Directory.Exists(InstallPath)) |
64 | { |
||
65 | 77cdac33 | taeseongkim | CheckAdmin(); |
66 | 8f86cf42 | taeseongkim | Directory.CreateDirectory(InstallPath); |
67 | } |
||
68 | |||
69 | d8f5045e | taeseongkim | //File.Copy(KcomPath, System.IO.Path.Combine(tempStoragePath, "Kcom.exe")); |
70 | |||
71 | //KcomPath = System.IO.Path.Combine(tempStoragePath, "Kcom.exe"); |
||
72 | |||
73 | if (!File.Exists(KcomPath)) |
||
74 | { |
||
75 | 77cdac33 | taeseongkim | CheckAdmin(); |
76 | d8f5045e | taeseongkim | File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "kcom.exe"), KcomPath); |
77 | } |
||
78 | |||
79 | 1e91e783 | taeseongkim | _sparkle = new SparkleUpdater(App.AppCastUri, new Ed25519Checker(Enums.SecurityMode.Unsafe, null, "NetSparkle_Ed25519.pub"), KcomPath) |
80 | d8f5045e | taeseongkim | { |
81 | UIFactory = new NetSparkleUpdater.UI.WPF.UIFactory(NetSparkleUpdater.UI.WPF.IconUtilities.ToImageSource(icon)), |
||
82 | ShowsUIOnMainThread = true, |
||
83 | TmpDownloadFilePath = tempStoragePath, |
||
84 | UserInteractionMode = Enums.UserInteractionMode.DownloadNoInstall, |
||
85 | //UseNotificationToast = true |
||
86 | RelaunchAfterUpdate = true, |
||
87 | 4fae0905 | taeseongkim | SecurityProtocolType = System.Net.SecurityProtocolType.Tls12 |
88 | }; |
||
89 | d8f5045e | taeseongkim | |
90 | // TLS 1.2 required by GitHub (https://developer.github.com/changes/2018-02-01-weak-crypto-removal-notice/) |
||
91 | _sparkle.DownloadFinished += FullUpdate_DownloadFileIsReady; |
||
92 | _sparkle.UpdateCheckFinished += _sparkle_UpdateCheckFinished; |
||
93 | _sparkle.DownloadHadError += _sparkle_DownloadError; |
||
94 | _sparkle.DownloadMadeProgress += _sparkle_DownloadMadeProgress; |
||
95 | //_sparkle.StartLoop(true, true); |
||
96 | Updater(); |
||
97 | App.logger.Info("main Initialize"); |
||
98 | |||
99 | this.Loaded += MainWindow_Loaded; |
||
100 | } |
||
101 | catch (Exception ex) |
||
102 | { |
||
103 | App.logger.Error("main Initialize", ex); |
||
104 | } |
||
105 | |||
106 | } |
||
107 | 77cdac33 | taeseongkim | private void CheckAdmin() |
108 | { |
||
109 | if (!VistaSecurity.IsAdmin()) |
||
110 | { |
||
111 | var param = new ExtractParam(App.KcomParam); |
||
112 | |||
113 | VistaSecurity.RestartElevated($"ADMIN {ObjectToBytesStringConvert.ObjectToBytesString(param)}"); |
||
114 | } |
||
115 | } |
||
116 | d8f5045e | taeseongkim | |
117 | private void _sparkle_DownloadMadeProgress(object sender, AppCastItem item, ItemDownloadProgressEventArgs args) |
||
118 | { |
||
119 | viewModel.DownloadProgress = args.ProgressPercentage; |
||
120 | } |
||
121 | |||
122 | private void _sparkle_DownloadError(AppCastItem item, string path, Exception exception) |
||
123 | { |
||
124 | App.logger.Error($"Download Error", exception); |
||
125 | } |
||
126 | |||
127 | private async void Updater() |
||
128 | { |
||
129 | App.logger.Info("main Load"); |
||
130 | try |
||
131 | { |
||
132 | var _updateInfo = await _sparkle.CheckForUpdatesQuietly(); |
||
133 | |||
134 | if (_updateInfo != null) |
||
135 | { |
||
136 | App.logger.Info("Update Status : " + _updateInfo.Status); |
||
137 | |||
138 | switch (_updateInfo.Status) |
||
139 | { |
||
140 | case Enums.UpdateStatus.UpdateAvailable: |
||
141 | this.Visibility = Visibility.Visible; |
||
142 | //string a = "There's an update available!"; |
||
143 | var castitem = _updateInfo.Updates.First(); |
||
144 | |||
145 | viewModel.UpdateVersion = castitem.Version; |
||
146 | System.Threading.Thread.Sleep(1000); |
||
147 | 8de0cf00 | taeseongkim | |
148 | if (App.IsExternal) |
||
149 | { |
||
150 | if (!string.IsNullOrWhiteSpace(castitem.DownloadLink)) |
||
151 | { |
||
152 | var uri = UriHelper.ChangeAddress(new Uri(castitem.DownloadLink), App.ExternalAddress); |
||
153 | castitem.DownloadLink = uri.ToString(); |
||
154 | } |
||
155 | |||
156 | if (!string.IsNullOrWhiteSpace(castitem.ReleaseNotesLink)) |
||
157 | { |
||
158 | var uri2 = UriHelper.ChangeAddress(new Uri(castitem.ReleaseNotesLink), App.ExternalAddress); |
||
159 | castitem.ReleaseNotesLink = uri2.ToString(); |
||
160 | } |
||
161 | |||
162 | } |
||
163 | d8f5045e | taeseongkim | |
164 | await _sparkle.InitAndBeginDownload(castitem); |
||
165 | |||
166 | App.logger.Info($"Update available Version : {castitem.Version} Link : {castitem.DownloadLink}"); |
||
167 | |||
168 | break; |
||
169 | case Enums.UpdateStatus.UpdateNotAvailable: |
||
170 | case Enums.UpdateStatus.CouldNotDetermine: |
||
171 | _sparkle.CloseApplication += () => |
||
172 | { |
||
173 | App.logger.Info($"Start Markus : {App.KcomParam}"); |
||
174 | |||
175 | Process.Start(KcomPath, App.KcomParam); |
||
176 | Application.Current.Shutdown(); |
||
177 | }; |
||
178 | |||
179 | App.logger.Info($"Update Not available. Application Exit."); |
||
180 | await _sparkle.QuitApplication(); |
||
181 | |||
182 | break; |
||
183 | } |
||
184 | } |
||
185 | else |
||
186 | { |
||
187 | if (_updateInfo != null) |
||
188 | { |
||
189 | App.logger.Error("Update Is Null"); |
||
190 | Process.Start(KcomPath, App.KcomParam); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | catch (Exception ex) |
||
195 | { |
||
196 | App.logger.Error($"main Load", ex); |
||
197 | } |
||
198 | |||
199 | } |
||
200 | |||
201 | private async void MainWindow_Loaded(object sender, RoutedEventArgs e) |
||
202 | { |
||
203 | |||
204 | } |
||
205 | |||
206 | private void _sparkle_UpdateCheckFinished(object sender, Enums.UpdateStatus status) |
||
207 | { |
||
208 | |||
209 | } |
||
210 | |||
211 | //[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] |
||
212 | private void FullUpdate_DownloadFileIsReady(AppCastItem item, string downloadPath) |
||
213 | { |
||
214 | viewModel.IsWait = true; |
||
215 | |||
216 | ExtractFiles(InstallPath,tempStoragePath,downloadPath,KcomPath, App.KcomParam); |
||
217 | 77cdac33 | taeseongkim | if (VistaSecurity.IsAdmin()) |
218 | { |
||
219 | ExtractFiles(InstallPath, tempStoragePath, downloadPath, KcomPath, App.KcomParam); |
||
220 | } |
||
221 | else |
||
222 | { |
||
223 | var param = new ExtractParam(tempStoragePath, downloadPath, App.KcomParam); |
||
224 | VistaSecurity.RestartElevated($"EXT {ObjectToBytesStringConvert.ObjectToBytesString(param)}"); |
||
225 | } |
||
226 | d8f5045e | taeseongkim | //ExtractProcess(item, downloadPath); |
227 | } |
||
228 | |||
229 | [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] |
||
230 | private void ExtractFiles(string InstallPath, string tempStoragePath, string downloadFile, string KcomPath, string Param) |
||
231 | { |
||
232 | viewModel.IsWait = true; |
||
233 | try |
||
234 | { |
||
235 | var extractFolder = Path.Combine(tempStoragePath, "Extract"); |
||
236 | |||
237 | ZipFile.ExtractToDirectory(downloadFile, extractFolder); |
||
238 | |||
239 | foreach (var file in Directory.EnumerateFiles(extractFolder, "*.*", SearchOption.AllDirectories)) |
||
240 | { |
||
241 | var target = System.IO.Path.Combine(InstallPath, file.Replace(extractFolder, "").Substring(1)); |
||
242 | |||
243 | var dir = Path.GetDirectoryName(target); |
||
244 | |||
245 | if (!Directory.Exists(dir)) |
||
246 | { |
||
247 | Directory.CreateDirectory(dir); |
||
248 | } |
||
249 | |||
250 | File.Copy(file, target, FileExits(target)); |
||
251 | App.logger.Info($"Copy - {target}"); |
||
252 | } |
||
253 | |||
254 | Process.Start(KcomPath, Param); |
||
255 | } |
||
256 | catch (Exception ex) |
||
257 | { |
||
258 | App.logger.Error($"ExtractFile Error", ex); |
||
259 | |||
260 | if (ex.InnerException != null) |
||
261 | { |
||
262 | App.logger.Error($"ExtractFile Error InnerException", ex.InnerException); |
||
263 | } |
||
264 | } |
||
265 | finally |
||
266 | { |
||
267 | Application.Current.Shutdown(); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | private static bool FileExits(string fileName) |
||
272 | { |
||
273 | try |
||
274 | { |
||
275 | return File.Exists(fileName); |
||
276 | } |
||
277 | catch (Exception) |
||
278 | { |
||
279 | return false; |
||
280 | } |
||
281 | } |
||
282 | |||
283 | private void ExtractProcess(AppCastItem item, string downloadPath) |
||
284 | { |
||
285 | try |
||
286 | { |
||
287 | downloadFile = downloadPath; |
||
288 | |||
289 | //var args = $"{InstallPath} {tempStoragePath} {downloadFile} {KcomPath} {App.KcomParam}"; |
||
290 | |||
291 | string endpoint = IIpc.Commons.shortGuid(); |
||
292 | |||
293 | var wcfServer = new IIpc.WcfServer(endpoint + "M"); |
||
294 | wcfServer.Start(); |
||
295 | |||
296 | wcfServer.ProcessStart += (snd, ect) => |
||
297 | { |
||
298 | var ipcClient = new IIpc.WcfClient(endpoint + "E"); |
||
299 | try |
||
300 | { |
||
301 | ipcClient.ParamReceived(InstallPath, tempStoragePath, downloadFile, KcomPath, App.KcomParam); |
||
302 | |||
303 | //Application.Current.Shutdown(); |
||
304 | } |
||
305 | catch (Exception ex) |
||
306 | { |
||
307 | App.logger.Error($"ParamReceived ", ex); |
||
308 | } |
||
309 | }; |
||
310 | |||
311 | //MarkusExtract.GetFile(); |
||
312 | |||
313 | var exrtractFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "MarkusExtractUpdate", @"MarkusUpdateExtract.exe"); |
||
314 | |||
315 | Process DownloadProcess = new Process(); |
||
316 | |||
317 | ProcessStartInfo startInfo = new ProcessStartInfo |
||
318 | { |
||
319 | UseShellExecute = false, |
||
320 | FileName = exrtractFile, |
||
321 | WindowStyle = ProcessWindowStyle.Hidden, |
||
322 | CreateNoWindow = true, |
||
323 | ErrorDialog = false, |
||
324 | Verb = "runas", |
||
325 | RedirectStandardInput = false, |
||
326 | RedirectStandardError = false, |
||
327 | Arguments = endpoint |
||
328 | }; |
||
329 | |||
330 | //$"{ InstallPath} {tempStoragePath} {downloadFile} {KcomPath} {param}" |
||
331 | |||
332 | DownloadProcess.StartInfo = startInfo; |
||
333 | DownloadProcess.EnableRaisingEvents = false; |
||
334 | |||
335 | DownloadProcess.Start(); |
||
336 | } |
||
337 | catch (Exception ex) |
||
338 | { |
||
339 | App.logger.Error($"DownloadFileIsReady ", ex); |
||
340 | } |
||
341 | |||
342 | } |
||
343 | } |
||
344 | } |