Fix mods not being serialised correctly in ScoreInfo

This commit is contained in:
smoogipoo
2021-04-12 19:50:24 +09:00
parent e8c248f2b2
commit d2d7f77430
2 changed files with 57 additions and 25 deletions

View File

@ -10,6 +10,7 @@ using Newtonsoft.Json.Converters;
using osu.Framework.Extensions;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Online.API;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Scoring;
@ -55,9 +56,10 @@ namespace osu.Game.Scoring
[JsonIgnore]
public virtual RulesetInfo Ruleset { get; set; }
private APIMod[] localAPIMods;
private Mod[] mods;
[JsonProperty("mods")]
[JsonIgnore]
[NotMapped]
public Mod[] Mods
{
@ -66,45 +68,50 @@ namespace osu.Game.Scoring
if (mods != null)
return mods;
if (modsJson == null)
if (apiMods == null)
return Array.Empty<Mod>();
return getModsFromRuleset(JsonConvert.DeserializeObject<DeserializedMod[]>(modsJson));
var rulesetInstance = Ruleset.CreateInstance();
return apiMods.Select(m => m.ToMod(rulesetInstance)).ToArray();
}
set
{
modsJson = null;
localAPIMods = null;
mods = value;
}
}
private Mod[] getModsFromRuleset(DeserializedMod[] mods) => Ruleset.CreateInstance().GetAllMods().Where(mod => mods.Any(d => d.Acronym == mod.Acronym)).ToArray();
private string modsJson;
[JsonIgnore]
[Column("Mods")]
public string ModsJson
// Used for API serialisation/deserialisation.
[JsonProperty("mods")]
private APIMod[] apiMods
{
get
{
if (modsJson != null)
return modsJson;
if (localAPIMods != null)
return localAPIMods;
if (mods == null)
return null;
return Array.Empty<APIMod>();
return modsJson = JsonConvert.SerializeObject(mods.Select(m => new DeserializedMod { Acronym = m.Acronym }));
return localAPIMods = mods.Select(m => new APIMod(m)).ToArray();
}
set
{
modsJson = value;
localAPIMods = value;
// 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;
}
}
// Used for database serialisation/deserialisation.
[Column("Mods")]
private string modsString
{
get => JsonConvert.SerializeObject(apiMods);
set => apiMods = JsonConvert.DeserializeObject<APIMod[]>(value);
}
[NotMapped]
[JsonProperty("user")]
public User User { get; set; }
@ -251,14 +258,6 @@ namespace osu.Game.Scoring
}
}
[Serializable]
protected class DeserializedMod : IMod
{
public string Acronym { get; set; }
public bool Equals(IMod other) => Acronym == other?.Acronym;
}
public override string ToString() => $"{User} playing {Beatmap}";
public bool Equals(ScoreInfo other)