1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 00:07:25 +08:00
osu-lazer/osu.Game/Skinning/LegacySkin.cs

119 lines
4.1 KiB
C#
Raw Normal View History

2018-02-22 16:16:48 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2018-03-05 20:27:37 +08:00
using System;
using System.IO;
using System.Linq;
2018-02-22 16:16:48 +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;
2018-03-14 19:45:04 +08:00
using osu.Game.Database;
using OpenTK;
2018-02-22 16:16:48 +08:00
namespace osu.Game.Skinning
{
public class LegacySkin : Skin
{
2018-03-14 19:45:04 +08:00
protected TextureStore Textures;
2018-02-22 16:16:48 +08:00
2018-03-15 23:14:29 +08:00
protected SampleManager Samples;
2018-02-22 16:16:48 +08:00
public LegacySkin(SkinInfo skin, IResourceStore<byte[]> storage, AudioManager audioManager)
: this(skin, new LegacySkinResourceStore<SkinFileInfo>(skin, storage), audioManager, "skin.ini")
2018-02-22 16:16:48 +08:00
{
}
protected LegacySkin(SkinInfo skin, IResourceStore<byte[]> storage, AudioManager audioManager, string filename) : base(skin)
{
Stream stream = storage.GetStream(filename);
if (stream != null)
using (StreamReader reader = new StreamReader(stream))
Configuration = new LegacySkinDecoder().Decode(reader);
else
Configuration = new SkinConfiguration();
2018-02-22 16:16:48 +08:00
Samples = audioManager.GetSampleManager(storage);
Textures = new TextureStore(new RawTextureLoaderStore(storage));
2018-03-14 19:45:04 +08:00
}
2018-02-22 16:16:48 +08:00
public override Drawable GetDrawableComponent(string componentName)
{
2018-03-12 10:30:13 +08:00
switch (componentName)
{
case "Play/Miss":
componentName = "hit0";
break;
case "Play/Meh":
componentName = "hit50";
break;
case "Play/Good":
componentName = "hit100";
break;
case "Play/Great":
componentName = "hit300";
break;
}
float ratio = 0.72f; // brings sizing roughly in-line with stable
var texture = GetTexture($"{componentName}@2x");
if (texture == null)
{
ratio *= 2;
2018-03-24 10:40:45 +08:00
texture = GetTexture(componentName);
}
2018-02-22 16:16:48 +08:00
if (texture == null) return null;
return new Sprite
{
Texture = texture,
Scale = new Vector2(ratio),
};
2018-02-22 16:16:48 +08:00
}
public override Texture GetTexture(string componentName) => Textures.Get(componentName);
2018-03-15 23:14:29 +08:00
public override SampleChannel GetSample(string sampleName) => Samples.Get(sampleName);
2018-03-05 20:27:37 +08:00
2018-03-14 19:45:04 +08:00
protected class LegacySkinResourceStore<T> : IResourceStore<byte[]>
where T : INamedFileInfo
2018-03-05 20:27:37 +08:00
{
2018-03-14 19:45:04 +08:00
private readonly IHasFiles<T> source;
2018-03-05 20:27:37 +08:00
private readonly IResourceStore<byte[]> underlyingStore;
2018-03-12 21:01:18 +08:00
private string getPathForFile(string filename)
{
bool hasExtension = filename.Contains('.');
2018-03-12 21:01:18 +08:00
string lastPiece = filename.Split('/').Last();
2018-03-14 19:45:04 +08:00
var file = source.Files.FirstOrDefault(f =>
string.Equals(hasExtension ? f.Filename : Path.GetFileNameWithoutExtension(f.Filename), lastPiece, StringComparison.InvariantCultureIgnoreCase));
2018-03-12 21:01:18 +08:00
return file?.FileInfo.StoragePath;
}
2018-03-05 20:27:37 +08:00
2018-03-14 19:45:04 +08:00
public LegacySkinResourceStore(IHasFiles<T> source, IResourceStore<byte[]> underlyingStore)
2018-03-05 20:27:37 +08:00
{
2018-03-14 19:45:04 +08:00
this.source = source;
2018-03-05 20:27:37 +08:00
this.underlyingStore = underlyingStore;
}
public Stream GetStream(string name)
{
string path = getPathForFile(name);
return path == null ? null : underlyingStore.GetStream(path);
}
2018-03-05 20:27:37 +08:00
byte[] IResourceStore<byte[]>.Get(string name)
{
string path = getPathForFile(name);
return path == null ? null : underlyingStore.Get(path);
}
2018-03-05 20:27:37 +08:00
}
2018-02-22 16:16:48 +08:00
}
}