# fate-grand-automata/fga architecture

> Architecture diagram of the GitHub repository fate-grand-automata/fga, 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/fate-grand-automata/fga
- Repository: https://github.com/fate-grand-automata/fga

## Overview

##### Project Overview
- Project Type: Native Android auto-battle application
- Purpose: Automates routine gameplay actions for Fate/Grand Order by capturing the screen, recognizing game elements via OpenCV, and simulating touches through Android’s Accessibility Services.
- Key Technologies: Kotlin (primary language), OpenCV for image matching, MediaProjection for screen capture, and Accessibility Services for automating taps/swipes. The project is divided into several modules such as the main app, libraries (libautomata), preferences/configuration, and script modules.

##### Main Components
a. User Interface (UI) Module:
- Located mainly in the “app” directory (e.g., ui folder structure with MainActivity, Onboarding, launcher screens, battle config items).
- Responsible for user interactions, configuration screens, and launching the automation service.

b. Automation Service & Core Engine:
- Implements the core automation logic using image recognition and gesture simulation.
- Split across modules:
- “libautomata” – offers low-level automation API (screen capture, image matching, gesture simulation, OCR processing).
- “scripts” – holds automatic action “scripts” for various tasks (AutoBattle, AutoCEBomb, etc.) and modules for game logic, script entry-points, and support selection.

c. Preferences/Configuration Module:
- Found in “prefs” (and editing options in “app/ui/prefs”) to store user configuration, battle config options, preferred support, etc.
- Provides settings management that other modules (like scripts and service modules) reference.

d. Supporting Components:
- MediaProjection: Handles taking screenshots.
- Accessibility Service: Manages UI interactions (taps, swipes).
- Tesseract OCR: There is asset support for trained data in assets/tessdata.
- Fastlane: Deployment/automation scripts (fastlane directory) which can be shown as a continuous integration/deployment pipeline on the side.
- Wiki & Documentation: Included as external support/documentation but not core to functional architecture.

##### Architectural Relationships and Data Flow
- The UI module (front-end) communicates with the automation/service components through clearly defined interfaces.
- The core engine in libautomata and scripts takes input from MediaProjection (screenshot) and Accessibility Service for executing UI actions.
- Preferences are read by both UI and Automation service to tailor behaviors (e.g., using dependency injection modules, as seen in the “di” folders).
- The automation engine calls out to external libraries (OpenCV, OCR, etc.) for specialized processing.
- User actions flow from UI → Automation Service.
- Screen capture flows from the system’s Media Projection → Automation Service → (then triggers image processing with OpenCV).
- Configuration settings flow from the Preferences module to the UI and service modules.
- Optionally, include a side module or CI/CD pipeline accessory representing the GitHub workflows and fastlane for deployment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    U["User"]:::actor
    U -->|"input"| UI

    subgraph "User Interface"
        UI["UI Module (Activities/Fragments)"]:::ui
    end

    subgraph "Automation Engine"
        ALIB["Core Engine (libautomata)"]:::engine
        ASCR["Automation Scripts"]:::engine
        ALIB -->|"executes-scripts"| ASCR
    end

    Config["Configuration (Preferences)"]:::config

    subgraph "Android System Services"
        MP["MediaProjection"]:::android
        AS["Accessibility Service"]:::android
    end

    subgraph "External Libraries"
        OCV["OpenCV Integration"]:::external
        Tesseract["Tesseract OCR Integration"]:::external
    end

    DI["Dependency Injection Modules"]:::di
    Fastlane["Fastlane (CI/CD Pipeline)"]:::pipeline
    Wiki["Wiki/Documentation"]:::docs

    %% Relationships
    UI -->|"start-pause"| ALIB
    Config -->|"settings"| UI
    Config -->|"settings"| ALIB
    MP -->|"screenshot"| ALIB
    AS -->|"tap-swipe"| ALIB
    ALIB -->|"image-matching"| OCV
    ALIB -->|"ocr"| Tesseract
    DI -->|"injects"| UI
    DI -->|"injects"| ALIB
    UI -.->|"deploy"| Fastlane
    UI -.->|"docs"| Wiki

    %% Click Events
    click UI "https://github.com/fate-grand-automata/fga/tree/master/app/src/main/java/io/github/fate_grand_automata/ui"
    click ALIB "https://github.com/fate-grand-automata/fga/tree/master/libautomata/src/main/java/io/github/lib_automata"
    click ASCR "https://github.com/fate-grand-automata/fga/tree/master/scripts/src/main/java/io/github/fate_grand_automata/scripts"
    click Config "https://github.com/fate-grand-automata/fga/tree/master/prefs/src/main/java/io/github/fate_grand_automata/prefs"
    click MP "https://github.com/fate-grand-automata/fga/blob/master/app/src/main/java/io/github/fate_grand_automata/imaging/MediaProjectionRecording.kt"
    click MP "https://github.com/fate-grand-automata/fga/blob/master/app/src/main/java/io/github/fate_grand_automata/imaging/MediaProjectionScreenshotService.kt"
    click AS "https://github.com/fate-grand-automata/fga/tree/master/app/src/main/java/io/github/fate_grand_automata/accessibility"
    click OCV "https://github.com/fate-grand-automata/fga/blob/master/app/src/main/java/io/github/fate_grand_automata/imaging/DroidCvPattern.kt"
    click Tesseract "https://github.com/fate-grand-automata/fga/blob/master/app/src/main/java/io/github/fate_grand_automata/imaging/TesseractOcrService.kt"
    click Tesseract "https://github.com/fate-grand-automata/fga/blob/master/app/src/main/assets/tessdata/eng.traineddata"
    click DI "https://github.com/fate-grand-automata/fga/tree/master/app/src/main/java/io/github/fate_grand_automata/di"
    click Fastlane "https://github.com/fate-grand-automata/fga/tree/master/fastlane"
    click Wiki "https://github.com/fate-grand-automata/fga/tree/master/wiki"

    %% Styles
    classDef actor fill:#FCE4D6,stroke:#E67E22,stroke-width:2px;
    classDef ui fill:#AED6F1,stroke:#2980b9,stroke-width:2px;
    classDef engine fill:#ABEBC6,stroke:#27ae60,stroke-width:2px;
    classDef config fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
    classDef android fill:#FAD7A0,stroke:#E67E22,stroke-width:2px;
    classDef external fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
    classDef di fill:#D5DBDB,stroke:#566573,stroke-width:2px;
    classDef pipeline fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef docs fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
```
