1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 12:45:09 +08:00

Extract invariant instantiation info extension method

This commit is contained in:
Bartłomiej Dach 2021-05-12 22:42:26 +02:00
parent 4464204e33
commit 1b579dd838
5 changed files with 37 additions and 32 deletions

View File

@ -0,0 +1,31 @@
// 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.Linq;
namespace osu.Game.Extensions
{
internal static class TypeExtensions
{
/// <summary>
/// Returns <paramref name="type"/>'s <see cref="Type.AssemblyQualifiedName"/>
/// with the assembly version, culture and public key token values removed.
/// </summary>
/// <remarks>
/// This method is usually used in extensibility scenarios (i.e. for custom rulesets or skins)
/// when a version-agnostic identifier associated with a C# class - potentially originating from
/// an external assembly - is needed.
/// Leaving only the type and assembly names in such a scenario allows to preserve compatibility
/// across assembly versions.
/// </remarks>
internal static string GetInvariantInstantiationInfo(this Type type)
{
string assemblyQualifiedName = type.AssemblyQualifiedName;
if (assemblyQualifiedName == null)
throw new ArgumentException($"{type}'s assembly-qualified name is null. Ensure that it is a concrete type and not a generic type parameter.", nameof(type));
return string.Join(',', assemblyQualifiedName.Split(',').Take(2));
}
}
}

View File

@ -26,6 +26,7 @@ using JetBrains.Annotations;
using osu.Framework.Extensions; using osu.Framework.Extensions;
using osu.Framework.Extensions.EnumExtensions; using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Game.Extensions;
using osu.Game.Rulesets.Filter; using osu.Game.Rulesets.Filter;
using osu.Game.Screens.Ranking.Statistics; using osu.Game.Screens.Ranking.Statistics;
@ -135,7 +136,7 @@ namespace osu.Game.Rulesets
Name = Description, Name = Description,
ShortName = ShortName, ShortName = ShortName,
ID = (this as ILegacyRuleset)?.LegacyID, ID = (this as ILegacyRuleset)?.LegacyID,
InstantiationInfo = GetType().AssemblyQualifiedName, InstantiationInfo = GetType().GetInvariantInstantiationInfo(),
Available = true, Available = true,
}; };
} }

View File

@ -3,7 +3,6 @@
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
using osu.Framework.Testing; using osu.Framework.Testing;
@ -18,20 +17,7 @@ namespace osu.Game.Rulesets
public string ShortName { get; set; } public string ShortName { get; set; }
private string instantiationInfo; public string InstantiationInfo { get; set; }
public string InstantiationInfo
{
get => instantiationInfo;
set => instantiationInfo = abbreviateInstantiationInfo(value);
}
private string abbreviateInstantiationInfo(string value)
{
// exclude version onwards, matching only on namespace and type.
// this is mainly to allow for new versions of already loaded rulesets to "upgrade" from old.
return string.Join(',', value.Split(',').Take(2));
}
[JsonIgnore] [JsonIgnore]
public bool Available { get; set; } public bool Available { get; set; }

View File

@ -3,7 +3,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using osu.Framework.IO.Stores; using osu.Framework.IO.Stores;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Database; using osu.Game.Database;
@ -25,20 +24,7 @@ namespace osu.Game.Skinning
public string Creator { get; set; } public string Creator { get; set; }
private string instantiationInfo; public string InstantiationInfo { get; set; }
public string InstantiationInfo
{
get => instantiationInfo;
set => instantiationInfo = abbreviateInstantiationInfo(value);
}
private string abbreviateInstantiationInfo(string value)
{
// exclude version onwards, matching only on namespace and type.
// this is mainly to allow for new versions of already loaded rulesets to "upgrade" from old.
return string.Join(',', value.Split(',').Take(2));
}
public virtual Skin CreateInstance(IResourceStore<byte[]> legacyDefaultResources, IStorageResourceProvider resources) public virtual Skin CreateInstance(IResourceStore<byte[]> legacyDefaultResources, IStorageResourceProvider resources)
{ {

View File

@ -22,6 +22,7 @@ using osu.Framework.Testing;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Audio; using osu.Game.Audio;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.IO; using osu.Game.IO;
using osu.Game.IO.Archives; using osu.Game.IO.Archives;
@ -124,7 +125,7 @@ namespace osu.Game.Skinning
var instance = GetSkin(model); var instance = GetSkin(model);
model.InstantiationInfo ??= instance.GetType().AssemblyQualifiedName; model.InstantiationInfo ??= instance.GetType().GetInvariantInstantiationInfo();
if (model.Name?.Contains(".osk", StringComparison.OrdinalIgnoreCase) == true) if (model.Name?.Contains(".osk", StringComparison.OrdinalIgnoreCase) == true)
populateMetadata(model, instance); populateMetadata(model, instance);