Beispiel #1
0
        void SelfPatch()
        {
            try
            {
                Fingerprint myFingerprint = new Fingerprint(Settings.GamePath, "Bourbon.exe");

                if (DontSelfUpdate)
                {
                    return;
                }
                MyToolkit.ActivityLog("Starting self-patch process.");

                // Before we go far... lets see if there are any old temp files hanging around and get rid of them

                string[] oldFiles = Directory.GetFiles(Settings.GamePath, "*.old");
                foreach (string oldFile in oldFiles)
                {
                    try { File.Delete(oldFile); }
                    catch (Exception ex) { }
                }

                // OK now thats out of the way, lets determine if we need to self patch or not!!
                IEnumerable <XElement> launchers = m_manifest.Descendants("launcher");
                m_Status = "Self patching";
                foreach (XElement launcher in launchers)
                {
                    if (launcher.Attribute("id").Value == "Bourbon")
                    {
                        long size = 0;
                        long.TryParse(launcher.Attribute("size").Value.ToString(), out size);
                        string md5 = launcher.Attribute("md5").Value;

                        Fingerprint remoteLauncher = new Fingerprint(Settings.GamePath, "Bourbon.exe", md5, size);

                        if (!myFingerprint.Equals(remoteLauncher))
                        {
                            MyToolkit.ActivityLog("Patcher out of date...");

                            // We need to update!!! yay...
                            IEnumerable <XElement> urls = launcher.Descendants("url");

                            // Get every possible download URL into the remoteLauncher fingerprint
                            foreach (XElement url in urls)
                            {
                                remoteLauncher.AddDownloadURL(url.Value);
                            }


                            // Start the download process
                            HTTP selfPatcherClient = new HTTP();

                            m_DownloadSize = remoteLauncher.Size;
                            string downloadURL = remoteLauncher.DownloadURL;
                            MyToolkit.ActivityLog("Downloading new version from \"" + downloadURL + "\"");
                            if (selfPatcherClient.StartDownload(new AsyncCompletedEventHandler(DownloadFileComplete),
                                                                new DownloadProgressChangedEventHandler(dlProgress),
                                                                downloadURL,
                                                                remoteLauncher.FullName + ".download"))
                            {
                                m_Status         = "Downloading";
                                m_DownloadActive = true;
                            }

                            m_current = remoteLauncher.FullName;

                            // Wait until download is complete

                            MyToolkit.ActivityLog("Waiting for patcher download to complete...");
                            while (selfPatcherClient.Active)
                            {
                                if (Kill)
                                {
                                    selfPatcherClient.CancelDownload();
                                    return;
                                }
                                System.Threading.Thread.Sleep(10);
                            }
                            m_DownloadActive = false;
                            MyToolkit.ActivityLog("New patcher version downloaded...");

                            // Make sure the downloaded file is not corrupted

                            Fingerprint downloadedFile = new Fingerprint(remoteLauncher.RootPath, remoteLauncher.FileName + ".download");

                            if (!downloadedFile.Equals(remoteLauncher))
                            {
                                string error = "Was unable to self patch. Downloaded file did not match expected checksum.";
                                error += "\r\n" + remoteLauncher.FileName
                                         + "\r\n md5: " + remoteLauncher.Checksum + " vs " + downloadedFile.Checksum
                                         + "\r\n size: " + remoteLauncher.Size + " vs " + downloadedFile.Size;

                                MyToolkit.ActivityLog(error);

                                File.Delete(downloadedFile.FullName + ".download");
                                m_ErrorLog.Add(error);
                                m_Status = "Done";
                                return;
                            }
                            else
                            {
                                // Find an available _#.old file name
                                long   i         = 0;
                                string TrashName = myFingerprint.FullName + "_";
                                while (File.Exists(TrashName + i.ToString() + ".old"))
                                {
                                    i++;
                                }

                                TrashName = TrashName + i.ToString() + ".old";
                                File.Move(myFingerprint.FullName, TrashName);
                                File.Move(myFingerprint.FullName + ".download", myFingerprint.FullName);

                                var startInfo = new ProcessStartInfo();
                                startInfo.FileName  = myFingerprint.FullName;
                                startInfo.Arguments = MyToolkit.AllArgs();

                                MyToolkit.ActivityLog("Bourbon has been patched successfuly. Restarting.");

                                Process.Start(startInfo);

                                Application.Exit();
                                return;
                            }
                        }
                    }
                }

                MyToolkit.ActivityLog("Self patching process complete.");
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "WorkThread.SelfPatch()");
            }
        }
Beispiel #2
0
        void ManifestDownloadComplete(object sender, AsyncCompletedEventArgs e)
        {
            // Check if we had any HTTP download errors
            if (e != null)
            {
                if (e.Error != null)
                {
                    MyToolkit.ActivityLog("Manifest download error for " + ManifestURL + "\r\n" + e.Error.Message);
                    m_ErrorLog.Add("Manifest download error for " + ManifestURL + "\r\n" + e.Error.Message);
                    m_Status = "Done";
                    return;
                }
            }

            // Check if the downloaded file is where it should be
            if (!File.Exists(LocalManifest))
            {
                MyToolkit.ActivityLog("Error downloading manifest, download complete but no file found locally.");
                m_ErrorLog.Add("Error downloading manifest");
                m_Status = "Done";
                return;
            }

            // Make certain the downloaded manifest and the one we
            // requested match in size
            FileInfo dlInfo = new FileInfo(LocalManifest);

            if (dlInfo.Length != client.Length)
            {
                MyToolkit.ActivityLog("Error downloading manifest, downloaded file not the right size. Expected: " + dlInfo.Length + " received: " + client.Length);
                m_ErrorLog.Add("Error downloading manifest");
                m_Status = "Done";
                return;
            }

            // We got a manifest, lets start reading through it
            m_current = "";


            MyToolkit.ActivityLog("Manifest downloaded successfully, starting to process it.");

            m_ManifestFileList = new ArrayList();
            try
            {
                m_manifest = XElement.Load(LocalManifest);


                // try to get the forum URL

                IEnumerable <XElement> forumLinks = m_manifest.Descendants("webpage");

                foreach (XElement forumLink in forumLinks)
                {
                    ForumURL = forumLink.Value;
                    break;
                }


                SelfPatch();

                m_Status = "Reading manifest";
                IEnumerable <XElement> files = m_manifest.Descendants("file");

                foreach (XElement file in files)
                {
                    if (Kill)
                    {
                        return;
                    }

                    // Lets get this file's manifest information
                    long size;
                    bool parseSucceed = long.TryParse(file.Attribute("size").Value.ToString(), out size);
                    bool Warn         = true;
                    if (file.Attribute("warn") != null)
                    {
                        if (file.Attribute("warn").Value == "no")
                        {
                            Warn = false;
                        }
                    }
                    string md5      = file.Attribute("md5").Value;
                    string fileName = file.Attribute("name").Value;

                    if (fileName.Trim() != "")
                    {
                        Fingerprint ManifestFingerprint = new Fingerprint(PathRoot, fileName, md5, size);
                        ManifestFingerprint.Warn = Warn;

                        IEnumerable <XElement> URLs = file.Descendants("url");

                        foreach (XElement URL in URLs)
                        {
                            ManifestFingerprint.AddDownloadURL(URL.Value.ToString().Trim());
                        }

                        m_ManifestFileList.Add(ManifestFingerprint);
                    }
                }

                m_progress   = 0;
                m_Status     = "Verifying";
                myWorkThread = new Thread(new ThreadStart(Validate));
                myWorkThread.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "WorkThread.ManifestDownloadComplete()");
                string a = ex.Message;
            }
        }