# flenarndev/engine-level architecture

> Architecture diagram of the GitHub repository flenarndev/engine-level, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/flenarndev/engine-level
- Repository: https://github.com/flenarndev/engine-level

## Overview

1. **What this repo is**
   - `Engine-Level` is a native **F4SE plugin** for Fallout 4 / Fallout Cascadia.
   - It sits inside the game process and extends or alters engine behavior at runtime, rather than acting as an external app.

2. **Core boundary**
   - The main architectural boundary is between:
     - **the game engine/F4SE runtime**, and
     - **the plugin’s feature modules** under `src/`.
   - The plugin consumes engine events, hooks game functions, and exposes script-facing APIs back into the game.

3. **Bootstrap and composition**
   - `src/main.cpp` is the entry point that likely wires the whole plugin together.
   - From there, initialization fans out into shared hook setup, subsystem registration, script registration, and patch application.
   - This is a classic “single plugin binary, multiple feature modules” layout.

4. **Shared infrastructure layer**
   - `src/Shared` contains cross-cutting code used by most subsystems:
     - `Hooks` for detouring/interception,
     - `InputWatcher` for player input state,
     - helper utilities like `NPCFileUtils`, `PerkHelpers`, `SharedFunctions`, and shared declarations.
   - This layer is the glue between low-level engine interaction and higher-level gameplay features.

5. **Gameplay systems**
   - `src/Systems` holds the main feature areas:
     - `AmmoSwitch`
     - `Dialogue`
     - `ItemDegradation`
     - `LockLevels`
     - `Skills`
   - These are the likely “business logic” modules of the plugin, each focused on one gameplay mechanic or rule set.

6. **Event-driven integration**
   - `src/Events` contains handlers for game lifecycle and gameplay events:
     - menu open/close,
     - cell load,
     - hit events,
     - harvest,
     - script init,
     - load game,
     - animation graph,
     - level increase.
   - These events are the main input stream from the engine into the plugin’s systems, allowing feature modules to react to game state changes.

7. **UI/menu boundary**
   - `src/Menus` defines custom or extended menus such as:
     - dialogue,
     - examine/confirm flows,
     - level-up,
     - Pip-Boy tabs,
     - text input.
   - This indicates the plugin doesn’t only alter backend logic; it also injects or modifies UI flows that the player interacts with.

8. **Script exposure layer**
   - `src/Scripts` provides game-script-facing APIs in two forms:
     - **ObScript/console-style commands** under `src/Scripts/ObScript`
     - **Papyrus functions** under `src/Scripts/Papyrus`
   - Examples like `ToggleMarkers`, `GetBaseObject`, `SavePlayerFace`, and `Skills_Papyrus` suggest this repo exposes engine features to mod scripts and in-game scripting systems.

9. **Papyrus manager**
   - `src/Papyrus/Manager.cpp` and `.h` form the registration/coordination point for Papyrus-native functions.
   - This is the bridge between the scripting runtime and the C++ gameplay systems.

10. **Persistence and save-state**
   - `src/Serialization` handles save/load persistence for plugin state.
   - In a plugin like this, serialization is the mechanism that keeps custom gameplay state consistent across saves, loads, and session transitions.

11. **Low-level patching**
   - `src/Patches` contains direct engine/game behavior changes such as:
     - `Sprint`
     - `MoonRotation`
     - `LoadEditorIDs`
     - `TESObjectREFR`
   - These patches are the most invasive layer: they modify engine behavior when hooks or events are not enough.

12. **Data flow at a high level**
   - The usual path is:
     - engine/F4SE runtime starts the plugin,
     - hooks and patches are installed,
     - engine events and UI events enter the plugin,
     - systems process them,
     - script APIs expose controlled entry points back to the game,
     - serialization preserves state across load/save boundaries.
   - This makes the architecture event-driven, runtime-patched, and feature-modular.

13. **Technology and build context**
   - The project is built with **XMake** and depends on **CommonLibF4** for Fallout 4 engine access.
   - `src/detourXS` suggests function detouring support for runtime hooks.
   - This is a native C++ mod/plugin codebase, not a standalone application.

14. **How to diagram it**
   - A good architecture diagram would place the **game/F4SE runtime** at the center boundary, with arrows into:
     - **Shared hook/infrastructure**
     - **Event handlers**
     - **Gameplay systems**
     - **UI menus**
     - **Script APIs**
     - **Serialization**
     - **Patches**
   - The key idea is that these are not separate services; they are cooperating modules inside one injected runtime plugin.

Main entry points: [`src/main.cpp`](https://github.com/flenarndev/engine-level/blob/main/src/main.cpp) (Plugin entry), [`src/Shared/Hooks.cpp`](https://github.com/flenarndev/engine-level/blob/main/src/Shared/Hooks.cpp) (Hooks), [`src/Shared/SharedFunctions.cpp`](https://github.com/flenarndev/engine-level/blob/main/src/Shared/SharedFunctions.cpp) (Shared utils), [`src/Systems/Skills.cpp`](https://github.com/flenarndev/engine-level/blob/main/src/Systems/Skills.cpp) (Skills)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime Boundary"]
  node_game_runtime(("Game + F4SE<br/>host runtime"))
  node_commonlib[("CommonLibF4<br/>engine library")]
  node_detourxs[("detourXS<br/>detour library")]
  node_xmake["XMake<br/>build system<br/>[xmake.lua]"]
end

subgraph group_core["Plugin Core"]
  node_main["Plugin entry<br/>bootstrap<br/>[main.cpp]"]
  node_hooks["Hooks<br/>interception layer<br/>[Hooks.cpp]"]
  node_input_watcher["Input watcher<br/>input state<br/>[InputWatcher.cpp]"]
  node_shared_utils["Shared utils<br/>common helpers"]
end

subgraph group_features["Gameplay Systems"]
  node_ammo_switch["Ammo switch<br/>gameplay system<br/>[AmmoSwitch.cpp]"]
  node_dialogue["Dialogue<br/>gameplay system<br/>[Dialogue.cpp]"]
  node_degradation["Item degradation<br/>gameplay system"]
  node_lock_levels["Lock levels<br/>gameplay system<br/>[LockLevels.cpp]"]
  node_skills["Skills<br/>gameplay system<br/>[Skills.cpp]"]
end

subgraph group_integration["Integration"]
  node_events["Events"]
  node_menus["Menus<br/>ui layer"]
  node_papyrus_mgr["Papyrus manager<br/>script bridge<br/>[Manager.cpp]"]
  node_serialization["Serialization<br/>save-state<br/>[Serialization.cpp]"]
  node_patches["Patches<br/>[Patches.h]"]
  node_obscript["OBScript APIs<br/>script commands<br/>[ObScript.h]"]
  node_papyrus_api["Papyrus APIs<br/>script functions<br/>[Papyrus.h]"]
end

node_game_runtime -->|"loads"| node_main
node_main -->|"initializes"| node_hooks
node_main -->|"registers"| node_events
node_main -->|"registers"| node_menus
node_main -->|"registers"| node_papyrus_mgr
node_main -->|"enables"| node_serialization
node_main -->|"applies"| node_patches
node_hooks -->|"uses"| node_detourxs
node_hooks -->|"depends on"| node_shared_utils
node_input_watcher -->|"uses"| node_shared_utils
node_events -->|"drives"| node_ammo_switch
node_events -->|"drives"| node_dialogue
node_events -->|"drives"| node_degradation
node_events -->|"drives"| node_lock_levels
node_events -->|"drives"| node_skills
node_menus -->|"extends"| node_dialogue
node_menus -->|"extends"| node_skills
node_papyrus_mgr -->|"exposes"| node_papyrus_api
node_papyrus_mgr -->|"bridges"| node_skills
node_papyrus_api -->|"calls"| node_skills
node_obscript -->|"uses"| node_shared_utils
node_obscript -->|"controls"| node_degradation
node_serialization -->|"persists"| node_shared_utils
node_patches -->|"modifies"| node_game_runtime
node_patches -->|"cooperates with"| node_hooks
node_commonlib -->|"supports"| node_main
node_xmake -->|"builds"| node_main

click node_main "https://github.com/flenarndev/engine-level/blob/main/src/main.cpp"
click node_hooks "https://github.com/flenarndev/engine-level/blob/main/src/Shared/Hooks.cpp"
click node_input_watcher "https://github.com/flenarndev/engine-level/blob/main/src/Shared/InputWatcher.cpp"
click node_shared_utils "https://github.com/flenarndev/engine-level/blob/main/src/Shared/SharedFunctions.cpp"
click node_ammo_switch "https://github.com/flenarndev/engine-level/blob/main/src/Systems/AmmoSwitch.cpp"
click node_dialogue "https://github.com/flenarndev/engine-level/blob/main/src/Systems/Dialogue.cpp"
click node_degradation "https://github.com/flenarndev/engine-level/blob/main/src/Systems/ItemDegradation.cpp"
click node_lock_levels "https://github.com/flenarndev/engine-level/blob/main/src/Systems/LockLevels.cpp"
click node_skills "https://github.com/flenarndev/engine-level/blob/main/src/Systems/Skills.cpp"
click node_papyrus_mgr "https://github.com/flenarndev/engine-level/blob/main/src/Papyrus/Manager.cpp"
click node_serialization "https://github.com/flenarndev/engine-level/blob/main/src/Serialization/Serialization.cpp"
click node_patches "https://github.com/flenarndev/engine-level/blob/main/src/Patches/Patches.h"
click node_obscript "https://github.com/flenarndev/engine-level/blob/main/src/Scripts/ObScript.h"
click node_papyrus_api "https://github.com/flenarndev/engine-level/blob/main/src/Scripts/Papyrus.h"
click node_commonlib "https://github.com/flenarndev/engine-level/tree/main/lib/commonlibf4"
click node_detourxs "https://github.com/flenarndev/engine-level/tree/main/src/detourXS"
click node_xmake "https://github.com/flenarndev/engine-level/blob/main/xmake.lua"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_game_runtime,node_commonlib,node_detourxs,node_xmake toneBlue
class node_main,node_hooks,node_input_watcher,node_shared_utils toneAmber
class node_ammo_switch,node_dialogue,node_degradation,node_lock_levels,node_skills toneMint
class node_events,node_menus,node_papyrus_mgr,node_serialization,node_patches,node_obscript,node_papyrus_api toneRose
```

## Components

### Runtime Boundary

Game engine and F4SE host environment

- **Game + F4SE**: host runtime · Fallout 4 / Fallout Cascadia process with F4SE plugin loading
- [CommonLibF4](https://github.com/flenarndev/engine-level/tree/main/lib/commonlibf4): engine library · External engine access library used by the plugin · `lib/commonlibf4`
- [detourXS](https://github.com/flenarndev/engine-level/tree/main/src/detourXS): detour library · Function detouring support for hooks and patches · `src/detourXS`
- [XMake](https://github.com/flenarndev/engine-level/blob/main/xmake.lua): build system · Build configuration and project generation · `xmake.lua`

### Plugin Core

Bootstrap and shared infrastructure inside the injected module

- [Plugin entry](https://github.com/flenarndev/engine-level/blob/main/src/main.cpp): bootstrap · Entry point that wires initialization and module setup · `src/main.cpp`
- [Hooks](https://github.com/flenarndev/engine-level/blob/main/src/Shared/Hooks.cpp): interception layer · Shared detours and function interception setup · `src/Shared/Hooks.cpp`
- [Input watcher](https://github.com/flenarndev/engine-level/blob/main/src/Shared/InputWatcher.cpp): input state · Tracks player input state for gameplay features · `src/Shared/InputWatcher.cpp`
- [Shared utils](https://github.com/flenarndev/engine-level/blob/main/src/Shared/SharedFunctions.cpp): common helpers · Cross-cutting helpers and declarations used by multiple systems · `src/Shared/SharedFunctions.cpp`

### Gameplay Systems

Main feature modules that implement gameplay rules

- [Ammo switch](https://github.com/flenarndev/engine-level/blob/main/src/Systems/AmmoSwitch.cpp): gameplay system · Ammo selection and switching behavior · `src/Systems/AmmoSwitch.cpp`
- [Dialogue](https://github.com/flenarndev/engine-level/blob/main/src/Systems/Dialogue.cpp): gameplay system · Dialogue-related gameplay logic · `src/Systems/Dialogue.cpp`
- [Item degradation](https://github.com/flenarndev/engine-level/blob/main/src/Systems/ItemDegradation.cpp): gameplay system · Weapon and armor degradation rules · `src/Systems/ItemDegradation.cpp`
- [Lock levels](https://github.com/flenarndev/engine-level/blob/main/src/Systems/LockLevels.cpp): gameplay system · Lock difficulty and level logic · `src/Systems/LockLevels.cpp`
- [Skills](https://github.com/flenarndev/engine-level/blob/main/src/Systems/Skills.cpp): gameplay system · Skill progression and related rules · `src/Systems/Skills.cpp`

### Integration

Events, menus, scripting, and persistence bridges

- **Events**: engine events · Lifecycle and gameplay event handlers
- **Menus**: ui layer · Custom and extended in-game menus
- [Papyrus manager](https://github.com/flenarndev/engine-level/blob/main/src/Papyrus/Manager.cpp): script bridge · Registers and coordinates Papyrus-native functions · `src/Papyrus/Manager.cpp`
- [Serialization](https://github.com/flenarndev/engine-level/blob/main/src/Serialization/Serialization.cpp): save-state · Persists plugin state across save/load boundaries · `src/Serialization/Serialization.cpp`
- [Patches](https://github.com/flenarndev/engine-level/blob/main/src/Patches/Patches.h): runtime patches · Direct engine behavior modifications and fixes · `src/Patches/Patches.h`
- [OBScript APIs](https://github.com/flenarndev/engine-level/blob/main/src/Scripts/ObScript.h): script commands · Console-style commands exposed to scripts and commands · `src/Scripts/ObScript.h`
- [Papyrus APIs](https://github.com/flenarndev/engine-level/blob/main/src/Scripts/Papyrus.h): script functions · Papyrus-facing functions exposed by the plugin · `src/Scripts/Papyrus.h`

## Connections

- Game + F4SE → Plugin entry: loads · The game process loads the plugin entry point
- Plugin entry → Hooks: initializes · Bootstrap sets up shared hooks
- Plugin entry → Events: registers · Bootstrap wires engine event handlers
- Plugin entry → Menus: registers · Bootstrap installs UI/menu integrations
- Plugin entry → Papyrus manager: registers · Bootstrap connects Papyrus-native functions
- Plugin entry → Serialization: enables · Bootstrap enables persistent plugin state
- Plugin entry → Patches: applies · Bootstrap activates runtime patches
- Hooks → detourXS: uses · Shared hook layer relies on detouring support
- Hooks → Shared utils: depends on · Hook logic shares common helpers and declarations
- Input watcher → Shared utils: uses · Input tracking relies on shared declarations
- Events → Ammo switch: drives · Engine events trigger gameplay systems
- Events → Dialogue: drives · Engine events trigger gameplay systems
- Events → Item degradation: drives · Engine events trigger gameplay systems
- Events → Lock levels: drives · Engine events trigger gameplay systems
- Events → Skills: drives · Engine events trigger gameplay systems
- Menus → Dialogue: extends · UI flows support dialogue behavior
- Menus → Skills: extends · Level-up and related UI connect to skills
- Papyrus manager → Papyrus APIs: exposes · Manager publishes Papyrus functions
- Papyrus manager → Skills: bridges · Script functions reach gameplay logic
- Papyrus APIs → Skills: calls · Papyrus functions operate on skill systems
- OBScript APIs → Shared utils: uses · Console commands reuse shared helpers
- OBScript APIs → Item degradation: controls · Commands toggle degradation-related behavior
- Serialization → Shared utils: persists · Save/load logic preserves shared plugin state
- Patches → Game + F4SE: modifies · Patches alter engine behavior at runtime
- Patches → Hooks: cooperates with · Patches and hooks jointly intercept engine behavior
- CommonLibF4 → Plugin entry: supports · Engine access library underpins the plugin code
- XMake → Plugin entry: builds · Build system produces the plugin binary
