# nightmoon1996/knm-save architecture

> Architecture diagram of the GitHub repository nightmoon1996/knm-save, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nightmoon1996/knm-save
- Repository: https://github.com/nightmoon1996/knm-save

## Overview

##### Project type and purpose
- This is a server-side plugin/script for Garry’s Mod (specifically the DarkRP gamemode).
- Its sole purpose is to persist player data (job, ammo, health, shield) into an SQLite database on various events (spawn, disconnect, shutdown) and reload it when they rejoin.

##### File structure
- Top level:
- README.md  (documentation)
- sv_knmsave.lua  (the single entrypoint Lua script)
- No separate frontend/backend folders – everything lives in one server-script file.
- No build or deployment scripts; installation is a simple file drop.

##### README for additional insights
- Hooks used: PlayerInitialSpawn, PlayerDisconnected, ShutDown
- SQL module: built-in Garry’s Mod SQLite interface
- Operational flow:
a. On PlayerInitialSpawn → read data from database → apply to player
b. On PlayerDisconnected / ShutDown → collect current data → write to database

##### Main components and interactions
a. Components:
1. Game Client (the player running Garry’s Mod)
2. Garry’s Mod Server (running DarkRP gamemode)
3. sv_knmsave.lua plugin
4. SQLite Database (local file on the server)
b. Interactions:
- The Game Client connects to the Garry’s Mod Server.
- DarkRP triggers PlayerInitialSpawn → the plugin issues “SELECT” queries to SQLite to load saved state → plugin updates the Player object.
- During gameplay, state is held in memory by DarkRP.
- On PlayerDisconnected or on Server ShutDown → plugin reads in-memory state from DarkRP’s Player object → issues “INSERT/UPDATE” queries to SQLite.
c. Architectural patterns/design principles:
- Event-driven architecture (hook into lifecycle events)
- Single responsibility: plugin only handles persistence
- Direct database access via built-in SQL module (no ORM, no external service)
d. Key technologies:
- Lua (sv_knmsave.lua)
- Garry’s Mod hook system (PlayerInitialSpawn, PlayerDisconnected, ShutDown)
- SQLite (built-in Garry’s Mod sql module)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Components
    Player["Player (Game Client)"]:::game
    DarkRP["DarkRP Gamemode"]:::game
    Plugin["sv_knmsave.lua"]:::persistence
    DB[(SQLite Database)]:::db
    Doc["README.md"]:::doc

    %% Connections
    Player -->|"connect"| DarkRP
    DarkRP -->|"PlayerInitialSpawn\n(loadData())"| Plugin
    Plugin -->|"SELECT * WHERE steamID=..."| DB
    DB -->|"row data"| Plugin
    Plugin -->|"applyPlayerData()"| DarkRP
    DarkRP -->|"PlayerDisconnected/ShutDown\n(capture state)"| Plugin
    Plugin -->|"INSERT OR REPLACE..."| DB

    %% Click Events
    click Plugin "https://github.com/nightmoon1996/knm-save/blob/master/sv_knmsave.lua"
    click Doc "https://github.com/nightmoon1996/knm-save/blob/master/README.md"

    %% Styles
    classDef game fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef persistence fill:#90EE90,stroke:#000,stroke-width:1px
    classDef db fill:#FFD700,stroke:#000,stroke-width:1px
    classDef doc fill:#E6E6FA,stroke:#000,stroke-width:1px
```
