Esempio n. 1
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();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates the update script on disk.
        /// </summary>
        /// <returns>ProcessStartInfo for the update script.</returns>
        private static ProcessStartInfo CreateUpdateScript()
        {
            try
            {
                //maintain the executable name if it was renamed to something other than 'Launchpad'
                string fullName       = Assembly.GetEntryAssembly().Location;
                string executableName = Path.GetFileName(fullName);                 // should be "Launchpad", unless the user has renamed it

                if (ChecksHandler.IsRunningOnUnix())
                {
                    //creating a .sh script
                    string scriptPath = String.Format(@"{0}launchpadupdate.sh",
                                                      ConfigHandler.GetTempDir());


                    FileStream updateScript = File.Create(scriptPath);
                    TextWriter tw           = new StreamWriter(updateScript);

                    //write commands to the script
                    //wait five seconds, then copy the new executable
                    string copyCom = String.Format("cp -rf {0} {1}",
                                                   ConfigHandler.GetTempDir() + "launchpad/*",
                                                   ConfigHandler.GetLocalDir());

                    string delCom = String.Format("rm -rf {0}",
                                                  ConfigHandler.GetTempDir() + "launchpad");

                    string dirCom    = String.Format("cd {0}", ConfigHandler.GetLocalDir());
                    string launchCom = String.Format(@"nohup ./{0} &", executableName);
                    tw.WriteLine(@"#!/bin/sh");
                    tw.WriteLine("sleep 5");
                    tw.WriteLine(copyCom);
                    tw.WriteLine(delCom);
                    tw.WriteLine(dirCom);
                    tw.WriteLine("chmod +x " + executableName);
                    tw.WriteLine(launchCom);
                    tw.Close();

                    UnixHandler.MakeExecutable(scriptPath);


                    //Now create some ProcessStartInfo for this script
                    ProcessStartInfo updateShellProcess = new ProcessStartInfo();

                    updateShellProcess.FileName               = scriptPath;
                    updateShellProcess.UseShellExecute        = false;
                    updateShellProcess.RedirectStandardOutput = false;
                    updateShellProcess.WindowStyle            = ProcessWindowStyle.Hidden;

                    return(updateShellProcess);
                }
                else
                {
                    //creating a .bat script
                    string scriptPath = String.Format(@"{0}launchpadupdate.bat",
                                                      ConfigHandler.GetTempDir());

                    FileStream updateScript = File.Create(scriptPath);

                    TextWriter tw = new StreamWriter(updateScript);

                    //write commands to the script
                    //wait three seconds, then copy the new executable
                    tw.WriteLine(String.Format(@"timeout 3 & xcopy /e /s /y ""{0}\launchpad"" ""{1}"" && rmdir /s /q {0}\launchpad",
                                               ConfigHandler.GetTempDir(),
                                               ConfigHandler.GetLocalDir()));

                    //then start the new executable
                    tw.WriteLine(String.Format(@"start {0}", executableName));
                    tw.Close();

                    ProcessStartInfo updateBatchProcess = new ProcessStartInfo();

                    updateBatchProcess.FileName               = scriptPath;
                    updateBatchProcess.UseShellExecute        = true;
                    updateBatchProcess.RedirectStandardOutput = false;
                    updateBatchProcess.WindowStyle            = ProcessWindowStyle.Hidden;

                    return(updateBatchProcess);
                }
            }
            catch (IOException ioex)
            {
                Console.WriteLine("IOException in CreateUpdateScript(): " + ioex.Message);

                return(null);
            }
        }
Esempio n. 3
0
        private void InstallGameAsync()
        {
            //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 fileReturn = "";

            try
            {
                FTPHandler           FTP             = new FTPHandler();
                ManifestHandler      manifestHandler = new ManifestHandler();
                List <ManifestEntry> Manifest        = manifestHandler.Manifest;

                //create the .install file to mark that an installation has begun
                //if it exists, do nothing.
                ConfigHandler.CreateInstallCookie();


                //raise the progress changed event by binding to the
                //event in the FTP class
                FTP.FileProgressChanged += OnDownloadProgressChanged;

                //in order to be able to resume downloading, we check if there is an entry
                //stored in the install cookie.
                ManifestEntry lastDownloadedFile = null;
                string        installCookiePath  = ConfigHandler.GetInstallCookiePath();

                //attempt to parse whatever is inside the install cookie
                if (ManifestEntry.TryParse(File.ReadAllText(installCookiePath), out lastDownloadedFile))
                {
                    //loop through all the entries in the manifest until we encounter
                    //an entry which matches the one in the install cookie

                    foreach (ManifestEntry Entry in Manifest)
                    {
                        if (lastDownloadedFile == Entry)
                        {
                            //remove all entries before the one we were last at.
                            Manifest.RemoveRange(0, Manifest.IndexOf(Entry));
                        }
                    }
                }

                //then, start downloading the entries that remain in the manifest.
                foreach (ManifestEntry Entry in Manifest)
                {
                    string RemotePath = String.Format("{0}{1}",
                                                      Config.GetGameURL(true),
                                                      Entry.RelativePath);

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

                    //make sure we have a game directory to put files in
                    Directory.CreateDirectory(Path.GetDirectoryName(LocalPath));

                    //write the current file progress to the install cookie
                    TextWriter textWriterProgress = new StreamWriter(ConfigHandler.GetInstallCookiePath());
                    textWriterProgress.WriteLine(Entry.ToString());
                    textWriterProgress.Close();

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

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

                            if (localHash != Entry.Hash)
                            {
                                Console.WriteLine("InstallGameAsync: Resumed file hash was invalid, downloading fresh copy from server.");
                                OnProgressChanged();
                                fileReturn = FTP.DownloadFTPFile(RemotePath, LocalPath, false);
                            }
                        }
                    }
                    else
                    {
                        //no file, download it
                        OnProgressChanged();
                        fileReturn = 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);
                        }
                    }
                }

                //we've finished the download, so empty the cookie
                File.WriteAllText(ConfigHandler.GetInstallCookiePath(), String.Empty);

                //raise the finished event
                OnGameDownloadFinished();

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

                DownloadFailedArgs.Result     = "1";
                DownloadFailedArgs.ResultType = "Install";
                DownloadFinishedArgs.Metadata = fileReturn;

                OnGameDownloadFailed();
            }
        }