Skip to content

Commit

Permalink
Merge branch 'master' into linux_settingsWorkaround
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotrekol committed Jul 21, 2022
2 parents ed7d1ae + dc6744a commit f4a23a3
Show file tree
Hide file tree
Showing 69 changed files with 1,998 additions and 286 deletions.
2 changes: 1 addition & 1 deletion App/App.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net472</TargetFramework>
<TargetFramework>net48</TargetFramework>
<AssemblyTitle>osu! Collection Manager</AssemblyTitle>
<Product>Gui</Product>
<Copyright>Copyright © 2017-present Piotrekol</Copyright>
Expand Down
112 changes: 109 additions & 3 deletions App/BeatmapListingActionsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
using CollectionManagerExtensionsDll.Utils;
using Common;
using GuiComponents.Interfaces;
using SharpCompress.Archives.Zip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;

namespace App
{
Expand Down Expand Up @@ -42,24 +48,124 @@ public BeatmapListingActionsHandler(ICollectionEditor collectionEditor, IUserDia
{BeatmapListingAction.OpenBeatmapPages, OpenBeatmapPages },
{BeatmapListingAction.OpenBeatmapFolder, OpenBeatmapFolder },
{BeatmapListingAction.PullWholeMapSet, PullWholeMapsets },
{BeatmapListingAction.ExportBeatmapSets, ExportBeatmapSets },
};
}

public void Bind(IBeatmapListingModel beatmapListingModel)
public void Bind(IBeatmapListingModel beatmapListingModel, ICollectionListingModel collectionListingModel = null)
{
beatmapListingModel.BeatmapOperation += BeatmapListingModel_BeatmapOperation;
if (collectionListingModel != null)
collectionListingModel.CollectionEditing += CollectionListingModel_CollectionEditing;
}


public void UnBind(IBeatmapListingModel beatmapListingModel)
public void UnBind(IBeatmapListingModel beatmapListingModel, ICollectionListingModel collectionListingModel = null)
{
beatmapListingModel.BeatmapOperation -= BeatmapListingModel_BeatmapOperation;
if (collectionListingModel != null)
collectionListingModel.CollectionEditing -= CollectionListingModel_CollectionEditing;
}

private void BeatmapListingModel_BeatmapOperation(object sender, BeatmapListingAction args)
{
_beatmapOperationHandlers[args](sender);
}
private void CollectionListingModel_CollectionEditing(object sender, CollectionEditArgs e)
{
if (e.Action != CollectionManager.Enums.CollectionEdit.ExportBeatmaps)
return;

ExportBeatmapSets(e.Collections.AllBeatmaps().Cast<Beatmap>().ToList());
}

private void ExportBeatmapSets(object sender)
{
if (sender is not IBeatmapListingModel beatmapListingModel)
return;

ExportBeatmapSets(beatmapListingModel.SelectedBeatmaps?.ToList());
}

private void ExportBeatmapSets(List<Beatmap> beatmaps)
{
if (beatmaps?.Count == 0)
{
_userDialogs.OkMessageBox("No beatmaps selected", "Info");
return;
}

var saveDirectory = _userDialogs.SelectDirectory("Select directory for exported maps", true);
var beatmapSets = beatmaps.Where(b => !string.IsNullOrWhiteSpace(b.Dir)).Select(b => (Beatmap: b, FileName: OsuDownloadManager.CreateOszFileName(b)))
.GroupBy(e => e.FileName).Select(e => e.First()).ToList();
if (!Directory.Exists(saveDirectory))
return;

var backgroundWorker = new BackgroundWorker();
var stringProgress = new Progress<string>();
var percentageProgress = new Progress<int>();
using var cancelationTokenSource = new CancellationTokenSource();
var progressForm = _userDialogs.ProgressForm(stringProgress, percentageProgress);
progressForm.AbortClicked += (_, __) => cancelationTokenSource.Cancel();
backgroundWorker.DoWork += (_, eventArgs) =>
{
var cancellationToken = cancelationTokenSource.Token;
var totalCount = beatmapSets.Count();
var stringProgressReporter = (IProgress<string>)stringProgress;
var percentageProgressReporter = (IProgress<int>)percentageProgress;
var failedMapSets = new StringBuilder();
var failedMapSetsCount = 0;
for (int i = 0; i < totalCount; i++)
{
var beatmapset = beatmapSets[i];
if (cancellationToken.IsCancellationRequested)
{
progressForm.Close();
return;
}

stringProgressReporter.Report($"Processing map set {i + 1} of {totalCount}.{Environment.NewLine}\"{beatmapset.FileName}\"");
var fileSaveLocation = Path.Combine(saveDirectory, beatmapset.FileName);
if (File.Exists(fileSaveLocation))
{
percentageProgressReporter.Report(Convert.ToInt32((double)(i + 1) / totalCount * 100));
continue;
}

var beatmapDirectory = beatmapset.Beatmap.BeatmapDirectory();
if (!Directory.Exists(beatmapDirectory))
{
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);
failedMapSetsCount++;
}
else
{
try
{
ZipFile.CreateFromDirectory(beatmapDirectory, fileSaveLocation);
}
catch (Exception e)
{
failedMapSets.AppendFormat("failed processing map set located at \"{0}\" with exception:{2}{1}{2}{2}", beatmapDirectory, e, Environment.NewLine);
failedMapSetsCount++;
}

percentageProgressReporter.Report(Convert.ToInt32((double)(i + 1) / totalCount * 100));
}
}

if (failedMapSetsCount > 0)
{
File.WriteAllText(Path.Combine(saveDirectory, "log.txt"), failedMapSets.ToString());
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");
}
else
stringProgressReporter.Report($"Processed {totalCount} map sets without issues.");
};

backgroundWorker.RunWorkerAsync();
progressForm.ShowAndBlock();
}

private void PullWholeMapsets(object sender)
{
var model = (IBeatmapListingModel)sender;
Expand Down
2 changes: 1 addition & 1 deletion App/CollectionEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void EditCollection(CollectionEditArgs e)
break;
}
}
else if (e.Action == CollectionEdit.Intersect)
else if (e.Action == CollectionEdit.Intersect || e.Action == CollectionEdit.Difference)
{
if (e.Collections.Count < 2)
return;
Expand Down
6 changes: 3 additions & 3 deletions App/GuiActionsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public GuiActionsHandler(OsuFileIo osuFileIo, ICollectionEditor collectionEditor
SidePanelActionsHandler = new SidePanelActionsHandler(osuFileIo, collectionEditor, userDialogs, mainFormView, this, mainFormPresenter, loginForm);

_beatmapListingActionsHandler = new BeatmapListingActionsHandler(collectionEditor, userDialogs, loginForm, osuFileIo);
_beatmapListingActionsHandler.Bind(mainFormPresenter.BeatmapListingModel);
_beatmapListingActionsHandler.Bind(mainFormPresenter.BeatmapListingModel, mainFormPresenter.CollectionListingModel);

Initalizer.CollectionsManager.LoadedCollections.CollectionChanged += LoadedCollectionsOnCollectionChanged;
}
Expand All @@ -47,12 +47,12 @@ private void LoadedCollectionsOnCollectionChanged(object sender, NotifyCollectio

public void Bind(IBeatmapListingModel model)
{
_beatmapListingActionsHandler.Bind(model);
_beatmapListingActionsHandler.Bind(model, null);
}

public void UnBind(IBeatmapListingModel model)
{
_beatmapListingActionsHandler.UnBind(model);
_beatmapListingActionsHandler.UnBind(model, null);
}
}
}
64 changes: 15 additions & 49 deletions App/Initalizer.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using App.Interfaces;
using App.Misc;
using App.Models;
using App.Models.Forms;
using App.Presenters.Controls;
using App.Presenters.Forms;
using App.Properties;
using CollectionManager.DataTypes;
using CollectionManager.Modules.CollectionsManager;
using CollectionManager.Modules.FileIO;
using CollectionManagerExtensionsDll.Modules.API.osustats;
using CollectionManagerExtensionsDll.Utils;
using Common;
using GuiComponents.Interfaces;

namespace App
Expand All @@ -27,51 +26,14 @@ public class Initalizer : ApplicationContext
public static CollectionEditor CollectionEditor { get; private set; }
private IUserDialogs UserDialogs { get; set; }// = new GuiComponents.UserDialogs();
public static OsuStatsApi WebCollectionProvider = new OsuStatsApi("", OsuFileIo.LoadedMaps);
public void Run(string[] args)
public static StartupPresenter StartupPresenter;

public async Task Run(string[] args)
{
//IUserDialogs can be implemented in WinForm or WPF or Gtk or Console or...?
UserDialogs = GuiComponentsProvider.Instance.GetClassImplementing<IUserDialogs>();

if (Settings.Default.DontAskAboutOsuDirectory)
OsuDirectory = OsuFileIo.OsuPathResolver.GetOsuDir(_ => false, _ => Settings.Default.OsuDirectory);
else
OsuDirectory = OsuFileIo.OsuPathResolver.GetOsuDir(dir =>
{
var result = UserDialogs.YesNoMessageBox($"Detected osu! in \"{dir}\"{Environment.NewLine}Is that correct?", "osu! directory", MessageBoxType.Question,
"Don't ask me again");
Settings.Default.DontAskAboutOsuDirectory = result.doNotAskAgain;
Settings.Default.Save();
return result.Result;
}, UserDialogs.SelectDirectory);

if (string.IsNullOrEmpty(OsuDirectory) && UserDialogs.YesNoMessageBox(
"osu! could not be found. Do you want to continue regardless?" + Environment.NewLine +
"This will cause all .db collections to show only as beatmap hashes.", "osu! location",
MessageBoxType.Question) == false)
{
Quit();
}

if (!string.IsNullOrEmpty(OsuDirectory))
{
Settings.Default.OsuDirectory = OsuDirectory;
Settings.Default.Save();
//Load osu database and setting files
var osuDbFile = Path.Combine(OsuDirectory, @"osu!.db");
var osuScoresFile = Path.Combine(OsuDirectory, @"scores.db");
OsuFileIo.OsuDatabase.Load(osuDbFile);
OsuFileIo.OsuSettings.Load(OsuDirectory);
try
{
OsuFileIo.ScoresLoader.ReadDb(osuScoresFile);
}
catch (Exception e)
{
UserDialogs.OkMessageBox("Warning", "osu! scores database could not be loaded. Score related searches will not work in this session." + Environment.NewLine + Environment.NewLine + e);
}

BeatmapUtils.OsuSongsDirectory = OsuFileIo.OsuSettings.CustomBeatmapDirectoryLocation;
}

//Init "main" classes
CollectionsManager = new CollectionsManagerWithCounts(LoadedBeatmaps);
Expand All @@ -89,6 +51,11 @@ public void Run(string[] args)
//set initial text info and update events
SetTextData(infoTextModel);

var loginForm = GuiComponentsProvider.Instance.GetClassImplementing<ILoginFormView>();
var guiActionsHandler = new GuiActionsHandler(OsuFileIo, CollectionsManager, UserDialogs, mainForm, mainPresenter, loginForm);

if (!string.IsNullOrWhiteSpace(Settings.Default.Osustats_apiKey))
guiActionsHandler.SidePanelActionsHandler.OsustatsLogin(null, Settings.Default.Osustats_apiKey);

if (args.Length > 0)
{
Expand All @@ -98,13 +65,12 @@ public void Run(string[] args)
}
}

var loginForm = GuiComponentsProvider.Instance.GetClassImplementing<ILoginFormView>();
var guiActionsHandler = new GuiActionsHandler(OsuFileIo, CollectionsManager, UserDialogs, mainForm, mainPresenter, loginForm);

if (!string.IsNullOrWhiteSpace(Settings.Default.Osustats_apiKey))
guiActionsHandler.SidePanelActionsHandler.OsustatsLogin(null, Settings.Default.Osustats_apiKey);
StartupPresenter = new StartupPresenter(GuiComponentsProvider.Instance.GetClassImplementing<IStartupForm>(), guiActionsHandler.SidePanelActionsHandler, UserDialogs, CollectionsManager);
await StartupPresenter.Run();


mainForm.ShowAndBlock();

Quit();
}

Expand All @@ -125,7 +91,7 @@ private void SetTextData(IInfoTextModel model)
}


private static void Quit()
public static void Quit()
{
Settings.Default.Save();

Expand Down
26 changes: 26 additions & 0 deletions App/Misc/CollectionEditArgsExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CollectionManager.DataTypes;
using CollectionManager.Enums;
using CollectionManager.Modules.CollectionsManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace App.Misc
{
internal class CollectionEditArgsExtension : CollectionEditArgs
{
public CollectionEditArgsExtension(CollectionEdit action) : base(action)
{
}

public static CollectionEditArgs ExportBeatmaps(Collections collections)
{
return new CollectionEditArgsExtension(CollectionEdit.ExportBeatmaps)
{
Collections = collections
};
}
}
}
12 changes: 12 additions & 0 deletions App/Models/StartupSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Common;

namespace App.Models
{
public class StartupSettings
{
public StartupDatabaseAction StartupDatabaseAction { get; set; }
public StartupCollectionAction StartupCollectionAction { get; set; }
public bool AutoLoadMode { get; set; }
public string OsuLocation { get; set; }
}
}
4 changes: 2 additions & 2 deletions App/OsuDownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ private DownloadItem GetDownloadItem(Beatmap beatmap)
if (beatmap.MapSetId < 1 || ListedMapSetIds.Contains(beatmap.MapSetId))
return null;
long currentId = ++_downloadId;
var oszFileName = CreateFileName(beatmap);
var oszFileName = CreateOszFileName(beatmap);
var downloadUrl = string.Format(SelectedDownloadSource.BaseDownloadUrl, beatmap.MapSetId) + (DownloadWithVideo != null && DownloadWithVideo.Value ? string.Empty : "?noVideo=1");

var downloadItem = _mapDownloader.DownloadFileAsync(downloadUrl, oszFileName, string.Format(SelectedDownloadSource.Referer, beatmap.MapSetId), currentId,SelectedDownloadSource.RequestTimeout);
downloadItem.Id = currentId;
return downloadItem;
}

private string CreateFileName(Beatmap map)
public static string CreateOszFileName(Beatmap map)
{
var filename = map.MapSetId + " " + map.ArtistRoman + " - " + map.TitleRoman;
return Helpers.StripInvalidFileNameCharacters(filename, "_") + ".osz";
Expand Down
2 changes: 1 addition & 1 deletion App/Presenters/Controls/BeatmapThumbnailPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void LoadData(BeatmapExtension beatmap)
if (imgPath != "")
img = Image.FromFile(imgPath);
else if (beatmap.MapSetId > 0)
url = "https://assets.ppy.sh//beatmaps/"+beatmap.MapSetId+"/covers/card.jpg";
url = "https://assets.ppy.sh/beatmaps/"+beatmap.MapSetId+"/covers/card.jpg";
if (currentId != Interlocked.Read(ref changeId))
{
img?.Dispose();
Expand Down
Loading

0 comments on commit f4a23a3

Please sign in to comment.