CoreMod
This page uses KeyCode from Unity. The glossary says what each one is.
CoreMod is a handle for one mod. It remembers your mod id and passes it on to the framework’s save data, keybind, log and patch calls. One handle belongs to one mod id.
CoreMod a handle for one mod
Call CoreMod.For once with your plugin, then use the handle for Save, Key, Log and the patch calls.
public sealed class CoreMod
{
public static CoreMod For(BaseUnityPlugin plugin);
public string Id { get; }
public CoreLog Log { get; }
public void Save<T>(string key, Func<T> save, Action<T> load) where T : class, new();
public ModKeybind Key(string id, string displayName, KeyCode defaultKey);
public bool Prefix(Type target, string method, Type handlerType, string handlerName);
public bool Prefix(Type target, string method, Type[] args, Type handlerType, string handlerName);
public bool Postfix(Type target, string method, Type handlerType, string handlerName);
public bool Postfix(Type target, string method, Type[] args, Type handlerType, string handlerName);
public bool Finalizer(Type target, string method, Type handlerType, string handlerName);
public bool Finalizer(Type target, string method, Type[] args, Type handlerType, string handlerName);
}
| Method | Returns | What it does |
|---|---|---|
For(plugin) | CoreMod | The handle for a BepInEx plugin. |
Save<T>(key, save, load) | void | Registers something to save in the player’s save. |
Key(id, displayName, defaultKey) | ModKeybind | Registers a keybind. |
Prefix(target, method, handlerType, handlerName) | bool | Runs your method before a game method. |
Prefix(target, method, args, handlerType, handlerName) | bool | The same, for a game method with several versions. |
Postfix(target, method, handlerType, handlerName) | bool | Runs your method after a game method. |
Postfix(target, method, args, handlerType, handlerName) | bool | The same, for a game method with several versions. |
Finalizer(target, method, handlerType, handlerName) | bool | Runs your method when a game method throws an error. |
Finalizer(target, method, args, handlerType, handlerName) | bool | The same, for a game method with several versions. |
| Property | Type | What it means |
|---|---|---|
Id | string | Your mod id. |
Log | CoreLog | Your mod’s log. |
Things to know
- A framework mod already has its handle, in
Mod. Only a BepInEx plugin callsCoreMod.For, becauseForneeds a BepInEx plugin object, and a framework mod has no BepInEx plugin class. (A framework mod gets its handle fromFrameworkMod.Mod.) - Asking for the handle twice gives you the same handle. The mod id is the plugin’s GUID, and capital letters do not matter. (
Forkeys the handle onplugin.Info.Metadata.GUID, compared without case.) Idis the mod id the handle was first made with. It is the id the patch helpers file their changes under, and the idKeyuses for the keybind. See Mod id and Owner.Logwrites to your mod’s own log file, and it stops a flood of errors from filling the file. It writes the same error only once in 10 seconds, no more than 20 errors in 10 seconds, and no more than 100 lines in a second. A mod can raise this withLog.LineLimit. When it drops lines, it writes one summary line, such as5 error lines suppressed: the same error is written once in 10 seconds and at most 20 errors are written in 10 secondsor5 lines suppressed: this log accepts 100 lines per second, raise CoreLog.LineLimit to allow more, with the next line it writes or when the game quits. That line names the limit that dropped the lines and how to raise it. (LogisCoreLog.For(Id). TheError(message, exception)overload counts an error as the same one when the exception type and first stack frame match.)Saveregisters something to save in the player’s save, under a name made from your mod id and your key. Use a key of letters, digits,.,_and-. It follows the same rules asSaveData.Register(see Game save data). (It callsSaveData.Registerwith the name<Id>.<key>.json.)Keyregisters a keybind and gives you whatKeybinds.Registergives you, which can benull. (It callsKeybinds.Register(Id, id, displayName, defaultKey).)Prefix,PostfixandFinalizerchange a game method by name, using your mod id. They are the same as theCorePatchershort helpers in the next section, with your mod id filled in for you.
Limits
- Passing
nulltoForthrows anArgumentNullException. - You can register each
Savename only once. The second time throws anInvalidOperationException.