MonkeyInjectionLibrary
A library mod that lets other mods add properties and methods to existing base-game classes at runtime, without modifying game files.
模組介紹 作者原文
MonkeyInjectionLibrary
============
A library mod that lets other mods add properties and methods to existing base-game classes at runtime, without modifying game files.
This mod does nothing on its own – it exists to support other mods.
Overview
This library provides an interface-based monkey-patching system for Unity games.
Instead of creating your own preloader, mod authors define interfaces that describe the methods/properties they want to add.
At runtime, the library injects those interfaces into the target classes using metadata provided by custom attributes.
This approach keeps patches:
- Declarative
- Easier to reason about
- Less fragile across game updates
How It Works
- Mod authors define one or more interfaces.
- Attributes are applied to describe:
- Which Class to inject into
- How errors should be handled
- The library scans loaded assemblies marked for injection.
- Interfaces are injected into the target Unity classes at runtime.
Required Attributes
InjectionIdentifier
Uniquely identifies an assembly and its version.
Usage
- Must be placed at the assembly level
- Accepts an identifier string (such as a mod GUID) and a version string
Fallback Behavior
For backwards compatibility if this attribute is missing, the library resolves the identifier and version in the following order:
- BepInPlugin attribute metadata (GUID and Version) if defined on any type in the assembly.
- The assembly's name (AssemblyName) and version (AssemblyVersion).
RequiresInjections
Marks an assembly as containing injectable interfaces.
This attribute is required for the library to detect your mod.
Usage
- Must be placed at the assembly level
- Should be included in every file that defines injection interfaces
Without this attribute, your mod will be ignored entirely.
InjectInterface
Specifies the base-game class that an interface should be injected into.
Usage
- Applied to an interface
- Accepts a Type reference or class name of a base-game class
An interface may target multiple classes by applying multiple InjectInterface attributes.
Error Handling
Injection errors can be handled using the HandleErrors attribute.
If no attribute is specified, the default behavior is Terminate.
HandleErrors
Defines how the library reacts when an error occurs during injection.
Usage locations
- Assembly
- Interface
- Individual property or method
More specific scopes override more general ones.
ErrorHandlingStrategy
Available error handling strategies:
- Terminate (Default)
Immediately stops the injection process when an error occurs.
- Ignore
Silently ignores errors and continues execution.
- LogWarning
Logs the error as a warning and continues execution.
- LogError
Logs the error as an error and continues execution.
Attribute Precedence
When multiple HandleErrors attributes are present, the following order applies (highest priority last):
- Default
- Assembly
- Interface
- Method / Property
The most specific setting always wins.
Intended Use
This library is intended for:
- Advanced mods that need controlled runtime modification of Unity classes
It is not intended for direct use by end users.
Notes
- This is a library mod and has no visible effect on gameplay by itself
- Other mods may depend on it
- Removing it may break mods that rely on interface injection
Example Interface
using InjectionLibrary;
using InjectionLibrary.Attributes;
[assembly: RequiresInjections]
[assembly: InjectionIdentifier("com.example.mod", "1.0.0")]
[assembly: HandleErrors(ErrorHandlingStrategy.Terminate)]
namespace ExampleMod.Interfaces;
[HandleErrors(ErrorHandlingStrategy.LogError)]
[InjectInterface(nameof(StartOfRound))]
public interface ICrashingInterface
{
void Awake();
[HandleErrors(ErrorHandlingStrategy.LogWarning)]
void Start();
[HandleErrors(ErrorHandlingStrategy.Ignore)]
void Update();
}
安裝與使用 安裝:BepInEx 外掛
- 本站下載的壓縮包就是模組本體,通常是 BepInEx 外掛(解壓後得到 .dll 或外掛資料夾)。
- 建議流程:用社群模組管理器(如 r2modman)選中本遊戲後匯入該模組,管理器會自動裝好 BepInEx 前置並處理依賴。
- 手動流程:先在遊戲目錄裝好 BepInEx 載入器,再把解壓出的 .dll 或外掛資料夾放進 BepInEx/plugins/。
- 啟動遊戲,BepInEx 控制台會列印載入日誌;確認這個模組出現在日誌裡即為安裝成功。
帶「前置依賴」的模組需要一併安裝,否則不會載入。功能開關通常在遊戲內或外掛自帶的設定檔裡調整。
下面這些內容需要先安裝,否則該模組不會生效: