2020-01-17 04:27:47 +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.
|
|
|
|
|
2020-01-22 11:00:36 +00:00
|
|
|
using System;
|
2020-01-17 04:27:47 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Humanizer;
|
2021-01-26 07:26:03 +00:00
|
|
|
using MessagePack;
|
2020-01-17 04:27:47 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Rulesets.Mods;
|
2021-04-12 17:10:22 +00:00
|
|
|
using osu.Game.Utils;
|
2020-01-17 04:27:47 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
{
|
2021-01-26 07:26:03 +00:00
|
|
|
[MessagePackObject]
|
2020-12-26 23:42:13 +00:00
|
|
|
public class APIMod : IMod, IEquatable<APIMod>
|
2020-01-17 04:27:47 +00:00
|
|
|
{
|
|
|
|
[JsonProperty("acronym")]
|
2021-01-26 07:26:03 +00:00
|
|
|
[Key(0)]
|
2020-01-17 04:27:47 +00:00
|
|
|
public string Acronym { get; set; }
|
|
|
|
|
|
|
|
[JsonProperty("settings")]
|
2021-01-26 07:26:03 +00:00
|
|
|
[Key(1)]
|
2021-02-03 10:46:47 +00:00
|
|
|
[MessagePackFormatter(typeof(ModSettingsDictionaryFormatter))]
|
2020-01-17 04:27:47 +00:00
|
|
|
public Dictionary<string, object> Settings { get; set; } = new Dictionary<string, object>();
|
|
|
|
|
|
|
|
[JsonConstructor]
|
2021-01-26 07:26:03 +00:00
|
|
|
[SerializationConstructor]
|
|
|
|
public APIMod()
|
2020-01-17 04:27:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public APIMod(Mod mod)
|
|
|
|
{
|
|
|
|
Acronym = mod.Acronym;
|
|
|
|
|
|
|
|
foreach (var (_, property) in mod.GetSettingsSourceProperties())
|
2021-01-03 06:47:15 +00:00
|
|
|
{
|
|
|
|
var bindable = (IBindable)property.GetValue(mod);
|
|
|
|
|
|
|
|
if (!bindable.IsDefault)
|
2021-01-03 08:04:16 +00:00
|
|
|
Settings.Add(property.Name.Underscore(), bindable);
|
2021-01-03 06:47:15 +00:00
|
|
|
}
|
2020-01-17 04:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Mod ToMod(Ruleset ruleset)
|
|
|
|
{
|
|
|
|
Mod resultMod = ruleset.GetAllMods().FirstOrDefault(m => m.Acronym == Acronym);
|
|
|
|
|
|
|
|
if (resultMod == null)
|
2020-01-22 11:00:36 +00:00
|
|
|
throw new InvalidOperationException($"There is no mod in the ruleset ({ruleset.ShortName}) matching the acronym {Acronym}.");
|
2020-01-17 04:27:47 +00:00
|
|
|
|
|
|
|
foreach (var (_, property) in resultMod.GetSettingsSourceProperties())
|
|
|
|
{
|
|
|
|
if (!Settings.TryGetValue(property.Name.Underscore(), out object settingValue))
|
|
|
|
continue;
|
|
|
|
|
2021-01-03 06:48:28 +00:00
|
|
|
resultMod.CopyAdjustedSetting((IBindable)property.GetValue(resultMod), settingValue);
|
2020-01-17 04:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resultMod;
|
|
|
|
}
|
|
|
|
|
2020-12-28 12:19:28 +00:00
|
|
|
public bool Equals(IMod other) => other is APIMod them && Equals(them);
|
2021-04-12 17:10:22 +00:00
|
|
|
|
|
|
|
public bool Equals(APIMod other)
|
|
|
|
{
|
|
|
|
if (ReferenceEquals(null, other)) return false;
|
|
|
|
if (ReferenceEquals(this, other)) return true;
|
|
|
|
|
|
|
|
return Acronym == other.Acronym &&
|
|
|
|
Settings.SequenceEqual(other.Settings, ModSettingsEqualityComparer.Default);
|
|
|
|
}
|
2020-10-22 08:38:16 +00:00
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
if (Settings.Count > 0)
|
|
|
|
return $"{Acronym} ({string.Join(',', Settings.Select(kvp => $"{kvp.Key}:{kvp.Value}"))})";
|
|
|
|
|
|
|
|
return $"{Acronym}";
|
|
|
|
}
|
2021-04-12 17:10:22 +00:00
|
|
|
|
|
|
|
private class ModSettingsEqualityComparer : IEqualityComparer<KeyValuePair<string, object>>
|
|
|
|
{
|
|
|
|
public static ModSettingsEqualityComparer Default { get; } = new ModSettingsEqualityComparer();
|
|
|
|
|
|
|
|
public bool Equals(KeyValuePair<string, object> x, KeyValuePair<string, object> y)
|
|
|
|
{
|
|
|
|
object xValue = ModUtils.GetSettingUnderlyingValue(x.Value);
|
|
|
|
object yValue = ModUtils.GetSettingUnderlyingValue(y.Value);
|
|
|
|
|
|
|
|
return x.Key == y.Key && EqualityComparer<object>.Default.Equals(xValue, yValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int GetHashCode(KeyValuePair<string, object> obj) => HashCode.Combine(obj.Key, ModUtils.GetSettingUnderlyingValue(obj.Value));
|
|
|
|
}
|
2020-01-17 04:27:47 +00:00
|
|
|
}
|
|
|
|
}
|