/// <summary>
 /// Use this method to trigger the <see cref="DownloadProgressChanged"/> event.
 /// </summary>
 /// <param name="e">The arguments.</param>
 protected virtual void OnDownloadProgressChanged(AssetDownloadProgressChangedEventArgs e)
 {
     DownloadProgressChanged?.Invoke(this, e);
 }
        private void InternalDownloadAssets(Uri remoteRootPath, Fingerprint fingerprint, string dstDir, bool checkHash)
        {
            //var localRootDir = Path.Combine(dstDir, _masterHash);
            var localRootDir = dstDir;

            // Create a new directory called as the value of _masterHash.
            if (!Directory.Exists(localRootDir))
            {
                Directory.CreateDirectory(localRootDir);
            }

            for (int i = 0; i < fingerprint.Count; i++)
            {
                var file = fingerprint[i];

                // Root directory's name of the file's path.
                var dirName = Path.GetDirectoryName(file.Path);
                // Local directory of the file's path
                var localDirPath = Path.Combine(localRootDir, dirName);

                // Make sure the directory exists first.
                if (!Directory.Exists(localDirPath))
                {
                    Directory.CreateDirectory(localDirPath);
                }

                var localFilePath = Path.Combine(localRootDir, file.Path);

                // Compute the SHA1 hashes of the files and check if the new files needs to downloaded
                if (checkHash)
                {
                    // If we're checking the SHA1 and if a file already exists with the same name/path.
                    if (File.Exists(localFilePath))
                    {
                        var existingFileBytes = File.ReadAllBytes(localFilePath);
                        var existingFileHash  = InternalUtils.BytesToString(_sha1.ComputeHash(existingFileBytes));

                        // If the existing file have the same SHA1 as the one in the fingerprint
                        // we continue and ignore it.
                        if (existingFileHash == file.Hash)
                        {
                            var e = new AssetDownloadProgressChangedEventArgs()
                            {
                                DownloadedCount    = i + 1,
                                FileDownloaded     = file,
                                WasDownloaded      = false,
                                NextDownload       = fingerprint[i],
                                ProgressPercentage = ((i + 1) / (double)fingerprint.Count) * 100,
                            };
                            OnDownloadProgressChanged(e);
                            continue;
                        }
                    }
                }

                var fileBytes = DownloadFile(remoteRootPath, file.Path);
                var args      = new AssetDownloadProgressChangedEventArgs()
                {
                    DownloadedCount    = i + 1,
                    FileDownloaded     = file,
                    WasDownloaded      = true,
                    NextDownload       = fingerprint[i],
                    ProgressPercentage = ((i + 1) / (double)fingerprint.Count) * 100,
                };
                OnDownloadProgressChanged(args);

                File.WriteAllBytes(localFilePath, fileBytes);
            }

            OnDownloadCompleted(new AssetDownloadCompletedEventArgs());
        }