Difference between revisions of "Modding:Modder Guide/APIs/Content Packs"
Pathoschild (talk | contribs) (+ footer nav links) |
Pathoschild (talk | contribs) (→Content pack API: + SMAPI 2.10 API) |
||
Line 28: | Line 28: | ||
tilesheet.ImageSource = contentPack.GetActualAssetKey("image.png"); | tilesheet.ImageSource = contentPack.GetActualAssetKey("image.png"); | ||
</source> | </source> | ||
+ | |||
+ | {{SMAPI upcoming|2.10| | ||
+ | You can get a list of content packs installed for your mod, and access the files within it: | ||
+ | <source lang="c#"> | ||
+ | foreach(IContentPack contentPack in this.Helper.ContentPacks.GetOwned()) | ||
+ | { | ||
+ | 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"); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | In specialised cases, you can create a fake content pack for a given directory path: | ||
+ | <source lang="c#"> | ||
+ | // create with random manifest data | ||
+ | IContentPack contentPack = this.Helper.ContentPack.CreateFake( | ||
+ | directoryPath: Path.Combine(this.Helper.DirectoryPath, "content-pack"), | ||
+ | ); | ||
+ | |||
+ | // create with given manifest fields | ||
+ | IContentPack contentPack = this.Helper.ContentPack.CreateFake( | ||
+ | directoryPath: Path.Combine(this.Helper.DirectoryPath, "content-pack"), | ||
+ | id: Guid.NewGuid().ToString("N"), | ||
+ | name: "temporary content pack", | ||
+ | description: "...", | ||
+ | author: "...", | ||
+ | version: new SemanticVersion(1, 0, 0) | ||
+ | ); | ||
+ | </source> | ||
+ | }} | ||
{{modding guide footer | {{modding guide footer |
Revision as of 06:11, 13 December 2018
- Get started
- Game fundamentals
- Test & troubleshoot
- Release
- API reference
- Basic SMAPI APIs:
- Advanced SMAPI APIs:
- Specific guides
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");
foreach(IContentPack contentPack in this.Helper.ContentPacks.GetOwned())
{
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");
}
In specialised cases, you can create a fake content pack for a given directory path:
// create with random manifest data
IContentPack contentPack = this.Helper.ContentPack.CreateFake(
directoryPath: Path.Combine(this.Helper.DirectoryPath, "content-pack"),
);
// create with given manifest fields
IContentPack contentPack = this.Helper.ContentPack.CreateFake(
directoryPath: Path.Combine(this.Helper.DirectoryPath, "content-pack"),
id: Guid.NewGuid().ToString("N"),
name: "temporary content pack",
description: "...",
author: "...",
version: new SemanticVersion(1, 0, 0)
);