From b47755d1eb4c587e71e7f5bc020063becb7fe53b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 31 Dec 2018 13:54:06 -0500 Subject: [PATCH] add HasFile content pack method --- docs/release-notes.md | 6 ++++++ src/SMAPI/Framework/ContentPack.cs | 27 +++++++++++++++++++++++---- src/SMAPI/IContentPack.cs | 4 ++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/docs/release-notes.md b/docs/release-notes.md index 5e7ca467..a023a64d 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1,4 +1,10 @@ # Release notes +## 3.0 (upcoming release) +These changes have not been released yet. + +* For modders: + * Added `IContentPack.HasFile` method. + ## 2.11.1 Released 17 March 2019 for Stardew Valley 1.3.36. diff --git a/src/SMAPI/Framework/ContentPack.cs b/src/SMAPI/Framework/ContentPack.cs index e39d03a1..5384d98f 100644 --- a/src/SMAPI/Framework/ContentPack.cs +++ b/src/SMAPI/Framework/ContentPack.cs @@ -47,6 +47,15 @@ namespace StardewModdingAPI.Framework this.JsonHelper = jsonHelper; } + /// Get whether a given file exists in the content pack. + /// The file path to check. + public bool HasFile(string path) + { + this.AssertRelativePath(path, nameof(this.HasFile)); + + return File.Exists(Path.Combine(this.DirectoryPath, path)); + } + /// Read a JSON file from the content pack folder. /// The model type. /// The file path relative to the contnet directory. @@ -54,8 +63,7 @@ namespace StardewModdingAPI.Framework /// The is not relative or contains directory climbing (../). public TModel ReadJsonFile(string path) where TModel : class { - if (!PathUtilities.IsSafeRelativePath(path)) - throw new InvalidOperationException($"You must call {nameof(IContentPack)}.{nameof(this.ReadJsonFile)} with a relative path."); + this.AssertRelativePath(path, nameof(this.ReadJsonFile)); path = Path.Combine(this.DirectoryPath, PathUtilities.NormalisePathSeparators(path)); return this.JsonHelper.ReadJsonFileIfExists(path, out TModel model) @@ -70,8 +78,7 @@ namespace StardewModdingAPI.Framework /// The is not relative or contains directory climbing (../). public void WriteJsonFile(string path, TModel data) where TModel : class { - if (!PathUtilities.IsSafeRelativePath(path)) - throw new InvalidOperationException($"You must call {nameof(IContentPack)}.{nameof(this.WriteJsonFile)} with a relative path."); + this.AssertRelativePath(path, nameof(this.WriteJsonFile)); path = Path.Combine(this.DirectoryPath, PathUtilities.NormalisePathSeparators(path)); this.JsonHelper.WriteJsonFile(path, data); @@ -95,5 +102,17 @@ namespace StardewModdingAPI.Framework return this.Content.GetActualAssetKey(key, ContentSource.ModFolder); } + + /********* + ** Private methods + *********/ + /// Assert that a relative path was passed it to a content pack method. + /// The path to check. + /// The name of the method which was invoked. + private void AssertRelativePath(string path, string methodName) + { + if (!PathUtilities.IsSafeRelativePath(path)) + throw new InvalidOperationException($"You must call {nameof(IContentPack)}.{methodName} with a relative path."); + } } } diff --git a/src/SMAPI/IContentPack.cs b/src/SMAPI/IContentPack.cs index 9ba32394..32cbc6fc 100644 --- a/src/SMAPI/IContentPack.cs +++ b/src/SMAPI/IContentPack.cs @@ -21,6 +21,10 @@ namespace StardewModdingAPI /********* ** Public methods *********/ + /// Get whether a given file exists in the content pack. + /// The file path to check. + bool HasFile(string path); + /// Read a JSON file from the content pack folder. /// The model type. This should be a plain class that has public properties for the data you want. The properties can be complex types. /// The file path relative to the content pack directory.