Add support for persisting score's mods to realm

This commit is contained in:
Dean Herbert 2022-01-10 14:18:34 +09:00
parent af5d3af664
commit ae8f522c20

View File

@ -183,6 +183,10 @@ namespace osu.Game.Scoring
[Ignored] [Ignored]
public bool IsLegacyScore => Mods.OfType<ModClassic>().Any(); public bool IsLegacyScore => Mods.OfType<ModClassic>().Any();
// Used for database serialisation/deserialisation.
[MapTo("Mods")]
public string ModsJson { get; set; } = string.Empty;
[Ignored] [Ignored]
public Mod[] Mods public Mod[] Mods
{ {
@ -203,6 +207,7 @@ namespace osu.Game.Scoring
{ {
localAPIMods = null; localAPIMods = null;
mods = value; mods = value;
ModsJson = JsonConvert.SerializeObject(APIMods);
} }
} }
@ -212,13 +217,18 @@ namespace osu.Game.Scoring
{ {
get get
{ {
if (localAPIMods != null) if (localAPIMods == null)
return localAPIMods; {
// prioritise reading from realm backing
if (!string.IsNullOrEmpty(ModsJson))
localAPIMods = JsonConvert.DeserializeObject<APIMod[]>(ModsJson);
if (mods == null) // then check mods set via Mods property.
return Array.Empty<APIMod>(); if (mods != null)
localAPIMods = mods.Select(m => new APIMod(m)).ToArray();
}
return localAPIMods = mods.Select(m => new APIMod(m)).ToArray(); return localAPIMods ?? Array.Empty<APIMod>();
} }
set set
{ {
@ -226,6 +236,7 @@ namespace osu.Game.Scoring
// We potentially can't update this yet due to Ruleset being late-bound, so instead update on read as necessary. // We potentially can't update this yet due to Ruleset being late-bound, so instead update on read as necessary.
mods = null; mods = null;
ModsJson = JsonConvert.SerializeObject(APIMods);
} }
} }