コード例 #1
0
        /// <summary>
        /// Performs the update.
        /// </summary>
        /// <returns><c>true</c> if the update completed successfully;
        /// <c>false</c> otherwise.</returns>
        public override bool Update()
        {
            Trace.TraceInformation("Checking for new client version...");
            Trace.Indent();
            SetProgressMaximum(2);
            SetMessage(String.Format("Checking for new {0} version...", EnvironmentInfo.Settings.ModManagerName));
            string  strDownloadUri = String.Empty;
            Version verNew         = GetNewProgrammeVersion(out strDownloadUri);

            SetProgress(1);

            if (CancelRequested)
            {
                Trace.Unindent();
                return(CancelUpdate());
            }

            if (verNew == new Version("69.69.69.69"))
            {
                SetMessage("Could not get version information from the update server.");
                return(false);
            }

            StringBuilder stbPromptMessage = new StringBuilder();
            DialogResult  drResult         = DialogResult.No;

            string strReleaseNotes = String.Empty;

            if ((verNew > new Version(ProgrammeMetadata.VersionString)) && !String.IsNullOrEmpty(strDownloadUri))
            {
                string strCheckDownloadedInstaller = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp", Path.GetFileName(strDownloadUri));

                stbPromptMessage.AppendFormat("A new version of {0} is available ({1}).{2}Would you like to download and install it?", EnvironmentInfo.Settings.ModManagerName, verNew, Environment.NewLine).AppendLine();
                stbPromptMessage.AppendLine();
                stbPromptMessage.AppendLine();
                stbPromptMessage.AppendLine("Below you can find the change log for the new release:");

                try
                {
                    HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create(m_strURI);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream       receiveStream = response.GetResponseStream();
                        StreamReader readStream    = null;

                        if (response.CharacterSet == null)
                        {
                            readStream = new StreamReader(receiveStream);
                        }
                        else
                        {
                            readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                        }

                        strReleaseNotes = readStream.ReadToEnd();

                        response.Close();
                        readStream.Close();
                    }
                }
                catch
                {
                    strReleaseNotes = "Unable to retrieve the Release Notes.";
                }

                try
                {
                    //the extended message box contains an activex control wich must be run in an STA thread,
                    // we can't control what thread this gets called on, so create one if we need to
                    ThreadStart    actShowMessage = () => drResult = ExtendedMessageBox.Show(null, stbPromptMessage.ToString(), "New version available", strReleaseNotes, 700, 450, ExtendedMessageBoxButtons.Backup, MessageBoxIcon.Question);
                    ApartmentState astState       = ApartmentState.Unknown;
                    Thread.CurrentThread.TrySetApartmentState(astState);
                    if (astState == ApartmentState.STA)
                    {
                        actShowMessage();
                    }
                    else
                    {
                        Thread thdMessage = new Thread(actShowMessage);
                        thdMessage.SetApartmentState(ApartmentState.STA);
                        thdMessage.Start();
                        thdMessage.Join();
                    }
                }
                catch
                {
                }

                if (drResult == DialogResult.Cancel)
                {
                    Trace.Unindent();
                    return(CancelUpdate());
                }

                if (drResult == DialogResult.Yes)
                {
                    UpdateManager.CreateBackup();
                }

                if (File.Exists(strCheckDownloadedInstaller))
                {
                    SetMessage("Launching installer...");
                    ProcessStartInfo psiInfo = new ProcessStartInfo(strCheckDownloadedInstaller);
                    Process.Start(psiInfo);
                    Trace.Unindent();
                    return(true);
                }

                SetMessage(String.Format("Downloading new {0} version...", EnvironmentInfo.Settings.ModManagerName));

                string strNewInstaller = string.Empty;
                try
                {
                    strNewInstaller = DownloadFile(new Uri(String.Format(strDownloadUri)));
                }
                catch (FileNotFoundException)
                {
                    StringBuilder stbAVMessage = new StringBuilder();
                    stbAVMessage.AppendLine("Unable to find the installer to download:");
                    stbAVMessage.AppendLine("this could be caused by a network issue or by your Firewall.");
                    stbAVMessage.AppendLine("As a result you won't be able to automatically update the program.");
                    stbAVMessage.AppendLine();
                    stbAVMessage.AppendFormat("You can download the update manually from:");
                    stbAVMessage.AppendLine(NexusLinks.Releases);
                    try
                    {
                        //the extended message box contains an activex control wich must be run in an STA thread,
                        // we can't control what thread this gets called on, so create one if we need to
                        ThreadStart    actShowMessage = () => drResult = ExtendedMessageBox.Show(null, stbAVMessage.ToString(), "Unable to update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ApartmentState astState       = ApartmentState.Unknown;
                        Thread.CurrentThread.TrySetApartmentState(astState);
                        if (astState == ApartmentState.STA)
                        {
                            actShowMessage();
                        }
                        else
                        {
                            Thread thdMessage = new Thread(actShowMessage);
                            thdMessage.SetApartmentState(ApartmentState.STA);
                            thdMessage.Start();
                            thdMessage.Join();
                        }
                    }
                    catch
                    {
                    }

                    Trace.Unindent();
                    return(CancelUpdate());
                }

                SetProgress(2);

                if (CancelRequested)
                {
                    Trace.Unindent();
                    return(CancelUpdate());
                }

                if (!String.IsNullOrEmpty(strNewInstaller))
                {
                    string strOldPath = strNewInstaller;
                    strNewInstaller = Path.Combine(Path.GetTempPath(), Path.GetFileName(strNewInstaller));
                    FileUtil.ForceDelete(strNewInstaller);

                    try
                    {
                        File.Move(strOldPath, strNewInstaller);
                    }
                    catch (FileNotFoundException)
                    {
                        StringBuilder stbAVMessage = new StringBuilder();
                        stbAVMessage.AppendLine("Unable to find the downloaded update:");
                        stbAVMessage.AppendLine("this could be caused by a network issue or by your anti-virus software deleting it falsely flagging the installer as a virus.");
                        stbAVMessage.AppendLine("As a result you won't be able to automatically update the program.");
                        stbAVMessage.AppendLine();
                        stbAVMessage.AppendFormat("To fix this issue you need to add {0}'s executable and all its folders to your", EnvironmentInfo.Settings.ModManagerName).AppendLine();
                        stbAVMessage.AppendLine("anti-virus exception list. You can also download the update manually from:");
                        stbAVMessage.AppendLine(NexusLinks.Releases);

                        try
                        {
                            //the extended message box contains an activex control wich must be run in an STA thread,
                            // we can't control what thread this gets called on, so create one if we need to
                            ThreadStart    actShowMessage = () => drResult = ExtendedMessageBox.Show(null, stbAVMessage.ToString(), "Unable to update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            ApartmentState astState       = ApartmentState.Unknown;
                            Thread.CurrentThread.TrySetApartmentState(astState);
                            if (astState == ApartmentState.STA)
                            {
                                actShowMessage();
                            }
                            else
                            {
                                Thread thdMessage = new Thread(actShowMessage);
                                thdMessage.SetApartmentState(ApartmentState.STA);
                                thdMessage.Start();
                                thdMessage.Join();
                            }
                        }
                        catch
                        {
                        }

                        Trace.Unindent();
                        return(CancelUpdate());
                    }

                    SetMessage("Launching installer...");
                    ProcessStartInfo psiInfo = new ProcessStartInfo(strNewInstaller);
                    Process.Start(psiInfo);
                    Trace.Unindent();
                    return(true);
                }
            }
            else if (!m_booIsAutoCheck)
            {
                stbPromptMessage.AppendFormat("{0} is already up to date.", EnvironmentInfo.Settings.ModManagerName).AppendLine();
                stbPromptMessage.AppendLine();
                stbPromptMessage.AppendLine();
                stbPromptMessage.AppendLine("NOTE: You can find the release notes and past versions here:");
                stbPromptMessage.AppendLine(NexusLinks.Releases);

                try
                {
                    //the extended message box contains an activex control wich must be run in an STA thread,
                    // we can't control what thread this gets called on, so create one if we need to
                    ThreadStart    actShowMessage = () => drResult = ExtendedMessageBox.Show(null, stbPromptMessage.ToString(), "Up to date", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ApartmentState astState       = ApartmentState.Unknown;
                    Thread.CurrentThread.TrySetApartmentState(astState);
                    if (astState == ApartmentState.STA)
                    {
                        actShowMessage();
                    }
                    else
                    {
                        Thread thdMessage = new Thread(actShowMessage);
                        thdMessage.SetApartmentState(ApartmentState.STA);
                        thdMessage.Start();
                        thdMessage.Join();
                    }
                }
                catch
                {
                }
            }

            SetMessage(String.Format("{0} is already up to date.", EnvironmentInfo.Settings.ModManagerName));
            SetProgress(2);
            Trace.Unindent();
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Performs the update.
        /// </summary>
        /// <returns><c>true</c> if the update completed successfully;
        /// <c>false</c> otherwise.</returns>
        public override bool Update()
        {
            Trace.TraceInformation("Checking for new client version...");
            Trace.Indent();

            SetProgressMaximum(2);
            SetMessage($"Checking for new {CommonData.ModManagerName} version...");

            var currentVersion     = new Version(CommonData.VersionString);
            var releaseInformation = GetReleaseInformation();

            var newVersion  = releaseInformation.Item1;
            var downloadUrl = releaseInformation.Item2;

            SetProgress(1);

            if (CancelRequested)
            {
                Trace.Unindent();
                return(CancelUpdate());
            }

            if (newVersion == null || string.IsNullOrEmpty(downloadUrl))
            {
                SetMessage("Could not get version information from the update server.");
                return(false);
            }

            var dialogResult = DialogResult.No;

            if (newVersion > currentVersion)
            {
                string releaseNotes;
                var    checkDownloadedInstaller = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp", Path.GetFileName(downloadUrl));

                var promptMessage =
                    $"A new version of {CommonData.ModManagerName} is available ({newVersion}).{Environment.NewLine}" +
                    $"Would you like to download and install it?{Environment.NewLine}{Environment.NewLine}" +
                    "Below you can find the change log for the new release:";

                try
                {
                    releaseNotes = ConstructChangeLog(currentVersion, newVersion);
                }
                catch
                {
                    releaseNotes = "Unable to retrieve change log.";
                }

                DisplayDialog(() => dialogResult = ExtendedMessageBox.Show(null, promptMessage, "New version available", releaseNotes, 700, 450, ExtendedMessageBoxButtons.Backup, MessageBoxIcon.Question));

                switch (dialogResult)
                {
                case DialogResult.Cancel:
                    Trace.Unindent();
                    return(CancelUpdate());

                case DialogResult.Yes:
                    _updateManager.CreateBackup();
                    break;
                }

                if (File.Exists(checkDownloadedInstaller))
                {
                    SetMessage("Launching installer...");
                    var processStartInfo = new ProcessStartInfo(checkDownloadedInstaller);
                    Process.Start(processStartInfo);
                    Trace.Unindent();

                    return(true);
                }

                SetMessage($"Downloading new {CommonData.ModManagerName} version...");

                string newInstaller;

                try
                {
                    newInstaller = DownloadFile(new Uri(string.Format(downloadUrl)));
                }
                catch (FileNotFoundException)
                {
                    var avMessage =
                        $"Unable to find the installer to download:{Environment.NewLine}" +
                        $"This could be caused by a network issue or by your Firewall.{Environment.NewLine}{Environment.NewLine}" +
                        $"As a result you won't be able to automatically update the program.{Environment.NewLine}{Environment.NewLine}" +
                        $"You can download the update manually from:{Environment.NewLine}{Links.Instance.Releases}";

                    DisplayDialog(() => dialogResult = ExtendedMessageBox.Show(null, avMessage, "Unable to update", MessageBoxButtons.OK, MessageBoxIcon.Information));

                    Trace.Unindent();
                    return(CancelUpdate());
                }

                SetProgress(2);

                if (CancelRequested)
                {
                    Trace.Unindent();
                    return(CancelUpdate());
                }

                if (!string.IsNullOrEmpty(newInstaller))
                {
                    var oldPath = newInstaller;
                    newInstaller = Path.Combine(Path.GetTempPath(), Path.GetFileName(newInstaller));
                    FileUtil.ForceDelete(newInstaller);

                    try
                    {
                        File.Move(oldPath, newInstaller);
                    }
                    catch (FileNotFoundException)
                    {
                        var avMessage =
                            $"Unable to find the downloaded update:{Environment.NewLine}" +
                            $"This could be caused by a network issue or by your anti-virus software deleting it falsely flagging the installer as a virus.{Environment.NewLine}" +
                            $"As a result you won't be able to automatically update the program.{Environment.NewLine}{Environment.NewLine}" +
                            $"To fix this issue you need to add {CommonData.ModManagerName}'s executable and all its folders to your{Environment.NewLine}" +
                            $"anti-virus exception list. You can also download the update manually from:{Environment.NewLine}{Links.Instance.Releases}";

                        DisplayDialog(() => dialogResult = ExtendedMessageBox.Show(null, avMessage, "Unable to update", MessageBoxButtons.OK, MessageBoxIcon.Information));

                        Trace.Unindent();
                        return(CancelUpdate());
                    }

                    SetMessage("Launching installer...");
                    var psiInfo = new ProcessStartInfo(newInstaller);
                    Process.Start(psiInfo);
                    Trace.Unindent();

                    return(true);
                }
            }
            else if (!_isAutomaticCheck)
            {
                var promptMessage =
                    $"{CommonData.ModManagerName} is already up to date.{Environment.NewLine}{Environment.NewLine}" +
                    $"NOTE: You can find the release notes and past versions here:{Environment.NewLine}{Links.Instance.Releases}";

                DisplayDialog(() => ExtendedMessageBox.Show(null, promptMessage, "Up to date", MessageBoxButtons.OK, MessageBoxIcon.Information));
            }

            SetMessage($"{CommonData.ModManagerName} is already up to date.");
            SetProgress(2);
            Trace.Unindent();

            return(true);
        }