コード例 #1
0
        void DownloadManager_StateChanged(object sender, DownloadState state)
        {
            DownloadManager downloadManager = sender as DownloadManager;

            switch (state)
            {
            case DownloadState.Completed:
                // update existing information, i.e., versions, hashes etc.
                ValidateAddonGroup(downloadManager.AddonGroup);

                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    MainWindow mainWindow                 = (MainWindow)Window.GetWindow(this);
                    mainWindow.ProgressBar.Value          = 0;
                    mainWindow.ProgressBarText.Text       = Properties.Resources.EverythingUpToDate;
                    downloadManager.AddonGroup.StatusText = "";
                    ListViewAddonGroups.Items.Refresh();
                }));
                break;

            case DownloadState.Paused:
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    downloadManager.AddonGroup.ButtonText = Properties.Resources.Continue;
                    downloadManager.AddonGroup.StatusText = Properties.Resources.ProgressDownloadPaused;
                    ListViewAddonGroups.Items.Refresh();
                }));
                break;

            case DownloadState.Downloading:
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    downloadManager.AddonGroup.ButtonText = Properties.Resources.Pause;
                    downloadManager.AddonGroup.StatusText = Properties.Resources.ProgressDownloading;
                    ListViewAddonGroups.Items.Refresh();
                }));
                break;
            }
        }
コード例 #2
0
        void DownloadWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            DownloadArguments args = (DownloadArguments)e.Argument;

            RemoteIndex remoteIndex = WebAPI.GetIndex();

            // determine files to delete
            AddonGroup addonGroup = args.AddonGroup;

            foreach (RemoteAddon webAddon in addonGroup.RemoteAddons)
            {
                Uuid webAddonUuid = webAddon.Uuid;
                if (!FileIndexer.Instance.addonUuidToLocalAddonMap.ContainsKey(webAddonUuid))
                {
                    continue;
                }

                RemoteAddon remAddon = remoteIndex.FilesIndex[webAddon.Uuid];

                List <LocalAddon> localAddons = FileIndexer.Instance.addonUuidToLocalAddonMap[webAddonUuid];
                foreach (LocalAddon localAddon in localAddons)
                {
                    List <FilePath> removals = new();
                    foreach (LocalFileIndex fileIndex in localAddon.Files.Values)
                    {
                        FilePath relativeFilepath = fileIndex.Relative_filepath;
                        if (!remAddon.Files.ContainsKey(relativeFilepath.Replace("\\", "/")))
                        {
                            FilePath filePath = fileIndex.Absolute_filepath;
                            Log.Debug("deleting " + filePath);
                            removals.Add(filePath);
                            File.Delete(@filePath.Value);
                        }
                    }
                    foreach (FilePath removal in removals)
                    {
                        localAddon.Files.Remove(removal);
                    }
                }
            }

            // determine files to download
            downloadManager = new DownloadManager(args.AddonGroup);
            downloadManager.StateChanged    += DownloadManager_StateChanged;
            downloadManager.ProgressChanged += DownloadManager_ProgressChanged;

            foreach (RemoteAddon webAddon in addonGroup.RemoteAddons)
            {
                RemoteAddon remoteAddon = FileIndexer.Instance.RemoteIndex.FilesIndex[webAddon.Uuid];
                Uuid        uuid        = webAddon.Uuid;
                string      name        = webAddon.Name;

                if (!remoteAddon.Uuid.Equals(uuid))
                {
                    throw new InvalidOperationException(string.Format("uuid {0} of local addon {1} does not match remote uuid {2} of addon {3}", uuid, name, remoteAddon.Uuid, remoteAddon.Name));
                }

                FilePath destinationFolder = args.DownloadDirectoryDict[webAddon];

                if (!FileIndexer.Instance.addonUuidToLocalAddonMap.ContainsKey(uuid))
                {
                    // download all
                    foreach (RemoteAddonFile remoteAddonFile in remoteAddon.Files.Values)
                    {
                        FilePath remoteFilePath      = remoteAddonFile.Path;
                        FilePath destinationFilePath = FilePath.Combine(destinationFolder, remoteFilePath.Replace("/", "\\"));
                        downloadManager.AddDownload(WebAPI.RepoUrl + "/" + remoteFilePath.OriginalValue, destinationFilePath, remoteAddonFile.Size);
                    }
                }
                else
                {
                    Dictionary <FilePath, LocalFileIndex> relativeFilePathToFileIndexMap = new();
                    List <LocalAddon> localAddons = FileIndexer.Instance.addonUuidToLocalAddonMap[uuid];
                    foreach (LocalAddon localAddon in localAddons)
                    {
                        foreach (LocalFileIndex fileIndex in localAddon.Files.Values)
                        {
                            relativeFilePathToFileIndexMap.Add(fileIndex.Relative_filepath, fileIndex);
                        }
                    }

                    // parts of the addon already exist, update existing and download missing
                    foreach (RemoteAddonFile remoteAddonFile in remoteAddon.Files.Values)
                    {
                        FilePath remoteFilePath = remoteAddonFile.Path.Replace("/", "\\");
                        string   remoteHash     = remoteAddonFile.Hash;

                        if (relativeFilePathToFileIndexMap.ContainsKey(remoteFilePath))
                        {
                            LocalFileIndex localFileIndex = relativeFilePathToFileIndexMap[remoteFilePath];
                            if (!remoteHash.Equals(localFileIndex.Hash))
                            {
                                FilePath destinationFilepath = FilePath.Combine(destinationFolder, remoteFilePath);
                                downloadManager.AddDownload(WebAPI.RepoUrl + "/" + remoteFilePath.OriginalValue, destinationFilepath, remoteAddonFile.Size);
                            }
                        }
                        else
                        {
                            FilePath destinationFilepath = FilePath.Combine(destinationFolder, remoteFilePath);
                            downloadManager.AddDownload(WebAPI.RepoUrl + "/" + remoteFilePath.OriginalValue, destinationFilepath, remoteAddonFile.Size);
                        }
                    }
                }
            }

            downloadManager.StartDownloads();
        }