Skip to content

Commit 338df68

Browse files
authored
Merge pull request #100 from Piotrekol/dev
2 parents 1a6571d + 7916c0e commit 338df68

26 files changed

Lines changed: 495 additions & 226 deletions

App/BeatmapListingActionsHandler.cs

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33
using CollectionManager.DataTypes;
44
using CollectionManager.Modules.CollectionsManager;
55
using CollectionManager.Modules.FileIO;
6+
using CollectionManagerApp.Presenters.Forms;
67
using CollectionManagerExtensionsDll.Enums;
8+
using CollectionManagerExtensionsDll.Modules.BeatmapExporter;
79
using CollectionManagerExtensionsDll.Modules.CollectionListGenerator;
810
using CollectionManagerExtensionsDll.Modules.CollectionListGenerator.ListTypes;
911
using CollectionManagerExtensionsDll.Utils;
1012
using Common;
1113
using GuiComponents.Interfaces;
12-
using SharpCompress.Archives.Zip;
1314
using System;
1415
using System.Collections.Generic;
15-
using System.ComponentModel;
1616
using System.Diagnostics;
1717
using System.IO;
18-
using System.IO.Compression;
1918
using System.Linq;
20-
using System.Text;
21-
using System.Threading;
19+
using System.Threading.Tasks;
2220

