Merge pull request #16676 from peppy/fix-editor-custom-ruleset

Fix editor crashing on custom rulesets due to `ChangeHandler` not being supported
This commit is contained in:
Dan Balasescu 2022-01-28 16:26:56 +09:00 committed by GitHub
commit bdc9ca40f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 8 deletions

View File

@ -82,6 +82,11 @@ private void load(EditorClipboard clipboard)
protected override void LoadComplete()
{
base.LoadComplete();
// May be null in the case of a ruleset that doesn't have editor support, see CreateMainContent().
if (composer == null)
return;
EditorBeatmap.SelectedHitObjects.BindCollectionChanged((_, __) => updateClipboardActionAvailability());
clipboard.BindValueChanged(_ => updateClipboardActionAvailability());
composer.OnLoadComplete += _ => updateClipboardActionAvailability();

View File

@ -28,6 +28,8 @@
using osu.Game.Input.Bindings;
using osu.Game.Online.API;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Edit;
using osu.Game.Screens.Edit.Components;
using osu.Game.Screens.Edit.Components.Menus;
@ -61,7 +63,16 @@ public class Editor : ScreenWithBeatmapBackground, IKeyBindingHandler<GlobalActi
public override bool? AllowTrackAdjustments => false;
protected bool HasUnsavedChanges => lastSavedHash != changeHandler.CurrentStateHash;
protected bool HasUnsavedChanges
{
get
{
if (!canSave)
return false;
return lastSavedHash != changeHandler?.CurrentStateHash;
}
}
[Resolved]
private BeatmapManager beatmapManager { get; set; }
@ -72,10 +83,15 @@ public class Editor : ScreenWithBeatmapBackground, IKeyBindingHandler<GlobalActi
[Resolved(canBeNull: true)]
private DialogOverlay dialogOverlay { get; set; }
[Resolved(canBeNull: true)]
private NotificationOverlay notifications { get; set; }
public IBindable<bool> SamplePlaybackDisabled => samplePlaybackDisabled;
private readonly Bindable<bool> samplePlaybackDisabled = new Bindable<bool>();
private bool canSave;
private bool exitConfirmed;
private string lastSavedHash;
@ -92,6 +108,8 @@ public class Editor : ScreenWithBeatmapBackground, IKeyBindingHandler<GlobalActi
private IBeatmap playableBeatmap;
private EditorBeatmap editorBeatmap;
[CanBeNull] // Should be non-null once it can support custom rulesets.
private EditorChangeHandler changeHandler;
private EditorMenuBar menuBar;
@ -172,8 +190,14 @@ private void load(OsuColour colours, OsuConfigManager config)
AddInternal(editorBeatmap = new EditorBeatmap(playableBeatmap, loadableBeatmap.GetSkin(), loadableBeatmap.BeatmapInfo));
dependencies.CacheAs(editorBeatmap);
changeHandler = new EditorChangeHandler(editorBeatmap);
dependencies.CacheAs<IEditorChangeHandler>(changeHandler);
canSave = editorBeatmap.BeatmapInfo.Ruleset.CreateInstance() is ILegacyRuleset;
if (canSave)
{
changeHandler = new EditorChangeHandler(editorBeatmap);
dependencies.CacheAs<IEditorChangeHandler>(changeHandler);
}
beatDivisor.Value = editorBeatmap.BeatmapInfo.BeatDivisor;
beatDivisor.BindValueChanged(divisor => editorBeatmap.BeatmapInfo.BeatDivisor = divisor.NewValue);
@ -311,8 +335,8 @@ private void load(OsuColour colours, OsuConfigManager config)
}
});
changeHandler.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true);
changeHandler.CanRedo.BindValueChanged(v => redoMenuItem.Action.Disabled = !v.NewValue, true);
changeHandler?.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true);
changeHandler?.CanRedo.BindValueChanged(v => redoMenuItem.Action.Disabled = !v.NewValue, true);
menuBar.Mode.ValueChanged += onModeChanged;
}
@ -353,6 +377,12 @@ public void RestoreState([NotNull] EditorState state) => Schedule(() =>
protected void Save()
{
if (!canSave)
{
notifications?.Post(new SimpleErrorNotification { Text = "Saving is not supported for this ruleset yet, sorry!" });
return;
}
// no longer new after first user-triggered save.
isNewBeatmap = false;
@ -648,9 +678,9 @@ private void rebindClipboardBindables()
#endregion
protected void Undo() => changeHandler.RestoreState(-1);
protected void Undo() => changeHandler?.RestoreState(-1);
protected void Redo() => changeHandler.RestoreState(1);
protected void Redo() => changeHandler?.RestoreState(1);
private void resetTrack(bool seekToStart = false)
{
@ -761,7 +791,7 @@ private void exportBeatmap()
private void updateLastSavedHash()
{
lastSavedHash = changeHandler.CurrentStateHash;
lastSavedHash = changeHandler?.CurrentStateHash;
}
private List<MenuItem> createFileMenuItems()