public static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; bool shouldUpdate = false; if (Directory.Exists(Maintenance.Updater.TMP_DIR)) { Directory.Delete(Maintenance.Updater.TMP_DIR, true); } File.Delete(Maintenance.LocalFileIndex.SWAP_FILE_NAME); if (args.Length > 0) { switch (args[0]) { case "no-update": NoNetwork = true; break; case "force-update": shouldUpdate = true; Logger.DefaultLogger.WriteLine("Forcing update due to flag"); break; case "make-index": { var localIndex = new Maintenance.LocalFileIndex(false); localIndex.ToFile("index"); return; } default: Logger.DefaultLogger.WriteLine("Unknown flag `{0}` not used"); break; } } #if DEBUG Logger.DefaultLogger.WriteLine("Built with DEBUG configuration"); NoNetwork = true; #endif foreach (var requiredFile in RequiredFiles) { if (!File.Exists(requiredFile)) { Logger.DefaultLogger.WriteLine("Detected missing required file, `{0}`, forcing maintenance", requiredFile); shouldUpdate = true; break; } } if (shouldUpdate) { if (NoNetwork) { Logger.DefaultLogger.WriteLine("Networking disabled, failing maintenance"); MessageBox.Show("An update is required, but networking is disabled.", "Failed"); return; } Logger.DefaultLogger.WriteLine("Maintenance update started"); Application.Run(new Maintenance.UpdaterGraphical()); if (Maintenance.Updater.DefaultUpdater.Ready) { Logger.DefaultLogger.WriteLine("Maintenance update succesful"); Program.Restart("no-update"); } else { // implies that the Maintenance window was closed early Logger.DefaultLogger.WriteLine("Maintenence update interrupted"); } return; } try { try { EntryPoint(); } catch (TypeInitializationException e) { throw e.InnerException; } } catch (DllNotFoundException e) { Logger.DefaultLogger.WriteLine("Detected missing .dll `{0}`", e.Message); Logger.DefaultLogger.WriteError(e); DoUpdateRetryDialog("Missing .DLL", string.Format("The .dll `{0}` was not found. OK to force an update ?", e.Message)); } #if !DEBUG catch (Exception e) { Logger.DefaultLogger.WriteLine("FATAL"); Logger.DefaultLogger.WriteError(e); DoRetryDialog("Useless Error Handler", string.Format("{0}\n{1}\n\nError details included in file `{2}`, include it in your crash report.", e.GetType(), e.Message, Logger.DefaultLogger.FileName)); } #endif finally { Logger.DefaultLogger.WriteLine("Program exited"); Logger.DefaultLogger.Dispose(); } }
/// <summary> /// Runs the updater /// </summary> /// <param name="fullUpdate">true if the updater should ignore the local cache</param> /// <returns>true when successful</returns> public bool Run(bool fullUpdate) { if (!this.Ready) { throw new InvalidOperationException("The updater is still busy"); } if (Program.NoNetwork) { throw new InvalidOperationException("Networking is disabled"); } this.Ready = false; if (this.PercentChanged != null) { this.PercentChanged(this, 0); } if (this.TextChanged != null) { this.TextChanged(this, "Getting file index"); } this.Log.WriteLine("Getting file index"); try { var localIndex = new LocalFileIndex(!fullUpdate); this.Client.DownloadFile(this.GetWebUrl("index"), LocalFileIndex.SWAP_FILE_NAME); var index = new FileIndex(); index.FromFile(LocalFileIndex.SWAP_FILE_NAME); var filesToGet = new List <string>(); foreach (var item in index) { if (localIndex[item.Key] == null || !localIndex[item.Key].Equals(item.Value)) { filesToGet.Add(item.Key); } } this.Log.WriteLine("{0} files are missing or outdated", filesToGet.Count); if (filesToGet.Count == 0) { if (this.TextChanged != null) { this.TextChanged(this, "Vega is up to date"); } this.Ready = true; return(true); } #if INSTALL_WARNING if (filesToGet.Count >= index.Count - 1) { System.Windows.Forms.Application.Run(new InstallWarning()); } #endif Directory.CreateDirectory(TMP_DIR); foreach (var file in filesToGet) { string status = "Downloading " + file; this.Log.WriteLine(status); if (this.TextChanged != null) { this.TextChanged(this, status); } var dlFile = Path.Combine(TMP_DIR, file + ".dl"); var trFile = Path.Combine(TMP_DIR, file + ".trash"); this.Client.DownloadFile(this.GetWebUrl(file), dlFile); if (File.Exists(file)) { File.Move(file, trFile); } File.Move(dlFile, file); } if (this.TextChanged != null) { this.TextChanged(this, "Update finished, restart to apply"); } } catch (WebException e) { this.Log.WriteLine("Failed: No internet connection?"); this.Log.WriteError(e); Program.NoNetwork = true; if (this.TextChanged != null) { this.TextChanged(this, "There is no internet connection"); } File.Delete(LocalFileIndex.SWAP_FILE_NAME); return(false); } finally { if (File.Exists(LocalFileIndex.SWAP_FILE_NAME)) { File.Delete(LocalFileIndex.FILE_NAME); File.Move(LocalFileIndex.SWAP_FILE_NAME, LocalFileIndex.FILE_NAME); } this.Log.WriteLine("Updater finished"); this.Log.WriteLine("-------------------"); } this.Ready = true; return(true); }