# suprcode/crystal architecture

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

- Interactive diagram: https://gitdiagram.com/suprcode/crystal
- Repository: https://github.com/suprcode/crystal

## Overview

##### Project Type and Purpose
- It consists of:
- A Windows client (WinForms + custom rendering via SlimDX)
- A game server (core logic, world simulation, network interfaces)
- Administrative tools (AutoPatcherAdmin, Server.MirForms, LibraryEditor, LibraryViewer)
- A patcher website (static assets + Web.config / HTTP service)
- A shared library for packets, data models, and utilities

##### File‐Structure Analysis
- “Client” holds the game client code (UI under Forms, rendering under MirGraphics, input, network under MirNetwork, game objects under MirObjects, sounds under MirSounds, utilities, settings).
- “Server” contains:
- Server.Library – common game-server libraries
- Server.MirForms – WinForms admin console for database/tuning/config
- Server – core server engine (MirEnvir, MirDatabase, MirNetwork, MirObjects, Helpers, Utils)
- “Shared” houses protocol definitions (ClientPackets, ServerPackets), data models, enums, extension methods – referenced by both client & server.
- “AutoPatcherAdmin” is a WinForms tool to generate/apply patches.
- “PatcherWebSite” serves static + JS/CSS assets; likely hosts patch manifest & files via HTTP.
- “LibraryEditor” / “LibraryViewer” are separate tools for inspecting graphic asset libraries.

##### Key Architectural Patterns
- Layered architecture: UI → Application Services → Domain (game logic) → Data (DB)
- Client-server separation via TCP (MirConnection for game traffic; MirStatusConnection).
- Shared contract layer (Shared project) enforces protocol compatibility.
- Admin and patcher tools reuse Shared and Server.Library where applicable.
- Web patcher uses HTTP service for delivering updates.

##### Main Components
a. Client Application
– UI Layer (WinForms Forms & custom MirControls)
– Rendering Layer (MirGraphics + SlimDX)
– Sound Layer (MirSounds)
– Network Layer (MirNetwork)
– Game Objects Layer (MirObjects & MirScenes)

b. Server Application
– Network Interfaces (MirConnection, MirStatusConnection)
– Core Simulation (MirEnvir, Timers, Robot)
– Domain Objects (MirObjects)
– Persistence Layer (MirDatabase via ORM/ADO.NET to SQL DB)
– Admin UI (Server.MirForms for config & tuning)

c. Shared Library
– Protocol Contracts (ClientPackets, ServerPackets)
– Data Models & Enums
– Utility Functions (FileIO, Crypto, IniReader)

d. Patching Infrastructure
– AutoPatcherAdmin Tool (desktop app to build patch manifests)
– Patcher Website (HTTP Server exposing manifests & files)

e. External Dependencies
– SQL Server / MySQL (game data storage)
– SlimDX (graphics)
– log4net (logging)

##### Relationships & Data Flows
- Client ↔ Server: bi‐directional TCP over “MirConnection” for gameplay; “MirStatusConnection” for login/realm status.
- Server ↔ Database: CRUD via MirDatabase classes.
- Admin UI (Server.MirForms) ↔ Server.Library / Database: direct in‐process calls or DB access.
- AutoPatcherAdmin → generates patch manifest → deploy to PatcherWebSite HTTP endpoints → Client fetches updates.
- LibraryEditor/Viewer: offline tools reading asset files.

