Difference between revisions of "Modding:Modder Guide/APIs/Mod structure"

From Stardew Valley Wiki
Jump to navigation Jump to search
(direct link to "Modder Guide")
(→‎Mod entry: seal by default.)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{../../header}}
 
{{../../header}}
 +
 
'''See [[Modding:Modder Guide/Get Started|Modding:Modder Guide]] for an introduction to creating a SMAPI mod. This is a technical reference page which assumes you've already followed that guide.'''
 
'''See [[Modding:Modder Guide/Get Started|Modding:Modder Guide]] for an introduction to creating a SMAPI mod. This is a technical reference page which assumes you've already followed that guide.'''
 +
 
__TOC__
 
__TOC__
 +
 
==Basic structure==
 
==Basic structure==
 
A SMAPI mod must have...
 
A SMAPI mod must have...
 
# a compiled DLL file which contains the code to run as part of the mod (see [[#Mod entry|''mod entry'' below]]);
 
# a compiled DLL file which contains the code to run as part of the mod (see [[#Mod entry|''mod entry'' below]]);
# a [[Modding:Modder Guide/APIs/Manifest|<tt>manifest.json</tt> file]] which describes the mod.
+
# a [[Modding:Modder Guide/APIs/Manifest|<samp>manifest.json</samp> file]] which describes the mod.
  
 
It may optionally have...
 
It may optionally have...
# any number of [[#Dependencies|compiled <tt>.dll</tt> and <tt>.pdb</tt> files referenced by the main mod DLL]];
+
# any number of [[#Dependencies|compiled <samp>.dll</samp> and <samp>.pdb</samp> files referenced by the main mod DLL]];
# an [[Modding:Modder Guide/APIs/Translation|<tt>i18n</tt> folder containing translations]];
+
# an [[Modding:Modder Guide/APIs/Translation|<samp>i18n</samp> folder containing translations]];
# any number of custom files used by the mod code (usually in an <tt>assets</tt> folder by convention).
+
# any number of custom files used by the mod code (usually in an <samp>assets</samp> folder by convention).
  
 
==Mod entry==
 
==Mod entry==
The main mod project must have a subclass of <tt>StardewModdingAPI.Mod</tt>. The class is often named <tt>ModEntry</tt> by convention, but you can use any valid C# name. The class must implement an <tt>Entry</tt> method, which SMAPI will call once the mod is loaded. The <tt>helper</tt> argument provides access to nearly all [[Modding:Modder Guide/APIs|SMAPI APIs]] (available as <tt>this.Helper</tt> in other methods).
+
The main mod project must have a subclass of <samp>StardewModdingAPI.Mod</samp>. The class is often named <samp>ModEntry</samp> by convention, but you can use any valid C# name. The class must implement an <samp>Entry</samp> method, which SMAPI will call once the mod is loaded. The <samp>helper</samp> argument provides access to nearly all [[Modding:Modder Guide/APIs|SMAPI APIs]] (available as <samp>this.Helper</samp> in other methods).
  
 
Here's the minimum possible implementation (which does nothing at all):
 
Here's the minimum possible implementation (which does nothing at all):
<source lang="c#">
+
<syntaxhighlight lang="c#">
 
using StardewModdingAPI;
 
using StardewModdingAPI;
  
 
/// <summary>The mod entry point.</summary>
 
/// <summary>The mod entry point.</summary>
public class ModEntry : Mod
+
internal sealed class ModEntry : Mod
 
{
 
{
 
     /// <summary>The mod entry point, called after the mod is first loaded.</summary>
 
     /// <summary>The mod entry point, called after the mod is first loaded.</summary>
Line 29: Line 32:
 
     }
 
     }
 
}
 
}
</source>
+
</syntaxhighlight>
  
The <tt>Entry</tt> method is called very early in the launch process, so some things aren't initialised yet. You can use events like <tt>GameLaunched</tt>, <tt>SaveLoaded</tt>, or <tt>DayStarted</tt> to access all features. Here's a quick summary of using APIs in <tt>Entry</tt>:
+
The <samp>Entry</samp> method is called very early in the launch process, so some things aren't initialised yet. You can use events like <samp>GameLaunched</samp>, <samp>SaveLoaded</samp>, or <samp>DayStarted</samp> to access all features. Here's a quick summary of using APIs in <samp>Entry</samp>:
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
Line 40: Line 43:
 
