diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-12-02 18:49:49 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-12-02 18:49:49 -0500 |
commit | 5cc5f089b9645a60385ff293b5a7202f260bfc0f (patch) | |
tree | 68f8bee734d164277f711b8ac54bd1064c0757d6 /src/SMAPI | |
parent | e0b72374cd14298aacc6f71dc391fdc9814be37c (diff) | |
parent | dc4f89acb6cd8f838934b60e8f5645c6145706f8 (diff) | |
download | SMAPI-5cc5f089b9645a60385ff293b5a7202f260bfc0f.tar.gz SMAPI-5cc5f089b9645a60385ff293b5a7202f260bfc0f.tar.bz2 SMAPI-5cc5f089b9645a60385ff293b5a7202f260bfc0f.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI')
-rw-r--r-- | src/SMAPI/App.config | 9 | ||||
-rw-r--r-- | src/SMAPI/Constants.cs | 2 | ||||
-rw-r--r-- | src/SMAPI/Events/EventArgsInput.cs | 19 | ||||
-rw-r--r-- | src/SMAPI/Framework/ModHelpers/ContentHelper.cs | 31 | ||||
-rw-r--r-- | src/SMAPI/Framework/ModLoading/ModResolver.cs | 12 | ||||
-rw-r--r-- | src/SMAPI/Framework/Models/ManifestDependency.cs | 1 | ||||
-rw-r--r-- | src/SMAPI/Framework/SContentManager.cs | 52 | ||||
-rw-r--r-- | src/SMAPI/IContentHelper.cs | 6 | ||||
-rw-r--r-- | src/SMAPI/Program.cs | 10 | ||||
-rw-r--r-- | src/SMAPI/StardewModdingAPI.config.json | 3 | ||||
-rw-r--r-- | src/SMAPI/StardewModdingAPI.csproj | 3 | ||||
-rw-r--r-- | src/SMAPI/Utilities/SDate.cs | 58 |
12 files changed, 118 insertions, 88 deletions
diff --git a/src/SMAPI/App.config b/src/SMAPI/App.config deleted file mode 100644 index 27cdf0f7..00000000 --- a/src/SMAPI/App.config +++ /dev/null @@ -1,9 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<configuration> - <startup> - <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> - </startup> - <runtime> - <loadFromRemoteSources enabled="true"/> - </runtime> -</configuration> diff --git a/src/SMAPI/Constants.cs b/src/SMAPI/Constants.cs index a2dbdd98..41b79272 100644 --- a/src/SMAPI/Constants.cs +++ b/src/SMAPI/Constants.cs @@ -29,7 +29,7 @@ namespace StardewModdingAPI ** Public ****/ /// <summary>SMAPI's current semantic version.</summary> - public static ISemanticVersion ApiVersion { get; } = new SemanticVersion(2, 1, 0); + public static ISemanticVersion ApiVersion { get; } = new SemanticVersion("2.2"); /// <summary>The minimum supported version of Stardew Valley.</summary> public static ISemanticVersion MinimumGameVersion { get; } = new SemanticVersion("1.2.30"); diff --git a/src/SMAPI/Events/EventArgsInput.cs b/src/SMAPI/Events/EventArgsInput.cs index ff904675..54ce9b53 100644 --- a/src/SMAPI/Events/EventArgsInput.cs +++ b/src/SMAPI/Events/EventArgsInput.cs @@ -56,11 +56,11 @@ namespace StardewModdingAPI.Events public void SuppressButton(SButton button) { // keyboard - if (this.Button.TryGetKeyboard(out Keys key)) + if (button.TryGetKeyboard(out Keys key)) Game1.oldKBState = new KeyboardState(Game1.oldKBState.GetPressedKeys().Union(new[] { key }).ToArray()); // controller - else if (this.Button.TryGetController(out Buttons controllerButton)) + else if (button.TryGetController(out Buttons controllerButton)) { var newState = GamePad.GetState(PlayerIndex.One); var thumbsticks = Game1.oldPadState.ThumbSticks; @@ -127,6 +127,21 @@ namespace StardewModdingAPI.Events Game1.oldPadState = new GamePadState(thumbsticks, triggers, buttons, dpad); } + + // mouse + else if (button == SButton.MouseLeft || button == SButton.MouseMiddle || button == SButton.MouseRight || button == SButton.MouseX1 || button == SButton.MouseX2) + { + Game1.oldMouseState = new MouseState( + x: Game1.oldMouseState.X, + y: Game1.oldMouseState.Y, + scrollWheel: Game1.oldMouseState.ScrollWheelValue, + leftButton: button == SButton.MouseLeft ? ButtonState.Pressed : Game1.oldMouseState.LeftButton, + middleButton: button == SButton.MouseMiddle ? ButtonState.Pressed : Game1.oldMouseState.MiddleButton, + rightButton: button == SButton.MouseRight ? ButtonState.Pressed : Game1.oldMouseState.RightButton, + xButton1: button == SButton.MouseX1 ? ButtonState.Pressed : Game1.oldMouseState.XButton1, + xButton2: button == SButton.MouseX2 ? ButtonState.Pressed : Game1.oldMouseState.XButton2 + ); + } } } } diff --git a/src/SMAPI/Framework/ModHelpers/ContentHelper.cs b/src/SMAPI/Framework/ModHelpers/ContentHelper.cs index be9594ee..4a1d3853 100644 --- a/src/SMAPI/Framework/ModHelpers/ContentHelper.cs +++ b/src/SMAPI/Framework/ModHelpers/ContentHelper.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; using System.IO; using System.Linq; using Microsoft.Xna.Framework.Content; @@ -26,9 +27,6 @@ namespace StardewModdingAPI.Framework.ModHelpers /// <summary>The absolute path to the mod folder.</summary> private readonly string ModFolderPath; - /// <summary>The path to the mod's folder, relative to the game's content folder (e.g. "../Mods/ModName").</summary> - private readonly string ModFolderPathFromContent; - /// <summary>The friendly mod name for use in errors.</summary> private readonly string ModName; @@ -73,7 +71,6 @@ namespace StardewModdingAPI.Framework.ModHelpers this.ContentManager = contentManager; this.ModFolderPath = modFolderPath; this.ModName = modName; - this.ModFolderPathFromContent = this.ContentManager.GetRelativePath(modFolderPath); this.Monitor = monitor; } @@ -102,7 +99,7 @@ namespace StardewModdingAPI.Framework.ModHelpers throw GetContentError($"there's no matching file at path '{file.FullName}'."); // get asset path - string assetName = this.GetModAssetPath(key, file.FullName); + string assetName = this.ContentManager.GetAssetNameFromFilePath(file.FullName); // try cache if (this.ContentManager.IsLoaded(assetName)) @@ -138,6 +135,14 @@ namespace StardewModdingAPI.Framework.ModHelpers } } + /// <summary>Normalise an asset name so it's consistent with those generated by the game. This is mainly useful for string comparisons like <see cref="string.StartsWith(string)"/> on generated asset names, and isn't necessary when passing asset names into other content helper methods.</summary> + /// <param name="assetName">The asset key.</param> + [Pure] + public string NormaliseAssetName(string assetName) + { + return this.ContentManager.NormaliseAssetName(assetName); + } + /// <summary>Get the underlying key in the game's content cache for an asset. This can be used to load custom map tilesheets, but should be avoided when you can use the content API instead. This does not validate whether the asset exists.</summary> /// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content file relative to the mod folder.</param> /// <param name="source">Where to search for a matching content asset.</param> @@ -151,7 +156,7 @@ namespace StardewModdingAPI.Framework.ModHelpers case ContentSource.ModFolder: FileInfo file = this.GetModFile(key); - return this.ContentManager.NormaliseAssetName(this.GetModAssetPath(key, file.FullName)); + return this.ContentManager.NormaliseAssetName(this.ContentManager.GetAssetNameFromFilePath(file.FullName)); default: throw new NotSupportedException($"Unknown content source '{source}'."); @@ -356,19 +361,5 @@ namespace StardewModdingAPI.Framework.ModHelpers // get file return new FileInfo(path); } - - /// <summary>Get the asset path which loads a mod folder through a content manager.</summary> - /// <param name="localPath">The file path relative to the mod's folder.</param> - /// <param name="absolutePath">The absolute file path.</param> - private string GetModAssetPath(string localPath, string absolutePath) - { -#if SMAPI_FOR_WINDOWS - // XNA doesn't allow absolute asset paths, so get a path relative to the content folder - return Path.Combine(this.ModFolderPathFromContent, localPath); -#else - // MonoGame is weird about relative paths on Mac, but allows absolute paths - return absolutePath; -#endif - } } } diff --git a/src/SMAPI/Framework/ModLoading/ModResolver.cs b/src/SMAPI/Framework/ModLoading/ModResolver.cs index d0ef1b08..9802d9e9 100644 --- a/src/SMAPI/Framework/ModLoading/ModResolver.cs +++ b/src/SMAPI/Framework/ModLoading/ModResolver.cs @@ -142,6 +142,18 @@ namespace StardewModdingAPI.Framework.ModLoading continue; } + // validate DLL value + if (string.IsNullOrWhiteSpace(mod.Manifest.EntryDll)) + { + mod.SetStatus(ModMetadataStatus.Failed, "its manifest has no EntryDLL field."); + continue; + } + if (mod.Manifest.EntryDll.Intersect(Path.GetInvalidFileNameChars()).Any()) + { + mod.SetStatus(ModMetadataStatus.Failed, $"its manifest has invalid filename '{mod.Manifest.EntryDll}' for the EntryDLL field."); + continue; + } + // validate DLL path string assemblyPath = Path.Combine(mod.DirectoryPath, mod.Manifest.EntryDll); if (!File.Exists(assemblyPath)) 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 /// <summary>Whether the dependency must be installed to use the mod.</summary> public bool IsRequired { get; set; } + /********* ** Public methods *********/ diff --git a/src/SMAPI/Framework/SContentManager.cs b/src/SMAPI/Framework/SContentManager.cs index a755a6df..524b2d17 100644 --- a/src/SMAPI/Framework/SContentManager.cs +++ b/src/SMAPI/Framework/SContentManager.cs @@ -102,7 +102,7 @@ namespace StardewModdingAPI.Framework this.Monitor = monitor ?? throw new ArgumentNullException(nameof(monitor)); this.Cache = new ContentCache(this, reflection, SContentManager.PossiblePathSeparators, SContentManager.PreferredPathSeparator); this.GetKeyLocale = reflection.GetPrivateMethod(this, "languageCode"); - this.ModContentPrefix = this.GetRelativePath(Constants.ModPath); + this.ModContentPrefix = this.GetAssetNameFromFilePath(Constants.ModPath); // get asset data this.CoreAssets = new CoreAssets(this.NormaliseAssetName); @@ -140,19 +140,17 @@ namespace StardewModdingAPI.Framework throw new ArgumentException("The asset key or local path contains invalid characters."); } - /// <summary>Get a directory path relative to the content root.</summary> - /// <param name="targetPath">The target file path.</param> - public string GetRelativePath(string targetPath) + /// <summary>Convert an absolute file path into a appropriate asset name.</summary> + /// <param name="absolutePath">The absolute path to the file.</param> + public string GetAssetNameFromFilePath(string absolutePath) { - // convert to URIs - Uri from = new Uri(this.FullRootDirectory + "/"); - Uri to = new Uri(targetPath + "/"); - if (from.Scheme != to.Scheme) - throw new InvalidOperationException($"Can't get path for '{targetPath}' relative to '{this.FullRootDirectory}'."); - - // get relative path - return Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString()) - .Replace(Path.DirectorySeparatorChar == '/' ? '\\' : '/', Path.DirectorySeparatorChar); // use correct separator for platform +#if SMAPI_FOR_WINDOWS + // XNA doesn't allow absolute asset paths, so get a path relative to the content folder + return this.GetRelativePath(absolutePath); +#else + // MonoGame is weird about relative paths on Mac, but allows absolute paths + return absolutePath; +#endif } /**** @@ -395,6 +393,21 @@ namespace StardewModdingAPI.Framework /**** ** Asset name/key handling ****/ + /// <summary>Get a directory or file path relative to the content root.</summary> + /// <param name="targetPath">The target file path.</param> + private string GetRelativePath(string targetPath) + { + // convert to URIs + Uri from = new Uri(this.FullRootDirectory + "/"); + Uri to = new Uri(targetPath + "/"); + if (from.Scheme != to.Scheme) + throw new InvalidOperationException($"Can't get path for '{targetPath}' relative to '{this.FullRootDirectory}'."); + + // get relative path + return Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString()) + .Replace(Path.DirectorySeparatorChar == '/' ? '\\' : '/', Path.DirectorySeparatorChar); // use correct separator for platform + } + /// <summary>Get the locale codes (like <c>ja-JP</c>) used in asset keys.</summary> /// <param name="reflection">Simplifies access to private game code.</param> private IDictionary<string, LanguageCode> GetKeyLocales(Reflector reflection) @@ -551,19 +564,6 @@ namespace StardewModdingAPI.Framework return file; } - /// <summary>Get a file from the game's content folder.</summary> - /// <param name="key">The asset key.</param> - private FileInfo GetContentFolderFile(string key) - { - // get file path - string path = Path.Combine(this.FullRootDirectory, key); - if (!path.EndsWith(".xnb")) - path += ".xnb"; - - // get file - return new FileInfo(path); - } - /// <summary>Load the initial asset from the registered <see cref="Loaders"/>.</summary> /// <param name="info">The basic asset metadata.</param> /// <returns>Returns the loaded asset metadata, or <c>null</c> if no loader matched.</returns> diff --git a/src/SMAPI/IContentHelper.cs b/src/SMAPI/IContentHelper.cs index e3362502..1b87183d 100644 --- a/src/SMAPI/IContentHelper.cs +++ b/src/SMAPI/IContentHelper.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using StardewValley; @@ -37,6 +38,11 @@ namespace StardewModdingAPI /// <exception cref="ContentLoadException">The content asset couldn't be loaded (e.g. because it doesn't exist).</exception> T Load<T>(string key, ContentSource source = ContentSource.ModFolder); + /// <summary>Normalise an asset name so it's consistent with those generated by the game. This is mainly useful for string comparisons like <see cref="string.StartsWith(string)"/> on generated asset names, and isn't necessary when passing asset names into other content helper methods.</summary> + /// <param name="assetName">The asset key.</param> + [Pure] + string NormaliseAssetName(string assetName); + /// <summary>Get the underlying key in the game's content cache for an asset. This can be used to load custom map tilesheets, but should be avoided when you can use the content API instead. This does not validate whether the asset exists.</summary> /// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content file relative to the mod folder.</param> /// <param name="source">Where to search for a matching content asset.</param> diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs index b742467b..3ba35e43 100644 --- a/src/SMAPI/Program.cs +++ b/src/SMAPI/Program.cs @@ -653,11 +653,8 @@ namespace StardewModdingAPI { // get basic info IManifest manifest = metadata.Manifest; - string assemblyPath = metadata.Manifest?.EntryDll != null - ? Path.Combine(metadata.DirectoryPath, metadata.Manifest.EntryDll) - : null; - this.Monitor.Log(assemblyPath != null - ? $"Loading {metadata.DisplayName} from {assemblyPath.Replace(Constants.ModPath, "").TrimStart(Path.DirectorySeparatorChar)}..." + this.Monitor.Log(metadata.Manifest?.EntryDll != null + ? $"Loading {metadata.DisplayName} from {metadata.DirectoryPath.Replace(Constants.ModPath, "").TrimStart(Path.DirectorySeparatorChar)}{Path.DirectorySeparatorChar}{metadata.Manifest.EntryDll}..." // don't use Path.Combine here, since EntryDLL might not be valid : $"Loading {metadata.DisplayName}...", LogLevel.Trace); // validate status @@ -669,6 +666,9 @@ namespace StardewModdingAPI } // preprocess & load mod assembly + string assemblyPath = metadata.Manifest?.EntryDll != null + ? Path.Combine(metadata.DirectoryPath, metadata.Manifest.EntryDll) + : null; Assembly modAssembly; try { diff --git a/src/SMAPI/StardewModdingAPI.config.json b/src/SMAPI/StardewModdingAPI.config.json index fa3bdcc9..6718806e 100644 --- a/src/SMAPI/StardewModdingAPI.config.json +++ b/src/SMAPI/StardewModdingAPI.config.json @@ -509,7 +509,8 @@ This file contains advanced configuration for SMAPI. You generally shouldn't cha "ID": "Platonymous.CFAutomate", "Compatibility": { "~1.0.1": { "Status": "AssumeBroken" } // no longer compatible with Automate - } + }, + "AlternativeUrl": "https://www.nexusmods.com/stardewvalley/mods/991" }, { // Custom Farm Types diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 605292b2..380ed733 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -234,9 +234,6 @@ <Compile Include="Framework\CursorPosition.cs" /> </ItemGroup> <ItemGroup> - <None Include="App.config"> - <SubType>Designer</SubType> - </None> <None Include="packages.config"> <SubType>Designer</SubType> </None> diff --git a/src/SMAPI/Utilities/SDate.cs b/src/SMAPI/Utilities/SDate.cs index 2630731a..e589e9a4 100644 --- a/src/SMAPI/Utilities/SDate.cs +++ b/src/SMAPI/Utilities/SDate.cs @@ -35,6 +35,9 @@ namespace StardewModdingAPI.Utilities /// <summary>The day of week.</summary> public DayOfWeek DayOfWeek { get; } + /// <summary>The number of days since the game began (starting at 1 for the first day of spring in Y1).</summary> + 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."); @@ -115,12 +118,7 @@ namespace StardewModdingAPI.Utilities /// <summary>Get a hash code which uniquely identifies a date.</summary> 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; } /**** @@ -132,7 +130,7 @@ namespace StardewModdingAPI.Utilities /// <returns>The equality of the dates</returns> public static bool operator ==(SDate date, SDate other) { - return date?.GetHashCode() == other?.GetHashCode(); + return date?.DaysSinceStart == other?.DaysSinceStart; } /// <summary>Get whether one date is not equal to another.</summary> @@ -140,7 +138,7 @@ namespace StardewModdingAPI.Utilities /// <param name="other">The other date to compare.</param> public static bool operator !=(SDate date, SDate other) { - return date?.GetHashCode() != other?.GetHashCode(); + return date?.DaysSinceStart != other?.DaysSinceStart; } /// <summary>Get whether one date is more than another.</summary> @@ -148,7 +146,7 @@ namespace StardewModdingAPI.Utilities /// <param name="other">The other date to compare.</param> public static bool operator >(SDate date, SDate other) { - return date?.GetHashCode() > other?.GetHashCode(); + return date?.DaysSinceStart > other?.DaysSinceStart; } /// <summary>Get whether one date is more than or equal to another.</summary> @@ -156,7 +154,7 @@ namespace StardewModdingAPI.Utilities /// <param name="other">The other date to compare.</param> public static bool operator >=(SDate date, SDate other) { - return date?.GetHashCode() >= other?.GetHashCode(); + return date?.DaysSinceStart >= other?.DaysSinceStart; } /// <summary>Get whether one date is less than or equal to another.</summary> @@ -164,7 +162,7 @@ namespace StardewModdingAPI.Utilities /// <param name="other">The other date to compare.</param> public static bool operator <=(SDate date, SDate other) { - return date?.GetHashCode() <= other?.GetHashCode(); + return date?.DaysSinceStart <= other?.DaysSinceStart; } /// <summary>Get whether one date is less than another.</summary> @@ -172,7 +170,7 @@ namespace StardewModdingAPI.Utilities /// <param name="other">The other date to compare.</param> public static bool operator <(SDate date, SDate other) { - return date?.GetHashCode() < other?.GetHashCode(); + return date?.DaysSinceStart < other?.DaysSinceStart; } @@ -203,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); + } /// <summary>Get whether a date represents 0 spring Y1, which is the date during the in-game intro.</summary> @@ -215,10 +215,11 @@ namespace StardewModdingAPI.Utilities return day == 0 && season == "spring" && year == 1; } - /// <summary>Get the day of week for the current date.</summary> - private DayOfWeek GetDayOfWeek() + /// <summary>Get the day of week for a given date.</summary> + /// <param name="day">The day of month.</param> + private DayOfWeek GetDayOfWeek(int day) { - switch (this.Day % 7) + switch (day % 7) { case 0: return DayOfWeek.Sunday; @@ -239,13 +240,28 @@ namespace StardewModdingAPI.Utilities } } - /// <summary>Get the current season index.</summary> + /// <summary>Get the number of days since the game began (starting at 1 for the first day of spring in Y1).</summary> + /// <param name="day">The day of month.</param> + /// <param name="season">The season name.</param> + /// <param name="year">The year.</param> + 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; + } + + /// <summary>Get a season index.</summary> + /// <param name="season">The season name.</param> /// <exception cref="InvalidOperationException">The current season wasn't recognised.</exception> - 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; } } |