private void RepairGameAsync()
        {
            //This value is filled with either a path to the last downloaded file, or with an exception message
            //this message is used in the main UI to determine how it responds to a failed download.
            string repairMetadata = "";
            try
            {
                //check all local file MD5s against latest manifest. Resume partial files, download broken files.
                FTPHandler FTP = new FTPHandler ();

                //bind event handlers
                FTP.FileProgressChanged += OnDownloadProgressChanged;

                //first, verify that the manifest is correct.
                string LocalManifestHash = MD5Handler.GetFileHash(File.OpenRead(ConfigHandler.GetManifestPath ()));
                string RemoteManifestHash = FTP.GetRemoteManifestChecksum ();

                //if it is not, download a new copy.
                if (!(LocalManifestHash == RemoteManifestHash))
                {
                    LauncherHandler Launcher = new LauncherHandler ();
                    Launcher.DownloadManifest ();
                }

                //then, begin repairing the game
                ManifestHandler manifestHandler = new ManifestHandler ();
                List<ManifestEntry> Manifest = manifestHandler.Manifest;

                ProgressArgs.TotalFiles = Manifest.Count;

                int i = 0;
                foreach (ManifestEntry Entry in Manifest)
                {

                    string RemotePath = String.Format("{0}{1}",
                                                       Config.GetGameURL(true),
                                                       Entry.RelativePath);

                    string LocalPath = String.Format ("{0}{1}",
                                                      Config.GetGamePath (true),
                                                      Entry.RelativePath);

                    ProgressArgs.FileName = Path.GetFileName(LocalPath);

                    //make sure the directory for the file exists
                    Directory.CreateDirectory(Directory.GetParent(LocalPath).ToString());

                    if (File.Exists(LocalPath))
                    {

                        FileInfo fileInfo = new FileInfo(LocalPath);
                        if (fileInfo.Length != Entry.Size)
                        {
                            //Resume the download of this partial file.
                            OnProgressChanged();
                            repairMetadata = FTP.DownloadFTPFile(RemotePath, LocalPath, fileInfo.Length, false);

                            //Now verify the file
                            string localHash = MD5Handler.GetFileHash(File.OpenRead(LocalPath));

                            if (localHash != Entry.Hash)
                            {
                                Console.WriteLine ("RepairGameAsync: Resumed file hash was invalid, downloading fresh copy from server.");

                                //download the file, since it was broken
                                OnProgressChanged ();
                                repairMetadata = FTP.DownloadFTPFile (RemotePath, LocalPath, false);
                            }
                        }
                    }
                    else
                    {
                        //download the file, since it was missing
                        OnProgressChanged ();
                        repairMetadata = FTP.DownloadFTPFile (RemotePath, LocalPath, false);
                    }

                    if (ChecksHandler.IsRunningOnUnix())
                    {
                        //if we're dealing with a file that should be executable,
                        string gameName = Config.GetGameName();
                        bool bFileIsGameExecutable = (Path.GetFileName(LocalPath).EndsWith(".exe")) || (Path.GetFileNameWithoutExtension(LocalPath) == gameName);
                        if (bFileIsGameExecutable)
                        {
                            //set the execute bits.
                            UnixHandler.MakeExecutable(LocalPath);
                        }
                    }

                    ++i;
                    ProgressArgs.DownloadedFiles = i;
                    OnProgressChanged ();
                }

                OnGameRepairFinished ();

                //clear out the event handler
                FTP.FileProgressChanged -= OnDownloadProgressChanged;
            }
            catch (IOException ioex)
            {
                Console.WriteLine ("IOException in RepairGameAsync(): " + ioex.Message);

                DownloadFailedArgs.Result = "1";
                DownloadFailedArgs.ResultType = "Repair";
                DownloadFailedArgs.Metadata = repairMetadata;

                OnGameRepairFailed ();
            }
        }
Exemple #2
0
        private void RepairGameAsync()
        {
            //This value is filled with either a path to the last downloaded file, or with an exception message
            //this message is used in the main UI to determine how it responds to a failed download.
            string repairMetadata = "";

            try
            {
                //check all local file MD5s against latest manifest. Resume partial files, download broken files.
                FTPHandler FTP = new FTPHandler();

                //bind event handlers
                FTP.FileProgressChanged += OnDownloadProgressChanged;

                //first, verify that the manifest is correct.
                string LocalManifestHash  = MD5Handler.GetFileHash(File.OpenRead(ConfigHandler.GetManifestPath()));
                string RemoteManifestHash = FTP.GetRemoteManifestChecksum();

                //if it is not, download a new copy.
                if (!(LocalManifestHash == RemoteManifestHash))
                {
                    LauncherHandler Launcher = new LauncherHandler();
                    Launcher.DownloadManifest();
                }

                //then, begin repairing the game
                ManifestHandler      manifestHandler = new ManifestHandler();
                List <ManifestEntry> Manifest        = manifestHandler.Manifest;

                ProgressArgs.TotalFiles = Manifest.Count;

                int i = 0;
                foreach (ManifestEntry Entry in Manifest)
                {
                    string RemotePath = String.Format("{0}{1}",
                                                      Config.GetGameURL(true),
                                                      Entry.RelativePath);

                    string LocalPath = String.Format("{0}{1}",
                                                     Config.GetGamePath(true),
                                                     Entry.RelativePath);

                    ProgressArgs.FileName = Path.GetFileName(LocalPath);

                    //make sure the directory for the file exists
                    Directory.CreateDirectory(Directory.GetParent(LocalPath).ToString());

                    if (File.Exists(LocalPath))
                    {
                        FileInfo fileInfo = new FileInfo(LocalPath);
                        if (fileInfo.Length != Entry.Size)
                        {
                            //Resume the download of this partial file.
                            OnProgressChanged();
                            repairMetadata = FTP.DownloadFTPFile(RemotePath, LocalPath, fileInfo.Length, false);

                            //Now verify the file
                            string localHash = MD5Handler.GetFileHash(File.OpenRead(LocalPath));

                            if (localHash != Entry.Hash)
                            {
                                Console.WriteLine("RepairGameAsync: Resumed file hash was invalid, downloading fresh copy from server.");

                                //download the file, since it was broken
                                OnProgressChanged();
                                repairMetadata = FTP.DownloadFTPFile(RemotePath, LocalPath, false);
                            }
                        }
                    }
                    else
                    {
                        //download the file, since it was missing
                        OnProgressChanged();
                        repairMetadata = FTP.DownloadFTPFile(RemotePath, LocalPath, false);
                    }

                    if (ChecksHandler.IsRunningOnUnix())
                    {
                        //if we're dealing with a file that should be executable,
                        string gameName = Config.GetGameName();
                        bool   bFileIsGameExecutable = (Path.GetFileName(LocalPath).EndsWith(".exe")) || (Path.GetFileNameWithoutExtension(LocalPath) == gameName);
                        if (bFileIsGameExecutable)
                        {
                            //set the execute bits.
                            UnixHandler.MakeExecutable(LocalPath);
                        }
                    }

                    ++i;
                    ProgressArgs.DownloadedFiles = i;
                    OnProgressChanged();
                }

                OnGameRepairFinished();

                //clear out the event handler
                FTP.FileProgressChanged -= OnDownloadProgressChanged;
            }
            catch (IOException ioex)
            {
                Console.WriteLine("IOException in RepairGameAsync(): " + ioex.Message);

                DownloadFailedArgs.Result     = "1";
                DownloadFailedArgs.ResultType = "Repair";
                DownloadFailedArgs.Metadata   = repairMetadata;

                OnGameRepairFailed();
            }
        }