Example #1
0
        private static Stack <UpdateXML> FindNeededUpdates(UpdateXML firstUpdateToStack)
        {
            //Console.WriteLine("Finding what updates to download");

            Stack <UpdateXML> updateStack = new Stack <UpdateXML>();

            updateStack.Push(firstUpdateToStack);

            UpdateXML currentUpdate = firstUpdateToStack;

            while (true)
            {
                // Look at the previous version provided by the current UpdateXML.
                // If the previous version is not greater than this client's version, or parsing
                // failed, stop stacking and get ready for updating.
                if (!UpdateExistsAndIsNewerThanClient(out UpdateXML lastUpdate,
                                                      UpdateInfo.GetPreviousVersionXMLPath(currentUpdate.PreviousVersion)))
                {
                    break;
                }

                // Otherwise, add it to the stack
                updateStack.Push(lastUpdate);

                // Prepare for next loop
                currentUpdate = lastUpdate;
            }

            return(updateStack);
        }
Example #2
0
        private static bool UpdateExistsAndIsNewerThanClient(out UpdateXML updateXML, string xmlPath)
        {
            // updateXML is expected to be changed.
            updateXML = null;

            //Console.WriteLine("Checking if updateXML location exists.");

            // If the xml doesn't exist on the server, return false.
            Uri xmlLocation = new Uri(xmlPath);

            if (!UpdateXML.Exists(xmlLocation))
            {
                return(false);
            }

            //Console.WriteLine("It exists!");

            // If it does exist, assign updateXML to it and compare to the client version.
            updateXML = UpdateXML.Parse(xmlLocation);
            return(updateXML != null && updateXML.PreviousVersion >= Pulsarc.CurrentVersion);
        }
Example #3
0
        private void HandleUpdateXML(UpdateXML update, bool retry = false)
        {
            string tempFilePath = Path.GetTempFileName();

            try
            {
                using (webClient)
                {
                    webClient.DownloadFile(update.DownloadUri, tempFilePath);
                }
            }
            catch
            {
                // If we already tried once, stop trying.
                if (retry)
                {
                    return;
                }

                // Try one more time.
                DeleteFileAndTryAgain();
            }

            // Checksum with md5, try again if there's an issue
            if (!File.Exists(tempFilePath) ||
                Hasher.HashFile(tempFilePath, HashType.MD5) != update.MD5)
            {
                // If we already tried once, stop trying.
                if (retry)
                {
                    return;
                }

                // Try one more time
                DeleteFileAndTryAgain();
            }

            // Otherwise add it to the queue of patches
            patchFileLocations.Enqueue(tempFilePath);

            bool anotherOne = updates.TryPop(out UpdateXML nextUpdate);

            // If the Pop was successful, handle it, otherwise we are done and have suceeded!
            if (anotherOne)
            {
                HandleUpdateXML(nextUpdate);
            }

            void DeleteFileAndTryAgain()
            {
                // Give everything a break
                Thread.Sleep(333);

                // Try to delete the temp file twice
                try { File.Delete(tempFilePath); }
                catch
                {
                    try { File.Delete(tempFilePath); }
                    catch { }
                }

                HandleUpdateXML(update, true);
            }
        }