using System;
using Newtonsoft.Json;
using StardewModdingAPI.Framework;
namespace StardewModdingAPI
{
/// A semantic version with an optional release tag.
[Obsolete("Use " + nameof(SemanticVersion) + " or " + nameof(Manifest) + "." + nameof(Manifest.Version) + " instead")]
public struct Version : ISemanticVersion
{
/*********
** Accessors
*********/
/// The major version incremented for major API changes.
public int MajorVersion { get; set; }
/// The minor version incremented for backwards-compatible changes.
public int MinorVersion { get; set; }
/// The patch version for backwards-compatible bug fixes.
public int PatchVersion { get; set; }
/// An optional build tag.
public string Build { get; set; }
/// Obsolete.
[JsonIgnore]
[Obsolete("Use " + nameof(Version) + "." + nameof(Version.ToString) + " instead.")]
public string VersionString
{
get
{
Program.DeprecationManager.Warn($"{nameof(Version)}.{nameof(Version.VersionString)}", "1.0", DeprecationLevel.Info);
return this.GetSemanticVersion().ToString();
}
}
/*********
** Public methods
*********/
/// Construct an instance.
/// The major version incremented for major API changes.
/// The minor version incremented for backwards-compatible changes.
/// The patch version for backwards-compatible bug fixes.
/// An optional build tag.
public Version(int major, int minor, int patch, string build)
: this(major, minor, patch, build, suppressDeprecationWarning: false)
{ }
/// Get an integer indicating whether this version precedes (less than 0), supercedes (more than 0), or is equivalent to (0) the specified version.
/// The version to compare with this instance.
public int CompareTo(Version other)
{
return this.GetSemanticVersion().CompareTo(other);
}
/// Get whether this version is newer than the specified version.
/// The version to compare with this instance.
[Obsolete("Use " + nameof(ISemanticVersion) + "." + nameof(ISemanticVersion.IsNewerThan) + " instead")]
public bool IsNewerThan(Version other)
{
return this.GetSemanticVersion().IsNewerThan(other);
}
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes in the sort order. Zero This instance occurs in the same position in the sort order as . Greater than zero This instance follows in the sort order.
/// An object to compare with this instance.
int IComparable.CompareTo(ISemanticVersion other)
{
return this.GetSemanticVersion().CompareTo(other);
}
/// Get whether this version is older than the specified version.
/// The version to compare with this instance.
bool ISemanticVersion.IsOlderThan(ISemanticVersion other)
{
return this.GetSemanticVersion().IsOlderThan(other);
}
/// Get whether this version is newer than the specified version.
/// The version to compare with this instance.
bool ISemanticVersion.IsNewerThan(ISemanticVersion other)
{
return this.GetSemanticVersion().IsNewerThan(other);
}
/// Get a string representation of the version.
public override string ToString()
{
return this.GetSemanticVersion().ToString();
}
/*********
** Private methods
*********/
/// Construct an instance.
/// The major version incremented for major API changes.
/// The minor version incremented for backwards-compatible changes.
/// The patch version for backwards-compatible bug fixes.
/// An optional build tag.
/// Whether to suppress the deprecation warning.
internal Version(int major, int minor, int patch, string build, bool suppressDeprecationWarning)
{
if (!suppressDeprecationWarning)
Program.DeprecationManager.Warn($"{nameof(Version)}", "1.5", DeprecationLevel.Notice);
this.MajorVersion = major;
this.MinorVersion = minor;
this.PatchVersion = patch;
this.Build = build;
}
/// Get the equivalent semantic version.
/// This is a hack so the struct can wrap without a mutable backing field, which would cause a due to recreating the struct value on each change.
private SemanticVersion GetSemanticVersion()
{
return new SemanticVersion(this.MajorVersion, this.MinorVersion, this.PatchVersion, this.Build);
}
}
}