osu/osu.Game/Skinning/LegacySkin.cs

333 lines
14 KiB
C#
Raw Normal View History

2019-08-21 06:11:33 +00:00
// 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.
2018-04-13 09:19:50 +00:00
2020-04-01 14:32:33 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
2018-04-13 09:19:50 +00:00
using System.IO;
using System.Linq;
2019-09-04 06:59:09 +00:00
using JetBrains.Annotations;
2018-04-13 09:19:50 +00:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
2019-08-23 11:32:43 +00:00
using osu.Game.Audio;
using osu.Game.Beatmaps.Formats;
using osu.Game.IO;
2019-08-30 06:12:03 +00:00
using osu.Game.Rulesets.Scoring;
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Skinning
{
public class LegacySkin : Skin
{
2019-09-04 06:59:09 +00:00
[CanBeNull]
2018-04-13 09:19:50 +00:00
protected TextureStore Textures;
2019-09-04 06:59:09 +00:00
[CanBeNull]
protected IResourceStore<SampleChannel> Samples;
2018-04-13 09:19:50 +00:00
2020-04-01 14:32:33 +00:00
/// <summary>
/// Whether texture for the keys exists.
/// Used to determine if the mania ruleset is skinned.
/// </summary>
private readonly Lazy<bool> hasKeyTexture;
protected virtual bool AllowManiaSkin => hasKeyTexture.Value;
public new LegacySkinConfiguration Configuration
{
get => base.Configuration as LegacySkinConfiguration;
set => base.Configuration = value;
}
2019-10-09 20:04:34 +00:00
private readonly Dictionary<int, LegacyManiaSkinConfiguration> maniaConfigurations = new Dictionary<int, LegacyManiaSkinConfiguration>();
2018-04-13 09:19:50 +00:00
public LegacySkin(SkinInfo skin, IResourceStore<byte[]> storage, AudioManager audioManager)
: this(skin, new LegacySkinResourceStore<SkinFileInfo>(skin, storage), audioManager, "skin.ini")
{
}
2019-02-28 04:31:40 +00:00
protected LegacySkin(SkinInfo skin, IResourceStore<byte[]> storage, AudioManager audioManager, string filename)
: base(skin)
2018-04-13 09:19:50 +00:00
{
using (var stream = storage?.GetStream(filename))
2019-11-11 11:53:22 +00:00
{
if (stream != null)
{
using (LineBufferedReader reader = new LineBufferedReader(stream, true))
Configuration = new LegacySkinDecoder().Decode(reader);
stream.Seek(0, SeekOrigin.Begin);
using (LineBufferedReader reader = new LineBufferedReader(stream))
{
var maniaList = new LegacyManiaSkinDecoder().Decode(reader);
foreach (var config in maniaList)
maniaConfigurations[config.Keys] = config;
}
}
else
Configuration = new LegacySkinConfiguration();
2019-11-11 11:53:22 +00:00
}
2018-04-13 09:19:50 +00:00
2019-09-04 06:59:09 +00:00
if (storage != null)
{
var samples = audioManager?.GetSampleStore(storage);
if (samples != null)
samples.PlaybackConcurrency = OsuGameBase.SAMPLE_CONCURRENCY;
Samples = samples;
2019-09-04 06:59:09 +00:00
Textures = new TextureStore(new TextureLoaderStore(storage));
(storage as ResourceStore<byte[]>)?.AddExtension("ogg");
2019-09-04 06:59:09 +00:00
}
2020-04-01 14:32:33 +00:00
// todo: this shouldn't really be duplicated here (from ManiaLegacySkinTransformer). we need to come up with a better solution.
hasKeyTexture = new Lazy<bool>(() => this.GetAnimation(
lookupForMania<string>(new LegacyManiaSkinConfigurationLookup(4, LegacyManiaSkinConfigurationLookups.KeyImage, 0))?.Value ?? "mania-key1", true,
true) != null);
2018-04-13 09:19:50 +00:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
Textures?.Dispose();
Samples?.Dispose();
}
public override IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
{
switch (lookup)
{
case GlobalSkinColours colour:
switch (colour)
{
case GlobalSkinColours.ComboColours:
var comboColours = Configuration.ComboColours;
if (comboColours != null)
return SkinUtils.As<TValue>(new Bindable<IReadOnlyList<Color4>>(comboColours));
break;
default:
return SkinUtils.As<TValue>(getCustomColour(Configuration, colour.ToString()));
}
break;
2019-11-20 06:40:35 +00:00
case LegacySkinConfiguration.LegacySetting legacy:
2019-10-09 20:04:34 +00:00
switch (legacy)
{
2019-11-20 06:40:35 +00:00
case LegacySkinConfiguration.LegacySetting.Version:
return SkinUtils.As<TValue>(new Bindable<decimal>(Configuration.LegacyVersion ?? LegacySkinConfiguration.LATEST_VERSION));
2019-10-09 20:04:34 +00:00
}
break;
case SkinCustomColourLookup customColour:
return SkinUtils.As<TValue>(getCustomColour(Configuration, customColour.Lookup.ToString()));
case LegacyManiaSkinConfigurationLookup maniaLookup:
if (!AllowManiaSkin)
return null;
2020-04-01 14:46:50 +00:00
var result = lookupForMania<TValue>(maniaLookup);
if (result != null)
return result;
break;
default:
// handles lookups like GlobalSkinConfiguration
try
{
if (Configuration.ConfigDictionary.TryGetValue(lookup.ToString(), out var val))
{
// special case for handling skins which use 1 or 0 to signify a boolean state.
if (typeof(TValue) == typeof(bool))
val = val == "1" ? "true" : "false";
var bindable = new Bindable<TValue>();
if (val != null)
bindable.Parse(val);
return bindable;
}
}
catch
{
}
break;
}
return null;
}
2020-04-01 14:46:50 +00:00
private IBindable<TValue> lookupForMania<TValue>(LegacyManiaSkinConfigurationLookup maniaLookup)
{
if (!maniaConfigurations.TryGetValue(maniaLookup.Keys, out var existing))
maniaConfigurations[maniaLookup.Keys] = existing = new LegacyManiaSkinConfiguration(maniaLookup.Keys);
switch (maniaLookup.Lookup)
{
case LegacyManiaSkinConfigurationLookups.ColumnWidth:
Debug.Assert(maniaLookup.TargetColumn != null);
return SkinUtils.As<TValue>(new Bindable<float>(existing.ColumnWidth[maniaLookup.TargetColumn.Value]));
case LegacyManiaSkinConfigurationLookups.ColumnSpacing:
Debug.Assert(maniaLookup.TargetColumn != null);
return SkinUtils.As<TValue>(new Bindable<float>(existing.ColumnSpacing[maniaLookup.TargetColumn.Value]));
case LegacyManiaSkinConfigurationLookups.HitPosition:
return SkinUtils.As<TValue>(new Bindable<float>(existing.HitPosition));
case LegacyManiaSkinConfigurationLookups.LightPosition:
return SkinUtils.As<TValue>(new Bindable<float>(existing.LightPosition));
case LegacyManiaSkinConfigurationLookups.ShowJudgementLine:
return SkinUtils.As<TValue>(new Bindable<bool>(existing.ShowJudgementLine));
2020-04-02 05:29:16 +00:00
case LegacyManiaSkinConfigurationLookups.ExplosionScale:
Debug.Assert(maniaLookup.TargetColumn != null);
if (GetConfig<LegacySkinConfiguration.LegacySetting, decimal>(LegacySkinConfiguration.LegacySetting.Version)?.Value < 2.5m)
return SkinUtils.As<TValue>(new Bindable<float>(1));
if (existing.ExplosionWidth[maniaLookup.TargetColumn.Value] != 0)
return SkinUtils.As<TValue>(new Bindable<float>(existing.ExplosionWidth[maniaLookup.TargetColumn.Value] / LegacyManiaSkinConfiguration.DEFAULT_COLUMN_SIZE));
return SkinUtils.As<TValue>(new Bindable<float>(existing.ColumnWidth[maniaLookup.TargetColumn.Value] / LegacyManiaSkinConfiguration.DEFAULT_COLUMN_SIZE));
case LegacyManiaSkinConfigurationLookups.ColumnLineColour:
return SkinUtils.As<TValue>(getCustomColour(existing, "ColourColumnLine"));
2020-04-07 07:50:08 +00:00
case LegacyManiaSkinConfigurationLookups.JudgementLineColour:
return SkinUtils.As<TValue>(getCustomColour(existing, "ColourJudgementLine"));
case LegacyManiaSkinConfigurationLookups.ColumnBackgroundColour:
Debug.Assert(maniaLookup.TargetColumn != null);
2020-04-07 08:11:32 +00:00
return SkinUtils.As<TValue>(getCustomColour(existing, $"Colour{maniaLookup.TargetColumn + 1}"));
case LegacyManiaSkinConfigurationLookups.ColumnLightColour:
Debug.Assert(maniaLookup.TargetColumn != null);
2020-04-07 08:11:32 +00:00
return SkinUtils.As<TValue>(getCustomColour(existing, $"ColourLight{maniaLookup.TargetColumn + 1}"));
case LegacyManiaSkinConfigurationLookups.MinimumColumnWidth:
return SkinUtils.As<TValue>(new Bindable<float>(existing.MinimumColumnWidth));
case LegacyManiaSkinConfigurationLookups.NoteImage:
Debug.Assert(maniaLookup.TargetColumn != null);
return SkinUtils.As<TValue>(getManiaImage(existing, $"NoteImage{maniaLookup.TargetColumn}"));
case LegacyManiaSkinConfigurationLookups.HoldNoteHeadImage:
Debug.Assert(maniaLookup.TargetColumn != null);
return SkinUtils.As<TValue>(getManiaImage(existing, $"NoteImage{maniaLookup.TargetColumn}H"));
case LegacyManiaSkinConfigurationLookups.HoldNoteTailImage:
Debug.Assert(maniaLookup.TargetColumn != null);
return SkinUtils.As<TValue>(getManiaImage(existing, $"NoteImage{maniaLookup.TargetColumn}T"));
case LegacyManiaSkinConfigurationLookups.HoldNoteBodyImage:
Debug.Assert(maniaLookup.TargetColumn != null);
return SkinUtils.As<TValue>(getManiaImage(existing, $"NoteImage{maniaLookup.TargetColumn}L"));
case LegacyManiaSkinConfigurationLookups.KeyImage:
Debug.Assert(maniaLookup.TargetColumn != null);
return SkinUtils.As<TValue>(getManiaImage(existing, $"KeyImage{maniaLookup.TargetColumn}"));
case LegacyManiaSkinConfigurationLookups.KeyImageDown:
Debug.Assert(maniaLookup.TargetColumn != null);
return SkinUtils.As<TValue>(getManiaImage(existing, $"KeyImage{maniaLookup.TargetColumn}D"));
2020-04-01 14:46:50 +00:00
}
return null;
}
private IBindable<Color4> getCustomColour(IHasCustomColours source, string lookup)
=> source.CustomColours.TryGetValue(lookup, out var col) ? new Bindable<Color4>(col) : null;
private IBindable<string> getManiaImage(LegacyManiaSkinConfiguration source, string lookup)
=> source.ImageLookups.TryGetValue(lookup, out var image) ? new Bindable<string>(image) : null;
public override Drawable GetDrawableComponent(ISkinComponent component)
2018-04-13 09:19:50 +00:00
{
2019-08-30 06:12:03 +00:00
switch (component)
2018-04-13 09:19:50 +00:00
{
2019-08-30 06:10:11 +00:00
case GameplaySkinComponent<HitResult> resultComponent:
2019-08-30 06:12:03 +00:00
switch (resultComponent.Component)
{
case HitResult.Miss:
return this.GetAnimation("hit0", true, false);
2019-04-01 03:16:05 +00:00
2019-08-30 06:12:03 +00:00
case HitResult.Meh:
return this.GetAnimation("hit50", true, false);
2019-04-01 03:16:05 +00:00
2019-08-30 06:12:03 +00:00
case HitResult.Good:
return this.GetAnimation("hit100", true, false);
2019-04-01 03:16:05 +00:00
2019-08-30 06:12:03 +00:00
case HitResult.Great:
return this.GetAnimation("hit300", true, false);
}
break;
2018-04-13 09:19:50 +00:00
}
return this.GetAnimation(component.LookupName, false, false);
2019-08-19 10:23:54 +00:00
}
2019-08-27 08:18:32 +00:00
public override Texture GetTexture(string componentName)
{
foreach (var name in getFallbackNames(componentName))
{
float ratio = 2;
var texture = Textures?.Get($"{name}@2x");
2019-08-27 08:18:32 +00:00
if (texture == null)
{
ratio = 1;
texture = Textures?.Get(name);
}
2019-08-27 08:18:32 +00:00
if (texture != null)
texture.ScaleAdjust = ratio;
2019-08-27 08:18:32 +00:00
return texture;
2019-08-27 08:18:32 +00:00
}
return null;
2019-08-27 08:18:32 +00:00
}
2019-08-23 11:32:43 +00:00
public override SampleChannel GetSample(ISampleInfo sampleInfo)
{
2019-08-23 11:32:43 +00:00
foreach (var lookup in sampleInfo.LookupNames)
{
var sample = Samples?.Get(lookup);
2019-08-23 11:32:43 +00:00
if (sample != null)
return sample;
}
2019-08-23 11:32:43 +00:00
if (sampleInfo is HitSampleInfo hsi)
// Try fallback to non-bank samples.
2019-09-04 06:59:09 +00:00
return Samples?.Get(hsi.Name);
2019-08-23 11:32:43 +00:00
return null;
}
2019-08-27 08:18:32 +00:00
private IEnumerable<string> getFallbackNames(string componentName)
2019-08-27 08:18:32 +00:00
{
// May be something like "Gameplay/osu/approachcircle" from lazer, or "Arrows/note1" from a user skin.
yield return componentName;
// Fall back to using the last piece for components coming from lazer (e.g. "Gameplay/osu/approachcircle" -> "approachcircle").
2019-08-27 08:18:32 +00:00
string lastPiece = componentName.Split('/').Last();
yield return componentName.StartsWith("Gameplay/taiko/") ? "taiko-" + lastPiece : lastPiece;
2019-08-27 08:18:32 +00:00
}
2018-04-13 09:19:50 +00:00
}
}