Changes

Jump to navigation Jump to search
m
Replace deprecated <source> tags with <syntaxhighlight> tags
Line 12: Line 12:  
===Get owned content packs===
 
===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]]):
 
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#">
+
<syntaxhighlight 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>
+
</syntaxhighlight>
    
Content packs are listed in load order, so they're already sorted for dependencies. For example, if content pack B requires A, they'll be listed in the order A → B.
 
Content packs are listed in load order, so they're already sorted for dependencies. For example, if content pack B requires A, they'll be listed in the order A → B.
Line 26: Line 26:  
You can check if a file exists in the content pack folder using <tt>HasFile</tt>. You can specify a relative path like <tt>data/content.json</tt>.
 
You can check if a file exists in the content pack folder using <tt>HasFile</tt>. You can specify a relative path like <tt>data/content.json</tt>.
   −
<source lang="c#">
+
<syntaxhighlight lang="c#">
 
if (!contentPack.HasFile("content.json"))
 
if (!contentPack.HasFile("content.json"))
 
{
 
{
 
   // show 'required file missing' error
 
   // show 'required file missing' error
 
}
 
}
</source>
+
</syntaxhighlight>
    
It is often a good idea to alert the player (and modders who may be using your mod) if the file is missing on deployment.
 
It is often a good idea to alert the player (and modders who may be using your mod) if the file is missing on deployment.
Line 38: Line 38:  
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.
 
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#">
+
<syntaxhighlight lang="c#">
 
YourDataModel data = contentPack.ReadJsonFile<YourDataFile>("content.json");
 
YourDataModel data = contentPack.ReadJsonFile<YourDataFile>("content.json");
</source>
+
</syntaxhighlight>
    
===Write a JSON file===
 
===Write a JSON file===
Line 47: Line 47:  
'''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.
 
'''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.
   −
<source lang="c#">
+
<syntaxhighlight lang="c#">
 
YourDataModel data = new YourDataModel();
 
YourDataModel data = new YourDataModel();
 
contentPack.WriteJsonFile("content.json", data);
 
contentPack.WriteJsonFile("content.json", data);
</source>
+
</syntaxhighlight>
    
===Read content asset===
 
===Read content asset===
 
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.
 
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#">
+
<syntaxhighlight lang="c#">
 
Texture2D image = contentPack.LoadAsset<Texture2D>("image.png");
 
Texture2D image = contentPack.LoadAsset<Texture2D>("image.png");
</source>
+
</syntaxhighlight>
    
If you need to pass an asset name to game code, you can use the <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#">
+
<syntaxhighlight lang="c#">
 
tilesheet.ImageSource = contentPack.GetActualAssetKey("image.png");
 
tilesheet.ImageSource = contentPack.GetActualAssetKey("image.png");
</source>
+
</syntaxhighlight>
    
===Get translations===
 
===Get translations===
 
You can read translations from the content pack's <tt>i18n</tt> folder. This is identical to the [[Modding:Modder Guide/APIs/Translation|translation API]], but accessed through <tt>contentPack.Translations</tt>:
 
You can read translations from the content pack's <tt>i18n</tt> folder. This is identical to the [[Modding:Modder Guide/APIs/Translation|translation API]], but accessed through <tt>contentPack.Translations</tt>:
<source lang="c#">
+
<syntaxhighlight lang="c#">
 
string text = contentPack.Translation.Get("item-type.label");
 
string text = contentPack.Translation.Get("item-type.label");
</source>
+
</syntaxhighlight>
    
===Create a fake content pack===
 
===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#">
+
<syntaxhighlight lang="c#">
 
// create with random manifest data
 
// create with random manifest data
 
IContentPack contentPack = this.Helper.ContentPacks.CreateFake(
 
IContentPack contentPack = this.Helper.ContentPacks.CreateFake(
Line 86: Line 86:  
   version: new SemanticVersion(1, 0, 0)
 
   version: new SemanticVersion(1, 0, 0)
 
);
 
);
</source>
+
</syntaxhighlight>
114

edits

Navigation menu