Difference between revisions of "Modding:Modder Guide/APIs/Content Packs"

From Stardew Valley Wiki
Jump to navigation Jump to search
(expand a bit)
Line 5: Line 5:
 
__TOC__
 
__TOC__
 
==Content pack format==
 
==Content pack format==
A content pack is a folder containing a <tt>manifest.json</tt> file which specifies your mod in its <tt>ContentPackFor</tt> field (see [[../Manifest|manifest]]). The format beyond is up to you to define. For example, {{nexus mod|1915|Content Patcher}} requires a <tt>content.json</tt> file, but that's not validated by SMAPI itself. Instead, SMAPI provides an API you can use to read any other file you need from the content pack.
+
A content pack is a folder containing a <tt>manifest.json</tt> file which specifies your mod in its <tt>ContentPackFor</tt> field (see [[../Manifest|manifest]]). The format beyond that is up to you to define. For example, {{nexus mod|1915|Content Patcher}} requires a <tt>content.json</tt> file, but that's not validated by SMAPI itself. Instead, SMAPI provides an API you can use to read any other file you need from the content pack.
  
 
See [[Modding:Content packs]] for more info about content packs.
 
See [[Modding:Content packs]] for more info about content packs.

Revision as of 04:13, 28 May 2018

Creating SMAPI mods SMAPI mascot.png


Modding:Index

A content pack is a special type of mod that isn't run by SMAPI directly, but contains files your own mod can read.

Content pack format

A content pack is a folder containing a manifest.json file which specifies your mod in its ContentPackFor field (see manifest). The format beyond that is up to you to define. For example, Content Patcher requires a content.json file, but that's not validated by SMAPI itself. Instead, SMAPI provides an API you can use to read any other file you need from the content pack.

See Modding:Content packs for more info about content packs.

Content pack API

You can get a list of content packs installed for your mod, and access the files within it:

foreach(IContentPack contentPack in this.Helper.GetContentPacks())
{
   this.Monitor.Log($"Reading content pack: {contentPack.Manifest.Name} {contentPack.Manifest.Version} from {contentPack.DirectoryPath}");

    // read a JSON file
    YourDataFile data = contentPack.ReadJsonFile<YourDataFile>("content.json");

    // load an asset or image
    Texture2D image = contentPack.LoadAsset<Texture2D>("image.png");
}

If you need to pass a content pack asset name to game code, you can use its GetActualAssetKey method:

tilesheet.ImageSource = contentPack.GetActualAssetKey("image.png");