Profiler
public static class ProfilerNamespace Snitch.Api
The Snitch profiler’s modder API. Reference Snitch.Api.dll OR drop this single file into your mod. Every call is a zero-overhead no-op when the Snitch profiler is not installed, and lights up automatically when it is - so you can ship this unconditionally with no hard dependency.
using Snitch.Api;using (Profiler.Sample("MyMod.Pathfinding")) { ...expensive work... } // times a sectionProfiler.RegisterCounter("MyMod.QueueLen", () => _queue.Count, "items");Profiler.RegisterStateProvider("MyMod.Jobs", () => new StateSnapshot { Title = "Jobs" } .Add("running", _running).Add("queued", _queued));Tip: a class named SnitchProbe with a static Register() is auto-discovered and called on bind (see AutoRegister), so your mod’s Core doesn’t need to wire anything. Snitch also auto-times every mod’s OnUpdate etc., so basic per-mod frame cost needs no code at all.
All calls MUST be made from the Unity main thread. Counter/state delegates are invoked by the host on the main thread, so they may safely touch game objects.
Ids and labels are free text. The convention is “<ModName>.<Thing>”, e.g. “MyMod.Pathfinding”. A section label groups by the part before the first dot; a label without a dot lands in “(ungrouped)”. Section times are in milliseconds. Counters and state providers are polled at the host’s PollHz preference, 4 times a second by default and 1 to 30 at most. Sections only record while sampling is on (Enabled); panels, controls and log lines exist as soon as they are registered.
Example
A panel with a counter, a slider and a log, set up from a probe class:
internal static class SnitchProbe{ public static void Register() { Profiler.RegisterPanel("MyMod", "My Mod") .Counter("Queue", () => Jobs.Count, "items") .Slider("Spawn radius", 5, 100, () => Prefs.Radius, v => Prefs.Radius = (float)v, step: 5, unit: "m") .Log(); }}Properties
Section titled “Properties”Enabled
Section titled “Enabled”public static bool Enabled { get; }True only when the Snitch host is installed AND sampling is currently armed. Gate hot loops on this for the absolutely-free path: if (Profiler.Enabled) using (Profiler.Sample("X")) { ... }.
Methods
Section titled “Methods”Sample
Section titled “Sample”public static Scope Sample(string label)Types: Scope
Time a section. using (Profiler.Sample("MyMod.Foo")) { ... }. No heap allocation; a no-op (default scope) when the host is absent or sampling is off.
| Parameter | |
|---|---|
label | The section name, e.g. “MyMod.Pathfinding”. The part before the first dot is the group. Null or empty records as “(unnamed)”. |
Returns A scope that stops the timer when it is disposed.
public static void Begin(string label)Manual section begin (pair with End). Prefer Sample.
| Parameter | |
|---|---|
label | The section name, e.g. “MyMod.Pathfinding”. End takes the same string. |
public static void End(string label)Manual section end for a Begin call.
| Parameter | |
|---|---|
label | The same string that was passed to Begin. |
RegisterCounter
Section titled “RegisterCounter”public static void RegisterCounter( string id, Func<double> read, string unit = null)Register a numeric gauge polled by the host at a few Hz. Re-registering the same id replaces it. Load-order-proof: if Snitch loads AFTER your mod, the registration is queued and applied on bind.
| Parameter | |
|---|---|
id | The counter id, e.g. “MyMod.QueueLen”. An id that starts with “<panelId>.” shows inside that panel. Null or empty is ignored by the host. |
read | Returns the current value. Called on the main thread at the poll rate. A throw shows the counter as UNAVAILABLE. |
unit | Text shown after the value, e.g. “items” or “ms”. Null shows no unit. |
UnregisterCounter
Section titled “UnregisterCounter”public static void UnregisterCounter(string id)Remove a counter registered with RegisterCounter.
| Parameter | |
|---|---|
id | The id passed to RegisterCounter. |
RegisterStateProvider
Section titled “RegisterStateProvider”public static void RegisterStateProvider( string id, Func<StateSnapshot> snapshot)Types: StateSnapshot
Register an entity/state-distribution snapshot, polled by the host at a few Hz on the main thread. Load-order-proof (queued until the host binds).
| Parameter | |
|---|---|
id | The provider id, e.g. “MyMod.Jobs”. An id that starts with “<panelId>.” shows inside that panel. Null or empty is ignored by the host. |
snapshot | Returns the current distribution. Called on the main thread at the poll rate. Null shows an empty distribution titled with the id. |
UnregisterStateProvider
Section titled “UnregisterStateProvider”public static void UnregisterStateProvider(string id)Remove a provider registered with RegisterStateProvider.
| Parameter | |
|---|---|
id | The id passed to RegisterStateProvider. |
public static void Mark(string label)Annotate a one-off event/spike in the timeline.
| Parameter | |
|---|---|
label | Short text for the event, e.g. “MyMod.SaveLoaded”. Null or empty is ignored. The host keeps the last 32 marks. |
RegisterAblationLever
Section titled “RegisterAblationLever”public static void RegisterAblationLever( string name, Action apply, Action restore)Register an ablation lever for your subsystem so ‘snitch ablate <name>’ can measure its causal frame-time cost. apply turns your subsystem OFF, restore turns it back ON. Both run on the main thread. Load-order-proof (queued until the host binds).
| Parameter | |
|---|---|
name | The lever name typed after snitch ablate, e.g. “mymod”. Matched without case. Null or empty is ignored. The same name again replaces the lever. |
apply | Turns the subsystem off. Null is ignored. |
restore | Turns the subsystem back on. Null marks the lever as not reversible. |
RegisterPanel
Section titled “RegisterPanel”public static Panel RegisterPanel(string id, string title = null)Types: Panel
Declare a panel: a named, toggleable, movable, resizable area in the Snitch overlay + web dashboard that groups everything this mod reports. Counters/state you register with an id starting “id.” automatically appear inside it. Returns a builder so you can fluently add text/actions/toggles/log. The in-game replacement for a mod’s own debug window. Load-order-proof; a no-op (the builder still works) if Snitch is absent.
| Parameter | |
|---|---|
id | The panel id, usually the mod name, e.g. “Siesta”. It is also the panel’s log channel. Null or empty registers nothing. |
title | The heading shown on the panel, e.g. “Siesta NPC LOD”. Null or empty shows the id. |
Returns A builder whose calls add to this panel.
RegisterAction
Section titled “RegisterAction”public static void RegisterAction(string panelId, string label, Action run)A clickable button in a mod’s panel - the in-game replacement for a debug hotkey action. Runs on the main thread when clicked (overlay/dashboard) or via ‘snitch act’. Load-order-proof.
| Parameter | |
|---|---|
panelId | The id passed to RegisterPanel, e.g. “Siesta”. |
label | The button text, e.g. “Force cosmetic”. Null or empty is ignored. The same label again replaces the button. The id for snitch act is “<panelId>:” plus the label in lower case, with each run of other characters than letters and digits as one ”-”, e.g. “Siesta:force-cosmetic”. |
run | Runs on the main thread when the button is pressed. |
RegisterToggle
Section titled “RegisterToggle”public static void RegisterToggle( string panelId, string label, Func<bool> get, Action<bool> set)An on/off control in a mod’s panel - the in-game replacement for a toggle hotkey. get reports the current state, set applies it; both run on the main thread. Load-order-proof.
| Parameter | |
|---|---|
panelId | The id passed to RegisterPanel, e.g. “MyMod”. |
label | The text next to the switch, e.g. “Freeze LOD”. Null or empty is ignored. The id for snitch toggle is built like the one in RegisterAction, e.g. “MyMod:freeze-lod”. |
get | Returns the current state. Null is ignored. |
set | Applies a new state. Null is ignored. |
RegisterSlider
Section titled “RegisterSlider”public static void RegisterSlider( string panelId, string label, double min, double max, Func<double> get, Action<double> set, double step = 0d, string unit = null)A draggable value in a mod’s panel - the in-game replacement for typing a number into a console to find it. Appears as a real slider in the Snitch overlay and in the web dashboard, and is settable by id with snitch slider.
get reports the current value, set applies it; both run on the main thread. The host clamps to [min, max] and snaps to step (0 = continuous) BEFORE calling the setter, so a setter never sees a value outside the range it declared - wherever the write came from. Load-order-proof.
| Parameter | |
|---|---|
panelId | The id passed to RegisterPanel, e.g. “MyMod”. |
label | The text next to the slider, e.g. “pull force”. Null or empty is ignored. The id for snitch slider is built like the one in RegisterAction, e.g. “MyMod:pull-force”. |
min | The lowest value. |
max | The highest value. It must be greater than min, otherwise nothing is registered. |
get | Returns the current value. Null is ignored. |
set | Applies a new value, already clamped and snapped. Null is ignored. |
step | The snap size, e.g. 500. 0 or less is continuous. |
unit | Text shown after the value, e.g. “N” or “m”. Null shows no unit. |
Example
panel.Slider("pull force", 0, 40000, () => Prefs.Pull, v => Prefs.Pull = (float)v, step: 500, unit: "N");RegisterText
Section titled “RegisterText”public static void RegisterText(string panelId, Func<string> provider)A free-text, multi-line readout in a mod’s panel (anything a counter/distribution can’t express). Polled by the host on the main thread. Load-order-proof.
| Parameter | |
|---|---|
panelId | The id passed to RegisterPanel, e.g. “MyMod”. |
provider | Returns the text to show. Null is ignored. Each call adds one more readout. |
BindPanelLog
Section titled “BindPanelLog”public static void BindPanelLog(string panelId)Mark that a panel should display its own log channel (the lines you send via Log / Panel.Write with the same id). Load-order-proof.
| Parameter | |
|---|---|
panelId | The id passed to RegisterPanel, e.g. “MyMod”. It is also the log channel. |
public static void Log( string channel, string message, LogLevel level = LogLevel.Info)Types: LogLevel
Send a log line to a channel (use your mod/panel id as the channel). It appears in that mod’s panel log AND in Snitch’s combined timeline. Keep calling your own MelonLogger too if you want it in Latest.log. Load-order-proof; a no-op if Snitch is absent.
| Parameter | |
|---|---|
channel | The channel name, the panel id for a panel’s log, e.g. “MyMod”. Null or empty logs to “misc”. |
message | The log text. Null or empty is ignored. |
level | Info, Warning or Error. The host keeps the last 400 lines per channel. |
AutoRegister
Section titled “AutoRegister”public static void AutoRegister()Discover a convention type named SnitchProbe with a static Register() in THIS mod’s own assembly and invoke it once - so a mod never has to wire a Register() call into its Core. Drive it from a [ModuleInitializer] in your probe file. No-op + load-order-proof: discovery is deferred until the host binds, and is a permanent no-op if Snitch is not installed.