|
3 | 3 | using CollectionManager.DataTypes; |
4 | 4 | using CollectionManager.Modules.CollectionsManager; |
5 | 5 | using CollectionManager.Modules.FileIO; |
| 6 | +using CollectionManagerApp.Presenters.Forms; |
6 | 7 | using CollectionManagerExtensionsDll.Enums; |
| 8 | +using CollectionManagerExtensionsDll.Modules.BeatmapExporter; |
7 | 9 | using CollectionManagerExtensionsDll.Modules.CollectionListGenerator; |
8 | 10 | using CollectionManagerExtensionsDll.Modules.CollectionListGenerator.ListTypes; |
9 | 11 | using CollectionManagerExtensionsDll.Utils; |
10 | 12 | using Common; |
11 | 13 | using GuiComponents.Interfaces; |
12 | | -using SharpCompress.Archives.Zip; |
13 | 14 | using System; |
14 | 15 | using System.Collections.Generic; |
15 | | -using System.ComponentModel; |
16 | 16 | using System.Diagnostics; |
17 | 17 | using System.IO; |
18 | | -using System.IO.Compression; |
19 | 18 | using System.Linq; |
20 | | -using System.Text; |
21 | | -using System.Threading; |
| 19 | +using System.Threading.Tasks; |
22 | 20 |
|
23 | 21 | namespace App |
24 | 22 | { |
@@ -75,97 +73,46 @@ private void CollectionListingModel_CollectionEditing(object sender, CollectionE |
75 | 73 | if (e.Action != CollectionManager.Enums.CollectionEdit.ExportBeatmaps) |
76 | 74 | return; |
77 | 75 |
|
78 | | - ExportBeatmapSets(e.Collections.AllBeatmaps().Cast<Beatmap>().ToList()); |
| 76 | + _ = ExportBeatmapSetsAsync(e.Collections.AllBeatmaps().Cast<Beatmap>().ToList()); |
79 | 77 | } |
80 | 78 |
|
81 | 79 | private void ExportBeatmapSets(object sender) |
82 | 80 | { |
83 | 81 | if (sender is not IBeatmapListingModel beatmapListingModel) |
84 | 82 | return; |
85 | 83 |
|
86 | | - ExportBeatmapSets(beatmapListingModel.SelectedBeatmaps?.ToList()); |
| 84 | + _ = ExportBeatmapSetsAsync(beatmapListingModel.SelectedBeatmaps?.ToList()); |
87 | 85 | } |
88 | 86 |
|
89 | | - private void ExportBeatmapSets(List<Beatmap> beatmaps) |
| 87 | + private async Task ExportBeatmapSetsAsync(List<Beatmap> beatmaps) |
90 | 88 | { |
91 | 89 | if (beatmaps?.Count == 0) |
92 | 90 | { |
93 | | - _userDialogs.OkMessageBox("No beatmaps selected", "Info"); |
| 91 | + _userDialogs.OkMessageBox("No beatmaps selected.", "Info"); |
| 92 | + |
94 | 93 | return; |
95 | 94 | } |
96 | 95 |
|
97 | 96 | var saveDirectory = _userDialogs.SelectDirectory("Select directory for exported maps", true); |
98 | | - var beatmapSets = beatmaps.Where(b => !string.IsNullOrWhiteSpace(b.Dir)).Select(b => (Beatmap: b, FileName: OsuDownloadManager.CreateOszFileName(b))) |
99 | | - .GroupBy(e => e.FileName).Select(e => e.First()).ToList(); |
| 97 | + |
100 | 98 | if (!Directory.Exists(saveDirectory)) |
| 99 | + { |
101 | 100 | return; |
| 101 | + } |
102 | 102 |
|
103 | | - var backgroundWorker = new BackgroundWorker(); |
104 | | - var stringProgress = new Progress<string>(); |
105 | | - var percentageProgress = new Progress<int>(); |
106 | | - using var cancelationTokenSource = new CancellationTokenSource(); |
107 | | - var progressForm = _userDialogs.ProgressForm(stringProgress, percentageProgress); |
108 | | - progressForm.AbortClicked += (_, __) => cancelationTokenSource.Cancel(); |
109 | | - backgroundWorker.DoWork += (_, eventArgs) => |
| 103 | + BeatmapExporter beatmapExporter = new(BeatmapUtils.OsuSongsDirectory, saveDirectory); |
| 104 | + BeatmapExportFormPresenter exportPresenter = new(_userDialogs, beatmapExporter); |
| 105 | + try |
110 | 106 | { |
111 | | - var cancellationToken = cancelationTokenSource.Token; |
112 | | - var totalCount = beatmapSets.Count(); |
113 | | - var stringProgressReporter = (IProgress<string>)stringProgress; |
114 | | - var percentageProgressReporter = (IProgress<int>)percentageProgress; |
115 | | - var failedMapSets = new StringBuilder(); |
116 | | - var failedMapSetsCount = 0; |
117 | | - for (int i = 0; i < totalCount; i++) |
118 | | - { |
119 | | - var beatmapset = beatmapSets[i]; |
120 | | - if (cancellationToken.IsCancellationRequested) |
121 | | - { |
122 | | - progressForm.Close(); |
123 | | - return; |
124 | | - } |
125 | | - |
126 | | - stringProgressReporter.Report($"Processing map set {i + 1} of {totalCount}.{Environment.NewLine}\"{beatmapset.FileName}\""); |
127 | | - var fileSaveLocation = Path.Combine(saveDirectory, beatmapset.FileName); |
128 | | - if (File.Exists(fileSaveLocation)) |
129 | | - { |
130 | | - percentageProgressReporter.Report(Convert.ToInt32((double)(i + 1) / totalCount * 100)); |
131 | | - continue; |
132 | | - } |
133 | | - |
134 | | - var beatmapDirectory = beatmapset.Beatmap.BeatmapDirectory(); |
135 | | - if (!Directory.Exists(beatmapDirectory)) |
136 | | - { |
137 | | - failedMapSets.AppendFormat("map set directory \"{0}\" doesn't exist - set skipped(mapId:{1} setId:{2} hash:{3}) {4}{4}", beatmapDirectory, beatmapset.Beatmap.MapId.ToString(), beatmapset.Beatmap.MapSetId.ToString(), beatmapset.Beatmap.Md5, Environment.NewLine); |
138 | | - failedMapSetsCount++; |
139 | | - } |
140 | | - else |
141 | | - { |
142 | | - try |
143 | | - { |
144 | | - ZipFile.CreateFromDirectory(beatmapDirectory, fileSaveLocation); |
145 | | - } |
146 | | - catch (Exception e) |
147 | | - { |
148 | | - failedMapSets.AppendFormat("failed processing map set located at \"{0}\" with exception:{2}{1}{2}{2}", beatmapDirectory, e, Environment.NewLine); |
149 | | - failedMapSetsCount++; |
150 | | - } |
151 | | - |
152 | | - percentageProgressReporter.Report(Convert.ToInt32((double)(i + 1) / totalCount * 100)); |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - if (failedMapSetsCount > 0) |
157 | | - { |
158 | | - File.WriteAllText(Path.Combine(saveDirectory, "log.txt"), failedMapSets.ToString()); |
159 | | - stringProgressReporter.Report($"Processed {totalCount - failedMapSetsCount} of {totalCount} map sets.{Environment.NewLine}{failedMapSetsCount} map sets failed to save, see full log in log.txt file in export directory"); |
160 | | - } |
161 | | - else |
162 | | - stringProgressReporter.Report($"Processed {totalCount} map sets without issues."); |
163 | | - }; |
164 | | - |
165 | | - backgroundWorker.RunWorkerAsync(); |
166 | | - progressForm.ShowAndBlock(); |
| 107 | + await exportPresenter.ExportAsync(beatmaps, saveDirectory); |
| 108 | + } |
| 109 | + catch (Exception ex) |
| 110 | + { |
| 111 | + _userDialogs.OkMessageBox($"Error occurred during beatmap export: {ex}", "Error", MessageBoxType.Error); |
| 112 | + } |
167 | 113 | } |
168 | 114 |
|
| 115 | + |
169 | 116 | private void PullWholeMapsets(object sender) |
170 | 117 | { |
171 | 118 | var model = (IBeatmapListingModel)sender; |
|
0 commit comments