private static bool SetProgress(AutoupdateProgressCallback progressCallback, string lobbyName, int numBytes, int totalBytes, int downloadedFiles, int totalFiles, DateTime downloadStart) { if (numBytes <= 0 || totalBytes <= 0) { return(true); } int progress = (int)((float)numBytes / totalBytes * 100); if (progressCallback != null) { return(progressCallback( lobbyName, string.Format("Downloading Updates ({0}/{1})\n\n{2} kB of {3} kB - {4} kB/s", downloadedFiles, totalFiles, numBytes / 1024, totalBytes / 1024, Math.Round(numBytes / 1024 / DateTime.Now.Subtract(downloadStart).TotalSeconds)), progress)); } return(true); }
private static bool DownloadFile(int index, int count, string baseUrl, int lobbyId, string filename, string destination, string lobbyName, AutoupdateProgressCallback progressCallback) { bool continueDownloading = true; const int interval = 800; int lastUpdate = Environment.TickCount - interval; var request = HttpWebRequest.Create(string.Format("{0}/Files/{1}/{2}", baseUrl, lobbyId, filename)); using (var response = request.GetResponse()) using (var sr = new BinaryReader(response.GetResponseStream())) using (var sw = new BinaryWriter(File.Open(destination, FileMode.Create))) { int length; if (Int32.TryParse(response.Headers["Content-Length"], out length) == false) { length = 0; } int read; var buffer = new byte[BufferSize]; DateTime downloadStart = DateTime.Now; int numBytesRead; while ((read = sr.Read(buffer, 0, BufferSize)) > 0 && continueDownloading == true) { sw.Write(buffer, 0, read); if (Environment.TickCount - lastUpdate > interval) { numBytesRead = (int)sw.BaseStream.Position; if (length == 0) { numBytesRead = 0; } continueDownloading = SetProgress(progressCallback, lobbyName, numBytesRead, length, index, count, downloadStart); lastUpdate = Environment.TickCount; } } numBytesRead = (int)sw.BaseStream.Position; if (length == 0) { numBytesRead = 0; } continueDownloading = SetProgress(progressCallback, lobbyName, numBytesRead, length, index, count, downloadStart); } return(continueDownloading); }
public static bool ProcessPendingUpdates(PendingUpdates pendingUpdates, LobbyResult lobby, AutoupdateProgressCallback progressCallback, Control parentControl) { string dataKey = "Files_" + lobby.LobbyId; var autoUpdate = DataStore.Open("autoupdate.ds", "Ga46^#a042"); bool updateExe = false; int received = 0; bool continueDownloadingFiles = true; //Get files from auto-update foreach (var file in pendingUpdates.PendingUpdateList[lobby.LobbyId]) { if (continueDownloadingFiles == false) { return(false); } var filename = file.Filename; var downloadPath = String.Empty; //Check if filename matches the ClientExecutableName if (string.Equals(file.Filename, GlobalSettings.ClientExecutableName)) { filename = GlobalSettings.TempExecutableName; updateExe = true; downloadPath = Path.Combine(AllegianceRegistry.LobbyPath, filename); } // Launcher.PDB also goes into the lobby root. else if (string.Equals(file.Filename, GlobalSettings.ClientExecutablePDB)) { downloadPath = Path.Combine(AllegianceRegistry.LobbyPath, file.Filename); } else { downloadPath = GetLobbyPath(lobby, filename); } if (!Directory.Exists(Path.GetDirectoryName(downloadPath))) { Directory.CreateDirectory(Path.GetDirectoryName(downloadPath)); } var message = string.Format("Updating {0}...", file.Filename); try { continueDownloadingFiles = DownloadFile(++received, pendingUpdates.PendingUpdateList[lobby.LobbyId].Count, pendingUpdates.AutoUpdateBaseAddress[lobby.LobbyId], lobby.LobbyId, file.Filename, downloadPath, lobby.Name, progressCallback); message += " Succeeded."; } catch (Exception error) { message += string.Concat(" Failed: ", error.Message); message += "\n autoUpdateURL: " + pendingUpdates.AutoUpdateBaseAddress[lobby.LobbyId]; message += "\n lobby.LobbyId: " + lobby.LobbyId; message += "\n received: " + received; message += "\n updateQueue.Count: " + pendingUpdates.PendingUpdateList[lobby.LobbyId].Count; message += "\n file.Filename: " + file.Filename; message += "\n downloadPath: " + downloadPath; message += "\n exception details: " + error.ToString(); throw; } finally { Log.Write(message); } } //Save updated AutoUpdateResults dictionary to datastore if (pendingUpdates.PendingUpdateList[lobby.LobbyId].Count != 0) { var dictionary = new Dictionary <string, FindAutoUpdateFilesResult>(); foreach (var f in pendingUpdates.AllFilesInUpdatePackage[lobby.LobbyId]) { dictionary.Add(f.Filename, f); } autoUpdate[dataKey] = dictionary; autoUpdate.Save(); } if (updateExe) //Start the new executable, which will wait for this process to complete its autoupdate { var signal = new UiInteraction(delegate() { MessageBox.Show("The Allegiance Security Client has an update and must restart."); var tempLauncher = Path.Combine(AllegianceRegistry.LobbyPath, GlobalSettings.TempExecutableName); Process.Start(tempLauncher); Application.Exit(); }); if (parentControl.InvokeRequired == true) { parentControl.Invoke(signal); } else { signal(); } } else { //Update the registry to assign the configuration url to the lobby's host value UpdateConfigurationUrlForSelectedLobby(lobby); } return(updateExe); }
private static bool DownloadFile(int index, int count, string baseUrl, int lobbyId, string filename, string destination, string lobbyName, AutoupdateProgressCallback progressCallback) { bool continueDownloading = true; const int interval = 800; int lastUpdate = Environment.TickCount - interval; var request = HttpWebRequest.Create(string.Format("{0}/Files/{1}/{2}", baseUrl, lobbyId, filename)); using (var response = request.GetResponse()) using (var sr = new BinaryReader(response.GetResponseStream())) using (var sw = new BinaryWriter(File.Open(destination, FileMode.Create))) { int length; if (Int32.TryParse(response.Headers["Content-Length"], out length) == false) length = 0; int read; var buffer = new byte[BufferSize]; DateTime downloadStart = DateTime.Now; int numBytesRead; while ((read = sr.Read(buffer, 0, BufferSize)) > 0 && continueDownloading == true) { sw.Write(buffer, 0, read); if (Environment.TickCount - lastUpdate > interval) { numBytesRead = (int)sw.BaseStream.Position; if (length == 0) numBytesRead = 0; continueDownloading = SetProgress(progressCallback, lobbyName, numBytesRead, length, index, count, downloadStart); lastUpdate = Environment.TickCount; } } numBytesRead = (int)sw.BaseStream.Position; if (length == 0) numBytesRead = 0; continueDownloading = SetProgress(progressCallback, lobbyName, numBytesRead, length, index, count, downloadStart); } return continueDownloading; }
private static bool SetProgress(AutoupdateProgressCallback progressCallback, string lobbyName, int numBytes, int totalBytes, int downloadedFiles, int totalFiles, DateTime downloadStart) { if (numBytes <= 0 || totalBytes <= 0) return true; int progress = (int)((float)numBytes / totalBytes * 100); if (progressCallback != null) return progressCallback( lobbyName, string.Format("Downloading Updates ({0}/{1})\n\n{2} kB of {3} kB - {4} kB/s", downloadedFiles, totalFiles, numBytes / 1024, totalBytes / 1024, Math.Round(numBytes / 1024 / DateTime.Now.Subtract(downloadStart).TotalSeconds)), progress); return true; }
public static bool ProcessPendingUpdates(PendingUpdates pendingUpdates, LobbyResult lobby, AutoupdateProgressCallback progressCallback, Control parentControl) { string dataKey = "Files_" + lobby.LobbyId; var autoUpdate = DataStore.Open("autoupdate.ds", "Ga46^#a042"); bool updateExe = false; int received = 0; bool continueDownloadingFiles = true; //Get files from auto-update foreach (var file in pendingUpdates.PendingUpdateList[lobby.LobbyId]) { if (continueDownloadingFiles == false) return false; var filename = file.Filename; var downloadPath = String.Empty; //Check if filename matches the ClientExecutableName if (string.Equals(file.Filename, GlobalSettings.ClientExecutableName)) { filename = GlobalSettings.TempExecutableName; updateExe = true; downloadPath = Path.Combine(AllegianceRegistry.LobbyPath, filename); } // Launcher.PDB also goes into the lobby root. else if (string.Equals(file.Filename, GlobalSettings.ClientExecutablePDB)) { filename = GlobalSettings.TempExecutablePDB; downloadPath = Path.Combine(AllegianceRegistry.LobbyPath, filename); } else { downloadPath = GetLobbyPath(lobby, filename); } if (!Directory.Exists(Path.GetDirectoryName(downloadPath))) Directory.CreateDirectory(Path.GetDirectoryName(downloadPath)); var message = string.Format("Updating {0}...", file.Filename); try { continueDownloadingFiles = DownloadFile(++received, pendingUpdates.PendingUpdateList[lobby.LobbyId].Count, pendingUpdates.AutoUpdateBaseAddress[lobby.LobbyId], lobby.LobbyId, file.Filename, downloadPath, lobby.Name, progressCallback); message += " Succeeded."; } catch (Exception error) { message += string.Concat(" Failed: ", error.Message); message += "\n autoUpdateURL: " + pendingUpdates.AutoUpdateBaseAddress[lobby.LobbyId]; message += "\n lobby.LobbyId: " + lobby.LobbyId; message += "\n received: " + received; message += "\n updateQueue.Count: " + pendingUpdates.PendingUpdateList[lobby.LobbyId].Count; message += "\n file.Filename: " + file.Filename; message += "\n downloadPath: " + downloadPath; message += "\n exception details: " + error.ToString(); throw; } finally { Log.Write(message); } } //Save updated AutoUpdateResults dictionary to datastore if (pendingUpdates.PendingUpdateList[lobby.LobbyId].Count != 0) { var dictionary = new Dictionary<string, FindAutoUpdateFilesResult>(); foreach (var f in pendingUpdates.AllFilesInUpdatePackage[lobby.LobbyId]) dictionary.Add(f.Filename, f); autoUpdate[dataKey] = dictionary; autoUpdate.Save(); } if (updateExe) //Start the new executable, which will wait for this process to complete its autoupdate { var signal = new UiInteraction(delegate() { MessageBox.Show("The Allegiance Security Client has an update and must restart."); var tempLauncher = Path.Combine(AllegianceRegistry.LobbyPath, GlobalSettings.TempExecutableName); Process.Start(tempLauncher); Application.Exit(); }); if (parentControl.InvokeRequired == true) parentControl.Invoke(signal); else signal(); } else { //Update the registry to assign the configuration url to the lobby's host value UpdateConfigurationUrlForSelectedLobby(lobby); } return updateExe; }