Line 9: |
Line 9: |
| Your mod can get information about loaded mods, or check if a particular mod is loaded. (All mods are loaded by the time your mod's <tt>Entry(…)</tt> method is called.) | | Your mod can get information about loaded mods, or check if a particular mod is loaded. (All mods are loaded by the time your mod's <tt>Entry(…)</tt> method is called.) |
| | | |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| // check if a mod is loaded | | // check if a mod is loaded |
| bool isLoaded = this.Helper.ModRegistry.IsLoaded("UniqueModID"); | | bool isLoaded = this.Helper.ModRegistry.IsLoaded("UniqueModID"); |
Line 20: |
Line 20: |
| // get info for all loaded mods | | // get info for all loaded mods |
| foreach(IModInfo mod in this.Helper.ModRegistry.GetAll()) { … } | | foreach(IModInfo mod in this.Helper.ModRegistry.GetAll()) { … } |
− | </source> | + | </syntaxhighlight> |
| | | |
| ==Mod-provided APIs== | | ==Mod-provided APIs== |
Line 29: |
Line 29: |
| <ol> | | <ol> |
| <li>Add a normal class to your mod project with the methods and properties you want to expose. | | <li>Add a normal class to your mod project with the methods and properties you want to expose. |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| public class YourModApi | | public class YourModApi |
| { | | { |
Line 39: |
Line 39: |
| } | | } |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| (You can use a [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors constructor] to initialise the API if desired.)</li> | | (You can use a [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors constructor] to initialise the API if desired.)</li> |
| <li>Override <tt>GetApi</tt> in your mod's entry class and return an instance of your API: | | <li>Override <tt>GetApi</tt> in your mod's entry class and return an instance of your API: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| public override object GetApi() | | public override object GetApi() |
| { | | { |
| return new YourModApi(); | | return new YourModApi(); |
| } | | } |
− | </source></li> | + | </syntaxhighlight></li> |
| </ol> | | </ol> |
| | | |
Line 61: |
Line 61: |
| <ol> | | <ol> |
| <li>Create an interface with only the properties and methods you want to access. (If you directly reference the mod DLL and it provides a public API interface, you can use that instead.) | | <li>Create an interface with only the properties and methods you want to access. (If you directly reference the mod DLL and it provides a public API interface, you can use that instead.) |
− | <source lang="C#"> | + | <syntaxhighlight lang="C#"> |
| public interface ISomeModApi | | public interface ISomeModApi |
| { | | { |
| bool GetThing(string example); | | bool GetThing(string example); |
| } | | } |
− | </source></li> | + | </syntaxhighlight></li> |
| <li>In your mod code (after <tt>Entry</tt>), you can get the API by specifying the interface you created in step 1, and the [[../Manifest#Basic fields|mod's unique ID]]: | | <li>In your mod code (after <tt>Entry</tt>), you can get the API by specifying the interface you created in step 1, and the [[../Manifest#Basic fields|mod's unique ID]]: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| ISomeModApi api = helper.ModRegistry.GetApi<ISomeModApi>("other-mod-ID"); | | ISomeModApi api = helper.ModRegistry.GetApi<ISomeModApi>("other-mod-ID"); |
| if (api != null) | | if (api != null) |
| bool result = api.GetThing("boop"); | | bool result = api.GetThing("boop"); |
− | </source></li> | + | </syntaxhighlight></li> |
| </ol> | | </ol> |
| | | |
| For a quick integration, you can also use reflection instead of creating an interface: | | For a quick integration, you can also use reflection instead of creating an interface: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| object api = helper.ModRegistry.GetApi("other-mod-id"); | | object api = helper.ModRegistry.GetApi("other-mod-id"); |
| if (api != null) | | if (api != null) |
| bool result = helper.Reflection.GetMethod(api, "GetThing").Invoke<bool>("boop"); | | bool result = helper.Reflection.GetMethod(api, "GetThing").Invoke<bool>("boop"); |
− | </source> | + | </syntaxhighlight> |
| | | |
| Notes: | | Notes: |