| ✓ available. That includes [[Modding:Modder Guide/APIs/Events|registering event handlers]], [[Modding:Modder Guide/APIs/Config|reading configuration]], [[Modding:Modder Guide/APIs/Content|loading assets]], and [[Modding:Modder Guide/APIs/Integrations#Mod registry|fetching mod info]].
 
| ✓ available. That includes [[Modding:Modder Guide/APIs/Events|registering event handlers]], [[Modding:Modder Guide/APIs/Config|reading configuration]], [[Modding:Modder Guide/APIs/Content|loading assets]], and [[Modding:Modder Guide/APIs/Integrations#Mod registry|fetching mod info]].
 
|-
 
|-
| <tt>helper.ModRegistry.GetApi</tt>
+
| <samp>helper.ModRegistry.GetApi</samp>
 
| ✖ Not available, since some mods aren't initialised yet.
 
| ✖ Not available, since some mods aren't initialised yet.
 
|-
 
|-
 
| Game fields
 
| Game fields
| ✖ Not reliably available, since mods are loaded early in the process. That includes core fields like <tt>Game1.objectInformation</tt>.
+
| ✖ Not reliably available, since mods are loaded early in the process. That includes core fields like <samp>Game1.objectInformation</samp>.
 
|}
 
|}
  
Line 54: Line 57:
 
* Reference projects/DLLs and copy their DLLs into your mod folder. SMAPI will detect the reference and load the DLLs from your mod folder automatically. '''Don't do this for referencing a mod''' — that can cause confusing errors when multiple versions are available (see the first option instead).
 
* Reference projects/DLLs and copy their DLLs into your mod folder. SMAPI will detect the reference and load the DLLs from your mod folder automatically. '''Don't do this for referencing a mod''' — that can cause confusing errors when multiple versions are available (see the first option instead).
  
{{modding guide footer
+
[[es:Modding:Guía del Modder/APIs/Mod structure]]
|prev = [[Modding:Modder Guide/APIs|SMAPI reference]]
+
[[zh:模组:制作指南/APIs/Mod structure]]
|next =
 
}}
 

Latest revision as of 02:35, 28 November 2022

Creating SMAPI mods SMAPI mascot.png


Modding:Index

See Modding:Modder Guide for an introduction to creating a SMAPI mod. This is a technical reference page which assumes you've already followed that guide.

Basic structure

A SMAPI mod must have...

  1. a compiled DLL file which contains the code to run as part of the mod (see mod entry below);
  2. a manifest.json file which describes the mod.

It may optionally have...

  1. any number of compiled .dll and .pdb files referenced by the main mod DLL;
  2. an i18n folder containing translations;
  3. any number of custom files used by the mod code (usually in an assets folder by convention).

Mod entry

The main mod project must have a subclass of StardewModdingAPI.Mod. The class is often named ModEntry by convention, but you can use any valid C# name. The class must implement an Entry method, which SMAPI will call once the mod is loaded. The helper argument provides access to nearly all SMAPI APIs (available as this.Helper in other methods).

Here's the minimum possible implementation (which does nothing at all):

using StardewModdingAPI;

/// <summary>The mod entry point.</summary>
internal sealed class ModEntry : Mod
{
    /// <summary>The mod entry point, called after the mod is first loaded.</summary>
    /// <param name="helper">Provides simplified APIs for writing mods.</param>
    public override void Entry(IModHelper helper)
    {
        
    }
}

The Entry method is called very early in the launch process, so some things aren't initialised yet. You can use events like GameLaunched, SaveLoaded, or DayStarted to access all features. Here's a quick summary of using APIs in Entry:

feature status
most SMAPI APIs ✓ available. That includes registering event handlers, reading configuration, loading assets, and fetching mod info.
helper.ModRegistry.GetApi ✖ Not available, since some mods aren't initialised yet.
Game fields ✖ Not reliably available, since mods are loaded early in the process. That includes core fields like Game1.objectInformation.

Dependencies

Most mods are self-contained and only have one DLL file, but that's not a hard limit. Here are some common patterns:

  • Declare a dependency on another mod, and SMAPI will load the other mod before yours and show a friendly error if it's not installed. You can use a mod-provided API if the mod has one, or reference the DLL directly. (If you reference a mod DLL, make sure it's not marked 'copy local' to avoid issues; SMAPI will load the installed version instead.)
  • Create a shared project and reference it from your mod project. The shared project's code will be compiled as part of your mod project (so it'll be one DLL containing both projects in the mod folder).
  • Reference projects/DLLs and copy their DLLs into your mod folder. SMAPI will detect the reference and load the DLLs from your mod folder automatically. Don't do this for referencing a mod — that can cause confusing errors when multiple versions are available (see the first option instead).