示例#1
0
 public static RSSFile Download(string url, ProgressDelegate listener, ICancellable cancelObject)
 {
     try
     {
         Logger.Log("Downloading RSS file from {0}", url);
         var request = HttpWebRequest.Create(url);
         request.Timeout = 15000;
         using (var response = request.GetResponse())
         {
             using (var stream = new ProgressStream(response.GetResponseStream(), response.ContentLength, listener, cancelObject))
             {
                 try
                 {
                     return(new RSSFile(stream));
                 }
                 finally
                 {
                     stream.Close();
                 }
             }
         }
     }
     catch (Exception e)
     {
         Logger.Log("Caught exception: {0}", e.ToString());
         return(null);
     }
 }
示例#2
0
 public static RSSFile Download(string url, ProgressDelegate listener, ICancellable cancelObject)
 {
     try
     {
         var request = HttpWebRequest.Create(url);
         request.Timeout = 15000;
         using (var response = request.GetResponse())
         {
             using (var stream = new ProgressStream(response.GetResponseStream(), response.ContentLength, listener, cancelObject))
             {
                 try
                 {
                     return(new RSSFile(stream));
                 }
                 finally
                 {
                     stream.Close();
                 }
             }
         }
     }
     catch (IOException)
     {
         return(null);
     }
     catch (WebException)
     {
         return(null);
     }
 }
示例#3
0
        private void BlobTransferCompletedCallback(IAsyncResult result)
        {
            BlobTransferAsyncState state  = (BlobTransferAsyncState)result.AsyncState;
            ICloudBlob             blob   = state.Blob;
            ProgressStream         stream = (ProgressStream)state.Stream;

            try
            {
                stream.Close();

                // End the operation.
                if (TransferType == TransferTypeEnum.Download)
                {
                    blob.EndDownloadToStream(result);
                }
                else if (TransferType == TransferTypeEnum.Upload)
                {
                    blob.EndUploadFromStream(result);
                }

                // Operation completed normally, raise the completed event
                AsyncCompletedEventArgs completedArgs = new AsyncCompletedEventArgs(null, false, null);
                asyncOp.PostOperationCompleted(delegate(object e) { OnTaskCompleted((AsyncCompletedEventArgs)e); }, completedArgs);
            }
            catch (StorageException ex)
            {
                if (!state.Cancelled)
                {
                    throw (ex);
                }

                // Operation was cancelled, raise the event with the cancelled flag = true
                AsyncCompletedEventArgs completedArgs = new AsyncCompletedEventArgs(null, true, null);
                asyncOp.PostOperationCompleted(delegate(object e) { OnTaskCompleted((AsyncCompletedEventArgs)e); }, completedArgs);
            }
        }