##### Architectural Patterns & Principles
- Separation of concerns: each layer/project has clear responsibility.
- Reuse of “Shared” contracts to avoid packet mismatches.
- Event‐driven simulation on server (Timer, DelayedAction, AI robots).
- Custom UI controls to encapsulate Mir-specific behavior.
- Modular project structure for extensibility (e.g., custom monsters, scenes).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Process"
        UI["Client.UI\n(WinForms & MirControls)"]:::client
        REND["Client.Rendering\n(MirGraphics + SlimDX)"]:::client
        SOUND["Client.Sound\n(MirSounds)"]:::client
        NET["Client.Network\n(Network.cs)"]:::client
        OBJ["Client.GameObjects\n(MirObjects & MirScenes)"]:::client
    end

    subgraph "Server Process"
        SNET["Server.Network Interfaces\n(MirConnection & MirStatusConnection)"]:::server
        CORE["Server.Core Simulation\n(MirEnvir, Timer, Robot, ChatSystem)"]:::server
        DOMAIN["Server.Domain Objects\n(MirObjects)"]:::server
        PERS["Server.Persistence\n(MirDatabase)"]:::server
    end

    subgraph "Admin Tools"
        ADMIN["Admin UI\n(Server.MirForms)"]:::tool
        LIBEDIT["LibraryEditor"]:::tool
        LIBVIEW["LibraryViewer"]:::tool
    end

    subgraph "Patch Infrastructure"
        AP["AutoPatcherAdmin"]:::tool
        WEB["Patcher Website"]:::web
    end

    subgraph "Shared Library"
        PROT["Protocol Contracts\n(ClientPackets & ServerPackets)"]:::shared
        DATA["Data Models & Enums"]:::shared
        UTIL["Utility Functions"]:::shared
    end

    DB[(Database\nSQL Server/MySQL)]:::database
    SLIMDX[/"SlimDX"/]:::external
    LOG4NET[/"log4net"/]:::external

    UI --> REND
    UI --> SOUND
    UI --> NET
    UI --> OBJ

    NET -->|"Game packets (TCP)"| SNET
    NET -->|"Status packets (TCP)"| SNET

    SNET --> CORE
    CORE --> PERS
    PERS --> DB
    CORE --> DOMAIN

    ADMIN -->|"in-process calls & DB"| PERS
    AP -->|"generates manifests"| WEB
    WEB -->|"HTTP GET/POST for updates"| UI

    PROT --> UI
    PROT --> SNET
    DATA --> CORE
    UTIL --> PERS

    SLIMDX --> REND
    LOG4NET --> CORE
    LOG4NET --> ADMIN

    click UI "https://github.com/suprcode/crystal/tree/master/Client/Forms"
    click REND "https://github.com/suprcode/crystal/tree/master/Client/MirGraphics"
    click SOUND "https://github.com/suprcode/crystal/tree/master/Client/MirSounds"
    click NET "https://github.com/suprcode/crystal/blob/master/Client/MirNetwork/Network.cs"
    click OBJ "https://github.com/suprcode/crystal/tree/master/Client/MirObjects"
    click SNET "https://github.com/suprcode/crystal/tree/master/Server/MirNetwork"
    click CORE "https://github.com/suprcode/crystal/tree/master/Server/MirEnvir"
    click DOMAIN "https://github.com/suprcode/crystal/tree/master/Server/MirObjects"
    click PERS "https://github.com/suprcode/crystal/tree/master/Server/MirDatabase"
    click ADMIN "https://github.com/suprcode/crystal/blob/master/Server.MirForms"
    click PROT "https://github.com/suprcode/crystal/blob/master/Shared/ClientPackets.cs"
    click DATA "https://github.com/suprcode/crystal/tree/master/Shared/Data/"
    click UTIL "https://github.com/suprcode/crystal/blob/master/Shared/Helpers/FileIO.cs"
    click AP "https://github.com/suprcode/crystal/tree/master/AutoPatcherAdmin"
    click WEB "https://github.com/suprcode/crystal/tree/master/PatcherWebSite"
    click LIBEDIT "https://github.com/suprcode/crystal/tree/master/LibraryEditor"
    click LIBVIEW "https://github.com/suprcode/crystal/tree/master/LibraryViewer"

    classDef client fill:#D0E6FF,stroke:#036,stroke-width:1px;
    classDef server fill:#D0FFD6,stroke:#060,stroke-width:1px;
    classDef shared fill:#FFE0B2,stroke:#E68A00,stroke-width:1px;
    classDef tool fill:#E1BEE7,stroke:#6A1B9A,stroke-width:1px;
    classDef web fill:#B3E5FC,stroke:#0277BD,stroke-width:1px;
    classDef database fill:#CFD8DC,stroke:#455A64,stroke-width:1px,shape:cylinder;
    classDef external fill:#EEEEEE,stroke:#999,stroke-width:1px,stroke-dasharray: 5 5;
```
