1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 07:43:00 +08:00

Remove "silent" parameter; consolidate import logic

This commit is contained in:
Dean Herbert 2019-02-25 18:24:06 +09:00
parent 706da017d7
commit 80737b9ef8
13 changed files with 60 additions and 82 deletions

View File

@ -101,7 +101,7 @@ namespace osu.Game.Tests.Beatmaps.IO
int fireCount = 0;
// ReSharper disable once AccessToModifiedClosure
manager.ItemAdded += (_, __, ___) => fireCount++;
manager.ItemAdded += (_, __) => fireCount++;
manager.ItemRemoved += _ => fireCount++;
var imported = LoadOszIntoOsu(osu);

View File

@ -11,7 +11,6 @@ using Microsoft.EntityFrameworkCore;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Extensions;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics.Textures;
using osu.Framework.Logging;
using osu.Framework.Platform;
@ -50,11 +49,6 @@ namespace osu.Game.Beatmaps
/// </summary>
public event Action<DownloadBeatmapSetRequest> BeatmapDownloadFailed;
/// <summary>
/// Fired when a beatmap load is requested (into the interactive game UI).
/// </summary>
public Action<BeatmapSetInfo> PresentBeatmap;
/// <summary>
/// A default representation of a WorkingBeatmap to use when no beatmap is available.
/// </summary>
@ -165,20 +159,10 @@ namespace osu.Game.Beatmaps
request.Success += filename =>
{
downloadNotification.Text = $"Importing {beatmapSetInfo.Metadata.Artist} - {beatmapSetInfo.Metadata.Title}";
Task.Factory.StartNew(() =>
{
// This gets scheduled back to the update thread, but we want the import to run in the background.
var importedBeatmap = Import(filename);
downloadNotification.CompletionClickAction = () =>
{
PresentCompletedImport(importedBeatmap.Yield());
return true;
};
downloadNotification.State = ProgressNotificationState.Completed;
Import(downloadNotification, filename);
currentDownloads.Remove(request);
}, TaskCreationOptions.LongRunning);
};
@ -211,12 +195,6 @@ namespace osu.Game.Beatmaps
return true;
}
protected override void PresentCompletedImport(IEnumerable<BeatmapSetInfo> imported)
{
base.PresentCompletedImport(imported);
PresentBeatmap?.Invoke(imported.LastOrDefault());
}
/// <summary>
/// Get an existing download request if it exists.
/// </summary>

View File

