private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { if (!TryNextURL((int)e.UserState)) { InvokeUpdateInfoCallbacksAndClear(null, null); } } else { AppInfo info = AppInfo.FromJson(e.Result); if (info == null) { if (!TryNextURL((int)e.UserState)) { InvokeUpdateInfoCallbacksAndClear(null, null); } return; } if (!LauncherSettings.AppID.Equals(info.appId)) { MessageBox.Show("Invalid update mirror: appID does not match local value!", "An error occured", MessageBoxButtons.OK, MessageBoxIcon.Error); if (!TryNextURL((int)e.UserState)) { InvokeUpdateInfoCallbacksAndClear(null, null); } return; } InvokeUpdateInfoCallbacksAndClear(info, UpdateHost.GetUpdateHosts()[(int)e.UserState]); } }
public UpdateInfoDownloadHelper(Action <UpdateInfo, UpdateHost, int> callback, UpdateHost appInfoSource, double[] version, int i) { this.multiUpdateCallback = callback; this.appInfoSource = appInfoSource; this.versions = version; this.index = i; }
private List <UpdateTask> GetOldFileTasks(AppInfo appInfo, UpdateInfo info, UpdateHost updateInfoSource, FileIndex index) { List <UpdateTask> updateTasks = new List <UpdateTask>(); for (int i = 0; i < index.files.Count; i++) { string curFile = index.files[i]; string curLocalFile = InstallationSettings.InstallationFolder + '/' + curFile; if (info.fileChecksums.ContainsKey(curFile)) { if (File.Exists(curLocalFile)) { string curHash = FileHasher.GetFileChecksum(curLocalFile); string updatedHash; info.fileChecksums.TryGetValue(curFile, out updatedHash); if (!curHash.Equals(updatedHash)) { updateTasks.Add(new UpdateTask(TaskType.DownloadReplacementFile, updateInfoSource.GetFileURL(appInfo, info, curFile), curLocalFile)); } } else { updateTasks.Add(new UpdateTask(TaskType.DownloadNewFile, updateInfoSource.GetFileURL(appInfo, info, curFile), curLocalFile)); } } else { updateTasks.Add(new UpdateTask(TaskType.DeleteFile, curLocalFile)); } } return(updateTasks); }
public void RetrieveUpdateInfo(AppInfo appInfo, UpdateHost appInfoSource, double version, Action <UpdateInfo, UpdateHost> callback) { if (client.IsBusy) { throw new Exception("Cannot execute multiple updateinfo requests at once."); } client.DownloadStringAsync(appInfoSource.GetVersionInfoURL(appInfo, version), new UpdateInfoDownloadHelper(callback, appInfoSource, version)); }
private void InvokeUpdateInfoCallbacksAndClear(AppInfo result, UpdateHost uri) { foreach (Action <AppInfo, UpdateHost> callback in appInfoCallbacks) { callback.Invoke(result, uri); } appInfoCallbacks.Clear(); }
private UpdateTask[] GetUpdaterTasks(AppInfo appInfo, UpdateInfo info, UpdateHost updateInfoSource, FileIndex index) { List <UpdateTask> updateTasks = new List <UpdateTask>(); updateTasks.AddRange(GetOldFileTasks(appInfo, info, updateInfoSource, index)); updateTasks.AddRange(GetNewFileTasks(appInfo, info, updateInfoSource, index)); return(updateTasks.ToArray()); }
public void InvokeCallback(UpdateInfo info, UpdateHost source) { if (IsSingleUpdate) { singleUpdateCallback.Invoke(info, source); } else { multiUpdateCallback.Invoke(info, source, index); } }
private bool TryNextURL(int currentIndex) { int nextIndex = (currentIndex) + 1; if (nextIndex < UpdateHost.GetUpdateHosts().Length) { client.DownloadStringAsync(UpdateHost.GetUpdateHosts()[nextIndex].AppInfoURL, nextIndex); return(true); } return(false); }
public void ApplyUpdate(AppInfo appInfo, UpdateInfo info, UpdateHost updateInfoSource) { string lockFile = InstallationSettings.InstallationFolder + "/Updater.lock"; if (File.Exists(lockFile)) { throw new Exception("Could not update: the application folder is already locked by another updater instance."); } else { File.Create(lockFile).Close(); } TriggerProgressEvent(0, "Calculating updater tasks"); FileIndex index = FileIndex.Deserialize(InstallationSettings.InstallationFolder + "/UpdateIndex.dat"); UpdateTask[] tasks = GetUpdaterTasks(appInfo, info, updateInfoSource, index); List <UpdateTask> failedTasks = RunTasks(tasks, true, 10); TriggerProgressEvent(percentageDone, "Retrying failed tasks"); for (int i = 0; i < failedTasks.Count; i++) { UpdateTask task = failedTasks[i]; try { task.Run(); } catch (WebException ex) { if (MessageBox.Show("Updater could not download file: " + ex.Message + "\r\n(" + ex.Data["Target"] + ")", "Failed to download file", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error).Equals(DialogResult.Retry)) { i--; //Retry } else { Application.Exit(); } } catch (UnauthorizedAccessException ex) { if (MessageBox.Show("Updater could access file or folder: " + ex.Message + "\r\n(" + ex.Data["Target"] + ")", "Failed to download file", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error).Equals(DialogResult.Retry)) { i--; //Retry } else { Application.Exit(); } } } TriggerProgressEvent(98, "Updating local file index"); index = new FileIndex(index.appID); index.applicationVersion = info.version; index.files.AddRange(info.fileChecksums.Keys); index.Serialize(InstallationSettings.InstallationFolder + "/UpdateIndex.dat"); File.Delete(lockFile); }
public SingleUpdateDetailsForm(Updater updater, AppInfo appInfo, UpdateHost updateHost, double targetVersion) { this.updater = updater; this.appInfo = appInfo; this.updateHost = updateHost; this.targetVersion = targetVersion; InitializeComponent(); this.Text = LauncherLocale.Current.Get("Updater.Single.Title"); detailsTextBox.Text = LauncherLocale.Current.Get("Updater.Single.NotesPlaceholder"); cancelButton.Text = LauncherLocale.Current.Get("Updater.Single.CancelButton"); updateButton.Text = LauncherLocale.Current.Get("Updater.Single.ApplyButton"); }
public void RetrieveUpdateInfos(AppInfo appInfo, UpdateHost appInfoSource, double[] versions, Action <UpdateInfo, UpdateHost, int> callback) { if (client.IsBusy) { throw new Exception("Cannot execute multiple updateinfo requests at once."); } for (int i = 0; i < versions.Length; i++) { WebClient curClient = new WebClient(); curClient.DownloadStringCompleted += client_DownloadStringCompleted; curClient.DownloadStringAsync(appInfoSource.GetVersionInfoURL(appInfo, versions[i]), new UpdateInfoDownloadHelper(callback, appInfoSource, versions, i)); } }
public MultiUpdateDetailsForm(Updater updater, AppInfo appInfo, UpdateHost hostURL, double[] targetVersions) { this.updater = updater; this.appInfo = appInfo; this.hostURL = hostURL; this.targetVersions = targetVersions; updateInfos = new UpdateInfo[targetVersions.Length]; InitializeComponent(); this.Text = LauncherLocale.Current.Get("Updater.Multi.Title"); infoLabel.Text = LauncherLocale.Current.Get("Updater.Multi.InfoLabel"); updateNotesDropdownLabel.Text = LauncherLocale.Current.Get("Updater.Multi.NotesDropDownLabel"); updateNotesComboBox.Items.AddRange(targetVersions.ToList().ConvertAll <string>(d => VersionFormatter.ToString(d)).ToArray()); detailsTextBox.Text = LauncherLocale.Current.Get("Updater.Multi.NotesPlaceholder"); cancelButton.Text = LauncherLocale.Current.Get("Updater.Multi.CancelButton"); updateButton.Text = LauncherLocale.Current.Get("Updater.Multi.ApplyButton").Replace("${updateCount}", targetVersions.Length.ToString()); }
private List <UpdateTask> GetNewFileTasks(AppInfo appInfo, UpdateInfo info, UpdateHost updateInfoSource, FileIndex index) { List <UpdateTask> updateTasks = new List <UpdateTask>(); for (int i = 0; i < info.fileChecksums.Keys.Count; i++) { string curFile = info.fileChecksums.Keys.ElementAt(i); string curLocalFile = InstallationSettings.InstallationFolder + '/' + curFile; if (index.files.Contains(curFile)) { //Already handled file continue; } if (File.Exists(curLocalFile)) { //File exists but is missing in index? string curHash = FileHasher.GetFileChecksum(curLocalFile); string updatedHash; info.fileChecksums.TryGetValue(curFile, out updatedHash); if (updatedHash.Equals(curHash)) { continue; } else { updateTasks.Add(new UpdateTask(TaskType.DownloadReplacementFile, updateInfoSource.GetFileURL(appInfo, info, curFile), curLocalFile)); } } else { updateTasks.Add(new UpdateTask(TaskType.DownloadNewFile, updateInfoSource.GetFileURL(appInfo, info, curFile), curLocalFile)); } } return(updateTasks); }
public UpdateInfoDownloadHelper(Action <UpdateInfo, UpdateHost> callback, UpdateHost appInfoSource, double version) { this.singleUpdateCallback = callback; this.appInfoSource = appInfoSource; this.versions = new double[] { version }; }
public void RetrieveUpdateInfos(AppInfo appInfo, UpdateHost appInfoSource, double[] version, Action <UpdateInfo, UpdateHost, int> callback) { updateInfoHelper.RetrieveUpdateInfos(appInfo, appInfoSource, version, callback); }
public void RetrieveAppInfo(Action <AppInfo, UpdateHost> callback) { client.DownloadStringCompleted += client_DownloadStringCompleted; appInfoCallbacks.Add(callback); client.DownloadStringAsync(UpdateHost.GetUpdateHosts()[0].AppInfoURL, 0); }