You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
13 KiB

  1. ---
  2. uid: articles.start.dev
  3. title: Making your own mod
  4. ---
  5. # Making a mod
  6. ## Overview
  7. What follows is a *very* barebones, and frankly not very useful plugin class, even as a starting point,
  8. but it should be enough to give a decent idea of how to do quick upgrades of existing mods for those who want to.
  9. [!code-cs[Plugin.cs](./dev-resources/Plugin.cs?range=1-3,6-8,12-16,29-37,39,50-51,52-55,60-63,65-)]
  10. There are basically 4 major concepts here:
  11. 1. <xref:IPA.Logging.Logger>, the logging system.
  12. 2. <xref:IPA.PluginAttribute>, which declares that this class is a plugin and how it should behave.
  13. 3. <xref:IPA.InitAttribute>, which declares the constructor (and optionally other methods) as being
  14. used for initialization.
  15. 4. The lifecycle event attributes <xref:IPA.OnStartAttribute> and <xref:IPA.OnExitAttribute>.
  16. I reccommend you read the docs for each of those to get an idea for what they do.
  17. It is worth noting that this example is of a mod that *cannot* be enabled and disabled at runtime, as marked by
  18. [RuntimeOptions.SingleStartInit](xref:IPA.RuntimeOptions.SingleStartInit).
  19. ### What can be changed
  20. Before we go adding more functionality, its worth mentioning that that is not the *only* way to have a plugin set up.
  21. For starters, we can add another *method* marked `[Init]`, and it will be called after the constructor, with the same
  22. injected parameters, if those are applicable.
  23. [!code-cs[Plugin.cs#Init(Logger)](./dev-resources/Plugin.cs?range=40-44)]
  24. If you only had a method marked `[Init]`, and no constructors marked `[Init]`, then the plugin type must expose a
  25. public default constructor. If multiple constructors are marked `[Init]`, only the one with the most parameters will
  26. be called.
  27. You may also mark as many methods as you wish with `[Init]` and all of them will be called, in no well-defined order on
  28. initialization. The same is true for `[OnStart]` and `[OnExit]`, respectively.
  29. ## From Scratch
  30. If you are starting from scratch, you will need one other thing to get your plugin up and running: a manifest.
  31. A basic manifest for that might look a little like this:
  32. [!code-json[manifest.json](./dev-resources/manifest.json?range=1,3,4,6-12,14-19,23-)]
  33. There is a lot going on there, but most of it should be decently obvious. Among the things that *aren't* immediately obvious,
  34. are
  35. - `id`: This represents a unique identifier for the mod, for use by package managers such as BeatMods. It may be null if the
  36. mod chooses not to support those.
  37. - `features`: Don't worry about this for now, this is a not-very-simple thing that will be touched on later.
  38. In addition, there are a few gatchas with it:
  39. - `description`: This can be either a string or an array representing different lines. Markdown formatting is permitted.
  40. - `gameVersion`: This should match *exactly* with the application version of the game being targeted. While this is not enforced
  41. by BSIPA, mod repositories like BeatMods may require it match, and it is good practice regardless.
  42. - `version`: This must be a valid SemVer version number for your mod.
  43. In order for your plugin to load, the manifest must be embedded into the plugin DLL as an embedded resource. This can be set in
  44. the Visual Studio file properties panel under `Build Action`, or in the `.csproj` like so:
  45. [!code-xml[Demo.csproj#manifest](./dev-resources/Demo.csproj?range=12-13,15)]
  46. At this point, if the main plugin source file and the manifest are in the same source location, and the plugin class is using the
  47. project's default namespace, the plugin will load just fine. However, this is somewhat difficult both to explain and verify, so I
  48. recommend you use the the `misc.plugin-hint` field in your manifest. It can be used like so:
  49. [!code-json[manifest.json#misc.plugin-hint](./dev-resources/manifest.json?range=20-22)]
  50. With this, you can set `plugin-hint` to the full typename of your plugin type, and it will correctly load. This is a hint though,
  51. and will also try it as a namespace if it fails to find the plugin type. If that fails, it will then fall back to using the manifest's
  52. embedded namespace.
  53. ### A less painful description
  54. If you want to have a relatively long or well-formatted description for your mod, it may start to become painful to embed it in a list
  55. of JSON strings in the manifest. Luckily, there is a way to handle this.
  56. The first step is to create another embedded file, but this time it should be a Markdown file, perhaps `description.md`. It may contain
  57. something like this:
  58. [!code-markdown[description.md](./dev-resources/description.md)]
  59. Then, in your manifest description, have the first line be something look like this, but replacing `Demo.description.md` with the fully
  60. namespaced name of the resource:
  61. [!code-json[manifest.json#description](./dev-resources/manifest.json?range=5)]
  62. Now, when loaded into memory, if anything reads your description metadata, they get the content of that file instead of the content of the
  63. manifest key.
  64. ### Configuring your plugin
  65. Something that many plugins want and need is configuration. Fortunately, BSIPA provides a fairly powerful configuration system out of the
  66. box. To start using it, first create a config class of some kind. Lets take a look at a fairly simple example of this:
  67. [!code-cs[PluginConfig.cs#basic](./dev-resources/PluginConfig.cs?range=9-10,12,15-17,28-30,78-)]
  68. Notice how the class is both marked `public` **and** is not marked `sealed`. For the moment, both of these are necessary. Also notice that
  69. all of the members are properties. While this doesn't change much now, it will be significant in the near future.
  70. Now, how do we get this object off of disk? Simple. Back in your plugin class, change your `[Init]` constructor to look like this:
  71. [!code-cs[Plugin.cs#config-init](./dev-resources/Plugin.cs?range=17-24,26)]
  72. For this to compile, though, we will need to add a few `using`s:
  73. [!code-cs[Plugin.cs#usings](./dev-resources/Plugin.cs?range=4,5)]
  74. With just this, you have your config automatically loading from disk! It's even reloaded when it gets changed mid-game! You can now access
  75. it from anywhere by simply accessing `PluginConfig.Instance`. Make sure you don't accidentally reassign this though, as then you will loose
  76. your only interaction with the user's preferences.
  77. By default, it will be named the same as is in your plugin's manifest's `name` field, and will use the built-in `json` provider. This means
  78. that the file that will be loaded from will be `UserData/Demo Plugin.json` for our demo plugin. You can, however, control both of those by
  79. applying attributes to the <xref:IPA.Config.Config> parameter, namely <xref:IPA.Config.Config.NameAttribute> to control the name, and
  80. <xref:IPA.Config.Config.PreferAttribute> to control the type. If the type preferences aren't registered though, it will just fall back to JSON.
  81. The config's behaviour can be found either later here, or in the remarks section of
  82. <xref:IPA.Config.Stores.GeneratedStore.Generated``1(IPA.Config.Config,System.Boolean)>.
  83. At this point, your main plugin file should look something like this:
  84. [!code-cs[Plugin.cs](./dev-resources/Plugin.cs?range=1-8,12-16,17-24,26,39,50-51,52-55,60-63,65-)]
  85. ***
  86. But what about more complex types than just `int` and `float`? What if you want sub-objects?
  87. Those are supported natively, and so are very easy to set up. We just add this to the config class:
  88. [!code-cs[PluginConfig.cs#sub-basic](./dev-resources/PluginConfig.cs?range=18-19,21,25,31,33)]
  89. Now this object will be automatically read from disk too.
  90. But there is one caveat to this: because `SubThingsObject` is a reference type, *`SubThings` can be null*.
  91. This is often undesireable. The obvious solution may be to simply change it to a `struct`, but that is both not supported *and* potentially
  92. undesirable for other reasons we'll get to later.
  93. Instead, you can use <xref:IPA.Config.Stores.Attributes.NonNullableAttribute>. Change the definition of `SubThings` to this:
  94. [!code-cs[PluginConfig.cs#sub-basic-nonnull](./dev-resources/PluginConfig.cs?range=32-33)]
  95. And add this to the `using`s:
  96. [!code-cs[PluginConfig.cs#includes-attributes](./dev-resources/PluginConfig.cs?range=4)]
  97. This attribute tells the serializer that `null` is an invalid value for the config object. This does, however, require that *you* take extra care
  98. ensure that it never becomes null in code, as that will break the serializer.
  99. ***
  100. What about collection types?
  101. Well, you can use those too, but you have to use something new: a converter.
  102. You may be familiar with them if you have used something like the popular Newtonsoft.Json library before. In BSIPA, they lie in the
  103. <xref:IPA.Config.Stores.Converters> namespace. All converters either implement <xref:IPA.Config.Stores.IValueConverter> or derive from
  104. <xref:IPA.Config.Stores.ValueConverter`1>. You will mostly use them with an <xref:IPA.Config.Stores.Attributes.UseConverterAttribute>.
  105. To use them, we'll want to import them:
  106. [!code-cs[PluginConfig.cs#includes-attributes](./dev-resources/PluginConfig.cs?range=1,3,5)]
  107. Then add a field, for example a list field:
  108. [!code-cs[PluginConfig.cs#list-basic](./dev-resources/PluginConfig.cs?range=35-36)]
  109. This uses a converter that is provided with BSIPA for <xref:System.Collections.Generic.List`1>s specifically. It converts the list to
  110. an ordered array, which is then written to disk as a JSON array.
  111. We could also potentially want use something like a <xref:System.Collections.Generic.HashSet`1>. Lets start by looking at the definition
  112. for such a member, then deciphering what exactly it means:
  113. [!code-cs[PluginConfig.cs#set-basic](./dev-resources/PluginConfig.cs?range=38-39)]
  114. The converter we're using here is <xref:IPA.Config.Stores.Converters.CollectionConverter`2>, a base type for converters of all kinds of
  115. collections. In fact, the <xref:IPA.Config.Stores.Converters.ListConverter`1> is derived from this, and uses it for most of its implementation.
  116. If a type implements <xref:System.Collections.Generic.ICollection`1>, <xref:IPA.Config.Stores.Converters.CollectionConverter`2> can convert it.
  117. It, like most other BSIPA provided aggregate converters, provides a type argument overload <xref:IPA.Config.Stores.Converters.CollectionConverter`3>
  118. to compose other converters with it to handle unusual element types.
  119. Now after all that, your plugin class has not changed, and your config class should look something like this:
  120. [!code-cs[PluginConfig.cs#basic-complete](./dev-resources/PluginConfig.cs?range=1,3-6,9-10,12,15-19,21,25-26,28-39,78-)]
  121. ***
  122. I mentioned earlier that your config file will be automatically reloaded -- but isn't that a bad thing? Doesn't that mean that the config could change
  123. under your feet without you having a way to tell?
  124. Not so- I just haven't introduced the mechanism.
  125. Define a public or protected virtual method named `OnReload`:
  126. [!code-cs[PluginConfig.cs#on-reload](./dev-resources/PluginConfig.cs?range=61-68)]
  127. This method will be called whenever BSIPA reloads your config from disk. When it is called, the object will already have been populated. Use it to
  128. notify all of your systems that configuration has changed.
  129. ***
  130. Now, we know how to read from disk, and how to use unusual types, but how do we write it back to disk?
  131. This config system is based on automatic saving (though we haven't quite gotten to the *automatic* part), and so the config is written to disk whenever
  132. the system recognizes that something has changed. To tell is as much, define a public or protected virtual method named `Changed`:
  133. [!code-cs[PluginConfig.cs#changed](./dev-resources/PluginConfig.cs?range=55-59)]
  134. This method can be called to tell BSIPA that this config object has changed. Later, when we enable automated change tracking, this will also be called
  135. when one of the config's members changes. You can use this body to validate something or, for example, write a timestamp for last change.
  136. ***
  137. I just mentioned automated change tracking -- lets add that now.
  138. To do this, just make all of the properties virtual, like so:
  139. [!code-cs[PluginConfig.cs#auto-props](./dev-resources/PluginConfig.cs?range=18-19,24-26,42-53)]
  140. Now, whenever you assign to any of those properties, your `Changed` method will be called, and the config object will be marked as changed and will be
  141. written to disk. Unfortunately, any properties that can be modified while only using the property getter do not trigger this, and so if you change any
  142. collections for example, you will have to manually call `Changed`.
  143. After doing all this, your config class should look something like this:
  144. [!code-cs[PluginConfig.cs#basic-complete](./dev-resources/PluginConfig.cs?range=1,3-6,9-10,12,15-19,24,25-26,42-68,78-)]
  145. ***
  146. There is one more major problem with this though: the main class is still public. Most configs shouldn't be. Lets make it internal.
  147. So we make it internal:
  148. [!code-cs[PluginConfig.cs#internal](./dev-resources/PluginConfig.cs?range=14)]
  149. But to make it actually work, we add this outside the namespace declaration:
  150. [!code-cs[PluginConfig.cs#internals-visible](./dev-resources/PluginConfig.cs?range=2,6-7)]
  151. And now our full file looks like this:
  152. [!code-cs[PluginConfig.cs#basic-complete](./dev-resources/PluginConfig.cs?range=1-10,14,15-19,24,25-26,42-68,78-)]