@ -32,7 +32,7 @@ namespace osu.Game.Database
where TModel : class, IHasFiles<TFileModel>, IHasPrimaryKey, ISoftDelete
where TFileModel : INamedFileInfo, new()
{
public delegate void ItemAddedDelegate(TModel model, bool existing, bool silent);
public delegate void ItemAddedDelegate(TModel model, bool existing);
/// <summary>
/// Set an endpoint for notifications to be posted to.
@ -110,7 +110,7 @@ namespace osu.Game.Database
ContextFactory = contextFactory;
ModelStore = modelStore;
ModelStore.ItemAdded += (item, silent) => handleEvent(() => ItemAdded?.Invoke(item, false, silent));
ModelStore.ItemAdded += item => handleEvent(() => ItemAdded?.Invoke(item, false));
ModelStore.ItemRemoved += s => handleEvent(() => ItemRemoved?.Invoke(s));
Files = new FileStore(contextFactory, storage);
@ -128,14 +128,16 @@ namespace osu.Game.Database
/// <param name="paths">One or more archive locations on disk.</param>
public void Import(params string[] paths)
{
var notification = new ProgressNotification
{
Text = "Import is initialising...",
Progress = 0,
State = ProgressNotificationState.Active,
};
var notification = new ProgressNotification { State = ProgressNotificationState.Active };
PostNotification?.Invoke(notification);
Import(notification, paths);
}
protected void Import(ProgressNotification notification, params string[] paths)
{
notification.Progress = 0;
notification.Text = "Import is initialising...";
List<TModel> imported = new List<TModel>();
@ -168,13 +170,20 @@ namespace osu.Game.Database
}
else
{
notification.CompletionText = $"Imported {current} {typeof(TModel).Name.Replace("Info", "").ToLower()}s!";
notification.CompletionClickAction += () =>
notification.CompletionText = imported.Count == 1
? $"Imported {imported.First()}!"
: $"Imported {current} {typeof(TModel).Name.Replace("Info", "").ToLower()}s!";
if (imported.Count > 0 && PresentImport != null)
{
if (imported.Count > 0)
PresentCompletedImport(imported);
return true;
};
notification.CompletionText += " Click to view.";
notification.CompletionClickAction = () =>
{
PresentImport?.Invoke(imported);
return true;
};
}
notification.State = ProgressNotificationState.Completed;
}
}
@ -207,9 +216,10 @@ namespace osu.Game.Database
return import;
}
protected virtual void PresentCompletedImport(IEnumerable<TModel> imported)
{
}
/// <summary>
/// Fired when the user requests to view the resulting import.
/// </summary>
public Action<IEnumerable<TModel>> PresentImport;
/// <summary>
/// Import an item from an <see cref="ArchiveReader"/>.
@ -225,7 +235,7 @@ namespace osu.Game.Database
model.Hash = computeHash(archive);
return Import(model, false, archive);
return Import(model, archive);
}
catch (Exception e)
{
@ -259,9 +269,8 @@ namespace osu.Game.Database
/// Import an item from a <see cref="TModel"/>.
/// </summary>
/// <param name="item">The model to be imported.</param>
/// <param name="silent">Whether the user should be notified fo the import.</param>
/// <param name="archive">An optional archive to use for model population.</param>
public TModel Import(TModel item, bool silent = false, ArchiveReader archive = null)
public TModel Import(TModel item, ArchiveReader archive = null)
{
delayEvents();
@ -281,7 +290,7 @@ namespace osu.Game.Database
{
Undelete(existing);
Logger.Log($"Found existing {typeof(TModel)} for {item} (ID {existing.ID}). Skipping import.", LoggingTarget.Database);
handleEvent(() => ItemAdded?.Invoke(existing, true, silent));
handleEvent(() => ItemAdded?.Invoke(existing, true));
return existing;
}
@ -291,7 +300,7 @@ namespace osu.Game.Database
Populate(item, archive);
// import to store
ModelStore.Add(item, silent);
ModelStore.Add(item);
}
catch (Exception e)
{

View File

@ -16,9 +16,7 @@ namespace osu.Game.Database
public abstract class MutableDatabaseBackedStore<T> : DatabaseBackedStore
where T : class, IHasPrimaryKey, ISoftDelete
{
public delegate void ItemAddedDelegate(T model, bool silent);
public event ItemAddedDelegate ItemAdded;
public event Action<T> ItemAdded;
public event Action<T> ItemRemoved;
protected MutableDatabaseBackedStore(IDatabaseContextFactory contextFactory, Storage storage = null)
@ -35,8 +33,7 @@ namespace osu.Game.Database
/// Add a <see cref="T"/> to the database.
/// </summary>
/// <param name="item">The item to add.</param>
/// <param name="silent">Whether the user should be notified of the addition.</param>
public void Add(T item, bool silent)
public void Add(T item)
{
using (var usage = ContextFactory.GetForWrite())
{
@ -44,7 +41,7 @@ namespace osu.Game.Database
context.Attach(item);
}
ItemAdded?.Invoke(item, silent);
ItemAdded?.Invoke(item);
}
/// <summary>
@ -57,7 +54,7 @@ namespace osu.Game.Database
usage.Context.Update(item);
ItemRemoved?.Invoke(item);
ItemAdded?.Invoke(item, true);
ItemAdded?.Invoke(item);
}
/// <summary>
@ -92,7 +89,7 @@ namespace osu.Game.Database
item.DeletePending = false;
}
ItemAdded?.Invoke(item, true);
ItemAdded?.Invoke(item);
return true;
}

View File

@ -148,12 +148,6 @@ namespace osu.Game
{
this.frameworkConfig = frameworkConfig;
ScoreManager.ItemAdded += (score, _, silent) =>
{
if (!silent)
Schedule(() => PresentScore(score));
};
if (!Host.IsPrimaryInstance)
{
Logger.Log(@"osu! does not support multiple running instances.", LoggingTarget.Runtime, LogLevel.Error);
@ -221,7 +215,8 @@ namespace osu.Game
public void ShowBeatmap(int beatmapId) => beatmapSetOverlay.FetchAndShowBeatmap(beatmapId);
/// <summary>
/// Present a beatmap at song select.
/// Present a beatmap at song select immediately..
/// The user should have already requested this interactively.
/// </summary>
/// <param name="beatmap">The beatmap to select.</param>
public void PresentBeatmap(BeatmapSetInfo beatmap)
@ -236,26 +231,23 @@ namespace osu.Game
return;
}
if (screenStack.CurrentScreen is PlaySongSelect)
// if we're already at song select then we don't need to return to the main menu.
setBeatmap();
else
performFromMainMenu(setBeatmap, $"load {beatmap}");
void setBeatmap()
performFromMainMenu(() =>
{
menuScreen.LoadToSolo();
// we might already be at song select, so a check is required before performing the load to solo.
if (menuScreen.IsCurrentScreen())
menuScreen.LoadToSolo();
// Use first beatmap available for current ruleset, else switch ruleset.
var first = databasedSet.Beatmaps.Find(b => b.Ruleset == ruleset.Value) ?? databasedSet.Beatmaps.First();
ruleset.Value = first.Ruleset;
Beatmap.Value = BeatmapManager.GetWorkingBeatmap(first);
}
}, $"load {beatmap}", bypassScreenAllowChecks: true, targetScreen: typeof(PlaySongSelect));
}
/// <summary>
/// Present a score's replay.
/// Present a score's replay immediately.
/// The user should have already requested this interactively.
/// </summary>
/// <param name="beatmap">The beatmap to select.</param>
public void PresentScore(ScoreInfo score)
@ -283,7 +275,7 @@ namespace osu.Game
Beatmap.Value.Mods.Value = databasedScoreInfo.Mods;
menuScreen.Push(new PlayerLoader(() => new ReplayPlayer(databasedScore)));
}, $"watch {databasedScoreInfo.User.Username} play {databasedScoreInfo.Beatmap}");
}, $"watch {databasedScoreInfo.User.Username} play {databasedScoreInfo.Beatmap}", bypassScreenAllowChecks: true);
}
private ScheduledDelegate performFromMainMenuTask;
@ -359,8 +351,10 @@ namespace osu.Game
BeatmapManager.PostNotification = n => notifications?.Post(n);
BeatmapManager.GetStableStorage = GetStorageForStableInstall;
BeatmapManager.PresentImport = items => PresentBeatmap(items.First());
BeatmapManager.PresentBeatmap = PresentBeatmap;
ScoreManager.PostNotification = n => notifications?.Post(n);
ScoreManager.PresentImport = items => PresentScore(items.First());
Container logoContainer;

