|
| 1 | +using MetroFramework; |
| 2 | +using System; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Net; |
| 7 | +using System.Threading; |
| 8 | +using System.Windows.Forms; |
| 9 | +using System.Xml; |
| 10 | +using VysorProAuto; |
| 11 | + |
| 12 | +namespace EzShell |
| 13 | +{ |
| 14 | + public delegate DialogResult ShowMessageDownloade(string message, string header, MessageBoxButtons buttons, MessageBoxIcon icon); |
| 15 | + public delegate DialogResult SwMsgDwnldUpt(string message, string header, MessageBoxButtons buttons, MessageBoxIcon icon); |
| 16 | + public class CheckUpdates |
| 17 | + { |
| 18 | + private static string GetRemoteVerLinq = Constants.link + @"/Files/version.xml"; |
| 19 | + private static string GetReleaseNotesLinq = Constants.link + @"/Files/news.xml"; |
| 20 | + public static string AppName { get; set; } |
| 21 | + private static string RemoteVersion { get { return RemoteVer(); } } |
| 22 | + private static string ThisVersion { get; set; } |
| 23 | + private static double RemoteVerDouble { get { return Convert.ToDouble(RemoteVer().Replace(".", "")); } } |
| 24 | + private static double ThisVerDouble { get { return Convert.ToDouble(ThisVersion.Replace(".", "")); } } |
| 25 | + private static SwMsgDwnldUpt swMsg { get; set; } |
| 26 | + public static MetroFramework.Controls.MetroProgressBar progressBar; |
| 27 | + public static Panel panelProgress; |
| 28 | + public static Label labelProgress; |
| 29 | + private static NotifyIcon notifyIcon; |
| 30 | + public static string UrlLinq { get; set; } |
| 31 | + public CheckUpdates(string _AppName, string _ThisVers, string _UrlLinq, SwMsgDwnldUpt _sw, MetroFramework.Controls.MetroProgressBar _progressBar, NotifyIcon notify, Panel panel, Label labelU) |
| 32 | + { |
| 33 | + progressBar = _progressBar; |
| 34 | + AppName = _AppName; |
| 35 | + ThisVersion = _ThisVers; |
| 36 | + UrlLinq = _UrlLinq; |
| 37 | + swMsg = _sw; |
| 38 | + notifyIcon = notify; |
| 39 | + panelProgress = panel; |
| 40 | + labelProgress = labelU; |
| 41 | + } |
| 42 | + |
| 43 | + // tag |
| 44 | + |
| 45 | + public void UpdaterProg(SwMsgDwnldUpt swMs) |
| 46 | + { |
| 47 | + try |
| 48 | + { |
| 49 | + if (ThisVerDouble < RemoteVerDouble) |
| 50 | + { |
| 51 | + if (DialogResult.Yes == swMs("New version detected (" + RemoteVersion + ")\nThe application will be automatically updated and restarted\nWhat new:\n" + |
| 52 | + WhatNewVer(swMs), AppName + " v" + ThisVersion, MessageBoxButtons.YesNo, MessageBoxIcon.Question)) |
| 53 | + { |
| 54 | + panelProgress.Invoke(new Action(()=> panelProgress.Visible = true)); |
| 55 | + var ClientDownloader = new WebClient(); |
| 56 | + ClientDownloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(download_ProgressChanged); |
| 57 | + ClientDownloader.DownloadFileCompleted += new AsyncCompletedEventHandler(download_Completed); |
| 58 | + ClientDownloader.DownloadFileAsync(new Uri(@UrlLinq), AppName + ".zip"); |
| 59 | + } |
| 60 | + } |
| 61 | + else if (ThisVerDouble == RemoteVerDouble || ThisVerDouble > RemoteVerDouble) |
| 62 | + swMs("You using the lastest version!\nCurrent version: " + ThisVersion, "No updates available!", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 63 | + } |
| 64 | + catch (Exception exception) |
| 65 | + { |
| 66 | + swMs(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 67 | + } |
| 68 | + } |
| 69 | + private static string WhatNewVer(SwMsgDwnldUpt swMsgDwnld) |
| 70 | + { |
| 71 | + try |
| 72 | + { |
| 73 | + XmlDocument docUpdates = new XmlDocument(); |
| 74 | + docUpdates.Load(GetReleaseNotesLinq); |
| 75 | + return docUpdates.GetElementsByTagName(AppName)[0].InnerText; |
| 76 | + } |
| 77 | + catch (Exception ex) |
| 78 | + { |
| 79 | + //swMsgDwnld(ex.Message, "Can't get release notes", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 80 | + return "\nError.\nCan't load update notes"; |
| 81 | + } |
| 82 | + } |
| 83 | + private static string RemoteVer() |
| 84 | + { |
| 85 | + XmlDocument docVersion = new XmlDocument(); |
| 86 | + try |
| 87 | + { |
| 88 | + docVersion.Load(GetRemoteVerLinq); |
| 89 | + var RemoteVers = docVersion.GetElementsByTagName(AppName)[0].InnerText; |
| 90 | + return RemoteVers; |
| 91 | + } |
| 92 | + catch (Exception) |
| 93 | + { |
| 94 | + return "0"; |
| 95 | + } |
| 96 | + } |
| 97 | + public string MsgUpdateAvailable() |
| 98 | + { |
| 99 | + Thread.Sleep(1500); |
| 100 | + if (ThisVerDouble < RemoteVerDouble) |
| 101 | + { |
| 102 | + return RemoteVersion; |
| 103 | + } |
| 104 | + return null; |
| 105 | + } |
| 106 | + private void Notify_BalloonTipClicked(object sender, EventArgs e) |
| 107 | + { |
| 108 | + UpdaterProg(swMsg); |
| 109 | + } |
| 110 | + private static void download_ProgressChanged(object sender, DownloadProgressChangedEventArgs e) |
| 111 | + { |
| 112 | + progressBar.Invoke(new Action(() => progressBar.Value = e.ProgressPercentage)); |
| 113 | + labelProgress.Invoke(new Action(() =>labelProgress.Text = Installer.SizeSuffix(e.BytesReceived) + " / " + Installer.SizeSuffix(e.TotalBytesToReceive))); |
| 114 | + } |
| 115 | + private static void download_Completed(object sender, AsyncCompletedEventArgs e) |
| 116 | + { |
| 117 | + panelProgress.Invoke(new Action(() => panelProgress.Visible = false)); |
| 118 | + try |
| 119 | + { |
| 120 | + if (File.Exists("updater.exe")) |
| 121 | + { |
| 122 | + Process.Start("updater.exe", AppName); |
| 123 | + Thread.Sleep(500); |
| 124 | + Process.GetCurrentProcess().Kill(); |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + if (DialogResult.Yes== swMsg("Updater.exe not founded! Download the updater?","Attention",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation)) |
| 129 | + { |
| 130 | + var ClientDownloader = new WebClient(); |
| 131 | + ClientDownloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(download_ProgressChanged); |
| 132 | + ClientDownloader.DownloadFileCompleted += new AsyncCompletedEventHandler(download_Completed); |
| 133 | + ClientDownloader.DownloadFileAsync(new Uri(Constants.updaterexelink),"updater.exe"); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + catch (Win32Exception ex) |
| 139 | + { |
| 140 | + swMsg("Updater.exe not founded! " + |
| 141 | + "Please download the lastest version of this patcher from my website\n"+ex.Message, "Error", |
| 142 | + MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments