private static async Task <RepoSync> BuildAsync(IRepositoryMod repository, StorageMod storage, IStorageMod implementation, Logger logger, CancellationToken cancellationToken) { logger.Debug($"Building sync actions {storage} to {repository}"); var allActions = new List <SyncWorkUnit>(); var repositoryList = await repository.GetFileList(cancellationToken); var storageList = await implementation.GetFileList(cancellationToken); var storageListCopy = new List <string>(storageList); foreach (var repoFile in repositoryList) { if (storageList.Contains(repoFile)) { var repoFileHash = await repository.GetFileHash(repoFile, cancellationToken); var storageFileHash = await implementation.GetFileHash(repoFile, cancellationToken); if (!repoFileHash.Equals(storageFileHash)) { var fileSize = await repository.GetFileSize(repoFile, cancellationToken); allActions.Add(new UpdateAction(repository, implementation, repoFile, fileSize)); storageListCopy.Remove(repoFile + ".part"); } storageListCopy.Remove(repoFile); } else { var fileSize = await repository.GetFileSize(repoFile, cancellationToken); allActions.Add(new DownloadAction(repository, implementation, repoFile, fileSize)); } } foreach (var storageModFile in storageListCopy) { allActions.Add(new DeleteAction(implementation, storageModFile)); } logger.Debug($"Download actions: {allActions.OfType<DownloadAction>().Count()}"); logger.Debug($"Update actions: {allActions.OfType<UpdateAction>().Count()}"); logger.Debug($"Delete actions: {allActions.OfType<DeleteAction>().Count()}"); return(new RepoSync(allActions, logger)); }
// TODO: use more specialized interface to get files public static async Task <VersionHash> CreateAsync(IStorageMod mod, CancellationToken cancellationToken) { var hashes = new Dictionary <string, FileHash>(); foreach (var file in await mod.GetFileList(cancellationToken)) { var stream = await mod.OpenRead(file, cancellationToken); if (stream == null) { throw new InvalidOperationException(); } var hash = await Sha1AndPboHash.BuildAsync(stream, Utils.GetExtension(file), cancellationToken); hashes.Add(file, hash); } return(new VersionHash(BuildHash(hashes))); }
// TODO: use more specialized interface to get files public static async Task <MatchHash> CreateAsync(IStorageMod mod, CancellationToken cancellationToken) { var modCpp = await mod.OpenRead("/mod.cpp", cancellationToken); string?name = null; if (modCpp != null) { using var reader = new StreamReader(modCpp); var entries = ModUtil.ParseModCpp(reader.ReadToEnd()); name = entries.GetValueOrDefault("name"); if (name != null) { name = CleanName(name); } } var pboNames = await mod.GetFileList(cancellationToken); var pboNameSet = pboNames.Where(p => AddonsPboRegex.IsMatch(p)).ToHashSet(); return(new MatchHash(pboNameSet, name)); }