View File

@ -115,7 +115,7 @@ namespace osu.Game.Overlays.Direct
private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null));
private void setAdded(BeatmapSetInfo s, bool existing, bool silent) => setDownloadStateFromManager(s, DownloadState.LocallyAvailable);
private void setAdded(BeatmapSetInfo s, bool existing) => setDownloadStateFromManager(s, DownloadState.LocallyAvailable);
private void setRemoved(BeatmapSetInfo s) => setDownloadStateFromManager(s, DownloadState.NotDownloaded);

View File

@ -75,7 +75,7 @@ namespace osu.Game.Overlays.Music
[BackgroundDependencyLoader]
private void load(BeatmapManager beatmaps, IBindable<WorkingBeatmap> beatmap)
{
beatmaps.GetAllUsableBeatmapSets().ForEach(b => addBeatmapSet(b, false, false));
beatmaps.GetAllUsableBeatmapSets().ForEach(b => addBeatmapSet(b, false));
beatmaps.ItemAdded += addBeatmapSet;
beatmaps.ItemRemoved += removeBeatmapSet;
@ -83,7 +83,7 @@ namespace osu.Game.Overlays.Music
beatmapBacking.ValueChanged += _ => updateSelectedSet();
}
private void addBeatmapSet(BeatmapSetInfo obj, bool existing, bool silent) => Schedule(() =>
private void addBeatmapSet(BeatmapSetInfo obj, bool existing) => Schedule(() =>
{
if (existing)
return;

View File

@ -212,7 +212,7 @@ namespace osu.Game.Overlays
beatmapSets.Insert(index, beatmapSetInfo);
}
private void handleBeatmapAdded(BeatmapSetInfo obj, bool existing, bool silent)
private void handleBeatmapAdded(BeatmapSetInfo obj, bool existing)
{
if (existing)
return;

View File

@ -82,7 +82,7 @@ namespace osu.Game.Overlays.Settings.Sections
private void itemRemoved(SkinInfo s) => Schedule(() => skinDropdown.Items = skinDropdown.Items.Where(i => i.ID != s.ID).ToArray());
private void itemAdded(SkinInfo s, bool existing, bool silent)
private void itemAdded(SkinInfo s, bool existing)
{
if (existing)
return;

View File

@ -53,7 +53,7 @@ namespace osu.Game.Screens.Multi.Match.Components
hasBeatmap = beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == beatmap.OnlineBeatmapID) != null;
}
private void beatmapAdded(BeatmapSetInfo model, bool existing, bool silent)
private void beatmapAdded(BeatmapSetInfo model, bool existing)
{
if (Beatmap.Value == null)
return;

View File

@ -202,7 +202,7 @@ namespace osu.Game.Screens.Multi.Match
/// <summary>
/// Handle the case where a beatmap is imported (and can be used by this match).
/// </summary>
private void beatmapAdded(BeatmapSetInfo model, bool existing, bool silent) => Schedule(() =>
private void beatmapAdded(BeatmapSetInfo model, bool existing) => Schedule(() =>
{
if (Beatmap.Value != beatmapManager.DefaultBeatmap)
return;

View File

@ -294,7 +294,7 @@ namespace osu.Game.Screens.Play
var score = CreateScore();
if (RulesetContainer.ReplayScore == null)
scoreManager.Import(score, true);
scoreManager.Import(score);
this.Push(CreateResults(score));

View File

@ -587,7 +587,7 @@ namespace osu.Game.Screens.Select
}
}
private void onBeatmapSetAdded(BeatmapSetInfo s, bool existing, bool silent) => Carousel.UpdateBeatmapSet(s);
private void onBeatmapSetAdded(BeatmapSetInfo s, bool existing) => Carousel.UpdateBeatmapSet(s);
private void onBeatmapSetRemoved(BeatmapSetInfo s) => Carousel.RemoveBeatmapSet(s);
private void onBeatmapRestored(BeatmapInfo b) => Carousel.UpdateBeatmapSet(beatmaps.QueryBeatmapSet(s => s.ID == b.BeatmapSetInfoID));
private void onBeatmapHidden(BeatmapInfo b) => Carousel.UpdateBeatmapSet(beatmaps.QueryBeatmapSet(s => s.ID == b.BeatmapSetInfoID));