2020-03-23 02:07:09 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 08:43:03 +00:00
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
2020-03-22 22:49:45 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-08-13 10:48:31 +00:00
|
|
|
|
using System.Diagnostics;
|
2020-03-22 22:49:45 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2018-11-28 04:12:29 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2020-03-22 22:49:45 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2019-03-27 10:29:27 +00:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2020-09-04 11:34:26 +00:00
|
|
|
|
using osu.Framework.Testing;
|
2020-03-22 22:49:45 +00:00
|
|
|
|
using osu.Game.Configuration;
|
2018-11-28 04:12:29 +00:00
|
|
|
|
using osu.Game.IO.Serialization;
|
2020-03-19 03:43:26 +00:00
|
|
|
|
using osu.Game.Rulesets.UI;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Mods
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The base class for gameplay modifiers.
|
|
|
|
|
/// </summary>
|
2020-09-04 11:34:26 +00:00
|
|
|
|
[ExcludeFromDynamicCompile]
|
2018-11-30 08:16:00 +00:00
|
|
|
|
public abstract class Mod : IMod, IJsonSerializable
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The name of this mod.
|
|
|
|
|
/// </summary>
|
2018-11-28 04:12:29 +00:00
|
|
|
|
[JsonIgnore]
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public abstract string Name { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The shortened name of this mod.
|
|
|
|
|
/// </summary>
|
2018-11-30 08:16:00 +00:00
|
|
|
|
public abstract string Acronym { get; }
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The icon of this mod.
|
|
|
|
|
/// </summary>
|
2018-11-28 04:12:29 +00:00
|
|
|
|
[JsonIgnore]
|
2020-01-14 13:22:00 +00:00
|
|
|
|
public virtual IconUsage? Icon => null;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The type of this mod.
|
|
|
|
|
/// </summary>
|
2018-11-28 04:12:29 +00:00
|
|
|
|
[JsonIgnore]
|
2018-07-31 09:00:42 +00:00
|
|
|
|
public virtual ModType Type => ModType.Fun;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The user readable description of this mod.
|
|
|
|
|
/// </summary>
|
2018-11-28 04:12:29 +00:00
|
|
|
|
[JsonIgnore]
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public virtual string Description => string.Empty;
|
|
|
|
|
|
2020-03-19 03:43:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The tooltip to display for this mod when used in a <see cref="ModIcon"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Differs from <see cref="Name"/>, as the value of attributes (AR, CS, etc) changeable via the mod
|
|
|
|
|
/// are displayed in the tooltip.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
[JsonIgnore]
|
2020-03-20 20:34:36 +00:00
|
|
|
|
public string IconTooltip
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-03-23 06:20:56 +00:00
|
|
|
|
string description = SettingDescription;
|
|
|
|
|
|
|
|
|
|
return string.IsNullOrEmpty(description) ? Name : $"{Name} ({description})";
|
2020-03-20 20:34:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The description of editable settings of a mod to use in the <see cref="IconTooltip"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Parentheses are added to the tooltip, surrounding the value of this property. If this property is <c>string.Empty</c>,
|
|
|
|
|
/// the tooltip will not have parentheses.
|
|
|
|
|
/// </remarks>
|
2020-03-22 22:49:45 +00:00
|
|
|
|
public virtual string SettingDescription
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var tooltipTexts = new List<string>();
|
|
|
|
|
|
|
|
|
|
foreach ((SettingSourceAttribute attr, PropertyInfo property) in this.GetOrderedSettingsSourceProperties())
|
|
|
|
|
{
|
|
|
|
|
object bindableObj = property.GetValue(this);
|
2020-03-23 06:20:56 +00:00
|
|
|
|
|
|
|
|
|
if ((bindableObj as IHasDefaultValue)?.IsDefault == true)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
tooltipTexts.Add($"{attr.Label} {bindableObj}");
|
2020-03-22 22:49:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 06:20:56 +00:00
|
|
|
|
return string.Join(", ", tooltipTexts.Where(s => !string.IsNullOrEmpty(s)));
|
2020-03-22 22:49:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-19 03:43:26 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The score multiplier of this mod.
|
|
|
|
|
/// </summary>
|
2018-11-28 04:12:29 +00:00
|
|
|
|
[JsonIgnore]
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public abstract double ScoreMultiplier { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if this mod is implemented (and playable).
|
|
|
|
|
/// </summary>
|
2018-11-28 04:12:29 +00:00
|
|
|
|
[JsonIgnore]
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public virtual bool HasImplementation => this is IApplicableMod;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns if this mod is ranked.
|
|
|
|
|
/// </summary>
|
2018-11-28 04:12:29 +00:00
|
|
|
|
[JsonIgnore]
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public virtual bool Ranked => false;
|
|
|
|
|
|
2020-01-20 16:06:36 +00:00
|
|
|
|
/// <summary>
|
2020-01-21 03:44:22 +00:00
|
|
|
|
/// Whether this mod requires configuration to apply changes to the game.
|
2020-01-20 16:06:36 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public virtual bool RequiresConfiguration => false;
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The mods this mod cannot be enabled with.
|
|
|
|
|
/// </summary>
|
2018-11-28 04:12:29 +00:00
|
|
|
|
[JsonIgnore]
|
2019-11-28 13:41:29 +00:00
|
|
|
|
public virtual Type[] IncompatibleMods => Array.Empty<Type>();
|
2019-03-14 14:39:45 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a copy of this <see cref="Mod"/> initialised to a default state.
|
|
|
|
|
/// </summary>
|
2020-08-13 10:48:31 +00:00
|
|
|
|
public virtual Mod CreateCopy()
|
|
|
|
|
{
|
|
|
|
|
var copy = (Mod)Activator.CreateInstance(GetType());
|
|
|
|
|
|
|
|
|
|
// Copy bindable values across
|
|
|
|
|
foreach (var (_, prop) in this.GetSettingsSourceProperties())
|
|
|
|
|
{
|
|
|
|
|
var origBindable = prop.GetValue(this);
|
|
|
|
|
var copyBindable = prop.GetValue(copy);
|
|
|
|
|
|
|
|
|
|
// The bindables themselves are readonly, so the value must be transferred through the Bindable<T>.Value property.
|
|
|
|
|
var valueProperty = origBindable.GetType().GetProperty(nameof(Bindable<object>.Value), BindingFlags.Public | BindingFlags.Instance);
|
|
|
|
|
Debug.Assert(valueProperty != null);
|
|
|
|
|
|
|
|
|
|
valueProperty.SetValue(copyBindable, valueProperty.GetValue(origBindable));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
|
}
|
2019-05-03 01:05:45 +00:00
|
|
|
|
|
|
|
|
|
public bool Equals(IMod other) => GetType() == other?.GetType();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|