1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:18:22 +08:00

Refactor further to address code quality complaints

This commit is contained in:
Bartłomiej Dach 2024-06-10 10:28:10 +02:00
parent e13e9abda9
commit 1d6b7e9c9b
No known key found for this signature in database
2 changed files with 55 additions and 62 deletions

View File

@ -78,7 +78,7 @@ namespace osu.Game.Screens.Edit
} }
}; };
saveInProgress = editor.SaveTracker.InProgress.GetBoundCopy(); saveInProgress = editor.MutationTracker.InProgress.GetBoundCopy();
} }
protected override void LoadComplete() protected override void LoadComplete()

View File

@ -6,6 +6,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using osu.Framework; using osu.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
@ -35,6 +36,7 @@ using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings; using osu.Game.Input.Bindings;
using osu.Game.Localisation; using osu.Game.Localisation;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.Multiplayer;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
using osu.Game.Overlays.OSD; using osu.Game.Overlays.OSD;
@ -144,7 +146,12 @@ namespace osu.Game.Screens.Edit
private bool canSave; private bool canSave;
private readonly List<MenuItem> saveRelatedMenuItems = new List<MenuItem>(); private readonly List<MenuItem> saveRelatedMenuItems = new List<MenuItem>();
public OngoingOperationTracker SaveTracker { get; private set; } = new OngoingOperationTracker();
/// <summary>
/// Tracks ongoing mutually-exclusive operations related to changing the beatmap
/// (e.g. save, export).
/// </summary>
public OngoingOperationTracker MutationTracker { get; } = new OngoingOperationTracker();
protected bool ExitConfirmed { get; private set; } protected bool ExitConfirmed { get; private set; }
@ -385,7 +392,7 @@ namespace osu.Game.Screens.Edit
}, },
}, },
bottomBar = new BottomBar(), bottomBar = new BottomBar(),
SaveTracker, MutationTracker,
} }
}); });
changeHandler?.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true); changeHandler?.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true);
@ -407,10 +414,10 @@ namespace osu.Game.Screens.Edit
musicController.TrackChanged += onTrackChanged; musicController.TrackChanged += onTrackChanged;
SaveTracker.InProgress.BindValueChanged(_ => MutationTracker.InProgress.BindValueChanged(_ =>
{ {
foreach (var item in saveRelatedMenuItems) foreach (var item in saveRelatedMenuItems)
item.Action.Disabled = SaveTracker.InProgress.Value; item.Action.Disabled = MutationTracker.InProgress.Value;
}, true); }, true);
} }
@ -450,17 +457,13 @@ namespace osu.Game.Screens.Edit
{ {
if (HasUnsavedChanges) if (HasUnsavedChanges)
{ {
dialogOverlay.Push(new SaveRequiredPopupDialog("The beatmap will be saved in order to test it.", () => dialogOverlay.Push(new SaveRequiredPopupDialog("The beatmap will be saved in order to test it.", () => attemptMutationOperation(() =>
{ {
if (SaveTracker.InProgress.Value) return; if (!Save()) return false;
using (SaveTracker.BeginOperation()) pushEditorPlayer();
{ return true;
if (!Save()) return; })));
pushEditorPlayer();
}
}));
} }
else else
{ {
@ -470,6 +473,26 @@ namespace osu.Game.Screens.Edit
void pushEditorPlayer() => this.Push(new EditorPlayerLoader(this)); void pushEditorPlayer() => this.Push(new EditorPlayerLoader(this));
} }
private bool attemptMutationOperation(Func<bool> mutationOperation)
{
if (MutationTracker.InProgress.Value)
return false;
using (MutationTracker.BeginOperation())
return mutationOperation.Invoke();
}
private bool attemptAsyncMutationOperation(Func<Task> mutationTask)
{
if (MutationTracker.InProgress.Value)
return false;
var operation = MutationTracker.BeginOperation();
var task = mutationTask.Invoke();
task.FireAndForget(operation.Dispose, _ => operation.Dispose());
return true;
}
/// <summary> /// <summary>
/// Saves the currently edited beatmap. /// Saves the currently edited beatmap.
/// </summary> /// </summary>
@ -535,12 +558,7 @@ namespace osu.Game.Screens.Edit
if (e.Repeat) if (e.Repeat)
return false; return false;
if (SaveTracker.InProgress.Value) return attemptMutationOperation(Save);
return false;
using (SaveTracker.BeginOperation())
Save();
return true;
} }
return false; return false;
@ -806,13 +824,8 @@ namespace osu.Game.Screens.Edit
private void confirmExitWithSave() private void confirmExitWithSave()
{ {
if (SaveTracker.InProgress.Value) return; if (!attemptMutationOperation(Save))
return;
using (SaveTracker.BeginOperation())
{
if (!Save())
return;
}
ExitConfirmed = true; ExitConfirmed = true;
this.Exit(); this.Exit();
@ -1053,13 +1066,7 @@ namespace osu.Game.Screens.Edit
yield return new EditorMenuItem(EditorStrings.DeleteDifficulty, MenuItemType.Standard, deleteDifficulty) { Action = { Disabled = Beatmap.Value.BeatmapSetInfo.Beatmaps.Count < 2 } }; yield return new EditorMenuItem(EditorStrings.DeleteDifficulty, MenuItemType.Standard, deleteDifficulty) { Action = { Disabled = Beatmap.Value.BeatmapSetInfo.Beatmaps.Count < 2 } };
yield return new OsuMenuItemSpacer(); yield return new OsuMenuItemSpacer();
var save = new EditorMenuItem(WebCommonStrings.ButtonsSave, MenuItemType.Standard, () => var save = new EditorMenuItem(WebCommonStrings.ButtonsSave, MenuItemType.Standard, () => attemptMutationOperation(Save));
{
if (SaveTracker.InProgress.Value) return;
using (SaveTracker.BeginOperation())
Save();
});
saveRelatedMenuItems.Add(save); saveRelatedMenuItems.Add(save);
yield return save; yield return save;
@ -1089,37 +1096,25 @@ namespace osu.Game.Screens.Edit
{ {
if (HasUnsavedChanges) if (HasUnsavedChanges)
{ {
dialogOverlay.Push(new SaveRequiredPopupDialog("The beatmap will be saved in order to export it.", () => dialogOverlay.Push(new SaveRequiredPopupDialog("The beatmap will be saved in order to export it.", () => attemptAsyncMutationOperation(() =>
{ {
if (SaveTracker.InProgress.Value)
return;
var operation = SaveTracker.BeginOperation();
if (!Save()) if (!Save())
{ return Task.CompletedTask;
operation.Dispose();
return;
}
runExport(operation); return runExport();
})); })));
} }
else else
{ {
if (SaveTracker.InProgress.Value) attemptAsyncMutationOperation(runExport);
return;
runExport(SaveTracker.BeginOperation());
} }
void runExport(IDisposable operationInProgress) Task runExport()
{ {
var task = legacy if (legacy)
? beatmapManager.ExportLegacy(Beatmap.Value.BeatmapSetInfo) return beatmapManager.ExportLegacy(Beatmap.Value.BeatmapSetInfo);
: beatmapManager.Export(Beatmap.Value.BeatmapSetInfo); else
return beatmapManager.Export(Beatmap.Value.BeatmapSetInfo);
task.ContinueWith(_ => operationInProgress.Dispose());
} }
} }
@ -1181,16 +1176,14 @@ namespace osu.Game.Screens.Edit
{ {
dialogOverlay.Push(new SaveRequiredPopupDialog("This beatmap will be saved in order to create another difficulty.", () => dialogOverlay.Push(new SaveRequiredPopupDialog("This beatmap will be saved in order to create another difficulty.", () =>
{ {
if (SaveTracker.InProgress.Value) attemptMutationOperation(() =>
return;
using (SaveTracker.BeginOperation())
{ {
if (!Save()) if (!Save())
return; return false;
CreateNewDifficulty(rulesetInfo); CreateNewDifficulty(rulesetInfo);
} return true;
});
})); }));
return; return;