Skip to content

1. PluginController

Venomaus edited this page Dec 31, 2023 · 3 revisions

Introduction

The PluginController class provides some basic setup functionalities for your plugin.

There are currently two generic variants of the class:

  • PluginController<TImpl> (Empty bindings)
  • PluginController<TImpl, TBindings> (Custom provided bindings)

TImpl represents your plugin class. Eg: public class Plugin : PluginController<Plugin> { }

Exposed Methods

Following methods can be overriden and are run in the same sequence:

  1. OnConfigureBindings(Runs at constructor level of PluginController, initializing the model based configuration bindings if any exist)
  2. Load(EntryPoint)
  3. Unload (Runs when the plugin is unloaded, unpatches self)

Exposed properties

  • Instance (static instance to your Plugin)
  • Config (Points to your binding, (or usual BepInEx ConfigFile if using the EmptyBindings variant)
  • ConfigFile (Points to BepInEx ConfigFile, this property is obsolete in the EmptyBindings variant)
  • Log (static instance to your ManualLogSource for logging information to console)
  • Harmony (protected property instance for harmony to do patching in your Plugin class)

Modelbased configuration

It is possible to model your configuration bindings with proxy interfaces, to easily access or set configuration options. These interface properties directly get/set from your mod's bepinex config file.

Example usage:

public interface IConfigBindings
{
    // Binds in the config file as: Prices.SyncDiskPrice
    [Binding(500, "The price for the sync disk.", "Prices.SyncDiskPrice")]
    int SyncDiskPrice { get; set; }

    // Binds in the config file as: General.SomeTextConfig
    [Binding("Hello", "Yep this is some text config!")]
    string SomeTextConfig { get; set; }
}

[BepInPlugin(PLUGIN_GUID, PLUGIN_NAME, PLUGIN_VERSION)]
[BepInDependency("Venomaus.SOD.Common")]
public class Plugin : PluginController<Plugin, IConfigBindings>
{
    public override void Load()
    {
        Log.LogInfo("SyncDiskPrice: " + Config.SyncDiskPrice);
        Log.LogInfo("SomeTextConfig: " + Config.SomeTextConfig);
    }
}

You can also combine multiple interfaces:

public interface IConfigBindings : ISomeOtherBindings, ISomeMoreBindings
{ }
Clone this wiki locally