Пример #1
0
        //TODO: Update this function to handle DLLs as well. May have to implement a full-blown
        //manifest system here as well.

        /// <summary>
        /// Updates the launcher synchronously.
        /// </summary>
        public void UpdateLauncher()
        {
            try
            {
                FTPHandler FTP = new FTPHandler();

                //crawl the server for all of the files in the /launcher/bin directory.
                List <string> remotePaths = FTP.GetFilePaths(Config.GetLauncherBinariesURL(), true);

                //download all of them
                foreach (string path in remotePaths)
                {
                    try
                    {
                        if (!String.IsNullOrEmpty(path))
                        {
                            string Local = String.Format("{0}launchpad{1}{2}",
                                                         ConfigHandler.GetTempDir(),
                                                         Path.DirectorySeparatorChar,
                                                         path);

                            string Remote = String.Format("{0}{1}",
                                                          Config.GetLauncherBinariesURL(),
                                                          path);

                            if (!Directory.Exists(Local))
                            {
                                Directory.CreateDirectory(Directory.GetParent(Local).ToString());
                            }

                            FTP.DownloadFTPFile(Remote, Local, false);
                        }
                    }
                    catch (WebException wex)
                    {
                        Console.WriteLine("WebException in UpdateLauncher(): " + wex.Message);
                    }
                }

                //TODO: Make the script copy recursively
                ProcessStartInfo script = CreateUpdateScript();

                Process.Start(script);
                Environment.Exit(0);
            }
            catch (IOException ioex)
            {
                Console.WriteLine("IOException in UpdateLauncher(): " + ioex.Message);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the relative paths for all files in the specified FTP directory.
        /// </summary>
        /// <param name="rawRemoteURL">The URL to search.</param>
        /// <param name="bRecursively">Should the search should include subdirectories?</param>
        /// <returns>A list of relative paths for the files in the specified directory.</returns>
        public List <string> GetFilePaths(string rawRemoteURL, bool bRecursively)
        {
            FtpWebRequest  request       = null;
            FtpWebResponse response      = null;
            string         remoteURL     = Utilities.Clean(rawRemoteURL) + "/";
            List <string>  relativePaths = new List <string>();

            if (DoesDirectoryExist(remoteURL))
            {
                try
                {
                    request = CreateFtpWebRequest(
                        remoteURL,
                        Config.GetFTPUsername(),
                        Config.GetFTPPassword(),
                        false);

                    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

                    response = (FtpWebResponse)request.GetResponse();
                    Stream       responseStream = response.GetResponseStream();
                    StreamReader sr             = new StreamReader(responseStream);

                    string        rawListing  = sr.ReadToEnd();
                    string[]      listing     = rawListing.Replace("\r", String.Empty).Split('\n');
                    List <string> directories = new List <string>();

                    foreach (string fileOrDir in listing)
                    {
                        //we only need to save the directories if we're searching recursively
                        if (bRecursively && fileOrDir.StartsWith("d"))
                        {
                            //it's a directory, add it to directories
                            string[] parts = fileOrDir.Split(' ');
                            string   relativeDirectoryPath = parts[parts.Length - 1];

                            directories.Add(relativeDirectoryPath);
                        }
                        else
                        {
                            //there's a file, add it to our relative paths
                            string[] filePath = fileOrDir.Split(' ');
                            if (!String.IsNullOrEmpty(filePath[filePath.Length - 1]))
                            {
                                string relativePath = "/" + filePath[filePath.Length - 1];
                                relativePaths.Add(relativePath);
                            }
                        }
                    }

                    //if we should search recursively, keep looking in subdirectories.
                    if (bRecursively)
                    {
                        if (directories.Count != 0)
                        {
                            for (int i = 0; i < directories.Count; ++i)
                            {
                                string directory       = directories[i];
                                string parentDirectory = remoteURL.Replace(Config.GetLauncherBinariesURL(), String.Empty);

                                string        recursiveURL = Config.GetLauncherBinariesURL() + parentDirectory + "/" + directory;
                                List <string> files        = GetFilePaths(recursiveURL, true);
                                foreach (string rawPath in files)
                                {
                                    string relativePath = "/" + directory + rawPath;
                                    relativePaths.Add(relativePath);
                                }
                            }
                        }
                    }
                }
                catch (WebException wex)
                {
                    Console.WriteLine("WebException in GetFileURLs(): " + wex.Message);
                    return(null);
                }
                finally
                {
                    if (request != null)
                    {
                        request.Abort();
                    }

                    if (response != null)
                    {
                        response.Close();
                    }
                }
            }

            return(relativePaths);
        }