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.

548 lines
28 KiB

8 months ago
  1. <!DOCTYPE html>
  2. <!--[if IE]><![endif]-->
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  7. <title>Making your own mod </title>
  8. <meta name="viewport" content="width=device-width">
  9. <meta name="title" content="Making your own mod ">
  10. <meta name="generator" content="docfx 2.59.4.0">
  11. <link rel="shortcut icon" href="../favicon.ico">
  12. <link rel="stylesheet" href="../styles/docfx.vendor.css">
  13. <link rel="stylesheet" href="../styles/docfx.css">
  14. <link rel="stylesheet" href="../styles/main.css">
  15. <link rel="stylesheet" href="../styles/fix.css">
  16. <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
  17. <meta property="docfx:navrel" content="../toc.html">
  18. <meta property="docfx:tocrel" content="toc.html">
  19. <meta property="docfx:rel" content="../">
  20. <meta property="docfx:newtab" content="true">
  21. </head> <body data-spy="scroll" data-target="#affix" data-offset="120">
  22. <div id="wrapper">
  23. <header>
  24. <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
  25. <div class="container">
  26. <div class="navbar-header">
  27. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
  28. <span class="sr-only">Toggle navigation</span>
  29. <span class="icon-bar"></span>
  30. <span class="icon-bar"></span>
  31. <span class="icon-bar"></span>
  32. </button>
  33. <a class="navbar-brand" href="../index.html">
  34. <img id="logo" class="svg" src="../logo.svg" alt="">
  35. </a>
  36. </div>
  37. <div class="collapse navbar-collapse" id="navbar">
  38. <form class="navbar-form navbar-right" role="search" id="search">
  39. <div class="form-group">
  40. <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
  41. </div>
  42. </form>
  43. </div>
  44. </div>
  45. </nav>
  46. <div class="subnav navbar navbar-default">
  47. <div class="container hide-when-search" id="breadcrumb">
  48. <ul class="breadcrumb">
  49. <li></li>
  50. </ul>
  51. </div>
  52. </div>
  53. </header>
  54. <div class="container body-content">
  55. <div id="search-results">
  56. <div class="search-list">Search Results for <span></span></div>
  57. <div class="sr-items">
  58. <p><i class="glyphicon glyphicon-refresh index-loading"></i></p>
  59. </div>
  60. <ul id="pagination" data-first="First" data-prev="Previous" data-next="Next" data-last="Last"></ul>
  61. </div>
  62. </div>
  63. <div role="main" class="container body-content hide-when-search">
  64. <div class="sidenav hide-when-search">
  65. <a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
  66. <div class="sidetoggle collapse" id="sidetoggle">
  67. <div id="sidetoc"></div>
  68. </div>
  69. </div>
  70. <div class="article row grid-right">
  71. <div class="col-md-10">
  72. <article class="content wrap" id="_content" data-uid="articles.start.dev">
  73. <h1 id="making-a-mod">Making a mod</h1>
  74. <h2 id="overview">Overview</h2>
  75. <p>What follows is a <em>very</em> barebones, and frankly not very useful plugin class, even as a starting point,
  76. but it should be enough to give a decent idea of how to do quick upgrades of existing mods for those who want to.</p>
  77. <pre><code class="lang-cs" name="Plugin.cs">using System;
  78. using IPA;
  79. using IPA.Logging;
  80. namespace Demo
  81. {
  82. [Plugin(RuntimeOptions.SingleStartInit)]
  83. internal class Plugin
  84. {
  85. public static Logger log { get; private set; }
  86. [Init]
  87. public Plugin(Logger logger)
  88. {
  89. log = logger;
  90. log.Debug(&quot;Basic plugin running!&quot;);
  91. // setup that does not require game code
  92. // this is only called once ever, so do once-ever initialization
  93. }
  94. [OnStart]
  95. public void OnStart()
  96. {
  97. // setup that requires game code
  98. }
  99. [OnExit]
  100. public void OnExit()
  101. {
  102. // teardown
  103. }
  104. }
  105. }
  106. </code></pre><p>There are basically 4 major concepts here:</p>
  107. <ol>
  108. <li><a class="xref" href="../api/IPA.Logging.Logger.html">Logger</a>, the logging system.</li>
  109. <li><a class="xref" href="../api/IPA.PluginAttribute.html">PluginAttribute</a>, which declares that this class is a plugin and how it should behave.</li>
  110. <li><a class="xref" href="../api/IPA.InitAttribute.html">InitAttribute</a>, which declares the constructor (and optionally other methods) as being
  111. used for initialization.</li>
  112. <li>The lifecycle event attributes <a class="xref" href="../api/IPA.OnStartAttribute.html">OnStartAttribute</a> and <a class="xref" href="../api/IPA.OnExitAttribute.html">OnExitAttribute</a>.</li>
  113. </ol>
  114. <p>I reccommend you read the docs for each of those to get an idea for what they do.</p>
  115. <p>It is worth noting that this example is of a mod that <em>cannot</em> be enabled and disabled at runtime, as marked by
  116. <a class="xref" href="../api/IPA.RuntimeOptions.html#IPA_RuntimeOptions_SingleStartInit">RuntimeOptions.SingleStartInit</a>.</p>
  117. <h3 id="what-can-be-changed">What can be changed</h3>
  118. <p>Before we go adding more functionality, its worth mentioning that that is not the <em>only</em> way to have a plugin set up.</p>
  119. <p>For starters, we can add another <em>method</em> marked <code>[Init]</code>, and it will be called after the constructor, with the same
  120. injected parameters, if those are applicable.</p>
  121. <pre><code class="lang-cs" name="Plugin.cs#Init(Logger)">[Init]
  122. public void Init(Logger logger)
  123. {
  124. // logger will be the same instance as log currently is
  125. }
  126. </code></pre><p>If you only had a method marked <code>[Init]</code>, and no constructors marked <code>[Init]</code>, then the plugin type must expose a
  127. public default constructor. If multiple constructors are marked <code>[Init]</code>, only the one with the most parameters will
  128. be called.</p>
  129. <p>You may also mark as many methods as you wish with <code>[Init]</code> and all of them will be called, in no well-defined order on
  130. initialization. The same is true for <code>[OnStart]</code> and <code>[OnExit]</code>, respectively.</p>
  131. <h2 id="from-scratch">From Scratch</h2>
  132. <p>If you are starting from scratch, you will need one other thing to get your plugin up and running: a manifest.</p>
  133. <p>A basic manifest for that might look a little like this:</p>
  134. <pre><code class="lang-json" name="manifest.json">{
  135. &quot;author&quot;: &quot;ExampleMan&quot;,
  136. &quot;description&quot;: [
  137. &quot;A demo plugin written for the BSIPA basic tutorial.&quot;
  138. ],
  139. &quot;gameVersion&quot;: &quot;1.6.0&quot;,
  140. &quot;id&quot;: null,
  141. &quot;name&quot;: &quot;Demo Plugin&quot;,
  142. &quot;version&quot;: &quot;0.0.1&quot;,
  143. &quot;links&quot;: {
  144. &quot;project-source&quot;: &quot;https://github.com/exampleman/demo-plugin/&quot;,
  145. &quot;donate&quot;: &quot;https://ko-fi.com/exampleman&quot;
  146. },
  147. }
  148. </code></pre><p>There is a lot going on there, but most of it should be decently obvious. Among the things that <em>aren&#39;t</em> immediately obvious,
  149. are</p>
  150. <ul>
  151. <li><code>id</code>: This represents a unique identifier for the mod, for use by package managers such as BeatMods. It may be null if the
  152. mod chooses not to support those.</li>
  153. <li><code>features</code>: Don&#39;t worry about this for now, this is a not-very-simple thing that will be touched on later.</li>
  154. </ul>
  155. <p>In addition, there are a few gatchas with it:</p>
  156. <ul>
  157. <li><code>description</code>: This can be either a string or an array representing different lines. Markdown formatting is permitted.</li>
  158. <li><code>gameVersion</code>: This should match <em>exactly</em> with the application version of the game being targeted. While this is not enforced
  159. by BSIPA, mod repositories like BeatMods may require it match, and it is good practice regardless.</li>
  160. <li><code>version</code>: This must be a valid SemVer version number for your mod.</li>
  161. </ul>
  162. <p>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
  163. the Visual Studio file properties panel under <code>Build Action</code>, or in the <code>.csproj</code> like so:</p>
  164. <pre><code class="lang-xml" name="Demo.csproj#manifest">&lt;ItemGroup&gt;
  165. &lt;EmbeddedResource Include=&quot;manifest.json&quot; /&gt;
  166. &lt;/ItemGroup&gt;
  167. </code></pre><p>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
  168. project&#39;s default namespace, the plugin will load just fine. However, this is somewhat difficult both to explain and verify, so I
  169. recommend you use the the <code>misc.plugin-hint</code> field in your manifest. It can be used like so:</p>
  170. <pre><code class="lang-json" name="manifest.json#misc.plugin-hint">&quot;misc&quot;: {
  171. &quot;plugin-hint&quot;: &quot;Demo.Plugin&quot;
  172. }
  173. </code></pre><p>With this, you can set <code>plugin-hint</code> to the full typename of your plugin type, and it will correctly load. This is a hint though,
  174. 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&#39;s
  175. embedded namespace.</p>
  176. <h3 id="a-less-painful-description">A less painful description</h3>
  177. <p>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
  178. of JSON strings in the manifest. Luckily, there is a way to handle this.</p>
  179. <p>The first step is to create another embedded file, but this time it should be a Markdown file, perhaps <code>description.md</code>. It may contain
  180. something like this:</p>
  181. <pre><code class="lang-markdown" name="description.md"># Demo Plugin
  182. A little demo for the BSIPA modding introduction.
  183. ---
  184. WE CAN USE MARKDOWN!!!
  185. </code></pre><p>Then, in your manifest description, have the first line be something look like this, but replacing <code>Demo.description.md</code> with the fully
  186. namespaced name of the resource:</p>
  187. <pre><code class="lang-json" name="manifest.json#description">&quot;#![Demo.description.md]&quot;,
  188. </code></pre><p>Now, when loaded into memory, if anything reads your description metadata, they get the content of that file instead of the content of the
  189. manifest key.</p>
  190. <h3 id="configuring-your-plugin">Configuring your plugin</h3>
  191. <p>Something that many plugins want and need is configuration. Fortunately, BSIPA provides a fairly powerful configuration system out of the
  192. box. To start using it, first create a config class of some kind. Lets take a look at a fairly simple example of this:</p>
  193. <pre><code class="lang-cs" name="PluginConfig.cs#basic">namespace Demo
  194. {
  195. public class PluginConfig
  196. {
  197. public static PluginConfig Instance { get; set; }
  198. public int IntValue { get; set; } = 42;
  199. public float FloatValue { get; set; } = 3.14159f;
  200. }
  201. }
  202. </code></pre><p>Notice how the class is both marked <code>public</code> <strong>and</strong> is not marked <code>sealed</code>. For the moment, both of these are necessary. Also notice that
  203. all of the members are properties. While this doesn&#39;t change much now, it will be significant in the near future.</p>
  204. <p>Now, how do we get this object off of disk? Simple. Back in your plugin class, change your <code>[Init]</code> constructor to look like this:</p>
  205. <pre><code class="lang-cs" name="Plugin.cs#config-init">[Init]
  206. public Plugin(Logger logger, Config conf)
  207. {
  208. log = logger;
  209. PluginConfig.Instance = conf.Generated&lt;PluginConfig&gt;();
  210. log.Debug(&quot;Config loaded&quot;);
  211. // setup that does not require game code
  212. }
  213. </code></pre><p>For this to compile, though, we will need to add a few <code>using</code>s:</p>
  214. <pre><code class="lang-cs" name="Plugin.cs#usings">using IPA.Config;
  215. using IPA.Config.Stores;
  216. </code></pre><p>With just this, you have your config automatically loading from disk! It&#39;s even reloaded when it gets changed mid-game! You can now access
  217. it from anywhere by simply accessing <code>PluginConfig.Instance</code>. Make sure you don&#39;t accidentally reassign this though, as then you will loose
  218. your only interaction with the user&#39;s preferences.</p>
  219. <p>By default, it will be named the same as is in your plugin&#39;s manifest&#39;s <code>name</code> field, and will use the built-in <code>json</code> provider. This means
  220. that the file that will be loaded from will be <code>UserData/Demo Plugin.json</code> for our demo plugin. You can, however, control both of those by
  221. applying attributes to the <a class="xref" href="../api/IPA.Config.Config.html">Config</a> parameter, namely <a class="xref" href="../api/IPA.Config.Config.NameAttribute.html">Config.NameAttribute</a> to control the name, and
  222. <a class="xref" href="../api/IPA.Config.Config.PreferAttribute.html">Config.PreferAttribute</a> to control the type. If the type preferences aren&#39;t registered though, it will just fall back to JSON.</p>
  223. <p>The config&#39;s behaviour can be found either later here, or in the remarks section of
  224. <a class="xref" href="../api/IPA.Config.Stores.GeneratedStore.html#IPA_Config_Stores_GeneratedStore_Generated__1_IPA_Config_Config_System_Boolean_">Generated&lt;T&gt;(Config, Boolean)</a>.</p>
  225. <p>At this point, your main plugin file should look something like this:</p>
  226. <pre><code class="lang-cs" name="Plugin.cs">using System;
  227. using IPA;
  228. using IPA.Logging;
  229. using IPA.Config;
  230. using IPA.Config.Stores;
  231. namespace Demo
  232. {
  233. [Plugin(RuntimeOptions.SingleStartInit)]
  234. internal class Plugin
  235. {
  236. public static Logger log { get; private set; }
  237. [Init]
  238. public Plugin(Logger logger, Config conf)
  239. {
  240. log = logger;
  241. PluginConfig.Instance = conf.Generated&lt;PluginConfig&gt;();
  242. log.Debug(&quot;Config loaded&quot;);
  243. // setup that does not require game code
  244. }
  245. [OnStart]
  246. public void OnStart()
  247. {
  248. // setup that requires game code
  249. }
  250. [OnExit]
  251. public void OnExit()
  252. {
  253. // teardown
  254. }
  255. }
  256. }
  257. </code></pre><hr>
  258. <p>But what about more complex types than just <code>int</code> and <code>float</code>? What if you want sub-objects?</p>
  259. <p>Those are supported natively, and so are very easy to set up. We just add this to the config class:</p>
  260. <pre><code class="lang-cs" name="PluginConfig.cs#sub-basic">public class SubThingsObject
  261. {
  262. public double DoubleValue { get; set; } = 2.718281828459045;
  263. }
  264. public SubThingsObject SubThings { get; set; } = new SubThingsObject();
  265. </code></pre><p>Now this object will be automatically read from disk too.</p>
  266. <p>But there is one caveat to this: because <code>SubThingsObject</code> is a reference type, <em><code>SubThings</code> can be null</em>.</p>
  267. <p>This is often undesireable. The obvious solution may be to simply change it to a <code>struct</code>, but that is both not supported <em>and</em> potentially
  268. undesirable for other reasons we&#39;ll get to later.</p>
  269. <p>Instead, you can use <a class="xref" href="../api/IPA.Config.Stores.Attributes.NonNullableAttribute.html">NonNullableAttribute</a>. Change the definition of <code>SubThings</code> to this:</p>
  270. <pre><code class="lang-cs" name="PluginConfig.cs#sub-basic-nonnull">[NonNullable]
  271. public SubThingsObject SubThings { get; set; } = new SubThingsObject();
  272. </code></pre><p>And add this to the <code>using</code>s:</p>
  273. <pre><code class="lang-cs" name="PluginConfig.cs#includes-attributes">using IPA.Config.Stores.Attributes;
  274. </code></pre><p>This attribute tells the serializer that <code>null</code> is an invalid value for the config object. This does, however, require that <em>you</em> take extra care
  275. ensure that it never becomes null in code, as that will break the serializer.</p>
  276. <hr>
  277. <p>What about collection types?</p>
  278. <p>Well, you can use those too, but you have to use something new: a converter.</p>
  279. <p>You may be familiar with them if you have used something like the popular Newtonsoft.Json library before. In BSIPA, they lie in the
  280. <a class="xref" href="../api/IPA.Config.Stores.Converters.html">IPA.Config.Stores.Converters</a> namespace. All converters either implement <a class="xref" href="../api/IPA.Config.Stores.IValueConverter.html">IValueConverter</a> or derive from
  281. <a class="xref" href="../api/IPA.Config.Stores.ValueConverter-1.html">ValueConverter&lt;T&gt;</a>. You will mostly use them with an <a class="xref" href="../api/IPA.Config.Stores.Attributes.UseConverterAttribute.html">UseConverterAttribute</a>.</p>
  282. <p>To use them, we&#39;ll want to import them:</p>
  283. <pre><code class="lang-cs" name="PluginConfig.cs#includes-attributes">using System.Collections.Generic;
  284. using IPA.Config.Stores;
  285. using IPA.Config.Stores.Converters;
  286. </code></pre><p>Then add a field, for example a list field:</p>
  287. <pre><code class="lang-cs" name="PluginConfig.cs#list-basic">[UseConverter(typeof(ListConverter&lt;string&gt;))]
  288. public List&lt;string&gt; ListValue { get; set; } = new List&lt;string&gt;();
  289. </code></pre><p>This uses a converter that is provided with BSIPA for <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.generic.list-1">List&lt;T&gt;</a>s specifically. It converts the list to
  290. an ordered array, which is then written to disk as a JSON array.</p>
  291. <p>We could also potentially want use something like a <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.generic.hashset-1">HashSet&lt;T&gt;</a>. Lets start by looking at the definition
  292. for such a member, then deciphering what exactly it means:</p>
  293. <pre><code class="lang-cs" name="PluginConfig.cs#set-basic">[UseConverter(typeof(CollectionConverter&lt;string, HashSet&lt;string&gt;&gt;))]
  294. public HashSet&lt;string&gt; SetValue { get; set; } = new HashSet&lt;string&gt;();
  295. </code></pre><p>The converter we&#39;re using here is <a class="xref" href="../api/IPA.Config.Stores.Converters.CollectionConverter-2.html">CollectionConverter&lt;T, TCollection&gt;</a>, a base type for converters of all kinds of
  296. collections. In fact, the <a class="xref" href="../api/IPA.Config.Stores.Converters.ListConverter-1.html">ListConverter&lt;T&gt;</a> is derived from this, and uses it for most of its implementation.
  297. If a type implements <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.generic.icollection-1">ICollection&lt;T&gt;</a>, <a class="xref" href="../api/IPA.Config.Stores.Converters.CollectionConverter-2.html">CollectionConverter&lt;T, TCollection&gt;</a> can convert it.</p>
  298. <p>It, like most other BSIPA provided aggregate converters, provides a type argument overload <a class="xref" href="../api/IPA.Config.Stores.Converters.CollectionConverter-3.html">CollectionConverter&lt;T, TCollection, TConverter&gt;</a>
  299. to compose other converters with it to handle unusual element types.</p>
  300. <p>Now after all that, your plugin class has not changed, and your config class should look something like this:</p>
  301. <pre><code class="lang-cs" name="PluginConfig.cs#basic-complete">using System.Collections.Generic;
  302. using IPA.Config.Stores;
  303. using IPA.Config.Stores.Attributes;
  304. using IPA.Config.Stores.Converters;
  305. namespace Demo
  306. {
  307. public class PluginConfig
  308. {
  309. public static PluginConfig Instance { get; set; }
  310. public class SubThingsObject
  311. {
  312. public double DoubleValue { get; set; } = 2.718281828459045;
  313. }
  314. public int IntValue { get; set; } = 42;
  315. public float FloatValue { get; set; } = 3.14159f;
  316. [NonNullable]
  317. public SubThingsObject SubThings { get; set; } = new SubThingsObject();
  318. [UseConverter(typeof(ListConverter&lt;string&gt;))]
  319. public List&lt;string&gt; ListValue { get; set; } = new List&lt;string&gt;();
  320. [UseConverter(typeof(CollectionConverter&lt;string, HashSet&lt;string&gt;&gt;))]
  321. public HashSet&lt;string&gt; SetValue { get; set; } = new HashSet&lt;string&gt;();
  322. }
  323. }
  324. </code></pre><hr>
  325. <p>I mentioned earlier that your config file will be automatically reloaded -- but isn&#39;t that a bad thing? Doesn&#39;t that mean that the config could change
  326. under your feet without you having a way to tell?</p>
  327. <p>Not so- I just haven&#39;t introduced the mechanism.</p>
  328. <p>Define a public or protected virtual method named <code>OnReload</code>:</p>
  329. <pre><code class="lang-cs" name="PluginConfig.cs#on-reload">public virtual void OnReload()
  330. {
  331. // this is called whenever the config file is reloaded from disk
  332. // use it to tell all of your systems that something has changed
  333. // this is called off of the main thread, and is not safe to interact
  334. // with Unity in
  335. }
  336. </code></pre><p>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
  337. notify all of your systems that configuration has changed.</p>
  338. <hr>
  339. <p>Now, we know how to read from disk, and how to use unusual types, but how do we write it back to disk?</p>
  340. <p>This config system is based on automatic saving (though we haven&#39;t quite gotten to the <em>automatic</em> part), and so the config is written to disk whenever
  341. the system recognizes that something has changed. To tell is as much, define a public or protected virtual method named <code>Changed</code>:</p>
  342. <pre><code class="lang-cs" name="PluginConfig.cs#changed">public virtual void Changed()
  343. {
  344. // this is called whenever one of the virtual properties is changed
  345. // can be called to signal that the content has been changed
  346. }
  347. </code></pre><p>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
  348. when one of the config&#39;s members changes. You can use this body to validate something or, for example, write a timestamp for last change.</p>
  349. <hr>
  350. <p>I just mentioned automated change tracking -- lets add that now.</p>
  351. <p>To do this, just make all of the properties virtual, like so:</p>
  352. <pre><code class="lang-cs" name="PluginConfig.cs#auto-props">public class SubThingsObject
  353. {
  354. public virtual double DoubleValue { get; set; } = 2.718281828459045;
  355. }
  356. public virtual int IntValue { get; set; } = 42;
  357. public virtual float FloatValue { get; set; } = 3.14159f;
  358. [NonNullable]
  359. public virtual SubThingsObject SubThings { get; set; } = new SubThingsObject();
  360. [UseConverter(typeof(ListConverter&lt;string&gt;))]
  361. public virtual List&lt;string&gt; ListValue { get; set; } = new List&lt;string&gt;();
  362. [UseConverter(typeof(CollectionConverter&lt;string, HashSet&lt;string&gt;&gt;))]
  363. public virtual HashSet&lt;string&gt; SetValue { get; set; } = new HashSet&lt;string&gt;();
  364. </code></pre><p>Now, whenever you assign to any of those properties, your <code>Changed</code> method will be called, and the config object will be marked as changed and will be
  365. 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
  366. collections for example, you will have to manually call <code>Changed</code>.</p>
  367. <p>After doing all this, your config class should look something like this:</p>
  368. <pre><code class="lang-cs" name="PluginConfig.cs#basic-complete">using System.Collections.Generic;
  369. using IPA.Config.Stores;
  370. using IPA.Config.Stores.Attributes;
  371. using IPA.Config.Stores.Converters;
  372. namespace Demo
  373. {
  374. public class PluginConfig
  375. {
  376. public static PluginConfig Instance { get; set; }
  377. public class SubThingsObject
  378. {
  379. public virtual double DoubleValue { get; set; } = 2.718281828459045;
  380. }
  381. public virtual int IntValue { get; set; } = 42;
  382. public virtual float FloatValue { get; set; } = 3.14159f;
  383. [NonNullable]
  384. public virtual SubThingsObject SubThings { get; set; } = new SubThingsObject();
  385. [UseConverter(typeof(ListConverter&lt;string&gt;))]
  386. public virtual List&lt;string&gt; ListValue { get; set; } = new List&lt;string&gt;();
  387. [UseConverter(typeof(CollectionConverter&lt;string, HashSet&lt;string&gt;&gt;))]
  388. public virtual HashSet&lt;string&gt; SetValue { get; set; } = new HashSet&lt;string&gt;();
  389. public virtual void Changed()
  390. {
  391. // this is called whenever one of the virtual properties is changed
  392. // can be called to signal that the content has been changed
  393. }
  394. public virtual void OnReload()
  395. {
  396. // this is called whenever the config file is reloaded from disk
  397. // use it to tell all of your systems that something has changed
  398. // this is called off of the main thread, and is not safe to interact
  399. // with Unity in
  400. }
  401. }
  402. }
  403. </code></pre><hr>
  404. <p>There is one more major problem with this though: the main class is still public. Most configs shouldn&#39;t be. Lets make it internal.</p>
  405. <p>So we make it internal:</p>
  406. <pre><code class="lang-cs" name="PluginConfig.cs#internal">internal class PluginConfig
  407. </code></pre><p>But to make it actually work, we add this outside the namespace declaration:</p>
  408. <pre><code class="lang-cs" name="PluginConfig.cs#internals-visible">using System.Runtime.CompilerServices;
  409. [assembly: InternalsVisibleTo(GeneratedStore.AssemblyVisibilityTarget)]
  410. </code></pre><p>And now our full file looks like this:</p>
  411. <pre><code class="lang-cs" name="PluginConfig.cs#basic-complete">using System.Collections.Generic;
  412. using System.Runtime.CompilerServices;
  413. using IPA.Config.Stores;
  414. using IPA.Config.Stores.Attributes;
  415. using IPA.Config.Stores.Converters;
  416. [assembly: InternalsVisibleTo(GeneratedStore.AssemblyVisibilityTarget)]
  417. namespace Demo
  418. {
  419. internal class PluginConfig
  420. {
  421. public static PluginConfig Instance { get; set; }
  422. public class SubThingsObject
  423. {
  424. public virtual double DoubleValue { get; set; } = 2.718281828459045;
  425. }
  426. public virtual int IntValue { get; set; } = 42;
  427. public virtual float FloatValue { get; set; } = 3.14159f;
  428. [NonNullable]
  429. public virtual SubThingsObject SubThings { get; set; } = new SubThingsObject();
  430. [UseConverter(typeof(ListConverter&lt;string&gt;))]
  431. public virtual List&lt;string&gt; ListValue { get; set; } = new List&lt;string&gt;();
  432. [UseConverter(typeof(CollectionConverter&lt;string, HashSet&lt;string&gt;&gt;))]
  433. public virtual HashSet&lt;string&gt; SetValue { get; set; } = new HashSet&lt;string&gt;();
  434. public virtual void Changed()
  435. {
  436. // this is called whenever one of the virtual properties is changed
  437. // can be called to signal that the content has been changed
  438. }
  439. public virtual void OnReload()
  440. {
  441. // this is called whenever the config file is reloaded from disk
  442. // use it to tell all of your systems that something has changed
  443. // this is called off of the main thread, and is not safe to interact
  444. // with Unity in
  445. }
  446. }
  447. }
  448. </code></pre></article>
  449. </div>
  450. <div class="hidden-sm col-md-2" role="complementary">
  451. <div class="sideaffix">
  452. <div class="contribution">
  453. <ul class="nav">
  454. <li>
  455. <a href="https://github.com/nike4613/BeatSaber-IPA-Reloaded/blob/01a05403ef4edd9646be6b698e7da30c741965e5/docs/articles/start-dev.md/#L1" class="contribution-link">Improve this Doc</a>
  456. </li>
  457. </ul>
  458. </div>
  459. <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
  460. <h5>In This Article</h5>
  461. <div></div>
  462. </nav>
  463. </div>
  464. </div>
  465. </div>
  466. </div>
  467. <footer>
  468. <div class="grad-bottom"></div>
  469. <div class="footer">
  470. <div class="container">
  471. <span class="pull-right">
  472. <a href="#top">Back to top</a>
  473. </span>
  474. <span>Generated by <strong>DocFX</strong></span>
  475. </div>
  476. </div>
  477. </footer>
  478. </div>
  479. <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
  480. <script type="text/javascript" src="../styles/docfx.js"></script>
  481. <script type="text/javascript" src="../styles/main.js"></script>
  482. </body>
  483. </html>