1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 06:42:54 +08:00

Add safeties preventing creating multiple difficulties with same name

This commit is contained in:
Bartłomiej Dach 2022-01-23 19:42:07 +01:00
parent 87e2e83288
commit 4f1aac9345
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
3 changed files with 33 additions and 5 deletions

View File

@ -71,6 +71,12 @@ namespace osu.Game.Beatmaps
// AddFile generally handles updating/replacing files, but this is a case where the filename may have also changed so let's delete for simplicity.
var existingFileInfo = setInfo.Files.SingleOrDefault(f => string.Equals(f.Filename, beatmapInfo.Path, StringComparison.OrdinalIgnoreCase));
string targetFilename = getFilename(beatmapInfo);
// ensure that two difficulties from the set don't point at the same beatmap file.
if (setInfo.Beatmaps.Any(b => string.Equals(b.Path, targetFilename, StringComparison.OrdinalIgnoreCase)))
throw new InvalidOperationException($"{setInfo.GetDisplayString()} already has a difficulty with the name of '{beatmapInfo.DifficultyName}'.");
if (existingFileInfo != null)
DeleteFile(setInfo, existingFileInfo);

View File

@ -386,12 +386,20 @@ namespace osu.Game.Screens.Edit
return;
}
try
{
// save the loaded beatmap's data stream.
beatmapManager.Save(editorBeatmap.BeatmapInfo, editorBeatmap.PlayableBeatmap, editorBeatmap.BeatmapSkin);
}
catch (Exception ex)
{
// can fail e.g. due to duplicated difficulty names.
Logger.Error(ex, ex.Message);
return;
}
// no longer new after first user-triggered save.
isNewBeatmap = false;
// save the loaded beatmap's data stream.
beatmapManager.Save(editorBeatmap.BeatmapInfo, editorBeatmap.PlayableBeatmap, editorBeatmap.BeatmapSkin);
updateLastSavedHash();
}

View File

@ -6,6 +6,7 @@ using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging;
using osu.Framework.Screens;
using osu.Framework.Threading;
using osu.Game.Beatmaps;
@ -80,7 +81,20 @@ namespace osu.Game.Screens.Edit
}
public void ScheduleSwitchToNewDifficulty(BeatmapSetInfo beatmapSetInfo, RulesetInfo rulesetInfo, EditorState editorState)
=> scheduleDifficultySwitch(() => beatmapManager.CreateNewBlankDifficulty(beatmapSetInfo, rulesetInfo), editorState);
=> scheduleDifficultySwitch(() =>
{
try
{
return beatmapManager.CreateNewBlankDifficulty(beatmapSetInfo, rulesetInfo);
}
catch (Exception ex)
{
// if the beatmap creation fails (e.g. due to duplicated difficulty names),
// bring the user back to the previous beatmap as a best-effort.
Logger.Error(ex, ex.Message);
return Beatmap.Value;
}
}, editorState);
public void ScheduleSwitchToExistingDifficulty(BeatmapInfo beatmapInfo, EditorState editorState)
=> scheduleDifficultySwitch(() => beatmapManager.GetWorkingBeatmap(beatmapInfo), editorState);