From 4aa3545b58ccda79863d8d2136904f2ff0995cfb Mon Sep 17 00:00:00 2001 From: Nicholas Johnson Date: Wed, 8 Nov 2017 22:52:34 -0800 Subject: Adding a date function --- src/SMAPI/Utilities/SDate.cs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/SMAPI/Utilities/SDate.cs') diff --git a/src/SMAPI/Utilities/SDate.cs b/src/SMAPI/Utilities/SDate.cs index 2630731a..126673eb 100644 --- a/src/SMAPI/Utilities/SDate.cs +++ b/src/SMAPI/Utilities/SDate.cs @@ -95,6 +95,12 @@ namespace StardewModdingAPI.Utilities return $"{this.Day:00} {this.Season} Y{this.Year}"; } + /// This function will return the number of days since Year 1 Spring 0 for an arbitrary date + public static int TotalNumberOfDays(SDate date) + { + return (((date.Year - 1) * 112) + (date.GetSeasonIndex() * 28) + date.Day); + } + /**** ** IEquatable ****/ -- cgit From 3a832b99bf3f82cfe39de776b5e15db6c8ddff1a Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 26 Nov 2017 14:54:32 -0500 Subject: add date.DaysSinceStart property, add unit tests, update release notes (#390) --- docs/release-notes.md | 7 +++ src/SMAPI.Tests/Utilities/SDateTests.cs | 53 ++++++++++++++++++++ src/SMAPI/Framework/Models/ManifestDependency.cs | 1 + src/SMAPI/Utilities/SDate.cs | 64 ++++++++++++++---------- 4 files changed, 98 insertions(+), 27 deletions(-) (limited to 'src/SMAPI/Utilities/SDate.cs') diff --git a/docs/release-notes.md b/docs/release-notes.md index 1a9e4681..a74b1927 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1,4 +1,11 @@ # Release notes +## 2.2 +* For players: + * Fixed mods crashing when loading a custom asset on Linux/Mac. + +* For modders: + * Added `DaysSinceStart` property to `SDate` dates. + ## 2.1 * For players: * Added a log parser at [log.smapi.io](https://log.smapi.io). diff --git a/src/SMAPI.Tests/Utilities/SDateTests.cs b/src/SMAPI.Tests/Utilities/SDateTests.cs index 86a0d3d0..b89d8857 100644 --- a/src/SMAPI.Tests/Utilities/SDateTests.cs +++ b/src/SMAPI.Tests/Utilities/SDateTests.cs @@ -81,6 +81,59 @@ namespace StardewModdingAPI.Tests.Utilities Assert.Throws(() => _ = new SDate(day, season, year), "Constructing the invalid date didn't throw the expected exception."); } + /**** + ** DayOfWeek + ****/ + [Test(Description = "Assert the day of week.")] + [TestCase("01 spring Y1", ExpectedResult = System.DayOfWeek.Monday)] + [TestCase("02 spring Y2", ExpectedResult = System.DayOfWeek.Tuesday)] + [TestCase("03 spring Y3", ExpectedResult = System.DayOfWeek.Wednesday)] + [TestCase("04 spring Y4", ExpectedResult = System.DayOfWeek.Thursday)] + [TestCase("05 spring Y5", ExpectedResult = System.DayOfWeek.Friday)] + [TestCase("06 spring Y6", ExpectedResult = System.DayOfWeek.Saturday)] + [TestCase("07 spring Y7", ExpectedResult = System.DayOfWeek.Sunday)] + [TestCase("08 summer Y8", ExpectedResult = System.DayOfWeek.Monday)] + [TestCase("09 summer Y9", ExpectedResult = System.DayOfWeek.Tuesday)] + [TestCase("10 summer Y10", ExpectedResult = System.DayOfWeek.Wednesday)] + [TestCase("11 summer Y11", ExpectedResult = System.DayOfWeek.Thursday)] + [TestCase("12 summer Y12", ExpectedResult = System.DayOfWeek.Friday)] + [TestCase("13 summer Y13", ExpectedResult = System.DayOfWeek.Saturday)] + [TestCase("14 summer Y14", ExpectedResult = System.DayOfWeek.Sunday)] + [TestCase("15 fall Y15", ExpectedResult = System.DayOfWeek.Monday)] + [TestCase("16 fall Y16", ExpectedResult = System.DayOfWeek.Tuesday)] + [TestCase("17 fall Y17", ExpectedResult = System.DayOfWeek.Wednesday)] + [TestCase("18 fall Y18", ExpectedResult = System.DayOfWeek.Thursday)] + [TestCase("19 fall Y19", ExpectedResult = System.DayOfWeek.Friday)] + [TestCase("20 fall Y20", ExpectedResult = System.DayOfWeek.Saturday)] + [TestCase("21 fall Y21", ExpectedResult = System.DayOfWeek.Sunday)] + [TestCase("22 winter Y22", ExpectedResult = System.DayOfWeek.Monday)] + [TestCase("23 winter Y23", ExpectedResult = System.DayOfWeek.Tuesday)] + [TestCase("24 winter Y24", ExpectedResult = System.DayOfWeek.Wednesday)] + [TestCase("25 winter Y25", ExpectedResult = System.DayOfWeek.Thursday)] + [TestCase("26 winter Y26", ExpectedResult = System.DayOfWeek.Friday)] + [TestCase("27 winter Y27", ExpectedResult = System.DayOfWeek.Saturday)] + [TestCase("28 winter Y28" + "", ExpectedResult = System.DayOfWeek.Sunday)] + public DayOfWeek DayOfWeek(string dateStr) + { + // act + return this.GetDate(dateStr).DayOfWeek; + } + + /**** + ** DaysSinceStart + ****/ + [Test(Description = "Assert the number of days since 01 spring Y1 (inclusive).")] + [TestCase("01 spring Y1", ExpectedResult = 1)] + [TestCase("02 spring Y1", ExpectedResult = 2)] + [TestCase("28 spring Y1", ExpectedResult = 28)] + [TestCase("01 summer Y1", ExpectedResult = 29)] + [TestCase("01 summer Y2", ExpectedResult = 141)] + public int DaysSinceStart(string dateStr) + { + // act + return this.GetDate(dateStr).DaysSinceStart; + } + /**** ** ToString ****/ diff --git a/src/SMAPI/Framework/Models/ManifestDependency.cs b/src/SMAPI/Framework/Models/ManifestDependency.cs index 5646b335..97f0775a 100644 --- a/src/SMAPI/Framework/Models/ManifestDependency.cs +++ b/src/SMAPI/Framework/Models/ManifestDependency.cs @@ -15,6 +15,7 @@ namespace StardewModdingAPI.Framework.Models /// Whether the dependency must be installed to use the mod. public bool IsRequired { get; set; } + /********* ** Public methods *********/ diff --git a/src/SMAPI/Utilities/SDate.cs b/src/SMAPI/Utilities/SDate.cs index 126673eb..e589e9a4 100644 --- a/src/SMAPI/Utilities/SDate.cs +++ b/src/SMAPI/Utilities/SDate.cs @@ -35,6 +35,9 @@ namespace StardewModdingAPI.Utilities /// The day of week. public DayOfWeek DayOfWeek { get; } + /// The number of days since the game began (starting at 1 for the first day of spring in Y1). + public int DaysSinceStart { get; } + /********* ** Public methods @@ -67,7 +70,7 @@ namespace StardewModdingAPI.Utilities public SDate AddDays(int offset) { // get new hash code - int hashCode = this.GetHashCode() + offset; + int hashCode = this.DaysSinceStart + offset; if (hashCode < 1) throw new ArithmeticException($"Adding {offset} days to {this} would result in a date before 01 spring Y1."); @@ -95,12 +98,6 @@ namespace StardewModdingAPI.Utilities return $"{this.Day:00} {this.Season} Y{this.Year}"; } - /// This function will return the number of days since Year 1 Spring 0 for an arbitrary date - public static int TotalNumberOfDays(SDate date) - { - return (((date.Year - 1) * 112) + (date.GetSeasonIndex() * 28) + date.Day); - } - /**** ** IEquatable ****/ @@ -121,12 +118,7 @@ namespace StardewModdingAPI.Utilities /// Get a hash code which uniquely identifies a date. public override int GetHashCode() { - // return the number of days since 01 spring Y1 (inclusively) - int yearIndex = this.Year - 1; - return - yearIndex * this.DaysInSeason * this.SeasonsInYear - + this.GetSeasonIndex() * this.DaysInSeason - + this.Day; + return this.DaysSinceStart; } /**** @@ -138,7 +130,7 @@ namespace StardewModdingAPI.Utilities /// The equality of the dates public static bool operator ==(SDate date, SDate other) { - return date?.GetHashCode() == other?.GetHashCode(); + return date?.DaysSinceStart == other?.DaysSinceStart; } /// Get whether one date is not equal to another. @@ -146,7 +138,7 @@ namespace StardewModdingAPI.Utilities /// The other date to compare. public static bool operator !=(SDate date, SDate other) { - return date?.GetHashCode() != other?.GetHashCode(); + return date?.DaysSinceStart != other?.DaysSinceStart; } /// Get whether one date is more than another. @@ -154,7 +146,7 @@ namespace StardewModdingAPI.Utilities /// The other date to compare. public static bool operator >(SDate date, SDate other) { - return date?.GetHashCode() > other?.GetHashCode(); + return date?.DaysSinceStart > other?.DaysSinceStart; } /// Get whether one date is more than or equal to another. @@ -162,7 +154,7 @@ namespace StardewModdingAPI.Utilities /// The other date to compare. public static bool operator >=(SDate date, SDate other) { - return date?.GetHashCode() >= other?.GetHashCode(); + return date?.DaysSinceStart >= other?.DaysSinceStart; } /// Get whether one date is less than or equal to another. @@ -170,7 +162,7 @@ namespace StardewModdingAPI.Utilities /// The other date to compare. public static bool operator <=(SDate date, SDate other) { - return date?.GetHashCode() <= other?.GetHashCode(); + return date?.DaysSinceStart <= other?.DaysSinceStart; } /// Get whether one date is less than another. @@ -178,7 +170,7 @@ namespace StardewModdingAPI.Utilities /// The other date to compare. public static bool operator <(SDate date, SDate other) { - return date?.GetHashCode() < other?.GetHashCode(); + return date?.DaysSinceStart < other?.DaysSinceStart; } @@ -209,7 +201,9 @@ namespace StardewModdingAPI.Utilities this.Day = day; this.Season = season; this.Year = year; - this.DayOfWeek = this.GetDayOfWeek(); + this.DayOfWeek = this.GetDayOfWeek(day); + this.DaysSinceStart = this.GetDaysSinceStart(day, season, year); + } /// Get whether a date represents 0 spring Y1, which is the date during the in-game intro. @@ -221,10 +215,11 @@ namespace StardewModdingAPI.Utilities return day == 0 && season == "spring" && year == 1; } - /// Get the day of week for the current date. - private DayOfWeek GetDayOfWeek() + /// Get the day of week for a given date. + /// The day of month. + private DayOfWeek GetDayOfWeek(int day) { - switch (this.Day % 7) + switch (day % 7) { case 0: return DayOfWeek.Sunday; @@ -245,13 +240,28 @@ namespace StardewModdingAPI.Utilities } } - /// Get the current season index. + /// Get the number of days since the game began (starting at 1 for the first day of spring in Y1). + /// The day of month. + /// The season name. + /// The year. + private int GetDaysSinceStart(int day, string season, int year) + { + // return the number of days since 01 spring Y1 (inclusively) + int yearIndex = year - 1; + return + yearIndex * this.DaysInSeason * this.SeasonsInYear + + this.GetSeasonIndex(season) * this.DaysInSeason + + day; + } + + /// Get a season index. + /// The season name. /// The current season wasn't recognised. - private int GetSeasonIndex() + private int GetSeasonIndex(string season) { - int index = Array.IndexOf(this.Seasons, this.Season); + int index = Array.IndexOf(this.Seasons, season); if (index == -1) - throw new InvalidOperationException($"The current season '{this.Season}' wasn't recognised."); + throw new InvalidOperationException($"The season '{season}' wasn't recognised."); return index; } } -- cgit