Don't serialise empty mod settings

This commit is contained in:
Dan Balasescu 2022-08-04 19:15:28 +09:00
parent 55234e8069
commit 0de00e9b3f
2 changed files with 15 additions and 5 deletions

View File

@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
@ -148,6 +149,16 @@ public void TestAPIModDetachedFromSource()
Assert.That(apiMod.Settings["speed_change"], Is.EqualTo(1.01d));
}
[Test]
public void TestSerialisedModSettingPresence()
{
var mod = new TestMod();
mod.TestSetting.Value = mod.TestSetting.Default;
JObject serialised = JObject.Parse(JsonConvert.SerializeObject(new APIMod(mod)));
Assert.False(serialised.ContainsKey("settings"));
}
private class TestRuleset : Ruleset
{
public override IEnumerable<Mod> GetModsFor(ModType type) => new Mod[]

View File

@ -1,11 +1,8 @@
// 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.
#nullable disable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using MessagePack;
using Newtonsoft.Json;
@ -23,7 +20,7 @@ public class APIMod : IEquatable<APIMod>
{
[JsonProperty("acronym")]
[Key(0)]
public string Acronym { get; set; }
public string Acronym { get; set; } = string.Empty;
[JsonProperty("settings")]
[Key(1)]
@ -49,7 +46,7 @@ public APIMod(Mod mod)
}
}
public Mod ToMod([NotNull] Ruleset ruleset)
public Mod ToMod(Ruleset ruleset)
{
Mod resultMod = ruleset.CreateModFromAcronym(Acronym);
@ -80,6 +77,8 @@ public Mod ToMod([NotNull] Ruleset ruleset)
return resultMod;
}
public bool ShouldSerializeSettings() => Settings.Count > 0;
public bool Equals(APIMod other)
{
if (ReferenceEquals(null, other)) return false;