← README
This doc helps SMAPI mod authors use Content Patcher's token strings in their own mods.
To add custom tokens for content packs to use, see the extensibility API. See the main README for other info.
Content Patcher has a token system which lets content packs make complex strings using contextual values. For example:
""
"My favorite season is {{Season}}." // If a save is loaded, {{Season}} will be replaced with the current season.
Other SMAPI mods can use this token system too. They essentially create a string with the tokens they want to have evaluated and call the API below to get a 'managed token string' object, then use that to manage the token string.
To access the API:
- Add Content Patcher as a required dependency in your mod's
manifest.json
:"Dependencies": [ { "UniqueID": "Pathoschild.ContentPatcher", "MinimumVersion": "2.5.0" } ]
- Add a reference to the Content Patcher DLL in your mod's
.csproj
file. Make sure you setPrivate="False"
, so the DLL isn't added to your mod folder:<ItemGroup> <Reference Include="ContentPatcher" HintPath="$(GameModsPath)\ContentPatcher\ContentPatcher.dll" Private="False" /> </ItemGroup>
- Somewhere in your mod code (e.g. in the
GameLaunched
event), get a reference to Content Patcher's API:var api = this.Helper.ModRegistry.GetApi<ContentPatcher.IContentPatcherAPI>("Pathoschild.ContentPatcher");
Note: see caveats before calling this API.
Now that you have access to the API, you can parse token strings.
-
Create a string to evaluate. This can use Content Patcher features like tokens. For example:
string rawTokenString = "The current time is {{Time}} on {{Season}} {{Day}}, year {{Year}}.";
-
Call the API to parse the string into an
IManagedTokenString
wrapper. TheformatVersion
matches theFormat
field described in the author guide to enable forward compatibility with future versions of Content Patcher.var tokenString = api.ParseTokenString( manifest: this.ModManifest, rawValue: rawTokenString, formatVersion: new SemanticVersion("2.5.0") );
-
Get the parsed string from the
Value
property. For example:tokenString.UpdateContext(); string value = tokenString.Value; // The current time is 1430 on Spring 5, year 2.
If you want to allow custom tokens added by other SMAPI mods, you can specify a list of mod IDs
to assume are installed. You don't need to do this for your own mod ID or for mods listed as
required dependencies in your mod's manifest.json
.
var tokenString = api.ParseTokenString(
manifest: this.ModManifest,
rawValue: rawTokenString,
formatVersion: new SemanticVersion("2.5.0"),
assumeModIds: new[] { "spacechase0.JsonAssets" }
);
The IManagedTokenString
object you got above provides a number of properties and methods to
manage the parsed token string. You can check IntelliSense in Visual Studio to see what's available,
but here are some of the most useful properties:
property | type | description |
---|---|---|
IsValid
| bool |
Whether the token string was parsed successfully (regardless of whether its tokens are in scope currently). |
ValidationError |
string |
When
If the token string is valid, this is |
IsReady |
bool |
Whether the token string's tokens are all valid in the current context. For example, this would be
false if the token string uses |
Value |
string? |
If If |
And methods:
method | type | description |
---|---|---|
UpdateContext
| bool |
Updates the token string based on Content Patcher's current context, and returns whether |
- The token string API isn't available immediately.
-
The token string API is available two ticks after the
GameLaunched
event (and anytime after that point). That's due to the Content Patcher lifecycle:GameLaunched
: other mods can register custom tokens.GameLaunched + 1 tick
: Content Patcher initializes the token context (including custom tokens).GameLaunched + 2 ticks
: other mods can use the token string API.
- Token strings should be cached.
-
Parsing token strings through the API is a relatively expensive operation. If you'll recheck the same token string often, it's best to save and reuse the
IManagedTokenString
instance. - Token strings don't update automatically.
-
When using a cached
IManagedTokenString
object, make sure to update it usingtokenString.UpdateContext()
as needed.Note that token string updates are limited to Content Patcher's update rate. When you call
tokenString.UpdateContext()
, it will reflect the tokens as of Content Patcher's last internal context update. - Token strings handle split-screen automatically.
-
For example,
Value
returns the value for the current screen's context. The exception isUpdateContext
, which updates the context for all active screens.
- README for other info