# liuhaopen/unitymmo architecture

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

- Interactive diagram: https://gitdiagram.com/liuhaopen/unitymmo
- Repository: https://github.com/liuhaopen/unitymmo

## Overview

##### Main Components
- Client (Unity Frontend):
- Unity Engine and Scene: The project’s “Assets” directory contains all Unity scenes (e.g., main.unity) along with 3D models, animations, lighting, and physics setups.
- Lua Scripting Environment: Scripts under Assets/Scripts, Assets/XLuaFramework, and the “Lua” directory indicate that game logic, ECS frameworks, action systems, and UI behaviors are implemented in Lua. Also note the use of XLua to bind Unity and Lua.
- UI & Animation Modules: Organized separately under Assets/Scripts/UI and Assets/Scripts/Game/Timeline and Animation components. These control user interactions and visual feedback.
- Plugins & Third-Party Integration: Directories like Plugins, TextMesh Pro, UGUI-Editor, etc., showcase various external libraries and custom Unity plugins.

- Server (Backend):
- Skynet-based Game Server: The folder “Server” is the heart of your backend with subdirectories for “game” logic (account, bag, chat, scene, task, etc.), “service” modules (message agent, gateway, login, etc.), and supporting common utilities (e.g., sproto, helper files).
- Lua Game Logic: Much of the game processing (ECS, actions, AI, buff systems) is scripted in Lua. Specific directories under Server/game (like scene, fight, AI states, and system) emphasize that server behavior is also modular and data-driven.

- Communication & Networking:
- Sproto & Message Dispatchers: Both client and server use sproto (see Assets/Scripts/Net/Sproto and Server/common/sprotoloader.lua) to serialize and dispatch messages, indicating a custom protocol for client-server communication.
- Network Manager & Input Systems: On the client side, network modules (Net, NetMsgDispatcher.cs) handle sending requests and receiving responses. On the server, various “service” Lua files indicate endpoints that communicate with each other using Skynet processes.

- Tools & Build Infrastructure:
- Building and Encoding Tools: The Build folder, Tools (LuaEncoder, sprotodump), and various CMake scripts suggest a cross-platform build process (Android, iOS, Linux, etc.).
- Asset and Resource Management: References to AssetBundle resources, TextureFormatter (in the editor menu), and resource metadata indicate that asset processing is a key part of the client’s pipeline.

##### Defining Relationships & Interactions
- Client–Server Interaction:
- The client (Unity application) collects user input, animation events, and game state changes, then serializes and transmits these events (likely via sproto-over-socket messages) to the server.
- The server, running on Skynet processes, receives the messages, processes the game logic (movement, combat, chat, etc.) and sends responses back to the client.

- Intra-Client Modular Communication:
- Within the client, distinct modules (ECS-based game logic, UI management, timeline/animation, and network message dispatcher) interact through clearly defined interfaces – for example, the Lua “Game” modules talk to the Unity renderers and inputs via manager scripts and event dispatchers.

- Tool Chain / Build Processes:
- Tools and build scripts help compile platform-specific versions (e.g., special plugins for Android, iOS, WebGL) using CMake and Lua encoder tools, ensuring consistency across targets.

##### Architectural Patterns & Design Principles
- Component-Based & ECS Architecture:
- Both the client and server code reveal an ECS (Entity Component System) style of organization, with modules for separate concerns such as movement, animation, input, and damage calculations.

- Separation of Concerns & Modularization:
- The clear folder separation between “Assets” (client) and “Server” (backend) paired with numerous modules for individual gameplay features (chat, role, bag, task) demonstrates a modular design.

- Cross-Language Integration:
- The project combines C# (Unity), Lua (game logic on both client and server), and possibly C/C++ (the Build folder contains compiled libraries) by using bridging tools like XLua, promoting a mixed-language architecture.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Client (Unity Frontend)"
        A["Unity Engine & Scene Management"]:::client
        B["Lua Scripting Environment"]:::client
        C["UI Components"]:::client
        D["Timeline/Animation Modules"]:::client
        E["Plugins & Third-Party Integration"]:::client
        F["Networking & Sproto Handling"]:::client
    end

    subgraph "Server (Skynet-based Backend)"
        S1["Skynet Game Server"]:::server
        S2["Game Modules"]:::server
        S3["Communication Services"]:::server
        S4["Server Lua Game Logic & ECS"]:::server
        S5["Common Utilities"]:::server
    end

    subgraph "Tools & Build Infrastructure"
        T1["Build Tools"]:::tools
        T2["Supporting Tools"]:::tools
    end

    %% Client internal interactions
    A -->|"binds"| B
    B -->|"updatesUI"| C
    B -->|"drivesAnim"| D
    B -->|"sendsData"| F

    %% Client-Server communication via sproto
    F -->|"sproto_msg"| S3
    S3 -->|"sproto_msg"| F

    %% Server internal interactions
    S1 -->|"manages"| S2
    S1 -->|"integrates"| S4
    S1 -->|"uses"| S5
    S3 -->|"routes"| S1

    %% Tools interaction with Client and Server
    T1 -->|"builds"| A
    T1 -->|"builds"| S1
    T2 -->|"tools"| A
    T2 -->|"tools"| S1

    %% Click Events for Client Components
    click A "https://github.com/liuhaopen/unitymmo/tree/master/Assets/Test"
    click A "https://github.com/liuhaopen/unitymmo/blob/master/Assets/XLuaFramework/main.unity"
    click B "https://github.com/liuhaopen/unitymmo/tree/master/Lua"
    click C "https://github.com/liuhaopen/unitymmo/tree/master/Assets/Scripts/UI"
    click D "https://github.com/liuhaopen/unitymmo/tree/master/Assets/Scripts/Game/Timeline"
    click E "https://github.com/liuhaopen/unitymmo/tree/master/Assets/Plugins"
    click E "https://github.com/liuhaopen/unitymmo/tree/master/Assets/TextMesh Pro"
    click E "https://github.com/liuhaopen/unitymmo/tree/master/Assets/UGUI-Editor"
    click E "https://github.com/liuhaopen/unitymmo/tree/master/Assets/XLuaFramework"
    click F "https://github.com/liuhaopen/unitymmo/tree/master/Assets/Scripts/Net/Sproto"

    %% Click Events for Server Components
    click S1 "https://github.com/liuhaopen/unitymmo/tree/master/Server"
    click S2 "https://github.com/liuhaopen/unitymmo/tree/master/Server/game"
    click S4 "https://github.com/liuhaopen/unitymmo/tree/master/Server/lualib"
    click S5 "https://github.com/liuhaopen/unitymmo/blob/master/Server/common/sprotoloader.lua"

    %% Click Events for Tools Components
    click T1 "https://github.com/liuhaopen/unitymmo/tree/master/Build"
    click T2 "https://github.com/liuhaopen/unitymmo/tree/master/Tools/LuaEncoder"
    click T2 "https://github.com/liuhaopen/unitymmo/tree/master/Tools/sprotodump"

    %% Styles
    classDef client fill:#9ec9ff,stroke:#1f78b4,stroke-width:2px;
    classDef server fill:#a0dca0,stroke:#2e8b57,stroke-width:2px;
    classDef tools fill:#d3d3d3,stroke:#808080,stroke-width:2px;
```
