Changes

Jump to navigation Jump to search
→‎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
translators
8,446

edits

Navigation menu