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
2017-03-12 13:13:43 +00:00
using System ;
2020-03-22 22:49:45 +00:00
using System.Collections.Generic ;
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 ;
2021-04-16 06:10:53 +00:00
using osu.Framework.Extensions.TypeExtensions ;
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 ;
2020-03-19 03:43:26 +00:00
using osu.Game.Rulesets.UI ;
2021-04-12 17:10:22 +00:00
using osu.Game.Utils ;
2018-04-13 09:19:50 +00:00
2017-04-18 07:05:58 +00:00
namespace osu.Game.Rulesets.Mods
2017-02-16 20:05:03 +00:00
{
2017-02-23 11:59:27 +00:00
/// <summary>
/// The base class for gameplay modifiers.
/// </summary>
2020-09-04 11:34:26 +00:00
[ExcludeFromDynamicCompile]
2021-08-31 05:38:35 +00:00
public abstract class Mod : IMod , IEquatable < Mod > , IDeepCloneable < Mod >
2017-02-16 20:05:03 +00:00
{
2018-11-28 04:12:29 +00:00
[JsonIgnore]
2017-03-06 09:28:30 +00:00
public abstract string Name { get ; }
2018-04-13 09:19:50 +00:00
2018-11-30 08:16:00 +00:00
public abstract string Acronym { get ; }
2018-04-13 09:19:50 +00:00
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
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
2018-11-28 04:12:29 +00:00
[JsonIgnore]
2021-04-15 05:32:01 +00:00
public abstract string Description { get ; }
2018-04-13 09:19:50 +00:00
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 ( ) )
{
2021-01-03 06:47:15 +00:00
var bindable = ( IBindable ) property . GetValue ( this ) ;
2020-03-23 06:20:56 +00:00
2021-01-03 06:47:15 +00:00
if ( ! bindable . IsDefault )
tooltipTexts . Add ( $"{attr.Label} {bindable}" ) ;
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
2017-02-23 11:57:58 +00:00
/// <summary>
2022-02-17 04:26:12 +00:00
/// The score multiplier of this mod.
2017-02-23 11:57:58 +00:00
/// </summary>
2018-11-28 04:12:29 +00:00
[JsonIgnore]
2022-02-17 04:26:12 +00:00
public abstract double ScoreMultiplier { get ; }
2018-04-13 09:19:50 +00:00
2017-12-30 19:09:51 +00:00
/// <summary>
/// Returns true if this mod is implemented (and playable).
/// </summary>
2018-11-28 04:12:29 +00:00
[JsonIgnore]
2017-12-30 19:09:51 +00:00
public virtual bool HasImplementation = > this is IApplicableMod ;
2018-04-13 09:19:50 +00:00
2018-11-28 04:12:29 +00:00
[JsonIgnore]
2021-06-09 05:17:01 +00:00
public virtual bool UserPlayable = > true ;
2021-07-02 07:55:25 +00:00
[Obsolete("Going forward, the concept of \"ranked\" doesn't exist. The only exceptions are automation mods, which should now override and set UserPlayable to false.")] // Can be removed 20211009
2021-07-02 03:50:53 +00:00
public virtual bool Ranked = > false ;
2018-04-13 09:19:50 +00:00
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 ;
2017-02-23 11:57:58 +00:00
/// <summary>
2017-03-02 00:57:33 +00:00
/// The mods this mod cannot be enabled with.
2017-02-23 11:57:58 +00:00
/// </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
2021-08-18 06:17:42 +00:00
private IReadOnlyList < IBindable > settingsBacking ;
/// <summary>
/// A list of the all <see cref="IBindable"/> settings within this mod.
/// </summary>
internal IReadOnlyList < IBindable > Settings = >
settingsBacking ? ? = this . GetSettingsSourceProperties ( )
. Select ( p = > p . Item2 . GetValue ( this ) )
. Cast < IBindable > ( )
. ToList ( ) ;
2019-03-14 14:39:45 +00:00
/// <summary>
/// Creates a copy of this <see cref="Mod"/> initialised to a default state.
/// </summary>
2021-07-19 03:38:22 +00:00
public virtual Mod DeepClone ( )
2020-08-13 10:48:31 +00:00
{
2021-01-01 12:40:40 +00:00
var result = ( Mod ) Activator . CreateInstance ( GetType ( ) ) ;
2021-01-01 00:46:09 +00:00
result . CopyFrom ( this ) ;
return result ;
}
/// <summary>
2021-02-09 04:44:11 +00:00
/// Copies mod setting values from <paramref name="source"/> into this instance, overwriting all existing settings.
2021-01-01 00:46:09 +00:00
/// </summary>
2021-01-10 14:35:53 +00:00
/// <param name="source">The mod to copy properties from.</param>
public void CopyFrom ( Mod source )
2021-01-01 00:46:09 +00:00
{
2021-01-10 14:35:53 +00:00
if ( source . GetType ( ) ! = GetType ( ) )
throw new ArgumentException ( $"Expected mod of type {GetType()}, got {source.GetType()}." , nameof ( source ) ) ;
2020-08-13 10:48:31 +00:00
foreach ( var ( _ , prop ) in this . GetSettingsSourceProperties ( ) )
{
2021-01-08 21:30:12 +00:00
var targetBindable = ( IBindable ) prop . GetValue ( this ) ;
2021-01-10 14:35:53 +00:00
var sourceBindable = ( IBindable ) prop . GetValue ( source ) ;
2020-08-13 10:48:31 +00:00
2021-02-09 04:44:11 +00:00
CopyAdjustedSetting ( targetBindable , sourceBindable ) ;
2020-08-13 10:48:31 +00:00
}
}
2021-01-03 06:48:28 +00:00
/// <summary>
2021-01-03 12:30:21 +00:00
/// When creating copies or clones of a Mod, this method will be called
/// to copy explicitly adjusted user settings from <paramref name="target"/>.
/// The base implementation will transfer the value via <see cref="Bindable{T}.Parse"/>
/// or by binding and unbinding (if <paramref name="source"/> is an <see cref="IBindable"/>)
/// and should be called unless replaced with custom logic.
2021-01-03 06:48:28 +00:00
/// </summary>
2021-01-03 12:30:21 +00:00
/// <param name="target">The target bindable to apply the adjustment to.</param>
2021-01-03 12:25:44 +00:00
/// <param name="source">The adjustment to apply.</param>
internal virtual void CopyAdjustedSetting ( IBindable target , object source )
2021-01-03 11:44:57 +00:00
{
2021-01-03 12:25:44 +00:00
if ( source is IBindable sourceBindable )
2021-01-03 11:44:57 +00:00
{
2021-01-03 12:30:21 +00:00
// copy including transfer of default values.
2021-01-03 12:25:44 +00:00
target . BindTo ( sourceBindable ) ;
target . UnbindFrom ( sourceBindable ) ;
2020-08-13 10:48:31 +00:00
}
2021-01-03 11:44:57 +00:00
else
2021-04-16 06:10:53 +00:00
{
if ( ! ( target is IParseable parseable ) )
2021-04-16 06:40:06 +00:00
throw new InvalidOperationException ( $"Bindable type {target.GetType().ReadableName()} is not {nameof(IParseable)}." ) ;
2021-04-16 06:10:53 +00:00
parseable . Parse ( source ) ;
}
2020-08-13 10:48:31 +00:00
}
2019-05-03 01:05:45 +00:00
2020-12-28 12:19:28 +00:00
public bool Equals ( IMod other ) = > other is Mod them & & Equals ( them ) ;
2021-04-12 17:10:22 +00:00
public bool Equals ( Mod other )
{
if ( ReferenceEquals ( null , other ) ) return false ;
if ( ReferenceEquals ( this , other ) ) return true ;
return GetType ( ) = = other . GetType ( ) & &
2021-08-18 06:17:42 +00:00
Settings . SequenceEqual ( other . Settings , ModSettingsEqualityComparer . Default ) ;
2021-04-12 17:10:22 +00:00
}
2021-02-09 04:44:42 +00:00
2021-08-17 01:27:04 +00:00
public override int GetHashCode ( )
{
var hashCode = new HashCode ( ) ;
hashCode . Add ( GetType ( ) ) ;
2021-08-18 06:17:42 +00:00
foreach ( var setting in Settings )
2022-03-15 05:53:50 +00:00
hashCode . Add ( setting . GetUnderlyingSettingValue ( ) ) ;
2021-08-17 01:27:04 +00:00
return hashCode . ToHashCode ( ) ;
}
2021-02-09 04:44:42 +00:00
/// <summary>
/// Reset all custom settings for this mod back to their defaults.
/// </summary>
public virtual void ResetSettingsToDefaults ( ) = > CopyFrom ( ( Mod ) Activator . CreateInstance ( GetType ( ) ) ) ;
2021-08-18 06:17:42 +00:00
private class ModSettingsEqualityComparer : IEqualityComparer < IBindable >
{
public static ModSettingsEqualityComparer Default { get ; } = new ModSettingsEqualityComparer ( ) ;
public bool Equals ( IBindable x , IBindable y )
{
2022-03-15 05:53:50 +00:00
object xValue = x ? . GetUnderlyingSettingValue ( ) ;
object yValue = y ? . GetUnderlyingSettingValue ( ) ;
2021-08-18 06:17:42 +00:00
return EqualityComparer < object > . Default . Equals ( xValue , yValue ) ;
}
2022-03-15 05:53:50 +00:00
public int GetHashCode ( IBindable obj ) = > obj . GetUnderlyingSettingValue ( ) . GetHashCode ( ) ;
2021-08-18 06:17:42 +00:00
}
2017-02-18 11:44:46 +00:00
}
2017-03-02 00:57:33 +00:00
}