-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
RulesetInfo.cs
116 lines (89 loc) · 3.5 KB
/
RulesetInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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
namespace osu.Game.Rulesets
{
[ExcludeFromDynamicCompile]
[MapTo("Ruleset")]
public class RulesetInfo : RealmObject, IEquatable<RulesetInfo>, IComparable<RulesetInfo>, IRulesetInfo
{
[PrimaryKey]
public string ShortName { get; set; } = string.Empty;
[Indexed]
public int OnlineID { get; set; } = -1;
public string Name { get; set; } = string.Empty;
public string InstantiationInfo { get; set; } = string.Empty;
public RulesetInfo(string shortName, string name, string instantiationInfo, int onlineID)
{
ShortName = shortName;
Name = name;
InstantiationInfo = instantiationInfo;
OnlineID = onlineID;
}
[UsedImplicitly]
public RulesetInfo()
{
}
public bool Available { get; set; }
public bool Equals(RulesetInfo? other)
{
if (ReferenceEquals(this, other)) return true;
if (other == null) return false;
return ShortName == other.ShortName;
}
public bool Equals(IRulesetInfo? other) => other is RulesetInfo r && Equals(r);
public int CompareTo(RulesetInfo other)
{
if (OnlineID >= 0 && other.OnlineID >= 0)
return OnlineID.CompareTo(other.OnlineID);
// Official rulesets are always given precedence for the time being.
if (OnlineID >= 0)
return -1;
if (other.OnlineID >= 0)
return 1;
return string.Compare(ShortName, other.ShortName, StringComparison.Ordinal);
}
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);
}
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();
}
public override string ToString() => Name;
public RulesetInfo Clone() => new RulesetInfo
{
OnlineID = OnlineID,
Name = Name,
ShortName = ShortName,
InstantiationInfo = InstantiationInfo,
Available = Available
};
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;
}
}
}