Skip to content

Hamunii/BepInEx.AutoPlugin

 
 

Repository files navigation

BepInEx.AutoPlugin

NuGet

BepInEx.AutoPlugin is an incremental C# source generator that takes the following properties from your project:

<PropertyGroup>
  <AssemblyName>com.example.ExamplePlugin</AssemblyName>
  <AssemblyTitle>ExamplePlugin</AssemblyTitle>
  <Version>0.1.0</Version>
</PropertyGroup>

And generates a partial class for your partial plugin class decorated with the BepInAutoPluginAttribute, decorating the generated class with the BepInPluginAttribute using the above properties:

[BepInEx.BepInPlugin(ExamplePlugin.Id, "ExamplePlugin", "0.1.0")]
partial class ExamplePlugin : BaseUnityPlugin
{
    public const string Id = "com.example.ExamplePlugin";
    public static string Name => "ExamplePlugin";
    public static string Version => "0.1.0";
}

A PatcherAutoPluginAttribute also exists for BepInEx 6 preloader patchers.

Usage

Add the following to your csproj:

<ItemGroup>
  <PackageReference Include="Hamunii.BepInEx.AutoPlugin" Version="2.0.*" PrivateAssets="all" />
</ItemGroup>

Mark your plugin class partial, and decorate it with the BepInAutoPluginAttribute:

[BepInAutoPlugin]
public partial class ExamplePlugin : BaseUnityPlugin
{
}

And you're done! You don't need to manually include the BepInPluginAttribute, as the generated partial class includes that.

You can also access all the properties in your code, as they are public members in your class:

[BepInAutoPlugin]
public partial class ExamplePlugin : BaseUnityPlugin
{
    void Awake()
    {
        Logger.LogInfo($"Plugin {Name} version {Version} is loaded!");
    }
}

Overriding Properties

AutoPlugin allows overriding any of the properties with the optional attribute arguments:

[BepInAutoPlugin(id: "com.example.MyOverrideId", name: "My Override Name", version: "1.2.3")]
public partial class ExamplePlugin : BaseUnityPlugin
{
}

MSBuild Configuration

AutoPlugin allows stripping version build metadata to turn 1.0.0-beta+1234567890 into 1.0.0-beta for the Version property by setting the following in your csproj:

<PropertyGroup>
  <BepInAutoPluginStripBuildMetadata>true</BepInAutoPluginStripBuildMetadata>
</PropertyGroup>

Note that this will do nothing on BepInEx 5 as it only accepts a version number without a pre-release version or build metadata, so it strips both.

About

Source generator for quickly creating a BepInEx plugin scaffolding.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%