Skip to content

Module System

Алексей . edited this page Sep 29, 2026 · 4 revisions

Module System Overview

The module system provides a structured runtime for building features as isolated, manageable units. Each module has metadata, lifecycle management, telemetry, and a management UI via /sfs. Built-ins use SFModuleBase; external plugins implement ISFModule from SF.Abstractions and are discovered from SF/modules/*/module.json. See External Plugins.

Core Concepts

Module = Class + Attribute + Registration

A module is a class that:

  1. Implements ISFModule; built-ins can inherit SFModuleBase
  2. Has an [SFModule] attribute with metadata
  3. A built-in is registered in Program.cs via container.RegisterModule<T>()

Execution Models

Model Description
MainThread Runs on the game's main thread via SFSynchronizationContext. Use for anything touching game state.
BackgroundWorker Runs on a Task.Run thread pool thread. Use for I/O-heavy or network work.
Hybrid Starts inline, can switch threads via Context.SwitchToMainThreadAsync().

Restart Policies

Policy Behavior
Manual Module stays stopped after fault or completion. User must /sfs start manually.
OnFault Auto-restarts on unhandled exception. A circuit breaker disables auto-restart after 5 faults within 60 seconds.

Lifecycle States

Created → Starting → Running → Stopping → Stopped
                         ↓
                      Faulted

Module Lifecycle

// 1. Container calls module.RunAsync(context)
// 2. SFModuleBase sets Context and Log, then:

OnStartingAsync()          // Setup, allocate resources
  ↓
ExecuteAsync(token)        // Main loop - runs until cancelled or throws
  ↓ (success)
OnCompletedAsync()         // Clean completion
  ↓ (exception)
OnFaultedAsync(ex)         // Handle fault, log, notify
  ↓ (always)
OnStoppingAsync()          // Teardown
  ↓
OnStoppedAsync()           // Final cleanup
  ↓
context.Dispose()          // All registered disposables released

Registration

Built-in modules are registered in Program.cs:

var container = new SFModuleContainer();
container.RegisterModule<DialogScraper>();
container.RegisterModule<BrightBinder>();
container.RegisterModule<NodShaker>();
// ...
await container.Run();

Declared Dependencies must be registered and running before a module can start. Among eligible modules, Order (ascending), then DisplayName (alphabetical) determine startup order. Stop, restart and plugin unload are blocked when they would remove a provider required by running dependents.

Management Commands

Command Description
/sfs Interactive dashboard dialog - shows all modules with state, uptime, load, and action buttons
/sfs status [page] Quick status in chat (paginated, 6 per page)
/sfs info <id> Detailed info dialog for a specific module
/sfs start <id> Start a stopped module
/sfs stop <id> Stop a running module
/sfs restart <id> Restart a module
/sfs plugins List loaded plugins
/sfs plugin-load <id|path> Load a plugin by id or manifest path
/sfs plugin-unload <id> Unload a plugin and its modules
/sfs plugin-reload <id> Unload and load a plugin again

Module can be resolved by id, display name, or partial match.

Telemetry

Each module automatically tracks:

  • State - current lifecycle state
  • Uptime - time since last start
  • Load - duty cycle percentage (sync work / iteration period)
  • Loop metrics - count, avg/max iteration time
  • Counters - custom named counters (e.g., commands.invoked)
  • Details - custom key-value pairs (e.g., status=idle)
  • Heartbeat - last activity timestamp
  • Faults - fault count, last exception info
  • Memory - process memory snapshot (polled every 2s)
  • Threads - start and last thread IDs

Clone this wiki locally