1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 10:33:01 +08:00

Merge branch 'master' into file-selector

This commit is contained in:
Dean Herbert 2020-09-25 12:33:18 +09:00
commit 1b8b417b85
2 changed files with 213 additions and 56 deletions

View File

@ -0,0 +1,134 @@
// 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.
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.OpenGL.Textures;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Textures;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.UI;
using osu.Game.Tests.Visual;
namespace osu.Game.Tests.Rulesets
{
public class TestSceneDrawableRulesetDependencies : OsuTestScene
{
[Test]
public void TestDisposalDoesNotDisposeParentStores()
{
DrawableWithDependencies drawable = null;
TestTextureStore textureStore = null;
TestSampleStore sampleStore = null;
AddStep("add dependencies", () =>
{
Child = drawable = new DrawableWithDependencies();
textureStore = drawable.ParentTextureStore;
sampleStore = drawable.ParentSampleStore;
});
AddStep("clear children", Clear);
AddUntilStep("wait for disposal", () => drawable.IsDisposed);
AddStep("GC", () =>
{
drawable = null;
GC.Collect();
GC.WaitForPendingFinalizers();
});
AddAssert("parent texture store not disposed", () => !textureStore.IsDisposed);
AddAssert("parent sample store not disposed", () => !sampleStore.IsDisposed);
}
private class DrawableWithDependencies : CompositeDrawable
{
public TestTextureStore ParentTextureStore { get; private set; }
public TestSampleStore ParentSampleStore { get; private set; }
public DrawableWithDependencies()
{
InternalChild = new Box { RelativeSizeAxes = Axes.Both };
}
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
dependencies.CacheAs<TextureStore>(ParentTextureStore = new TestTextureStore());
dependencies.CacheAs<ISampleStore>(ParentSampleStore = new TestSampleStore());
return new DrawableRulesetDependencies(new OsuRuleset(), dependencies);
}
public new bool IsDisposed { get; private set; }
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
IsDisposed = true;
}
}
private class TestTextureStore : TextureStore
{
public override Texture Get(string name, WrapMode wrapModeS, WrapMode wrapModeT) => null;
public bool IsDisposed { get; private set; }
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
IsDisposed = true;
}
}
private class TestSampleStore : ISampleStore
{
public bool IsDisposed { get; private set; }
public void Dispose()
{
IsDisposed = true;
}
public SampleChannel Get(string name) => null;
public Task<SampleChannel> GetAsync(string name) => null;
public Stream GetStream(string name) => null;
public IEnumerable<string> GetAvailableResources() => throw new NotImplementedException();
public BindableNumber<double> Volume => throw new NotImplementedException();
public BindableNumber<double> Balance => throw new NotImplementedException();
public BindableNumber<double> Frequency => throw new NotImplementedException();
public BindableNumber<double> Tempo => throw new NotImplementedException();
public void AddAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable) => throw new NotImplementedException();
public void RemoveAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable) => throw new NotImplementedException();
public void RemoveAllAdjustments(AdjustableProperty type) => throw new NotImplementedException();
public IBindable<double> AggregateVolume => throw new NotImplementedException();
public IBindable<double> AggregateBalance => throw new NotImplementedException();
public IBindable<double> AggregateFrequency => throw new NotImplementedException();
public IBindable<double> AggregateTempo => throw new NotImplementedException();
public int PlaybackConcurrency { get; set; }
}
}
}

View File

@ -10,6 +10,7 @@ using osu.Framework.Audio;
using osu.Framework.Audio.Sample; using osu.Framework.Audio.Sample;
using osu.Framework.Audio.Track; using osu.Framework.Audio.Track;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics.OpenGL.Textures;
using osu.Framework.Graphics.Textures; using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores; using osu.Framework.IO.Stores;
using osu.Game.Rulesets.Configuration; using osu.Game.Rulesets.Configuration;
@ -46,12 +47,11 @@ namespace osu.Game.Rulesets.UI
if (resources != null) if (resources != null)
{ {
TextureStore = new TextureStore(new TextureLoaderStore(new NamespacedResourceStore<byte[]>(resources, @"Textures"))); TextureStore = new TextureStore(new TextureLoaderStore(new NamespacedResourceStore<byte[]>(resources, @"Textures")));
TextureStore.AddStore(parent.Get<TextureStore>()); CacheAs(TextureStore = new FallbackTextureStore(TextureStore, parent.Get<TextureStore>()));
Cache(TextureStore);
SampleStore = parent.Get<AudioManager>().GetSampleStore(new NamespacedResourceStore<byte[]>(resources, @"Samples")); SampleStore = parent.Get<AudioManager>().GetSampleStore(new NamespacedResourceStore<byte[]>(resources, @"Samples"));
SampleStore.PlaybackConcurrency = OsuGameBase.SAMPLE_CONCURRENCY; SampleStore.PlaybackConcurrency = OsuGameBase.SAMPLE_CONCURRENCY;
CacheAs<ISampleStore>(new FallbackSampleStore(SampleStore, parent.Get<ISampleStore>())); CacheAs(SampleStore = new FallbackSampleStore(SampleStore, parent.Get<ISampleStore>()));
} }
RulesetConfigManager = parent.Get<RulesetConfigCache>().GetConfigFor(ruleset); RulesetConfigManager = parent.Get<RulesetConfigCache>().GetConfigFor(ruleset);
@ -82,69 +82,92 @@ namespace osu.Game.Rulesets.UI
isDisposed = true; isDisposed = true;
SampleStore?.Dispose(); SampleStore?.Dispose();
TextureStore?.Dispose();
RulesetConfigManager = null; RulesetConfigManager = null;
} }
#endregion #endregion
}
/// <summary> /// <summary>
/// A sample store which adds a fallback source. /// A sample store which adds a fallback source and prevents disposal of the fallback source.
/// </summary> /// </summary>
/// <remarks> private class FallbackSampleStore : ISampleStore
/// This is a temporary implementation to workaround ISampleStore limitations.
/// </remarks>
public class FallbackSampleStore : ISampleStore
{
private readonly ISampleStore primary;
private readonly ISampleStore secondary;
public FallbackSampleStore(ISampleStore primary, ISampleStore secondary)
{ {
this.primary = primary; private readonly ISampleStore primary;
this.secondary = secondary; private readonly ISampleStore fallback;
public FallbackSampleStore(ISampleStore primary, ISampleStore fallback)
{
this.primary = primary;
this.fallback = fallback;
}
public SampleChannel Get(string name) => primary.Get(name) ?? fallback.Get(name);
public Task<SampleChannel> GetAsync(string name) => primary.GetAsync(name) ?? fallback.GetAsync(name);
public Stream GetStream(string name) => primary.GetStream(name) ?? fallback.GetStream(name);
public IEnumerable<string> GetAvailableResources() => throw new NotSupportedException();
public void AddAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable) => throw new NotSupportedException();
public void RemoveAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable) => throw new NotSupportedException();
public void RemoveAllAdjustments(AdjustableProperty type) => throw new NotSupportedException();
public BindableNumber<double> Volume => throw new NotSupportedException();
public BindableNumber<double> Balance => throw new NotSupportedException();
public BindableNumber<double> Frequency => throw new NotSupportedException();
public BindableNumber<double> Tempo => throw new NotSupportedException();
public IBindable<double> GetAggregate(AdjustableProperty type) => throw new NotSupportedException();
public IBindable<double> AggregateVolume => throw new NotSupportedException();
public IBindable<double> AggregateBalance => throw new NotSupportedException();
public IBindable<double> AggregateFrequency => throw new NotSupportedException();
public IBindable<double> AggregateTempo => throw new NotSupportedException();
public int PlaybackConcurrency
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public void Dispose()
{
primary?.Dispose();
}
} }
public SampleChannel Get(string name) => primary.Get(name) ?? secondary.Get(name); /// <summary>
/// A texture store which adds a fallback source and prevents disposal of the fallback source.
public Task<SampleChannel> GetAsync(string name) => primary.GetAsync(name) ?? secondary.GetAsync(name); /// </summary>
private class FallbackTextureStore : TextureStore
public Stream GetStream(string name) => primary.GetStream(name) ?? secondary.GetStream(name);
public IEnumerable<string> GetAvailableResources() => throw new NotSupportedException();
public void AddAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable) => throw new NotSupportedException();
public void RemoveAdjustment(AdjustableProperty type, BindableNumber<double> adjustBindable) => throw new NotSupportedException();
public void RemoveAllAdjustments(AdjustableProperty type) => throw new NotSupportedException();
public BindableNumber<double> Volume => throw new NotSupportedException();
public BindableNumber<double> Balance => throw new NotSupportedException();
public BindableNumber<double> Frequency => throw new NotSupportedException();
public BindableNumber<double> Tempo => throw new NotSupportedException();
public IBindable<double> GetAggregate(AdjustableProperty type) => throw new NotSupportedException();
public IBindable<double> AggregateVolume => throw new NotSupportedException();
public IBindable<double> AggregateBalance => throw new NotSupportedException();
public IBindable<double> AggregateFrequency => throw new NotSupportedException();
public IBindable<double> AggregateTempo => throw new NotSupportedException();
public int PlaybackConcurrency
{ {
get => throw new NotSupportedException(); private readonly TextureStore primary;
set => throw new NotSupportedException(); private readonly TextureStore fallback;
}
public void Dispose() public FallbackTextureStore(TextureStore primary, TextureStore fallback)
{ {
this.primary = primary;
this.fallback = fallback;
}
public override Texture Get(string name, WrapMode wrapModeS, WrapMode wrapModeT)
=> primary.Get(name, wrapModeS, wrapModeT) ?? fallback.Get(name, wrapModeS, wrapModeT);
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
primary?.Dispose();
}
} }
} }
} }