Skip to content

Writing Code

You should have a project that looks like this:

  • CHANGELOG.md
  • Config.Build.user.props.template
  • Directory.Build.props
  • Directory.Build.targets
  • icon.png
  • LICENSE
  • PeakMod.sln
  • README.md
  • Directorysrc
    • DirectoryPeakMod
      • PeakMod.csproj
      • Plugin.cs
      • thunderstore.toml

Open the project in your IDE of choice where the .sln file is, and then open the Plugin.cs file. It should look something like this:

Plugin.cs
using BepInEx;
using BepInEx.Logging;
namespace PeakMod;
// Here are some basic resources on code style and naming conventions to help
// you in your first CSharp plugin!
// https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions
// https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names
// https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-namespaces
// This BepInAutoPlugin attribute comes from the Hamunii.BepInEx.AutoPlugin
// NuGet package, and it will generate the BepInPlugin attribute for you!
// For more info, see https://github.com/Hamunii/BepInEx.AutoPlugin
[BepInAutoPlugin]
public partial class Plugin : BaseUnityPlugin
{
internal static ManualLogSource Log { get; private set; } = null!;
private void Awake()
{
// BepInEx gives us a logger which we can use to log information.
// See https://lethal.wiki/dev/fundamentals/logging
Log = Logger;
// BepInEx also gives us a config file for easy configuration.
// See https://lethal.wiki/dev/intermediate/custom-configs
// We can apply our hooks here.
// See https://lethal.wiki/dev/fundamentals/patching-code
// Log our awake here so we can see it in LogOutput.log file
Log.LogInfo($"Plugin {Name} is loaded!");
}
}

This is a very basic BepInEx 5 plugin. For more info, see BepInEx’s own documentation:

Creating a new plugin project—Plugin structure

You can also follow the links to learn more about each thing mentioned:

Logging: lethal.wiki

Custom Configs: lethal.wiki

Patching Code: lethal.wiki

For patching/hooking methods, you can use MonoMod or HarmonyX. See lethal.wiki for more in-depth introductions on using them.

All of these libraries are compatible with each other as HarmonyX and MonoDetour simply use MonoMod.RuntimeDetour under the hood.

TODO