2021-10-11 06:25:00 +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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using osu.Framework.Testing;
|
|
|
|
using Realms;
|
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
2021-11-19 10:07:21 +00:00
|
|
|
namespace osu.Game.Rulesets
|
2021-10-11 06:25:00 +00:00
|
|
|
{
|
|
|
|
[ExcludeFromDynamicCompile]
|
|
|
|
[MapTo("Ruleset")]
|
2022-02-11 01:00:40 +00:00
|
|
|
public class RulesetInfo : RealmObject, IEquatable<RulesetInfo>, IComparable<RulesetInfo>, IRulesetInfo
|
2021-10-11 06:25:00 +00:00
|
|
|
{
|
|
|
|
[PrimaryKey]
|
|
|
|
public string ShortName { get; set; } = string.Empty;
|
|
|
|
|
2021-10-18 06:35:51 +00:00
|
|
|
[Indexed]
|
|
|
|
public int OnlineID { get; set; } = -1;
|
2021-10-11 06:25:00 +00:00
|
|
|
|
|
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
public string InstantiationInfo { get; set; } = string.Empty;
|
|
|
|
|
2021-11-19 10:07:21 +00:00
|
|
|
public RulesetInfo(string shortName, string name, string instantiationInfo, int onlineID)
|
2021-10-11 06:25:00 +00:00
|
|
|
{
|
|
|
|
ShortName = shortName;
|
|
|
|
Name = name;
|
|
|
|
InstantiationInfo = instantiationInfo;
|
2021-11-24 06:25:49 +00:00
|
|
|
OnlineID = onlineID;
|
2021-10-11 06:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[UsedImplicitly]
|
2021-11-19 10:24:07 +00:00
|
|
|
public RulesetInfo()
|
2021-10-11 06:25:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Available { get; set; }
|
|
|
|
|
2022-01-10 07:31:50 +00:00
|
|
|
public bool Equals(RulesetInfo? other)
|
|
|
|
{
|
|
|
|
if (ReferenceEquals(this, other)) return true;
|
|
|
|
if (other == null) return false;
|
|
|
|
|
|
|
|
return ShortName == other.ShortName;
|
|
|
|
}
|
2021-10-11 06:25:00 +00:00
|
|
|
|
2022-02-11 01:00:40 +00:00
|
|
|
public bool Equals(IRulesetInfo? other) => other is RulesetInfo r && Equals(r);
|
2021-12-03 09:01:29 +00:00
|
|
|
|
2022-01-27 06:59:20 +00:00
|
|
|
public int CompareTo(RulesetInfo other)
|
|
|
|
{
|
2022-01-27 12:58:04 +00:00
|
|
|
if (OnlineID >= 0 && other.OnlineID >= 0)
|
|
|
|
return OnlineID.CompareTo(other.OnlineID);
|
|
|
|
|
2022-01-27 06:59:20 +00:00
|
|
|
// Official rulesets are always given precedence for the time being.
|
|
|
|
if (OnlineID >= 0)
|
|
|
|
return -1;
|
2022-01-27 12:58:04 +00:00
|
|
|
if (other.OnlineID >= 0)
|
|
|
|
return 1;
|
2022-01-27 06:59:20 +00:00
|
|
|
|
|
|
|
return string.Compare(ShortName, other.ShortName, StringComparison.Ordinal);
|
|
|
|
}
|
|
|
|
|
2022-02-11 01:00:40 +00:00
|
|
|
public int CompareTo(IRulesetInfo other)
|
|
|
|
{
|
|
|
|
if (!(other is RulesetInfo ruleset))
|
|
|
|
throw new ArgumentException($@"Object is not of type {nameof(RulesetInfo)}.", nameof(other));
|
|
|
|
|
|
|
|
return CompareTo(ruleset);
|
|
|
|
}
|
|
|
|
|
2022-01-10 07:31:50 +00:00
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
// Importantly, ignore the underlying realm hash code, as it will usually not match.
|
|
|
|
var hashCode = new HashCode();
|
|
|
|
// ReSharper disable once NonReadonlyMemberInGetHashCode
|
|
|
|
hashCode.Add(ShortName);
|
|
|
|
return hashCode.ToHashCode();
|
|
|
|
}
|
|
|
|
|
2021-10-11 06:25:00 +00:00
|
|
|
public override string ToString() => Name;
|
|
|
|
|
2021-11-19 10:07:21 +00:00
|
|
|
public RulesetInfo Clone() => new RulesetInfo
|
2021-10-11 06:25:00 +00:00
|
|
|
{
|
|
|
|
OnlineID = OnlineID,
|
|
|
|
Name = Name,
|
|
|
|
ShortName = ShortName,
|
|
|
|
InstantiationInfo = InstantiationInfo,
|
|
|
|
Available = Available
|
|
|
|
};
|
2021-11-23 11:17:07 +00:00
|
|
|
|
|
|
|
public Ruleset CreateInstance()
|
|
|
|
{
|
|
|
|
if (!Available)
|
|
|
|
throw new RulesetLoadException(@"Ruleset not available");
|
|
|
|
|
|
|
|
var type = Type.GetType(InstantiationInfo);
|
|
|
|
|
|
|
|
if (type == null)
|
|
|
|
throw new RulesetLoadException(@"Type lookup failure");
|
|
|
|
|
|
|
|
var ruleset = Activator.CreateInstance(type) as Ruleset;
|
|
|
|
|
|
|
|
if (ruleset == null)
|
|
|
|
throw new RulesetLoadException(@"Instantiation failure");
|
|
|
|
|
|
|
|
// overwrite the pre-populated RulesetInfo with a potentially database attached copy.
|
|
|
|
// TODO: figure if we still want/need this after switching to realm.
|
|
|
|
// ruleset.RulesetInfo = this;
|
|
|
|
|
|
|
|
return ruleset;
|
|
|
|
}
|
2021-10-11 06:25:00 +00:00
|
|
|
}
|
|
|
|
}
|