コード例 #1
0
ファイル: MegaUpdater.cs プロジェクト: MaiKuraki/KKManager
        private async Task <List <UpdateTask> > CollectTasks(List <INode> nodes, CancellationToken cancellationToken)
        {
            var results = new List <UpdateTask>();

            cancellationToken.ThrowIfCancellationRequested();

            var root = nodes.Single(x => x.Type == NodeType.Root);

            var subNodes       = GetSubNodes(root).ToList();
            var updateManifest = subNodes.FirstOrDefault(x => x.Type == NodeType.File && x.Name == UpdateInfo.UpdateFileName);

            if (updateManifest == null)
            {
                throw new FileNotFoundException($"Failed to get the update list - {UpdateInfo.UpdateFileName} is missing in host: mega");
            }

            var result = await _client.DownloadAsync(updateManifest, null, cancellationToken);

            foreach (var updateInfo in UpdateInfo.ParseUpdateManifest(result, CurrentFolderLink.OriginalString, 10))
            {
                _latestModifiedDate = DateTime.MinValue;

                // Find the remote directory
                var updateNode = root;
                var pathParts  = updateInfo.ServerPath.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var pathPart in pathParts)
                {
                    updateNode = GetSubNodes(updateNode).FirstOrDefault(node => node.Type == NodeType.Directory && string.Equals(node.Name, pathPart, StringComparison.OrdinalIgnoreCase));
                    if (updateNode == null)
                    {
                        throw new DirectoryNotFoundException($"Could not find ServerPath: {updateInfo.ServerPath} in host: mega");
                    }
                }

                var versionEqualsComparer = GetVersionEqualsComparer(updateInfo);

                var updateItems = ProcessDirectory(updateNode, updateInfo.ClientPath,
                                                   updateInfo.Recursive, updateInfo.RemoveExtraClientFiles, versionEqualsComparer,
                                                   cancellationToken);

                results.Add(new UpdateTask(updateInfo.Name ?? updateNode.Name, updateItems, updateInfo, _latestModifiedDate));
            }

            return(results);
        }
コード例 #2
0
ファイル: FtpUpdater.cs プロジェクト: MaiKuraki/KKManager
        public async Task <List <UpdateTask> > GetUpdateItems(CancellationToken cancellationToken)
        {
            await Connect();

            var allResults = new List <UpdateTask>();

            using (var str = new MemoryStream())
            {
                var b = await _client.DownloadAsync(str, UpdateInfo.UpdateFileName, 0, null, cancellationToken);

                if (!b)
                {
                    throw new FileNotFoundException($"Failed to get the update list - {UpdateInfo.UpdateFileName} is missing in host: {_client.Host}");
                }

                str.Seek(0, SeekOrigin.Begin);
                var updateInfos = UpdateInfo.ParseUpdateManifest(str, _client.Host, 1).ToList();

                if (updateInfos.Any())
                {
                    _allNodes = _client.GetListing("/", FtpListOption.Recursive | FtpListOption.Size);

                    foreach (var updateInfo in updateInfos)
                    {
                        _latestModifiedDate = DateTime.MinValue;

                        var remote = GetNode(updateInfo.ServerPath);
                        if (remote == null)
                        {
                            throw new DirectoryNotFoundException($"Could not find ServerPath: {updateInfo.ServerPath} in host: {_client.Host}");
                        }

                        var versionEqualsComparer = GetVersionEqualsComparer(updateInfo);

                        var results = await ProcessDirectory(remote, updateInfo.ClientPath,
                                                             updateInfo.Recursive, updateInfo.RemoveExtraClientFiles, versionEqualsComparer,
                                                             cancellationToken);

                        allResults.Add(new UpdateTask(updateInfo.Name ?? remote.Name, results, updateInfo, _latestModifiedDate));
                    }
                }
            }
            return(allResults);
        }
コード例 #3
0
        public async Task <List <UpdateTask> > GetUpdateItems(CancellationToken cancellationToken)
        {
            var allResults = new List <UpdateTask>();

            var manifestFile = _zipfile.Entries.FirstOrDefault(entry => string.Equals(Path.GetFileName(entry.FileName), UpdateInfo.UpdateFileName, StringComparison.OrdinalIgnoreCase));

            if (manifestFile == null)
            {
                throw new FileNotFoundException($"Failed to get the update list - {UpdateInfo.UpdateFileName} is missing in host: {_zipfile.Name}");
            }
            using (var str = manifestFile.OpenReader())
            {
                foreach (var updateInfo in UpdateInfo.ParseUpdateManifest(str, _zipfile.Name, 100))
                {
                    _latestModifiedDate = DateTime.MinValue;

                    // Clean up the path into a usable form
                    var serverPath = updateInfo.ServerPath.Trim(' ', '\\', '/').Replace('\\', '/') + "/";

                    var remote = _zipfile.Entries.FirstOrDefault(entry => string.Equals(entry.FileName, serverPath, StringComparison.OrdinalIgnoreCase));
                    if (remote == null)
                    {
                        throw new DirectoryNotFoundException($"Could not find ServerPath: {updateInfo.ServerPath} in host: {_zipfile.Name}");
                    }

                    var versionEqualsComparer = GetVersionEqualsComparer(updateInfo);

                    var results = await ProcessDirectory(remote, updateInfo.ClientPath,
                                                         updateInfo.Recursive, updateInfo.RemoveExtraClientFiles, versionEqualsComparer,
                                                         cancellationToken);

                    allResults.Add(new UpdateTask(updateInfo.Name ?? Path.GetFileName(remote.FileName.Trim(' ', '\\', '/')), results, updateInfo, _latestModifiedDate));
                }
            }
            return(allResults);
        }
コード例 #4
0
ファイル: FtpUpdater.cs プロジェクト: StanNine/KKManager
        public async Task <List <UpdateTask> > GetUpdateItems(CancellationToken cancellationToken)
        {
            await Connect();

            var allResults = new List <UpdateTask>();

            using (var str = new MemoryStream())
            {
                var b = await _client.DownloadAsync(str, UpdateInfo.UpdateFileName, 0, null, cancellationToken);

                if (!b)
                {
                    throw new FileNotFoundException($"Failed to get the update list - {UpdateInfo.UpdateFileName} is missing in host: {_client.Host}");
                }

                str.Seek(0, SeekOrigin.Begin);
                var updateInfos = UpdateInfo.ParseUpdateManifest(str, _client.Host, 1).ToList();

                str.Seek(0, SeekOrigin.Begin);
                try
                {
                    if (await _client.DownloadAsync(str, "Updates1.xml", 0, null, cancellationToken))
                    {
                        str.Seek(0, SeekOrigin.Begin);
                        updateInfos.AddRange(UpdateInfo.ParseUpdateManifest(str, _client.Host, 1));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                updateInfos.RemoveAll(info =>
                {
                    if (!info.CheckConditions())
                    {
                        Console.WriteLine("Skipping " + info.GUID + " because of conditions");
                        return(true);
                    }
                    return(false);
                });

                if (updateInfos.Any())
                {
                    _allNodes = _client.GetListing("/", FtpListOption.Recursive | FtpListOption.Size);

                    foreach (var updateInfo in updateInfos)
                    {
                        _latestModifiedDate = DateTime.MinValue;

                        var remote = GetNode(updateInfo.ServerPath);
                        if (remote == null)
                        {
                            throw new DirectoryNotFoundException($"Could not find ServerPath: {updateInfo.ServerPath} in host: {_client.Host}");
                        }

                        var versionEqualsComparer = GetVersionEqualsComparer(updateInfo, remote);

                        var results = await ProcessDirectory(remote, updateInfo.ClientPathInfo,
                                                             updateInfo.Recursive, updateInfo.RemoveExtraClientFiles, versionEqualsComparer,
                                                             cancellationToken);

                        allResults.Add(new UpdateTask(updateInfo.Name ?? remote.Name, results, updateInfo, _latestModifiedDate));
                    }

                    // If a task is expanded by other tasks, remove the items that other tasks expand from it
                    foreach (var resultTask in allResults)
                    {
                        if (!string.IsNullOrEmpty(resultTask.Info.ExpandsGUID))
                        {
                            Console.WriteLine($"Expanding task {resultTask.Info.ExpandsGUID} with task {resultTask.Info.GUID}");
                            ApplyExtendedItems(resultTask.Info.ExpandsGUID, resultTask.Items, allResults);
                        }
                    }
                }
            }
            return(allResults);
        }
コード例 #5
0
        public virtual async Task <List <UpdateTask> > GetUpdateItems(CancellationToken cancellationToken)
        {
            var updateInfos = new List <UpdateInfo>();

            var filenamesToTry = new[] { UpdateInfo.UpdateFileName, "Updates1.xml", "Updates2.xml" };

            foreach (var fn in filenamesToTry)
            {
                Stream str = null;
                try
                {
                    str = await DownloadFileAsync(fn, cancellationToken);
                }
                catch (FileNotFoundException)
                {
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Failed to download Updates file {fn} from {Origin} - {ex.Message}");
                }

                if (str != null)
                {
                    try
                    {
                        updateInfos.AddRange(UpdateInfo.ParseUpdateManifest(str, this));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Failed to parse update manifest file {fn} from {Origin} - {ex.ToStringDemystified()}");
                    }
                    finally
                    {
                        str.Dispose();
                    }
                }
            }

            if (updateInfos.Count == 0)
            {
                throw new FileNotFoundException($"Failed to get update list from host {Origin} - check log for details.");
            }

            updateInfos.RemoveAll(
                info =>
            {
                if (!info.CheckConditions())
                {
                    Console.WriteLine($"Skipping {info.GUID} because of conditions");
                    return(true);
                }
                return(false);
            });

            var allResults = new List <UpdateTask>();

            if (updateInfos.Any())
            {
                foreach (var updateInfo in updateInfos)
                {
                    _latestModifiedDate = DateTime.MinValue;
                    var remoteItem = GetRemoteRootItem(updateInfo.ServerPath);
                    if (remoteItem == null)
                    {
                        throw new DirectoryNotFoundException($"Could not find ServerPath: {updateInfo.ServerPath} in host: {Origin}");
                    }

                    var versionEqualsComparer = GetVersionEqualsComparer(updateInfo);

                    await Task.Run(
                        () =>
                    {
                        var results = ProcessDirectory(
                            remoteItem, updateInfo.ClientPathInfo,
                            updateInfo.Recursive, updateInfo.RemoveExtraClientFiles, versionEqualsComparer,
                            cancellationToken);

                        allResults.Add(new UpdateTask(updateInfo.Name ?? remoteItem.Name, results, updateInfo, _latestModifiedDate));
                    }, cancellationToken);
                }

                // If a task is expanded by other tasks, remove the items that other tasks expand from it
                foreach (var resultTask in allResults)
                {
                    if (!string.IsNullOrEmpty(resultTask.Info.ExpandsGUID))
                    {
                        Console.WriteLine($"Expanding task {resultTask.Info.ExpandsGUID} with task {resultTask.Info.GUID}");
                        ApplyExtendedItems(resultTask.Info.ExpandsGUID, resultTask.Items, allResults);
                    }
                }
            }
            return(allResults);
        }