# momintlh/unitycord architecture

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

- Interactive diagram: https://gitdiagram.com/momintlh/unitycord
- Repository: https://github.com/momintlh/unitycord

## Overview

##### Project type and purpose
- This is a Unity plugin package ecosystem composed of two main libraries:
a) **PlayroomKit** – a Unity‐native SDK that provides runtime services (RPC, player management, match-making options, mock modes, build hooks) plus editor extensions (dev console, manager UI), example scenes, and automated tests.
b) **UnityCord** – a C# wrapper around Discord’s Embedded App SDK, with both Editor tooling (TypeScript/Vite-based NPM package to install/build) and C# runtime code that bridges Unity to Discord activities.

##### High‐level file‐structure overview
- Top level:
- Assets/PlayroomKit – the core Unity package:
– Editor/… (PlayroomDevEditor UI, dev-manager USS)
– modules/… (Helpers, Interfaces, RPC, Player, Options, MockMode)
– dependencies/… (third-party .NET libs: Castle.Core, NSubstitute, SimpleJSON, ParrelSync, UnityBrowserBridge, etc.)
– Tests/… (Editor unit tests)
– src/… & package.json, vite.config.js – a small JS build for PlayroomKit WebGL bridging
- Assets/UnityCord – the Discord bridge package:
– Editor/… (TypeScript source, NPM/Vite configs for building the JS plugin)
– Runtime/… (C# handlers, Commands.cs, DiscordSDK.cs)
– Plugins/… (UnityPlugin.jslib, browser resources, HTML/CSS)
- Assets/WebGLTemplates, Assets/Plugins – custom WebGL templates and jslib plugins used at runtime
- Assets/_project – example Unity scenes and scripts demonstrating PlayroomKit usage

##### Main components
A. **Unity Editor (Editor-time)**
- Package Manager & NPM (for the UnityCord Editor TS build)
- PlayroomDevEditor UI & PlayroomKit dev manager (C# Editor scripts)
- TypeScript/Vite toolchain (UnityCord Editor folder)
B. **PlayroomKit Runtime (C# in Unity build)**
- Modules: Helpers, Interfaces, RPC client, PlayerService, Options
- MockMode sub-system for local testing
- Build hooks (PlayroomBuildService)
- Dependencies (.NET libs, SimpleJSON, ParrelSync, UnityBrowserBridge)
C. **UnityCord Runtime (C#)**
- CallbackHandler, Commands, DiscordSDK wrapper
- Interop to the JS “UnityCordFrameworks.jspre/jslib” plugin
D. **WebGL‐side Bridge (JS/HTML/CSS)**
- Injected by UnityBrowserBridge and UnityCord plugins into the page
- HttpServer (local dev) or browser bridge scripts for message passing
E. **External Service**
- Discord Embedded App SDK and Discord backend

##### Relationships & data‐flows
1. **Editor‐time**
- Unity Editor invokes PlayroomKit Editor scripts → configures build profiles & dev manager.
- Unity Editor invokes UnityCord NPM/Vite toolchain → compiles TS → produces jslib plugin.
2. **Build pipeline**
- Unity build (WebGL) includes: PlayroomKit runtime code + UnityCord jslib + WebGL templates
3. **Runtime**
- Game code calls PlayroomKit modules → RPC, player service, match-making options.
- For Discord: PlayroomKit RPC → UnityCord Runtime → JS bridge (via jslib) → Discord Embedded App in browser → Discord servers.
- MockMode bypasses external calls by routing to local mock services.
4. **Testing**
- Editor unit tests target PlayroomKit modules (PlayerLocalTests, PlayroomKitTest).

##### Architectural patterns & principles
- **Editor vs Runtime separation**: everything under Editor folders runs in-Editor only; runtime modules are isolated under “modules.”
- **Modular package layout**: clear separation of concerns (Helpers, Interfaces, RPC, Player, Options, MockMode).
- **Abstraction via interfaces (IPlayroomBase, IRPC, IPlayerBase)** for DI/mocking.
- **RPC pattern**: dual local vs remote implementations (RPCLocal, BrowserMockRPC).
- **Plugin bridge**: C# ↔ JS via Unity’s jslib mechanism and browser templates.

- Clear, descriptive labels on each component
- Optionally, a “testing” inset showing how unit tests hook into PlayroomKit modules

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Editor-time Layer
    subgraph "Editor-time"
        direction TB
        UnityEditor["Unity Editor"]:::editor
        PlayroomDevEditor["PlayroomDevEditor UI & Dev Manager"]:::editor
        UnityCordToolchain["UnityCord TS/Vite Toolchain"]:::editor
    end

    %% Build Pipeline Layer
    subgraph "Build Pipeline"
        direction TB
        UnityBuild["WebGL Build Pipeline"]:::pipeline
    end

    %% Unity Runtime Layer
    subgraph "Unity C# Runtime"
        direction TB
        GameCode["Game Code"]:::runtime
        Helpers["Helpers"]:::runtime
        Interfaces["Interfaces"]:::runtime
        RPCClient["RPC Client"]:::runtime
        RPCLocal["RPCLocal Implementation"]:::runtime
        PlayerService["PlayerService"]:::runtime
        LocalPlayerService["LocalPlayerService"]:::runtime
        Options["Options"]:::runtime
        MockMode["MockMode Sub-system"]:::runtime
        PlayroomBuildService["PlayroomBuildService"]:::runtime
        SimpleJSON["SimpleJSON"]:::runtime
        UnityBrowserBridgeDep["UnityBrowserBridge"]:::runtime
    end

    %% UnityCord Runtime Bridge
    subgraph "UnityCord Runtime (C#)"
        direction TB
        CallbackHandler["CallbackHandler.cs"]:::runtime
        Commands["Commands.cs"]:::runtime
        DiscordSDKWrapper["DiscordSDK.cs"]:::runtime
    end

    %% JS Bridge Layer
    subgraph "JS/Browser Bridge"
        direction TB
        PlayroomIndexJS["PlayroomKit index.js"]:::js
        PlayroomFrameworksJS["PlayroomKit frameworks.js"]:::js
        UnityCordJSPlugin["UnityCord JS Plugin Bridge"]:::js
        PlayroomPlugin["Playroom C# JS Plugin Bridge"]:::js
        WebGLTemplates["Custom WebGL Templates"]:::js
    end

    %% External Services Layer
    subgraph "External Services"
        direction TB
        DiscordSDKExternal["Discord Embedded App SDK"]:::external
        DiscordBackend["Discord Backend"]:::external
    end

    %% Tests Inset
    subgraph "Editor Unit Tests" 
        direction TB
        PlayroomTests["PlayroomKit Editor Tests"]:::test
    end

    %% Connections - Editor-time to Build
    UnityEditor -->|"runs NPM build"| UnityCordToolchain
    UnityEditor -->|"configures build & dev manager"| PlayroomDevEditor
    PlayroomDevEditor -->|"triggers build"| UnityBuild
    UnityCordToolchain -->|"outputs jslib & assets"| UnityBuild

    %% Build to Runtime
    UnityBuild -->|"includes runtime code"| GameCode
    UnityBuild -->|"includes PlayroomKit modules"| Helpers
    UnityBuild -->|"includes PlayroomKit modules"| Interfaces
    UnityBuild -->|"includes PlayroomKit modules"| RPCClient
    UnityBuild -->|"includes PlayroomKit modules"| PlayerService
    UnityBuild -->|"includes PlayroomKit modules"| Options
    UnityBuild -->|"includes MockMode"| MockMode

    %% Runtime Data Flow - Normal
    GameCode -->|"RPC calls"| RPCClient
    RPCClient -->|"fallback"| RPCLocal
    GameCode -->|"player actions"| PlayerService
    GameCode -->|"options API"| Options
    RPCClient -->|"serializes via SimpleJSON"| SimpleJSON
    RPCClient -->|"uses browser bridge"| UnityBrowserBridgeDep
    RPCClient -->|"invokes"| DiscordSDKWrapper

    %% UnityCord Bridge Flow
    DiscordSDKWrapper -->|"calls via jslib"| UnityCordJSPlugin
    PlayroomBuildService -->|"hook during build"| UnityBuild

    %% JS Bridge to External
    UnityCordJSPlugin -->|"message via WebGL"| DiscordSDKExternal
    PlayroomIndexJS -->|"bridge messages"| DiscordSDKExternal
    PlayroomFrameworksJS -->|"bridge frameworks"| DiscordSDKExternal
    WebGLTemplates -->|"injects HTML/CSS"| DiscordSDKExternal

    %% MockMode Internal Flow
    GameCode -->|"uses"| MockMode
    MockMode -->|"local RPC & services"| RPCLocal

    %% Tests Connections
    PlayroomTests -->|"tests modules"| RPCClient
    PlayroomTests -->|"tests modules"| PlayerService
    PlayroomTests -->|"tests modules"| Interfaces

    %% Legend
    subgraph "Legend"
        direction LR
        E["Editor-time"]:::editor
        R["Runtime"]:::runtime
        J["JS Bridge"]:::js
        X["External"]:::external
        P["Pipeline"]:::pipeline
        T["Tests"]:::test
    end

    %% Click Events
    click PlayroomDevEditor "https://github.com/momintlh/unitycord/blob/main/Assets/PlayroomKit/Editor/PlayroomDevEditor.cs"
    click Helpers "https://github.com/momintlh/unitycord/tree/main/Assets/PlayroomKit/modules/Helpers/"
    click Interfaces "https://github.com/momintlh/unitycord/tree/main/Assets/PlayroomKit/modules/Interfaces/"
    click RPCClient "https://github.com/momintlh/unitycord/blob/main/Assets/PlayroomKit/modules/RPC/RPC.cs"
    click RPCLocal "https://github.com/momintlh/unitycord/blob/main/Assets/PlayroomKit/modules/RPC/RPCLocal.cs"
    click PlayerService "https://github.com/momintlh/unitycord/blob/main/Assets/PlayroomKit/modules/Player/PlayerService.cs"
    click LocalPlayerService "https://github.com/momintlh/unitycord/blob/main/Assets/PlayroomKit/modules/Player/LocalPlayerService.cs"
    click Options "https://github.com/momintlh/unitycord/tree/main/Assets/PlayroomKit/modules/Options/"
    click MockMode "https://github.com/momintlh/unitycord/tree/main/Assets/PlayroomKit/modules/MockMode/"
    click PlayroomBuildService "https://github.com/momintlh/unitycord/blob/main/Assets/PlayroomKit/modules/PlayroomBuildService.cs"
    click SimpleJSON "https://github.com/momintlh/unitycord/tree/main/Assets/PlayroomKit/dependencies/SimpleJSON/"
    click UnityBrowserBridgeDep "https://github.com/momintlh/unitycord/tree/main/Assets/PlayroomKit/dependencies/UnityBrowserBridge/"
    click PlayroomIndexJS "https://github.com/momintlh/unitycord/blob/main/Assets/PlayroomKit/src/index.js"
    click PlayroomFrameworksJS "https://github.com/momintlh/unitycord/blob/main/Assets/PlayroomKit/src/frameworks.js"
    click UnityCordToolchain "https://github.com/momintlh/unitycord/blob/main/Assets/UnityCord/Editor/package.json"
    click CallbackHandler "https://github.com/momintlh/unitycord/blob/main/Assets/UnityCord/Runtime/CallbackHandler.cs"
    click Commands "https://github.com/momintlh/unitycord/blob/main/Assets/UnityCord/Runtime/Commands.cs"
    click DiscordSDKWrapper "https://github.com/momintlh/unitycord/blob/main/Assets/UnityCord/Runtime/DiscordSDK.cs"
    click UnityCordJSPlugin "https://github.com/momintlh/unitycord/blob/main/Assets/Plugins/UnityCord/bridge.jslib"
    click PlayroomPlugin "https://github.com/momintlh/unitycord/blob/main/Assets/Plugins/Playroom/PlayroomPlugin.jslib"
    click WebGLTemplates "https://github.com/momintlh/unitycord/blob/main/Assets/WebGLTemplates/DiscordTemplate/index.html"

    %% Styles
    classDef editor fill:#ADD8E6,stroke:#000
    classDef runtime fill:#90EE90,stroke:#000
    classDef js fill:#FFA500,stroke:#000
    classDef external fill:#F08080,stroke:#000
    classDef pipeline fill:#E6E6FA,stroke:#000
    classDef test fill:#FFFACD,stroke:#000
```
