Line 10: |
Line 10: |
| | | |
| ==Content pack API== | | ==Content pack API== |
− | You can get a list of content packs installed for your mod, and access the files within it:
| + | ===Get owned content packs=== |
| + | To get the content packs installed for your mod (including a <tt>Manifest</tt> field with the content pack's [[Modding:Modder Guide/APIs/Manifest|full manifest info]]): |
| <source lang="c#"> | | <source lang="c#"> |
| foreach(IContentPack contentPack in this.Helper.ContentPacks.GetOwned()) | | foreach(IContentPack contentPack in this.Helper.ContentPacks.GetOwned()) |
| { | | { |
| this.Monitor.Log($"Reading content pack: {contentPack.Manifest.Name} {contentPack.Manifest.Version} from {contentPack.DirectoryPath}"); | | this.Monitor.Log($"Reading content pack: {contentPack.Manifest.Name} {contentPack.Manifest.Version} from {contentPack.DirectoryPath}"); |
| + | } |
| + | </source> |
| + | |
| + | ===Read a JSON file=== |
| + | You can read a JSON file from a content pack folder into [[Modding:Modder Guide/APIs/Data#Data model|a data model]] (<tt>YourDataModel</tt> in the example below) using <tt>ReadJsonFile</tt>. You can specify a relative path like <tt>data/content.json</tt>. This will return <tt>null</tt> if the file doesn't exist. |
| + | |
| + | <source lang="c#"> |
| + | YourDataModel data = contentPack.ReadJsonFile<YourDataFile>("content.json"); |
| + | </source> |
| + | |
| + | ===Write a JSON file=== |
| + | You can write a JSON file in a content pack folder using <tt>WriteJsonFile</tt>, using a [[Modding:Modder Guide/APIs/Data#Data model|data model]] (<tt>YourDataModel</tt> in the example below). You can specify a relative path like <tt>data/content.json</tt> (subdirectories will be created automatically if needed). This will create the file if it doesn't exist, or overwrite it if it does. |
| | | |
− | // read a JSON file
| + | '''Note:''' this is best used for generated files. Overwriting the original files isn't recommended, since a bug in your mod which changes an original file might permanently break the content pack and require the player to reinstall it. |
− | YourDataFile data = contentPack.ReadJsonFile<YourDataFile>("content.json");
| + | |
| + | <source lang="c#"> |
| + | YourDataModel data = new YourDataModel(); |
| + | contentPack.WriteJsonFile("content.json", data); |
| + | </source> |
| | | |
− | // load an asset or image
| + | ===Read content asset=== |
− | Texture2D image = contentPack.LoadAsset<Texture2D>("image.png");
| + | You can read [[Modding:Modder Guide/APIs/Content|a content asset]] from the content pack folder using <tt>LoadAsset</tt>. You can optionally specify a relative path to read from a subfolder. |
− | }
| + | <source lang="c#"> |
| + | Texture2D image = contentPack.LoadAsset<Texture2D>("image.png"); |
| </source> | | </source> |
| | | |
− | If you need to pass a content pack asset name to game code, you can use its <tt>GetActualAssetKey</tt> method: | + | If you need to pass an asset name to game code, you can use the <tt>GetActualAssetKey</tt> method: |
| <source lang="c#"> | | <source lang="c#"> |
| tilesheet.ImageSource = contentPack.GetActualAssetKey("image.png"); | | tilesheet.ImageSource = contentPack.GetActualAssetKey("image.png"); |
| </source> | | </source> |
| | | |
| + | ===Create a fake content pack=== |
| In specialised cases, you can create a temporary content pack for a given directory path: | | In specialised cases, you can create a temporary content pack for a given directory path: |
| <source lang="c#"> | | <source lang="c#"> |