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