| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 | using Comal.Classes;using InABox.Clients;using InABox.Core;using InABox.DynamicGrid;using InABox.WPF;using RestSharp;using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;using System.Windows;namespace PRS.Shared{    /// <summary>    /// Manages updating the app for both PRSDesktop and PRSServer    /// </summary>    public static class Update    {        public static RestResponse GetRemoteFile(string location)        {            var client = StaticRestClients.GetClient(Uri.EscapeUriString(location));            var versionrequest = new RestRequest(Uri.EscapeUriString(location), Method.Get);            return client.Execute(versionrequest);        }        private static string ParseReleaseNotes(string releaseNotes, string latestVersion, string currentVersion)        {            var display = new List<string>();            var latest = "Changes in " + latestVersion;            var current = "Changes in " + currentVersion;            var notes = releaseNotes.Split('\n').ToList();            var bActive = false;            foreach (var note in notes)            {                if (!string.IsNullOrWhiteSpace(note))                {                    if (note.Trim().ToUpper().StartsWith("CHANGES IN ") &&                        string.Compare(latest.Trim().ToUpper(), note.Trim().ToUpper()) <= 0)                        bActive = true;                    if (note.Trim().ToUpper().StartsWith("CHANGES IN ") &&                        string.Compare(current.Trim().ToUpper(), note.Trim().ToUpper()) >= 0)                        bActive = false;                    if (bActive && !note.ToUpper().StartsWith("CHANGES IN") && !note.StartsWith("="))                        display.Add(note);                }            }            if (!display.Any())                display.Add("Various Internal Updates");            return string.Join('\n', display);        }        private static bool? ShowReleaseNotes(string display, string latestVersion, string currentVersion)        {            var form = new NotesForm            {                Caption = string.Format("Release {0} is available!", latestVersion),                CancelEnabled = false,                Text = string.Format(                    "The following changes and improvements have been made since your last update (v{0}). Please review and click [OK] to update your software.\n\n{1} ",                    currentVersion,                    display                )            };            return form.ShowDialog();        }        private static string GenerateInstallerScript(string installerFile)        {            var commands = new List<string>();            commands.Add($"\"{installerFile}\" /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");            commands.Add($"\"{Path.ChangeExtension(Assembly.GetEntryAssembly().Location, ".exe")}\"");            return string.Join('\n', commands);        }        public static bool CheckForUpdates(            Func<string> getUpdateLocation,            Func<string, string> getLatestVersion,            Func<string, string> getReleaseNotes,            Func<string, byte[]?>? getInstaller,            Action? beforeUpdate,            bool elevated,            string tempName)        {            var display = "";            var latestVersion = "";            var currentVersion = CoreUtils.GetVersion();            var location = getUpdateLocation();            if (!String.IsNullOrWhiteSpace(location))            {                Progress.ShowModal("Checking for Updates", progress =>                {                    if (!string.Equals(currentVersion, "???"))                    {                        latestVersion = getLatestVersion(location);                        Logger.Send(LogType.Information, "",                            $"Update Check: Current: {currentVersion} Latest: {latestVersion}");                        if (!string.IsNullOrWhiteSpace(latestVersion))                            if (string.Compare(currentVersion, latestVersion) < 0)                            {                                var releasenotes = getReleaseNotes(location);                                display = ParseReleaseNotes(releasenotes, latestVersion, currentVersion);                            }                    }                });            }            if (display.IsNullOrWhiteSpace()) return false;            if (ShowReleaseNotes(display, latestVersion, currentVersion) == true)            {                if (getInstaller == null)                    return true;                                var bOK = false;                var tempinstall = Path.Combine(CoreUtils.GetPath(), tempName);                Progress.ShowModal("Retrieving Update", progress =>                {                    try                    {                        var installer = getInstaller(location);                        if (installer?.Any() == true)                        {                            File.WriteAllBytes(tempinstall, installer);                            var scriptFile = Path.Combine(CoreUtils.GetPath(), "install.bat");                            File.WriteAllText(scriptFile, GenerateInstallerScript(tempinstall));                            bOK = true;                            beforeUpdate?.Invoke();                            progress.Report("Launching Installer");                            /*var startInfo = new ProcessStartInfo(tempinstall,                                " /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");*/                            var startInfo = new ProcessStartInfo(scriptFile);                            startInfo.Verb = elevated ? "runas" : "open";                            startInfo.UseShellExecute = true;                            startInfo.WindowStyle = ProcessWindowStyle.Hidden;                            var p = Process.Start(startInfo);                            p.WaitForExit();                        }                    }                    catch (Exception e)                    {                        Logger.Send(LogType.Error, "", CoreUtils.FormatException(e));                        MessageBox.Show("Error during installation!");                    }                });                if (bOK)                    return true;                MessageBox.Show("Unable to retrieve installer!");                return false;            }            return false;        }    }}
 |