2020-01-17 12:27:47 +08: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 19:00:36 +08:00
|
|
|
using System;
|
2020-01-17 12:27:47 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Humanizer;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Rulesets.Mods;
|
|
|
|
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
{
|
|
|
|
public class APIMod : IMod
|
|
|
|
{
|
|
|
|
[JsonProperty("acronym")]
|
|
|
|
public string Acronym { get; set; }
|
|
|
|
|
|
|
|
[JsonProperty("settings")]
|
|
|
|
public Dictionary<string, object> Settings { get; set; } = new Dictionary<string, object>();
|
|
|
|
|
|
|
|
[JsonConstructor]
|
|
|
|
private APIMod()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public APIMod(Mod mod)
|
|
|
|
{
|
|
|
|
Acronym = mod.Acronym;
|
|
|
|
|
|
|
|
foreach (var (_, property) in mod.GetSettingsSourceProperties())
|
2021-01-03 14:47:15 +08:00
|
|
|
{
|
|
|
|
var bindable = (IBindable)property.GetValue(mod);
|
|
|
|
|
|
|
|
if (!bindable.IsDefault)
|
2021-01-03 16:04:16 +08:00
|
|
|
Settings.Add(property.Name.Underscore(), bindable);
|
2021-01-03 14:47:15 +08:00
|
|
|
}
|
2020-01-17 12:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public Mod ToMod(Ruleset ruleset)
|
|
|
|
{
|
2021-01-03 15:46:24 +08:00
|
|
|
Mod resultMod = ruleset.GetAllMods().FirstOrDefault(m => m.Acronym == Acronym);
|
2020-01-17 12:27:47 +08:00
|
|
|
|
|
|
|
if (resultMod == null)
|
2020-01-22 19:00:36 +08:00
|
|
|
throw new InvalidOperationException($"There is no mod in the ruleset ({ruleset.ShortName}) matching the acronym {Acronym}.");
|
2020-01-17 12:27:47 +08:00
|
|
|
|
|
|
|
foreach (var (_, property) in resultMod.GetSettingsSourceProperties())
|
|
|
|
{
|
|
|
|
if (!Settings.TryGetValue(property.Name.Underscore(), out object settingValue))
|
|
|
|
continue;
|
|
|
|
|
2021-01-03 14:48:28 +08:00
|
|
|
resultMod.CopyAdjustedSetting((IBindable)property.GetValue(resultMod), settingValue);
|
2020-01-17 12:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return resultMod;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(IMod other) => Acronym == other?.Acronym;
|
2020-10-22 16:38:16 +08:00
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
if (Settings.Count > 0)
|
|
|
|
return $"{Acronym} ({string.Join(',', Settings.Select(kvp => $"{kvp.Key}:{kvp.Value}"))})";
|
|
|
|
|
|
|
|
return $"{Acronym}";
|
|
|
|
}
|
2020-01-17 12:27:47 +08:00
|
|
|
}
|
|
|
|
}
|