add HasFile content pack method

This commit is contained in:
Jesse Plamondon-Willard 2018-12-31 13:54:06 -05:00
parent 013255d89e
commit b47755d1eb
No known key found for this signature in database
GPG Key ID: 7D7C8097B62033CE
3 changed files with 33 additions and 4 deletions

View File

@ -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.

View File

@ -47,6 +47,15 @@ namespace StardewModdingAPI.Framework
this.JsonHelper = jsonHelper;
}
/// <summary>Get whether a given file exists in the content pack.</summary>
/// <param name="path">The file path to check.</param>
public bool HasFile(string path)
{
this.AssertRelativePath(path, nameof(this.HasFile));
return File.Exists(Path.Combine(this.DirectoryPath, path));
}
/// <summary>Read a JSON file from the content pack folder.</summary>
/// <typeparam name="TModel">The model type.</typeparam>
/// <param name="path">The file path relative to the contnet directory.</param>
@ -54,8 +63,7 @@ namespace StardewModdingAPI.Framework
/// <exception cref="InvalidOperationException">The <paramref name="path"/> is not relative or contains directory climbing (../).</exception>
public TModel ReadJsonFile<TModel>(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
/// <exception cref="InvalidOperationException">The <paramref name="path"/> is not relative or contains directory climbing (../).</exception>
public void WriteJsonFile<TModel>(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
*********/
/// <summary>Assert that a relative path was passed it to a content pack method.</summary>
/// <param name="path">The path to check.</param>
/// <param name="methodName">The name of the method which was invoked.</param>
private void AssertRelativePath(string path, string methodName)
{
if (!PathUtilities.IsSafeRelativePath(path))
throw new InvalidOperationException($"You must call {nameof(IContentPack)}.{methodName} with a relative path.");
}
}
}

View File

@ -21,6 +21,10 @@ namespace StardewModdingAPI
/*********
** Public methods
*********/
/// <summary>Get whether a given file exists in the content pack.</summary>
/// <param name="path">The file path to check.</param>
bool HasFile(string path);
/// <summary>Read a JSON file from the content pack folder.</summary>
/// <typeparam name="TModel">The model type. This should be a plain class that has public properties for the data you want. The properties can be complex types.</typeparam>
/// <param name="path">The file path relative to the content pack directory.</param>