static void Main(string[] args) { JsonConvert.DefaultSettings = () => { var settings = new JsonSerializerSettings(); settings.Formatting = Formatting.Indented; settings.DateFormatHandling = DateFormatHandling.IsoDateFormat; settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); return(settings); }; var options = new CommandOptions(); string verbName = ""; object verbOptions = null; if (!CommandLine.Parser.Default.ParseArguments(args, options, (verb, obj) => { verbName = verb; verbOptions = obj; })) { Console.WriteLine("Failed to parse console args"); HelpText text = new HelpText(); if (options.BuilderOptions != null) { Console.WriteLine(text.RenderParsingErrorsText(options.BuilderOptions, 1)); } if (options.UpdaterOptions != null) { Console.WriteLine(text.RenderParsingErrorsText(options.UpdaterOptions, 1)); } Environment.Exit(CommandLine.Parser.DefaultExitCodeFail); } if (verbName == "createBuild") { BuilderOptions op = verbOptions as BuilderOptions; CreateBuild(op); } else if (verbName == "update") { UpdaterOptions op = verbOptions as UpdaterOptions; DownloadLatestVersion(op); } }
static void DownloadLatestVersion(UpdaterOptions options) { Console.WriteLine("Checking for " + options.UpdateChannel + " builds at " + options.URL); IEnumerable<Build> builds = null; //Get the builds from the server try { builds = UpdaterLib.Updater.GetBuildsForChannel(options.UpdateChannel, options.URL); } catch (Exception e) { Console.WriteLine("Error while getting builds: " + e.Message); Environment.Exit(1); } if (builds == null) { Console.WriteLine("No builds found at " + options.URL); Environment.Exit(1); } //Get the latest build Build b = builds.OrderByDescending(x => x.Date).FirstOrDefault(); if (b == null) { Console.WriteLine("There are no builds available for " + options.UpdateChannel); Environment.Exit(1); } Console.WriteLine("Downloading " + b.Version); WebClient cl = new WebClient(); cl.DownloadProgressChanged += cl_DownloadProgressChanged; cl.DownloadFileCompleted += cl_DownloadFileCompleted; cl.DownloadFileAsync(new Uri(b.DownloadURL + b.FileName), b.FileName); downloading = true; while (downloading) { Thread.Sleep(1); } //Compare the sha with the sha of the build manifest Console.WriteLine("Validating Download"); string hash = ""; using (FileStream stream = File.OpenRead(b.FileName)) { SHA256Managed sha = new SHA256Managed(); byte[] h = sha.ComputeHash(stream); hash = BitConverter.ToString(h).Replace("-", String.Empty); } if(hash != b.Sha) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("Error: "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(b.FileName + "Failed Sha Check. Run the updater again"); Environment.Exit(1); } Console.WriteLine("Sha hashes match"); //Extract the zip file Console.WriteLine("Extracting " + b.FileName); using (ZipFile zip = ZipFile.Read(b.FileName)) { foreach(ZipEntry e in zip) { if (e.FileName == "WorldsmithUpdater.exe") continue; try { e.Extract(ExtractExistingFileAction.OverwriteSilently); } catch(Exception ex) { Console.WriteLine("Error extracting: " + e.FileName + " message was: " + ex.Message); } } } Console.WriteLine("Extracted"); //Clean up the archive unless we don't want to if(!options.DontClean) { File.Delete(b.FileName); } //TODO: Make the executable discoverable to generalize this updater if (options.LaunchWhenComplete) { Console.WriteLine("Launching"); //Launch worldsmith ProcessStartInfo start = new ProcessStartInfo(); start.Arguments = ""; start.FileName = "Worldsmith.exe"; start.UseShellExecute = true; Process.Start(start); } }
static void DownloadLatestVersion(UpdaterOptions options) { Console.WriteLine("Checking for " + options.UpdateChannel + " builds at " + options.URL); IEnumerable <Build> builds = null; //Get the builds from the server try { builds = UpdaterLib.Updater.GetBuildsForChannel(options.UpdateChannel, options.URL); } catch (Exception e) { Console.WriteLine("Error while getting builds: " + e.Message); Environment.Exit(1); } if (builds == null) { Console.WriteLine("No builds found at " + options.URL); Environment.Exit(1); } //Get the latest build Build b = builds.OrderByDescending(x => x.Date).FirstOrDefault(); if (b == null) { Console.WriteLine("There are no builds available for " + options.UpdateChannel); Environment.Exit(1); } Console.WriteLine("Downloading " + b.Version); WebClient cl = new WebClient(); cl.DownloadProgressChanged += cl_DownloadProgressChanged; cl.DownloadFileCompleted += cl_DownloadFileCompleted; cl.DownloadFileAsync(new Uri(b.DownloadURL + b.FileName), b.FileName); downloading = true; while (downloading) { Thread.Sleep(1); } //Compare the sha with the sha of the build manifest Console.WriteLine("Validating Download"); string hash = ""; using (FileStream stream = File.OpenRead(b.FileName)) { SHA256Managed sha = new SHA256Managed(); byte[] h = sha.ComputeHash(stream); hash = BitConverter.ToString(h).Replace("-", String.Empty); } if (hash != b.Sha) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("Error: "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(b.FileName + "Failed Sha Check. Run the updater again"); Environment.Exit(1); } Console.WriteLine("Sha hashes match"); //Extract the zip file Console.WriteLine("Extracting " + b.FileName); using (ZipFile zip = ZipFile.Read(b.FileName)) { foreach (ZipEntry e in zip) { if (e.FileName == "WorldsmithUpdater.exe") { continue; } try { e.Extract(ExtractExistingFileAction.OverwriteSilently); } catch (Exception ex) { Console.WriteLine("Error extracting: " + e.FileName + " message was: " + ex.Message); } } } Console.WriteLine("Extracted"); //Clean up the archive unless we don't want to if (!options.DontClean) { File.Delete(b.FileName); } //TODO: Make the executable discoverable to generalize this updater if (options.LaunchWhenComplete) { Console.WriteLine("Launching"); //Launch worldsmith ProcessStartInfo start = new ProcessStartInfo(); start.Arguments = ""; start.FileName = "Worldsmith.exe"; start.UseShellExecute = true; Process.Start(start); } }