Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Session"/> class.
        /// </summary>
        protected Session()
        {
            _id = Crypto8.GenerateNonce();
#if DEBUG
            _ident = InternalUtils.BytesToString(_id);
#endif
        }
        /// <summary>
        /// Computes the master hash of the overall fingerprint file.
        /// </summary>
        /// <returns>Master hash of the overall fingerprint file.</returns>
        public string ComputeMasterHash()
        {
            var hashes = new StringBuilder();

            // Appends all hashes of the FingerprintFiles into hex-strings.
            for (int i = 0; i < _files.Count; i++)
            {
                var hash = _files[i].Hash;
                hashes.Append(hash);
            }

            var hashesBytes = Encoding.UTF8.GetBytes(hashes.ToString());

            using (var sha1 = SHA1.Create())
                return(InternalUtils.BytesToString(sha1.ComputeHash(hashesBytes)));
        }
        /// <summary>
        /// Creates a new <see cref="Fingerprint"/> for the specified directory path and specified version.
        /// </summary>
        /// <param name="path">Path pointing to the directory.</param>
        /// <param name="version">Version of the <see cref="Fingerprint"/>.</param>
        /// <returns>A <see cref="Fingerprint"/> representing the specified directory.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is empty or white space.</exception>
        /// <exception cref="DirectoryNotFoundException"><paramref name="path"/> does not exits.</exception>
        public static Fingerprint Create(string path, string version)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentException("Empty path is not valid.", "path");
            }
            if (!Directory.Exists(path))
            {
                throw new DirectoryNotFoundException("Could not find directory at '" + path + "'.");
            }

            var fingerprint      = new Fingerprint();
            var fingerprintFiles = new List <FingerprintFile>();
            var filePaths        = Directory.GetFiles(path, "*", SearchOption.AllDirectories);

            using (var sha1 = SHA1.Create())
            {
                for (int i = 0; i < filePaths.Length; i++)
                {
                    var filePath        = RemovePathRootDir(filePaths[i]);
                    var fileBytes       = File.ReadAllBytes(filePaths[i]);
                    var fingerprintFile = new FingerprintFile();

                    fingerprintFile.Path = filePath;
                    fingerprintFile.Hash = InternalUtils.BytesToString(sha1.ComputeHash(fileBytes));

                    fingerprintFiles.Add(fingerprintFile);
                }
            }

            fingerprint._files     = fingerprintFiles;
            fingerprint.MasterHash = fingerprint.ComputeMasterHash();
            fingerprint.Version    = version;

            return(fingerprint);
        }
        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());
        }