Пример #1
0
        private static void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            Complete = true;
            if (DownloadFile != null)
            {
                DownloadFile.Close();
            }
            if (sender == null && e == null)
            {
                logger.Info($"LOCAL FILE FOUND");
            }
            else
            {
                logger.Info($"]");
            }
            logger.Info($"Downloading {FileName} complete");
            string   ForgeHX_FilePath = Path.Combine(ProgramPath, FileName);
            FileInfo fi      = new FileInfo(ForgeHX_FilePath);
            var      content = File.ReadAllText(ForgeHX_FilePath);

            try
            {
                var startIndex = content.IndexOf(".BUILD_NUMBER=\"");
                var endIndex   = content.IndexOf(".TILE_SPEC_NAME_CONTEMPORARY_BUSHES=\"");
                content = content.Substring(startIndex, endIndex - startIndex);
                content = content.Replace("\n", "").Replace("\r", "");
                var regExSecret  = new Regex("\\.VERSION_SECRET=\"([a-zA-Z0-9_\\-\\+\\/==]+)\";", RegexOptions.IgnoreCase);
                var regExVersion = new Regex("\\.VERSION_MAJOR_MINOR=\"([0-9+.0-9+.0-9+]+)\";", RegexOptions.IgnoreCase);
                var VersionMatch = regExVersion.Match(content);
                var SecretMatch  = regExSecret.Match(content);
                if (VersionMatch.Success)
                {
                    SettingData.Version = VersionMatch.Groups[1].Value;
                }
                if (SecretMatch.Success)
                {
                    SettingData.Version_Secret = SecretMatch.Groups[1].Value;
                    SECRET_LOADED = true;
                }
            }
            catch (Exception ex)
            {
                logger.Info($"EXCEPTION: {ex.StackTrace}");
                fi.Delete();
                ForgeHXLoaded = false;
                DownloadForge();
            }
            finally
            {
                if (SECRET_LOADED)
                {
                    _ForgeHXLoaded?.Invoke(null, null);
                }
                SECRET_LOADED = false;
            }
        }
Пример #2
0
            //Starts the background thread for file download and reports the update to the delegates created above.
            private void StartAsyncDownloadThread()
            {
                //trying and catching exception for creating download request.
                try
                {
                    //creating a webrequest and defining its method as GET to download file.
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
                    request.KeepAlive        = true;
                    request.Timeout          = 30000;
                    request.ReadWriteTimeout = 30000;
                    FileStream DownloadFile;
                    request.Method = "GET";
                    //Getting Total Size of the file to download
                    TotalBytesToDownload = request.GetResponse().ContentLength;
                    FileInfo FileToDownload = new FileInfo(_downloadLocation);

                    //Switching cases for resumable and non resumable as per the value passed in the constructor.
                    if (_mode == DownloadMode.NonResumable)
                    {
                        if (File.Exists(_downloadLocation))
                        {
                            if (FileToDownload.Length > 0 && FileToDownload.Length < TotalBytesToDownload)
                            {
                                if (_DownloadAnyway)
                                {
                                    FileToDownload.Delete();
                                }
                                else
                                {
                                    UnityEngine.Debug.Log("File Already Exsists");
                                    AsyncThread.Join();
                                    return;
                                }
                            }
                        }
                    }
                    else if (_mode == DownloadMode.Resumable)
                    {
                        if (File.Exists(_downloadLocation))
                        {
                            if (_DownloadAnyway && FileToDownload.Length < TotalBytesToDownload)
                            {
                                request        = (HttpWebRequest)WebRequest.Create(_url);
                                request.Method = "GET";
                                request.AddRange((int)FileToDownload.Length);
                                UnityEngine.Debug.Log("Request Headers: " + request.Headers.ToString());
                            }
                            else
                            {
                                UnityEngine.Debug.Log("File Already Exsists");
                                if (DownloadCompletedEvent != null)
                                {
                                    DownloadCompletedEvent(new OnDownloadCompletedEvent(null, false));
                                }
                                AsyncThread.Join();
                                return;
                            }
                        }
                    }
                    ///Getting response from the server.
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    //checking if the file exists and it exists and server supports partial content the append the incomming data of else create new file of 0B.
                    if (File.Exists(_downloadLocation))
                    {
                        if (response.StatusCode == HttpStatusCode.PartialContent)
                        {
                            DownloadFile = new FileStream(_downloadLocation, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                        }
                        else
                        {
                            DownloadFile = new FileStream(_downloadLocation, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                            DownloadFile.SetLength(0);
                        }
                    }
                    else
                    {
                        DownloadFile = new FileStream(_downloadLocation, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                    }
                    BytesDownloaded = DownloadFile.Length;

                    TotalBytesThisSession = 0;

                    UnityEngine.Debug.Log("Response Headers: " + response.Headers);
                    UnityEngine.Debug.Log("Response Status: " + response.StatusDescription);
                    Stream ResponseStream = response.GetResponseStream();
                    //Writing Bytes to files
                    BytesRead = ResponseStream.Read(_Buffer, 0, _Buffer.Length);
                    while (BytesRead > 0 && !_Cancelled)
                    {
                        if (!Paused)
                        {
                            _isbusy = true;
                            DownloadFile.Write(_Buffer, 0, BytesRead);
                            BytesDownloaded       += BytesRead;
                            TotalBytesThisSession += BytesRead;
                            BytesRead              = ResponseStream.Read(_Buffer, 0, _Buffer.Length);

                            //Reporting progress if the given event is registered.
                            if (ProgressChangedEvent != null)
                            {
                                ProgressChangedEvent(new OnProgressChangedEvent(BytesDownloaded, TotalBytesToDownload, TotalBytesThisSession, Paused));
                            }
                        }
                    }
                    //Report the download completion at the end of while loop.
                    if (DownloadCompletedEvent != null)
                    {
                        if (!_Cancelled)
                        {
                            DownloadCompletedEvent(new OnDownloadCompletedEvent(null, false));
                        }
                        else
                        {
                            DownloadCompletedEvent(new OnDownloadCompletedEvent(null, true));
                        }
                    }
                    DownloadFile.Flush();
                    DownloadFile.Close();
                    response.Close();
                    AsyncThread.Abort();
                    AsyncThread.Join();
                    _isbusy = false;
                }
                catch (Exception ex)
                {
                    _isbusy = false;
                    if (DownloadCompletedEvent != null)
                    {
                        DownloadCompletedEvent(new OnDownloadCompletedEvent(ex, false));
                    }
                }
            }