1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 03:47:26 +08:00
osu-lazer/osu.Game/Skinning/LegacySkin.cs

220 lines
7.2 KiB
C#
Raw Normal View History

// 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 17:19:50 +08:00
using System;
using System.Collections.Generic;
2018-04-13 17:19:50 +08:00
using System.IO;
using System.Linq;
2018-08-27 16:05:58 +08:00
using System.Threading.Tasks;
2018-04-13 17:19:50 +08:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using osu.Game.Database;
2018-09-27 15:27:11 +08:00
using osu.Game.Graphics.Sprites;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Skinning
{
public class LegacySkin : Skin
{
protected TextureStore Textures;
protected IResourceStore<SampleChannel> Samples;
2018-04-13 17:19:50 +08:00
public LegacySkin(SkinInfo skin, IResourceStore<byte[]> storage, AudioManager audioManager)
: this(skin, new LegacySkinResourceStore<SkinFileInfo>(skin, storage), audioManager, "skin.ini")
{
}
2019-02-28 12:31:40 +08:00
protected LegacySkin(SkinInfo skin, IResourceStore<byte[]> storage, AudioManager audioManager, string filename)
: base(skin)
2018-04-13 17:19:50 +08:00
{
Stream stream = storage.GetStream(filename);
if (stream != null)
using (StreamReader reader = new StreamReader(stream))
Configuration = new LegacySkinDecoder().Decode(reader);
else
Configuration = new SkinConfiguration();
Samples = audioManager.GetSampleStore(storage);
2018-09-07 17:56:08 +08:00
Textures = new TextureStore(new TextureLoaderStore(storage));
2018-04-13 17:19:50 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
Textures?.Dispose();
Samples?.Dispose();
}
2018-04-13 17:19:50 +08:00
public override Drawable GetDrawableComponent(string componentName)
{
switch (componentName)
{
case "Play/Miss":
componentName = "hit0";
break;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case "Play/Meh":
componentName = "hit50";
break;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case "Play/Good":
componentName = "hit100";
break;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case "Play/Great":
componentName = "hit300";
break;
2019-04-01 11:16:05 +08:00
2018-09-27 15:27:11 +08:00
case "Play/osu/number-text":
return !hasFont(Configuration.HitCircleFont)
? null
: new LegacySpriteText(Textures, Configuration.HitCircleFont)
{
Scale = new Vector2(0.96f),
// Spacing value was reverse-engineered from the ratio of the rendered sprite size in the visual inspector vs the actual texture size
Spacing = new Vector2(-Configuration.HitCircleOverlap * 0.89f, 0)
};
2018-04-13 17:19:50 +08:00
}
// temporary allowance is given for skins the fact that stable handles non-animatable items such as hitcircles (incorrectly)
// by (incorrectly) displaying the first frame of animation rather than the non-animated version.
2019-07-01 21:56:53 +08:00
// users have used this to "hide" certain elements like hit300.
var texture = GetTexture($"{componentName}-0") ?? GetTexture(componentName);
2018-04-13 17:19:50 +08:00
2018-09-27 15:27:11 +08:00
if (texture == null)
return null;
return new Sprite { Texture = texture };
}
public override Texture GetTexture(string componentName)
{
float ratio = 2;
var texture = Textures.Get($"{componentName}@2x");
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
if (texture == null)
{
ratio = 1;
texture = Textures.Get(componentName);
2018-04-13 17:19:50 +08:00
}
if (texture != null)
texture.ScaleAdjust = ratio / 0.72f; // brings sizing roughly in-line with stable
2018-04-13 17:19:50 +08:00
return texture;
2018-04-13 17:19:50 +08:00
}
public override SampleChannel GetSample(string sampleName) => Samples.Get(sampleName);
2018-09-27 15:27:11 +08:00
private bool hasFont(string fontName) => GetTexture($"{fontName}-0") != null;
2018-04-13 17:19:50 +08:00
protected class LegacySkinResourceStore<T> : IResourceStore<byte[]>
where T : INamedFileInfo
{
private readonly IHasFiles<T> source;
private readonly IResourceStore<byte[]> underlyingStore;
private string getPathForFile(string filename)
{
bool hasExtension = filename.Contains('.');
string lastPiece = filename.Split('/').Last();
2019-01-21 13:20:37 +08:00
var legacyName = filename.StartsWith("Gameplay/taiko/") ? "taiko-" + lastPiece : lastPiece;
2018-04-13 17:19:50 +08:00
2019-01-06 00:35:33 +08:00
var file = source.Files.Find(f =>
2019-01-21 13:20:37 +08:00
string.Equals(hasExtension ? f.Filename : Path.ChangeExtension(f.Filename, null), legacyName, StringComparison.InvariantCultureIgnoreCase));
2018-04-13 17:19:50 +08:00
return file?.FileInfo.StoragePath;
}
public LegacySkinResourceStore(IHasFiles<T> source, IResourceStore<byte[]> underlyingStore)
{
this.source = source;
this.underlyingStore = underlyingStore;
}
public Stream GetStream(string name)
{
string path = getPathForFile(name);
return path == null ? null : underlyingStore.GetStream(path);
}
public IEnumerable<string> GetAvailableResources() => source.Files.Select(f => f.Filename);
2018-08-27 16:05:58 +08:00
byte[] IResourceStore<byte[]>.Get(string name) => GetAsync(name).Result;
2018-08-29 19:57:48 +08:00
public Task<byte[]> GetAsync(string name)
2018-04-13 17:19:50 +08:00
{
string path = getPathForFile(name);
2018-08-29 19:57:48 +08:00
return path == null ? Task.FromResult<byte[]>(null) : underlyingStore.GetAsync(path);
2018-04-13 17:19:50 +08:00
}
2018-04-21 17:15:27 +08:00
#region IDisposable Support
private bool isDisposed;
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
isDisposed = true;
}
}
~LegacySkinResourceStore()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
2018-04-13 17:19:50 +08:00
}
2018-09-27 15:27:11 +08:00
private class LegacySpriteText : OsuSpriteText
{
private readonly TextureStore textures;
private readonly string font;
public LegacySpriteText(TextureStore textures, string font)
{
this.textures = textures;
this.font = font;
Shadow = false;
UseFullGlyphHeight = false;
}
protected override Texture GetTextureForCharacter(char c)
{
string textureName = $"{font}-{c}";
2018-09-27 16:33:27 +08:00
// Approximate value that brings character sizing roughly in-line with stable
2018-09-27 15:27:11 +08:00
float ratio = 36;
var texture = textures.Get($"{textureName}@2x");
2019-04-01 11:16:05 +08:00
2018-09-27 15:27:11 +08:00
if (texture == null)
{
ratio = 18;
texture = textures.Get(textureName);
}
if (texture != null)
texture.ScaleAdjust = ratio;
return texture;
}
}
2018-04-13 17:19:50 +08:00
}
}