Skip to content

Module System

Алексей . edited this page Apr 12, 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.

Core Concepts

Module = Class + Attribute + Registration

A module is a class that:

  1. Inherits SFModuleBase
  2. Has an [SFModule] attribute with metadata
  3. 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

Modules are registered in Program.cs:

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

Modules are sorted by Order (ascending), then DisplayName (alphabetical).

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

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