//TODO: Update this function to handle DLLs as well. May have to implement a full-blown //manifest system here as well. /// <summary> /// Updates the launcher synchronously. /// </summary> public void UpdateLauncher() { try { FTPHandler FTP = new FTPHandler(); //crawl the server for all of the files in the /launcher/bin directory. List <string> remotePaths = FTP.GetFilePaths(Config.GetLauncherBinariesURL(), true); //download all of them foreach (string path in remotePaths) { try { if (!String.IsNullOrEmpty(path)) { string Local = String.Format("{0}launchpad{1}{2}", ConfigHandler.GetTempDir(), Path.DirectorySeparatorChar, path); string Remote = String.Format("{0}{1}", Config.GetLauncherBinariesURL(), path); if (!Directory.Exists(Local)) { Directory.CreateDirectory(Directory.GetParent(Local).ToString()); } FTP.DownloadFTPFile(Remote, Local, false); } } catch (WebException wex) { Console.WriteLine("WebException in UpdateLauncher(): " + wex.Message); } } //TODO: Make the script copy recursively ProcessStartInfo script = CreateUpdateScript(); Process.Start(script); Environment.Exit(0); } catch (IOException ioex) { Console.WriteLine("IOException in UpdateLauncher(): " + ioex.Message); } }
/// <summary> /// Creates the update script on disk. /// </summary> /// <returns>ProcessStartInfo for the update script.</returns> private static ProcessStartInfo CreateUpdateScript() { try { //maintain the executable name if it was renamed to something other than 'Launchpad' string fullName = Assembly.GetEntryAssembly().Location; string executableName = Path.GetFileName(fullName); // should be "Launchpad", unless the user has renamed it if (ChecksHandler.IsRunningOnUnix()) { //creating a .sh script string scriptPath = String.Format(@"{0}launchpadupdate.sh", ConfigHandler.GetTempDir()); FileStream updateScript = File.Create(scriptPath); TextWriter tw = new StreamWriter(updateScript); //write commands to the script //wait five seconds, then copy the new executable string copyCom = String.Format("cp -rf {0} {1}", ConfigHandler.GetTempDir() + "launchpad/*", ConfigHandler.GetLocalDir()); string delCom = String.Format("rm -rf {0}", ConfigHandler.GetTempDir() + "launchpad"); string dirCom = String.Format("cd {0}", ConfigHandler.GetLocalDir()); string launchCom = String.Format(@"nohup ./{0} &", executableName); tw.WriteLine(@"#!/bin/sh"); tw.WriteLine("sleep 5"); tw.WriteLine(copyCom); tw.WriteLine(delCom); tw.WriteLine(dirCom); tw.WriteLine("chmod +x " + executableName); tw.WriteLine(launchCom); tw.Close(); UnixHandler.MakeExecutable(scriptPath); //Now create some ProcessStartInfo for this script ProcessStartInfo updateShellProcess = new ProcessStartInfo(); updateShellProcess.FileName = scriptPath; updateShellProcess.UseShellExecute = false; updateShellProcess.RedirectStandardOutput = false; updateShellProcess.WindowStyle = ProcessWindowStyle.Hidden; return(updateShellProcess); } else { //creating a .bat script string scriptPath = String.Format(@"{0}launchpadupdate.bat", ConfigHandler.GetTempDir()); FileStream updateScript = File.Create(scriptPath); TextWriter tw = new StreamWriter(updateScript); //write commands to the script //wait three seconds, then copy the new executable tw.WriteLine(String.Format(@"timeout 3 & xcopy /e /s /y ""{0}\launchpad"" ""{1}"" && rmdir /s /q {0}\launchpad", ConfigHandler.GetTempDir(), ConfigHandler.GetLocalDir())); //then start the new executable tw.WriteLine(String.Format(@"start {0}", executableName)); tw.Close(); ProcessStartInfo updateBatchProcess = new ProcessStartInfo(); updateBatchProcess.FileName = scriptPath; updateBatchProcess.UseShellExecute = true; updateBatchProcess.RedirectStandardOutput = false; updateBatchProcess.WindowStyle = ProcessWindowStyle.Hidden; return(updateBatchProcess); } } catch (IOException ioex) { Console.WriteLine("IOException in CreateUpdateScript(): " + ioex.Message); return(null); } }