示例#4
0
        public static bool InstallGame(string gameTitle, string gameVersion, ProgressDelegate listener, ICancellable cancelObject)
        {
            var downloadPath = GetDownloadPath(gameTitle, gameVersion);
            var installPath  = GetInstallPath(gameTitle, gameVersion);

            if (File.Exists(downloadPath))
            {
                Logger.Log("Installing game ({0} {1})", gameTitle, gameVersion);
                try
                {
                    using (var zipFile = new ZipFile(downloadPath))
                    {
                        // Delete old install
                        if (Directory.Exists(installPath))
                        {
                            Directory.Delete(installPath, true);
                        }
                        Directory.CreateDirectory(installPath);

                        // Extract new install
                        int totalFiles     = zipFile.Entries.Count;
                        int filesInstalled = 0;
                        int lastProgress   = 0;
                        listener.Invoke(0);
                        foreach (var entry in zipFile.Entries)
                        {
                            // Extract the file
                            var entryInstallPath = Path.Combine(installPath, entry.FileName);
                            if (entry.IsDirectory)
                            {
                                Directory.CreateDirectory(entryInstallPath);
                            }
                            else
                            {
                                Directory.CreateDirectory(Path.GetDirectoryName(entryInstallPath));
                                using (var file = File.OpenWrite(entryInstallPath))
                                {
                                    try
                                    {
                                        using (var reader = new ProgressStream(entry.OpenReader(), -1, delegate {
                                            // TODO: Emit progress during installation of large individual files?
                                        }, cancelObject))
                                        {
                                            try
                                            {
                                                reader.CopyTo(file);
                                                try
                                                {
                                                    if (Program.Platform == Platform.Linux ||
                                                        Program.Platform == Platform.OSX)
                                                    {
                                                        MakeFileExecutable(entryInstallPath);
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    Logger.Log("Caught Exception: {0}", e.ToString());
                                                    Logger.Log("Failed to set file permissions on {0}", entryInstallPath);
                                                }
                                            }
                                            finally
                                            {
                                                reader.Close();
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        file.Close();
                                    }
                                }
                            }

                            // Check for cancellation
                            if (cancelObject.Cancelled)
                            {
                                throw new IOCancelledException();
                            }

                            // Notify the progress listener
                            filesInstalled++;
                            int progress = (filesInstalled * 100) / totalFiles;
                            if (progress != lastProgress)
                            {
                                listener.Invoke(progress);
                                lastProgress = progress;
                            }
                        }
                    }
                    return(true);
                }
                catch (Exception e)
                {
                    Logger.Log("Caught Exception: {0}", e.ToString());
                    if (Directory.Exists(installPath))
                    {
                        Directory.Delete(installPath, true);
                    }
                    return(false);
                }
            }
            return(false);
        }
示例#5
0
        public static bool DownloadGame(string gameTitle, string gameVersion, string url, string username, string password, ProgressDelegate listener, ICancellable cancelObject, out bool o_authFailure, out string o_customMessage)
        {
            if (url == null)
            {
                o_authFailure   = false;
                o_customMessage = null;
                return(false);
            }

            var downloadPath = GetDownloadPath(gameTitle, gameVersion);

            Logger.Log("Downloading game ({0} {1}) from {2}", gameTitle, gameVersion, downloadPath);
            try
            {
                var request = HttpWebRequest.Create(url);
                request.Timeout = 15000;
                if (username != null && password != null)
                {
                    request.Credentials = new NetworkCredential(username, password);
                }
                using (var response = request.GetResponse())
                {
                    // Read the message
                    o_customMessage = response.Headers.Get("X-IndieLauncher-Message");
                    if (o_customMessage != null)
                    {
                        Logger.Log("URL returned message: {0}", o_customMessage);
                    }

                    // Read the content
                    using (var stream = new ProgressStream(response.GetResponseStream(), response.ContentLength, listener, cancelObject))
                    {
                        try
                        {
                            // Delete old download
                            if (File.Exists(downloadPath))
                            {
                                File.Delete(downloadPath);
                            }

                            // Create new download
                            Directory.CreateDirectory(Path.GetDirectoryName(downloadPath));
                            using (var output = File.OpenWrite(downloadPath))
                            {
                                try
                                {
                                    stream.CopyTo(output);
                                }
                                finally
                                {
                                    output.Close();
                                }
                            }
                        }
                        finally
                        {
                            stream.Close();
                        }
                    }
                }
                o_authFailure = false;
                return(true);
            }
            catch (Exception e)
            {
                Logger.Log("Caught Exception: {0}", e.ToString());
                if (File.Exists(downloadPath))
                {
                    File.Delete(downloadPath);
                }
                if (e is WebException)
                {
                    WebException we = (WebException)e;
                    if (we.Response != null)
                    {
                        o_customMessage = we.Response.Headers.Get("X-IndieLauncher-Message");
                        if (o_customMessage != null)
                        {
                            Logger.Log("URL returned message: {0}", o_customMessage);
                        }
                        if (((HttpWebResponse)we.Response).StatusCode == HttpStatusCode.Unauthorized)
                        {
                            o_authFailure = true;
                            Logger.Log("URL returned HTTP Unauthorized");
                        }
                        else
                        {
                            o_authFailure = false;
                        }
                    }
                    else
                    {
                        o_customMessage = null;
                        o_authFailure   = false;
                    }
                }
                else
                {
                    o_customMessage = null;
                    o_authFailure   = false;
                }
                return(false);
            }
        }
示例#6
0
        public static bool ExtractEmbeddedGame(ProgressDelegate listener, ICancellable cancelObject)
        {
            string gameTitle, gameVersion, gameURL, username, password;

            if (GetEmbeddedGameInfo(out gameTitle, out gameVersion, out gameURL, out username, out password) && gameVersion != null)
            {
                Logger.Log("Extracting embedded game ({0} {1})", gameTitle, gameVersion);
                var downloadPath = GetDownloadPath(gameTitle, gameVersion);
                try
                {
                    var assembly = Assembly.GetExecutingAssembly();
                    var stream   = assembly.GetManifestResourceStream("EmbeddedGame." + Program.Platform + ".zip");
                    if (stream == null)
                    {
                        stream = assembly.GetManifestResourceStream("EmbeddedGame.zip");
                    }
                    if (stream != null)
                    {
                        using ( stream )
                        {
                            try
                            {
                                using (var progressStream = new ProgressStream(stream, -1, listener, cancelObject))
                                {
                                    // Delete old download
                                    if (File.Exists(downloadPath))
                                    {
                                        File.Delete(downloadPath);
                                    }

                                    // Create new download
                                    try
                                    {
                                        Directory.CreateDirectory(Path.GetDirectoryName(downloadPath));
                                        using (var output = File.OpenWrite(downloadPath))
                                        {
                                            try
                                            {
                                                progressStream.CopyTo(output);
                                            }
                                            finally
                                            {
                                                output.Close();
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        progressStream.Close();
                                    }
                                }
                            }
                            finally
                            {
                                stream.Close();
                            }
                        }
                        return(true);
                    }
                    return(false);
                }
                catch (Exception e)
                {
                    Logger.Log("Caught Exception: {0}", e.ToString());
                    if (File.Exists(downloadPath))
                    {
                        File.Delete(downloadPath);
                    }
                    return(false);
                }
            }
            return(false);
        }
示例#7
0
        public static bool DownloadGame(string gameTitle, string gameVersion, string url, string username, string password, ProgressDelegate listener, ICancellable cancelObject, out bool o_authFailure)
        {
            if (url == null)
            {
                o_authFailure = false;
                return(false);
            }

            var downloadPath = GetDownloadPath(gameTitle, gameVersion);

            try
            {
                var request = HttpWebRequest.Create(url);
                request.Timeout = 15000;
                if (username != null && password != null)
                {
                    request.Credentials = new NetworkCredential(username, password);
                }
                using (var response = request.GetResponse())
                {
                    using (var stream = new ProgressStream(response.GetResponseStream(), response.ContentLength, listener, cancelObject))
                    {
                        try
                        {
                            // Delete old download
                            if (File.Exists(downloadPath))
                            {
                                File.Delete(downloadPath);
                            }

                            // Create new download
                            Directory.CreateDirectory(Path.GetDirectoryName(downloadPath));
                            using (var output = File.OpenWrite(downloadPath))
                            {
                                try
                                {
                                    stream.CopyTo(output);
                                }
                                finally
                                {
                                    output.Close();
                                }
                            }
                        }
                        finally
                        {
                            stream.Close();
                        }
                    }
                }
                o_authFailure = false;
                return(true);
            }
            catch (IOException)
            {
                if (File.Exists(downloadPath))
                {
                    File.Delete(downloadPath);
                }
                o_authFailure = false;
                return(false);
            }
            catch (WebException e)
            {
                if (File.Exists(downloadPath))
                {
                    File.Delete(downloadPath);
                }
                if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
                {
                    o_authFailure = true;
                }
                else
                {
                    o_authFailure = false;
                }
                return(false);
            }
        }