mirror of
https://github.com/ppy/osu.git
synced 2025-01-15 00:02:54 +08:00
Add IDeepCloneable
interface and update existing CreateCopy
methods to use it
This commit is contained in:
parent
48b34457e7
commit
3c028ce05c
@ -3,6 +3,7 @@ M:System.Object.Equals(System.Object)~System.Boolean;Don't use object.Equals. Us
|
||||
M:System.ValueType.Equals(System.Object)~System.Boolean;Don't use object.Equals(Fallbacks to ValueType). Use IEquatable<T> or EqualityComparer<T>.Default instead.
|
||||
M:System.Nullable`1.Equals(System.Object)~System.Boolean;Use == instead.
|
||||
T:System.IComparable;Don't use non-generic IComparable. Use generic version instead.
|
||||
T:SixLabors.ImageSharp.IDeepCloneable`1;Use osu.Game.Utils.IDeepCloneable<T> instead.
|
||||
M:osu.Framework.Graphics.Sprites.SpriteText.#ctor;Use OsuSpriteText.
|
||||
M:osu.Framework.Bindables.IBindableList`1.GetBoundCopy();Fails on iOS. Use manual ctor + BindTo instead. (see https://github.com/mono/mono/issues/19900)
|
||||
T:Microsoft.EntityFrameworkCore.Internal.EnumerableExtensions;Don't use internal extension methods.
|
||||
|
@ -248,13 +248,13 @@ namespace osu.Game.Tests.NonVisual
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCreateCopyIsDeepClone()
|
||||
public void TestDeepClone()
|
||||
{
|
||||
var cpi = new ControlPointInfo();
|
||||
|
||||
cpi.Add(1000, new TimingControlPoint { BeatLength = 500 });
|
||||
|
||||
var cpiCopy = cpi.CreateCopy();
|
||||
var cpiCopy = cpi.DeepClone();
|
||||
|
||||
cpiCopy.Add(2000, new TimingControlPoint { BeatLength = 500 });
|
||||
|
||||
|
@ -88,7 +88,7 @@ namespace osu.Game.Tests.Visual.UserInterface
|
||||
AddStep("create mods", () =>
|
||||
{
|
||||
original = new OsuModDoubleTime();
|
||||
copy = (OsuModDoubleTime)original.CreateCopy();
|
||||
copy = (OsuModDoubleTime)original.DeepClone();
|
||||
});
|
||||
|
||||
AddStep("change property", () => original.SpeedChange.Value = 2);
|
||||
@ -106,7 +106,7 @@ namespace osu.Game.Tests.Visual.UserInterface
|
||||
AddStep("create mods", () =>
|
||||
{
|
||||
original = new MultiMod(new OsuModDoubleTime());
|
||||
copy = (MultiMod)original.CreateCopy();
|
||||
copy = (MultiMod)original.DeepClone();
|
||||
});
|
||||
|
||||
AddStep("change property", () => ((OsuModDoubleTime)original.Mods[0]).SpeedChange.Value = 2);
|
||||
|
@ -3,11 +3,12 @@
|
||||
|
||||
using System;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Utils;
|
||||
using osuTK.Graphics;
|
||||
|
||||
namespace osu.Game.Beatmaps.ControlPoints
|
||||
{
|
||||
public abstract class ControlPoint : IComparable<ControlPoint>
|
||||
public abstract class ControlPoint : IComparable<ControlPoint>, IDeepCloneable<ControlPoint>
|
||||
{
|
||||
/// <summary>
|
||||
/// The time at which the control point takes effect.
|
||||
@ -32,7 +33,7 @@ namespace osu.Game.Beatmaps.ControlPoints
|
||||
/// <summary>
|
||||
/// Create an unbound copy of this control point.
|
||||
/// </summary>
|
||||
public ControlPoint CreateCopy()
|
||||
public ControlPoint DeepClone()
|
||||
{
|
||||
var copy = (ControlPoint)Activator.CreateInstance(GetType());
|
||||
|
||||
|
@ -10,11 +10,12 @@ using osu.Framework.Bindables;
|
||||
using osu.Framework.Lists;
|
||||
using osu.Framework.Utils;
|
||||
using osu.Game.Screens.Edit;
|
||||
using osu.Game.Utils;
|
||||
|
||||
namespace osu.Game.Beatmaps.ControlPoints
|
||||
{
|
||||
[Serializable]
|
||||
public class ControlPointInfo
|
||||
public class ControlPointInfo : IDeepCloneable<ControlPointInfo>
|
||||
{
|
||||
/// <summary>
|
||||
/// All control points grouped by time.
|
||||
@ -350,12 +351,12 @@ namespace osu.Game.Beatmaps.ControlPoints
|
||||
}
|
||||
}
|
||||
|
||||
public ControlPointInfo CreateCopy()
|
||||
public ControlPointInfo DeepClone()
|
||||
{
|
||||
var controlPointInfo = new ControlPointInfo();
|
||||
|
||||
foreach (var point in AllControlPoints)
|
||||
controlPointInfo.Add(point.Time, point.CreateCopy());
|
||||
controlPointInfo.Add(point.Time, point.DeepClone());
|
||||
|
||||
return controlPointInfo;
|
||||
}
|
||||
|
@ -10,10 +10,11 @@ using osu.Game.IO.Serialization.Converters;
|
||||
using osu.Game.Online.Rooms.GameTypes;
|
||||
using osu.Game.Online.Rooms.RoomStatuses;
|
||||
using osu.Game.Users;
|
||||
using osu.Game.Utils;
|
||||
|
||||
namespace osu.Game.Online.Rooms
|
||||
{
|
||||
public class Room
|
||||
public class Room : IDeepCloneable<Room>
|
||||
{
|
||||
[Cached]
|
||||
[JsonProperty("id")]
|
||||
@ -120,7 +121,7 @@ namespace osu.Game.Online.Rooms
|
||||
/// Create a copy of this room without online information.
|
||||
/// Should be used to create a local copy of a room for submitting in the future.
|
||||
/// </summary>
|
||||
public Room CreateCopy()
|
||||
public Room DeepClone()
|
||||
{
|
||||
var copy = new Room();
|
||||
|
||||
|
@ -429,7 +429,7 @@ namespace osu.Game.Overlays.Mods
|
||||
if (!Stacked)
|
||||
modEnumeration = ModUtils.FlattenMods(modEnumeration);
|
||||
|
||||
section.Mods = modEnumeration.Select(getValidModOrNull).Where(m => m != null).Select(m => m.CreateCopy());
|
||||
section.Mods = modEnumeration.Select(getValidModOrNull).Where(m => m != null).Select(m => m.DeepClone());
|
||||
}
|
||||
|
||||
updateSelectedButtons();
|
||||
|
@ -32,7 +32,7 @@ namespace osu.Game.Rulesets.Difficulty
|
||||
/// <returns>A structure describing the difficulty of the beatmap.</returns>
|
||||
public DifficultyAttributes Calculate(params Mod[] mods)
|
||||
{
|
||||
mods = mods.Select(m => m.CreateCopy()).ToArray();
|
||||
mods = mods.Select(m => m.DeepClone()).ToArray();
|
||||
|
||||
IBeatmap playableBeatmap = beatmap.GetPlayableBeatmap(ruleset.RulesetInfo, mods);
|
||||
|
||||
|
@ -21,7 +21,7 @@ namespace osu.Game.Rulesets.Mods
|
||||
/// The base class for gameplay modifiers.
|
||||
/// </summary>
|
||||
[ExcludeFromDynamicCompile]
|
||||
public abstract class Mod : IMod, IEquatable<Mod>, IJsonSerializable
|
||||
public abstract class Mod : IMod, IEquatable<Mod>, IJsonSerializable, IDeepCloneable<Mod>
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of this mod.
|
||||
@ -132,7 +132,7 @@ namespace osu.Game.Rulesets.Mods
|
||||
/// <summary>
|
||||
/// Creates a copy of this <see cref="Mod"/> initialised to a default state.
|
||||
/// </summary>
|
||||
public virtual Mod CreateCopy()
|
||||
public virtual Mod DeepClone()
|
||||
{
|
||||
var result = (Mod)Activator.CreateInstance(GetType());
|
||||
result.CopyFrom(this);
|
||||
|
@ -20,7 +20,7 @@ namespace osu.Game.Rulesets.Mods
|
||||
Mods = mods;
|
||||
}
|
||||
|
||||
public override Mod CreateCopy() => new MultiMod(Mods.Select(m => m.CreateCopy()).ToArray());
|
||||
public override Mod DeepClone() => new MultiMod(Mods.Select(m => m.DeepClone()).ToArray());
|
||||
|
||||
public override Type[] IncompatibleMods => Mods.SelectMany(m => m.IncompatibleMods).ToArray();
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ namespace osu.Game.Screens.Edit
|
||||
|
||||
// clone these locally for now to avoid incurring overhead on GetPlayableBeatmap usages.
|
||||
// eventually we will want to improve how/where this is done as there are issues with *not* cloning it in all cases.
|
||||
playableBeatmap.ControlPointInfo = playableBeatmap.ControlPointInfo.CreateCopy();
|
||||
playableBeatmap.ControlPointInfo = playableBeatmap.ControlPointInfo.DeepClone();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -242,7 +242,7 @@ namespace osu.Game.Screens.OnlinePlay.Lounge.Components
|
||||
{
|
||||
new OsuMenuItem("Create copy", MenuItemType.Standard, () =>
|
||||
{
|
||||
parentScreen?.OpenNewRoom(Room.CreateCopy());
|
||||
parentScreen?.OpenNewRoom(Room.DeepClone());
|
||||
})
|
||||
};
|
||||
}
|
||||
|
@ -72,8 +72,8 @@ namespace osu.Game.Screens.OnlinePlay
|
||||
|
||||
// At this point, Mods contains both the required and allowed mods. For selection purposes, it should only contain the required mods.
|
||||
// Similarly, freeMods is currently empty but should only contain the allowed mods.
|
||||
Mods.Value = selectedItem?.Value?.RequiredMods.Select(m => m.CreateCopy()).ToArray() ?? Array.Empty<Mod>();
|
||||
FreeMods.Value = selectedItem?.Value?.AllowedMods.Select(m => m.CreateCopy()).ToArray() ?? Array.Empty<Mod>();
|
||||
Mods.Value = selectedItem?.Value?.RequiredMods.Select(m => m.DeepClone()).ToArray() ?? Array.Empty<Mod>();
|
||||
FreeMods.Value = selectedItem?.Value?.AllowedMods.Select(m => m.DeepClone()).ToArray() ?? Array.Empty<Mod>();
|
||||
|
||||
Mods.BindValueChanged(onModsChanged);
|
||||
Ruleset.BindValueChanged(onRulesetChanged);
|
||||
@ -108,8 +108,8 @@ namespace osu.Game.Screens.OnlinePlay
|
||||
}
|
||||
};
|
||||
|
||||
item.RequiredMods.AddRange(Mods.Value.Select(m => m.CreateCopy()));
|
||||
item.AllowedMods.AddRange(FreeMods.Value.Select(m => m.CreateCopy()));
|
||||
item.RequiredMods.AddRange(Mods.Value.Select(m => m.DeepClone()));
|
||||
item.AllowedMods.AddRange(FreeMods.Value.Select(m => m.DeepClone()));
|
||||
|
||||
SelectItem(item);
|
||||
return true;
|
||||
|
@ -55,10 +55,10 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
|
||||
item.Ruleset.Value = Ruleset.Value;
|
||||
|
||||
item.RequiredMods.Clear();
|
||||
item.RequiredMods.AddRange(Mods.Value.Select(m => m.CreateCopy()));
|
||||
item.RequiredMods.AddRange(Mods.Value.Select(m => m.DeepClone()));
|
||||
|
||||
item.AllowedMods.Clear();
|
||||
item.AllowedMods.AddRange(FreeMods.Value.Select(m => m.CreateCopy()));
|
||||
item.AllowedMods.AddRange(FreeMods.Value.Select(m => m.DeepClone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -184,7 +184,7 @@ namespace osu.Game.Screens.Play
|
||||
[BackgroundDependencyLoader(true)]
|
||||
private void load(AudioManager audio, OsuConfigManager config, OsuGameBase game)
|
||||
{
|
||||
Mods.Value = base.Mods.Value.Select(m => m.CreateCopy()).ToArray();
|
||||
Mods.Value = base.Mods.Value.Select(m => m.DeepClone()).ToArray();
|
||||
|
||||
if (Beatmap.Value is DummyWorkingBeatmap)
|
||||
return;
|
||||
|
16
osu.Game/Utils/IDeepCloneable.cs
Normal file
16
osu.Game/Utils/IDeepCloneable.cs
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
namespace osu.Game.Utils
|
||||
{
|
||||
/// <summary>A generic interface for a deeply cloneable type.</summary>
|
||||
/// <typeparam name="T">The type of object to clone.</typeparam>
|
||||
public interface IDeepCloneable<out T> where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <typeparamref name="T" /> that is a deep copy of the current instance.
|
||||
/// </summary>
|
||||
/// <returns>The <typeparamref name="T" />.</returns>
|
||||
T DeepClone();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user