Esempio n. 1
0
        void SelfPatch()
        {
            try
            {
                Fingerprint myFingerprint = new Fingerprint(Settings.GamePath, "Tequila.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 == "tequila")
                    {
                        long size = 0;
                        long.TryParse(launcher.Attribute("size").Value.ToString(), out size);
                        string md5 = launcher.Attribute("md5").Value;

                        Fingerprint remoteLauncher = new Fingerprint(Settings.GamePath, "Tequila.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("Tequila 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()");
            }
        }
Esempio n. 2
0
        public void DownloadFiles()
        {
            foreach (Fingerprint file in m_DownloadQueue)
            {
                if (Kill)
                {
                    return;
                }

                HTTP client = new HTTP();

                bool   keepTrying  = true;
                string DownloadURL = file.DownloadURL;;

                while (keepTrying)
                {
                    try
                    {
                        MyToolkit.ActivityLog("Downloading file \"" + file.FullName + "\" from \"" + DownloadURL + "\"");
                        if (client.StartDownload(new AsyncCompletedEventHandler(DownloadFileComplete),
                                                 new DownloadProgressChangedEventHandler(dlProgress),
                                                 DownloadURL,
                                                 file.FullName + ".download"))
                        {
                            m_Status         = "Downloading";
                            m_DownloadActive = true;
                        }
                    }
                    catch (Exception ex) {
                        string er = ex.Message;
                    }

                    m_current = file.FullName;

                    while (client.Active)
                    {
                        if (Kill)
                        {
                            client.CancelDownload();
                            return;
                        }
                        System.Threading.Thread.Sleep(10);
                    }

                    Fingerprint Downloaded = new Fingerprint(file.RootPath, file.FileName + ".download");

                    if (!Downloaded.Equals(file))
                    {
                        // OK this file is no good, delete it.
                        File.Delete(file.FullName + ".download");

                        // lets try a different url...
                        DownloadURL = file.DownloadURL;

                        // Did we get a blank URL?
                        if (DownloadURL == "")
                        {
                            MyToolkit.ActivityLog("Download failed, no more URL's to try from");

                            // OK stop trying and report error...
                            keepTrying = false;

                            string Msg = "Download error: " + file.FileName;
                            if (Downloaded.Size == 0)
                            {
                                Msg += "\r\nWas unable to download file";
                            }
                            else
                            {
                                if (Downloaded.Size != file.Size)
                                {
                                    Msg += "\r\nSize mismatch (" + Downloaded.Size + " vs " + file.Size + ")";
                                }
                                if (Downloaded.Checksum != file.Checksum)
                                {
                                    Msg += "\r\nChecksum Mismatch (" + Downloaded.Checksum + " vs " + file.Checksum + ")";
                                }
                            }
                            if (file.Warn)
                            {
                                m_ErrorLog.Add(Msg);
                            }
                            else
                            {
                                m_WarningLog.Add(Msg);
                            }
                        }
                        else
                        {
                            MyToolkit.ActivityLog("Download failed, trying from a different URL");
                        }
                    }
                    else
                    {
                        if (File.Exists(file.FullName))
                        {
                            File.SetAttributes(file.FullName, File.GetAttributes(file.FullName) & ~FileAttributes.ReadOnly);
                            File.Delete(file.FullName);
                        }

                        // We are done, we dont need to keep trying (infinite loop if we dont set this)
                        keepTrying = false;
                        File.Move(file.FullName + ".download", file.FullName);
                        FlagVerified(file.FullName, file.Size, file.Checksum);
                    }
                }

                m_Downloaded += file.Size;
            }

            m_Status  = "Done";
            m_current = "";
        }