2321
namespace App
2422
{
@@ -75,97 +73,46 @@ private void CollectionListingModel_CollectionEditing(object sender, CollectionE
7573
if (e.Action != CollectionManager.Enums.CollectionEdit.ExportBeatmaps)
7674
return;
7775

78-
ExportBeatmapSets(e.Collections.AllBeatmaps().Cast<Beatmap>().ToList());
76+
_ = ExportBeatmapSetsAsync(e.Collections.AllBeatmaps().Cast<Beatmap>().ToList());
7977
}
8078

8179
private void ExportBeatmapSets(object sender)
8280
{
8381
if (sender is not IBeatmapListingModel beatmapListingModel)
8482
return;
8583

86-
ExportBeatmapSets(beatmapListingModel.SelectedBeatmaps?.ToList());
84+
_ = ExportBeatmapSetsAsync(beatmapListingModel.SelectedBeatmaps?.ToList());
8785
}
8886

89-
private void ExportBeatmapSets(List<Beatmap> beatmaps)
87+
private async Task ExportBeatmapSetsAsync(List<Beatmap> beatmaps)
9088
{
9189
if (beatmaps?.Count == 0)
9290
{
93-
_userDialogs.OkMessageBox("No beatmaps selected", "Info");
91+
_userDialogs.OkMessageBox("No beatmaps selected.", "Info");
92+
9493
return;
9594
}
9695

9796
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+
10098
if (!Directory.Exists(saveDirectory))
99+
{
101100
return;
101+
}
102102

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
110106
{
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+
}
167113
}
168114

115+
169116
private void PullWholeMapsets(object sender)
170117
{
171118
var model = (IBeatmapListingModel)sender;

App/Misc/Helpers.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ namespace App.Misc
1515
{
1616
public static class Helpers
1717
{
18-
public static string StripInvalidFileNameCharacters(string name, string replacementString = "")
19-
{
20-
foreach (var invalidChar in Path.GetInvalidFileNameChars())
21-
{
22-
name = name.Replace(invalidChar.ToString(), replacementString);
23-
}
24-
25-
return name;
26-
}
27-
2818
public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
2919
{
3020
if (assembly == null) throw new ArgumentNullException("assembly");

App/OsuDownloadManager.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
using Common;
99
using Gui.Misc;
1010
using GuiComponents.Interfaces;
11-
using App.Properties;
1211
using System.IO;
1312
using App.Models;
1413
using Common.Interfaces;
1514
using Newtonsoft.Json;
1615
using System.Reflection;
1716
using CollectionManagerApp.Properties;
17+
using CollectionManagerExtensionsDll.Utils;
1818

1919
namespace App
2020
{
@@ -157,18 +157,12 @@ private DownloadItem GetDownloadItem(Beatmap beatmap)
157157
if (beatmap.MapSetId < 1 || ListedMapSetIds.Contains(beatmap.MapSetId))
158158
return null;
159159
long currentId = ++_downloadId;
160-
var oszFileName = CreateOszFileName(beatmap);
160+
var oszFileName = beatmap.OszFileName();
161161
var downloadUrl = string.Format(SelectedDownloadSource.BaseDownloadUrl, beatmap.MapSetId) + (DownloadWithVideo != null && DownloadWithVideo.Value ? string.Empty : "?noVideo=1");
162162

163163
var downloadItem = _mapDownloader.DownloadFileAsync(downloadUrl, oszFileName, string.Format(SelectedDownloadSource.Referer, beatmap.MapSetId), currentId,SelectedDownloadSource.RequestTimeout);
164164
downloadItem.Id = currentId;
165165
return downloadItem;
166166
}
167-
168-
public static string CreateOszFileName(Beatmap map)
169-
{
170-
var filename = map.MapSetId + " " + map.ArtistRoman + " - " + map.TitleRoman;
171-
return Helpers.StripInvalidFileNameCharacters(filename, "_") + ".osz";
172-
}
173167
}
174168
}

App/Presenters/Controls/CollectionListingPresenter.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
using System;
1+
using App.Interfaces;
2+
using App.Misc;
23
using CollectionManager.DataTypes;
4+
using CollectionManager.Extensions;
35
using CollectionManager.Modules.CollectionsManager;
4-
using App.Interfaces;
6+
using CollectionManagerApp.Properties;
7+
using Common;
58
using GuiComponents.Interfaces;
9+
using System;
10+
using System.Collections.Specialized;
611
using System.IO;
712
using System.Windows.Forms;
8-
using System.Collections.Specialized;
9-
using App.Misc;
10-
using Common;
11-
using App.Properties;
12-
using CollectionManager.Enums;
1313
using SortOrder = CollectionManager.Enums.SortOrder;
14-
using CollectionManagerApp.Properties;
1514

1615
namespace App.Presenters.Controls
1716
{
@@ -139,8 +138,8 @@ private void _view_RightClick(object sender, Gui.Misc.StringEventArgs e)
139138
if (Directory.Exists(tempFolder))
140139
Directory.Delete(tempFolder, true);
141140

142-
Directory.CreateDirectory(tempFolder);
143-
var fileName = Helpers.StripInvalidFileNameCharacters(selectedCollections[0].Name, "_");
141+
_ = Directory.CreateDirectory(tempFolder);
142+
var fileName = selectedCollections[0].Name.StripInvalidFileNameCharacters("_");
144143
var tempLocation = Path.Combine(tempFolder, $"{fileName}.osdb");
145144
Initalizer.OsuFileIo.CollectionLoader.SaveOsdbCollection(selectedCollections, tempLocation);
146145
Clipboard.SetFileDropList(new StringCollection { tempLocation });

App/Presenters/Controls/StartupPresenter.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
using System.Windows.Forms;
77
using App.Interfaces;
88
using App.Models;
9-
using App.Properties;
109
using CollectionManager.Modules.CollectionsManager;
10+
using CollectionManager.Modules.FileIO;
1111
using CollectionManagerApp.Properties;
1212
using CollectionManagerExtensionsDll.Utils;
1313
using Common;
@@ -71,6 +71,8 @@ public async Task Run()
7171
_view.UseSelectedOptionsOnStartupEnabled = !loadedCollectionFromFile;
7272
_view.CollectionButtonsEnabled = !(loadedCollectionFromFile || _startupSettings.AutoLoadMode);
7373
_view.DatabaseButtonsEnabled = true;
74+
_view.LoadLazerDatabaseButtonEnabled = OsuPathResolver.TryGetLazerDataPath(out _);
75+
7476
_view.CollectionStatusText = loadedCollectionFromFile
7577
? $"Going to load {_collectionsManager.CollectionsCount} collections with {_collectionsManager.BeatmapsInCollectionsCount} beatmaps"
7678
: string.Empty;
@@ -130,13 +132,22 @@ private async void _view_StartupDatabaseOperation(object sender, StartupDatabase
130132
break;
131133
case StartupDatabaseAction.LoadFromDifferentLocation:
132134
var osuDirectory = Initalizer.OsuFileIo.OsuPathResolver.GetManualOsuDir(_userDialogs.SelectDirectory);
135+
133136
if (!string.IsNullOrEmpty(osuDirectory))
134137
{
135138
_view.LoadOsuCollectionButtonEnabled = true;
136139
_startupSettings.OsuLocation = osuDirectory;
137140
_databaseLoadTask = Task.Run(() => LoadDatabase(_cancellationTokenSource.Token));
138141
}
139142

143+
break;
144+
case StartupDatabaseAction.LoadLazer:
145+
if (OsuPathResolver.TryGetLazerDataPath(out var lazerLocation))
146+
{
147+
_view.LoadOsuCollectionButtonEnabled = true;
148+
_startupSettings.OsuLocation = lazerLocation;
149+
_databaseLoadTask = Task.Run(() => LoadDatabase(_cancellationTokenSource.Token));
150+
}
140151
break;
141152
case StartupDatabaseAction.None:
142153
break;
@@ -191,15 +202,15 @@ private void LoadDatabase(CancellationToken cancellationToken)
191202
osuFileIo.OsuDatabase.LoadedMaps.UnloadBeatmaps();
192203
osuFileIo.ScoresDatabase.Clear();
193204

194-
try
195-
{
196-
_ = osuFileIo.OsuDatabase.Load(osuDbOrRealmPath, _databaseLoadProgressReporter, cancellationToken);
197-
}
198-
catch (Exception exception)
199-
{
200-
_view.LoadDatabaseStatusText = $"Error: {exception.Message}";
201-
return;
202-
}
205+
try
206+
{
207+
_ = osuFileIo.OsuDatabase.Load(osuDbOrRealmPath, _databaseLoadProgressReporter, cancellationToken);
208+
}
209+
catch (Exception exception)
210+
{
211+
_view.LoadDatabaseStatusText = $"Error: {exception.Message}";
212+
return;
213+
}
203214

204215
osuFileIo.OsuSettings.Load(osuDirectory);
205216

0 commit comments

Comments
 (0)