# zhangyxxyz/hotfixdemo architecture

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

- Interactive diagram: https://gitdiagram.com/zhangyxxyz/hotfixdemo
- Repository: https://github.com/zhangyxxyz/hotfixdemo

## Overview

##### Project type and purpose
- This is a Unity3D client application showcasing hot‑update capabilities via XLua (Lua scripts) and AssetBundles.
- It simulates a server by pulling resources from a local directory (C:/LuaServer).

##### File structure
- “Assets/Plugins” and “Assets/Extend/XLua” contain the XLua native integration, code-gen, examples, and editor tooling.
- “Assets/Scripts” houses the game’s C# modules:
- AssetBundle loading (AssetBundleLoader, Manager, ManifestLoader)
- Scene management (SceneManager)
- Hotfix orchestration (GameInit.cs, LuaManager.cs)
- “Assets/Src/Lua” holds the game’s hotfix scripts (event.lua, main.lua).
- “StreamingAssets/Windows” contains platform‑specific AssetBundles (models, UI, characters).

##### README for insights
- Confirms a “small hot‑update demo” based on XLua.
- Runtime fetches resources/scripts from a “local simulated server” folder.

##### Components
a. Unity Client
- Unity Engine runtime (mono/IL2CPP)
- C# application layer (GameInit, LuaManager)
- AssetBundle system (AssetBundleManager, loader classes)
b. XLua Plugin
- Native libs per platform (Android .so, iOS .a, macOS bundle)
- C# bindings (LuaEnv, LuaFunction, LuaTable, generated Wrap classes)
- Editor codegen tools (Generator, LinkXmlGen)
c. Lua Scripts
- Hotfix entrypoint (main.lua → event.lua → game logic)
d. Resource Server (simulated)
- Local filesystem (C:/LuaServer)
- Hosts AssetBundles & Lua scripts
e. StreamingAssets Cache
- Bundles deployed alongside client

##### Relationships and data flow
- On startup, GameInit → LuaManager initializes LuaEnv.
- AssetBundleManager retrieves manifests from StreamingAssets or remote folder.
- AssetBundleLoader requests bundles, Unity’s WWW/API fetches from “server” path.
- LuaManager loads main.lua via a custom loader, registers C# APIs.
- Lua scripts call into C# via registered functions (e.g., AssetBundleManager, SceneManager).
- Game logic runs in Lua, drawing Unity objects loaded from bundles.

##### Architectural patterns and principles
- Layered separation:
- Engine layer (Unity core & Mono runtime)
- Plugin layer (XLua native + binding)
- Application layer (C# managers & hotfix orchestrator)
- Scripting layer (Lua hotfix scripts)
- Resource layer (AssetBundles)
- Plugin/Adapter pattern: XLua bridges C# ↔ Lua.
- Hot‑swap pattern: Lua scripts loaded at runtime can override C# behaviour.
- AssetBundle caching & manifest-driven loading.

##### Essential elements in detail
- File‑based resource sources (StreamingAssets/, C:/LuaServer) as external services.
- Codegen step (XLua Editor → Wrap classes → C# compilation).
- Runtime components: AssetBundleManager, LuaManager, LuaEnv, main.lua.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Engine Layer"
        UE["Unity Engine Runtime"]:::engine
    end

    subgraph "Plugin Layer"
        subgraph "Native Libs"
            Android["Android .so"]:::plugin
            iOS["iOS libxlua.a"]:::plugin
            macOS["macOS xlua.bundle"]:::plugin
        end
        subgraph "C# Binding"
            LuaEnv["LuaEnv.cs"]:::plugin
            LuaFunc["LuaFunction.cs"]:::plugin
            LuaTable["LuaTable.cs"]:::plugin
            LuaDLL["LuaDLL.cs"]:::plugin
        end
        subgraph "Code-Gen Tools"
            Generator["Generator.cs"]:::plugin
            LinkXml["LinkXmlGen.cs"]:::plugin
        end
        subgraph "Generated Wraps"
            Wrap1["AssetBundleManagerWrap.cs"]:::plugin
            Wrap2["CSCallLuaItfDBridge.cs"]:::plugin
            Wrap3["WrapPusher.cs"]:::plugin
            Wrap4["XLuaGenAutoRegister.cs"]:::plugin
        end
    end

    subgraph "Application Layer"
        GameInit["GameInit.cs"]:::app
        LuaMgr["LuaManager.cs"]:::app
        ABMgr["AssetBundleManager.cs"]:::app
        ABLoader["AssetBundleLoader.cs"]:::app
        ABManifest["AssetBundleManifestLoader.cs"]:::app
        SceneMgr["SceneManager.cs"]:::app
    end

    subgraph "Scripting Layer"
        MainLua["main.lua"]:::script
        EventLua["event.lua"]:::script
    end

    subgraph "Resource Layer"
        Streaming["StreamingAssets/Windows"]:::resource
        Server["C:/LuaServer"]:::resource
    end

    UE --> GameInit
    GameInit --> LuaMgr
    LuaMgr --> LuaEnv
    LuaEnv --> MainLua
    MainLua --> EventLua
    ABMgr --> Streaming
    ABMgr --> Server
    ABLoader --> ABMgr
    ABLoader --> Streaming
    ABLoader --> Server
    MainLua -.->|"calls"| ABMgr
    MainLua -.->|"calls"| SceneMgr

    Generator --> Wrap1
    Generator --> Wrap2
    Generator --> Wrap3
    Generator --> Wrap4
    LinkXml --> Wrap4

    classDef engine fill:#ADD8E6,stroke:#000,color:#000
    classDef plugin fill:#FFA500,stroke:#000,color:#000
    classDef app fill:#90EE90,stroke:#000,color:#000
    classDef script fill:#FFFF00,stroke:#000,color:#000
    classDef resource fill:#D3D3D3,stroke:#000,color:#000

    click GameInit "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Scripts/HotFix/GameInit.cs"
    click LuaMgr "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Scripts/HotFix/LuaManager.cs"
    click ABMgr "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Scripts/Asset/AssetBundleManager.cs"
    click ABLoader "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Scripts/Asset/AssetBundleLoader.cs"
    click ABManifest "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Scripts/Asset/AssetBundleManifestLoader.cs"
    click SceneMgr "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Scripts/Asset/SceneManager.cs"
    click Android "https://github.com/zhangyxxyz/hotfixdemo/tree/master/Assets/Extend/Plugins/Android/libs/armeabi-v7a"
    click iOS "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/Plugins/iOS/libxlua.a"
    click macOS "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/Plugins/xlua.bundle"
    click LuaEnv "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/XLua/Src/LuaEnv.cs"
    click LuaFunc "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/XLua/Src/LuaFunction.cs"
    click LuaTable "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/XLua/Src/LuaTable.cs"
    click LuaDLL "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/XLua/Src/LuaDLL.cs"
    click Wrap1 "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/XLua/Gen/AssetBundleManagerWrap.cs"
    click Wrap2 "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/XLua/Gen/CSCallLuaItfDBridge.cs"
    click Wrap3 "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/XLua/Gen/WrapPusher.cs"
    click Wrap4 "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/XLua/Gen/XLuaGenAutoRegister.cs"
    click Generator "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/XLua/Src/Editor/Generator.cs"
    click LinkXml "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Extend/XLua/Src/Editor/LinkXmlGen/LinkXmlGen.cs"
    click MainLua "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Src/Lua/main.lua"
    click EventLua "https://github.com/zhangyxxyz/hotfixdemo/blob/master/Assets/Src/Lua/event.lua"
```
