# merryou6/onscripter-jh architecture

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

- Interactive diagram: https://gitdiagram.com/merryou6/onscripter-jh
- Repository: https://github.com/merryou6/onscripter-jh

## Overview

##### Project type and purpose
- This is ONScripter‑Jh, a native C++ visual‑novel engine that interprets NScripter scripts.
- It takes script files, loads assets (images, audio), executes commands (text, animation, sound), and renders frames via OpenGL ES2.

##### File structure
- Top‑level components:
- Core engine sources (ONScripter*.cpp/.h, ScriptParser*, ScriptHandler)
- File readers (DirectReader, SarReader, NsaReader)
- Renderer (renderer/gles_renderer.cpp, shaders)
- Effects (builtin_dll/*.cpp)
- Utilities (coding2utf16, conv_shared, resize_image, simd)
- Scripting extensions (LUAHandler)
- Wrappers (AVIWrapper for audio/video)
- Notice the “renderer” subdir implementing a GLES2 backend and shader pipeline, and “simd” for performance.

##### README for additional insights
- Confirms this is a direct port/repo migration of an existing NScripter interpreter.

##### Components
a. Core Script Engine
- ScriptParser (lexing & command nodes)
- ScriptHandler (dispatches commands at runtime)
- LUAHandler (optional Lua scripting)
b. Resource Loading Layer
- FileReaders (DirectReader, SarReader, NsaReader)
- Archive decoders & converters (nsadec, sardec, conv_shared)
c. Asset Managers / Utilities
- FontInfo / TextInfo (sjis2utf16, gbk2utf16)
- Image resizing & dirty‑rect tracker
- SIMD utilities for optimized loops
d. Renderer Subsystem
- GLES2 Renderer (gles_renderer, gles2funcs)
- Shader modules (post_cas.h, etc.)
- DirtyRect & AnimationInfo integration
e. Audio & Video Playback
- AVIWrapper (video/audio stream)
- Parallel threads for sound effects
f. Effects Modules
- Built‑in DLL effects (cascade, snow, oldmovie)
g. Host / Platform Integration
- Main entry (onscripter_main.cpp)
- Versioning & build scripts

##### Relationships & Data Flows
- Script files → ScriptParser → AST/command queue → ScriptHandler
- ScriptHandler → invokes Resource Loader for assets → feeds data to Renderer or AVIWrapper
- ScriptHandler → calls effect modules or push commands to Renderer
- Renderer → draws frames via GLES2, uses shaders & SIMDe optimizations
- Parallel threads handle audio and non‑blocking operations

##### Architectural patterns & principles
- Modular separation: parsing, handling, rendering, resource loading, effects
- Plugin‑style effects: builtin_dll folder loaded at runtime
- Platform abstraction: GLES2 under renderer, generic file readers
- Performance optimizations: SIMD layer

- Annotations for important technologies (OpenGL ES2, SIMD, Lua)

##### Final advice
- Be comprehensive: break the engine into all its modules (parsing, handling, rendering, I/O, utilities, plugins).
- How data moves from script files all the way to on‑screen output or audio playback.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Core Script Engine"
        SP["ScriptParser"]:::core
        SH["ScriptHandler"]:::core
        CoreMod["ONScripter Core Modules"]:::core
        LU["LUAHandler"]:::core
    end

    subgraph "Resource Loader"
        DR["DirectReader"]:::io
        SR["SarReader"]:::io
        NR["NsaReader"]:::io
        AD["Archive Decoders & Converters"]:::io
    end

    subgraph "Utilities & Asset Managers"
        FI["FontInfo / Text Encoding"]:::util
        IMG["Image & Layout"]:::util
        PAR["Parallel Execution Helper"]:::util
        MU["Misc Utilities"]:::util
    end

    subgraph "Renderer Subsystem"
        GLES["GLES2 Renderer"]:::render
        SHD["Shader Modules"]:::render
    end

    subgraph "Audio & Video Playback"
        AVI["AVIWrapper"]:::io
        SimAV["Simple AVI Player"]:::io
        Sound["Sound Threading & Dispatch"]:::io
    end

    subgraph "Effects Modules"
        PMC["Effect: Cascade"]:::plugin
        PTT["Effect: Trig"]:::plugin
        OLDO["OldMovie Layer"]:::plugin
        SNOW["Snow Layer"]:::plugin
        BL["builtin_layer.h"]:::plugin
    end

    subgraph "Platform Integration"
        MAIN["Main Entry (onscripter_main.cpp)"]:::core
        VER["version.h"]:::core
        BLDS["README.md & COPYING"]:::core
    end

    SP -->|"AST → Commands"| SH
    SH -->|"load assets"| DR
    SH -->|"load assets"| SR
    SH -->|"load assets"| NR
    DR --> AD
    SR --> AD
    NR --> AD
    AD -->|"Image / Audio Data"| SH
    SH -->|"invoke rendering"| GLES
    SH -->|"invoke effects"| PMC
    SH -->|"invoke effects"| PTT
    SH -->|"invoke effects"| OLDO
    SH -->|"invoke effects"| SNOW
    GLES -->|"uses shaders"| SHD
    SH -->|"audio stream"| AVI
    AVI --> SimAV
    AVI --> Sound
    IMG -->|"provides frames"| GLES
    FI -->|"provides text data"| SH
    PAR -->|"optimizes threads"| Sound
    MU -->|"utilities for all"| SH
    MAIN --> SP
    MAIN --> GLES
    MAIN --> AVI

    click SP "https://github.com/merryou6/onscripter-jh/blob/master/ScriptParser.cpp"
    click SP "https://github.com/merryou6/onscripter-jh/blob/master/ScriptParser.h"
    click SP "https://github.com/merryou6/onscripter-jh/blob/master/ScriptParser_command.cpp"
    click SH "https://github.com/merryou6/onscripter-jh/blob/master/ScriptHandler.cpp"
    click SH "https://github.com/merryou6/onscripter-jh/blob/master/ScriptHandler.h"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter.h"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_animation.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_command.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_effect.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_effect_breakup.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_event.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_file.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_file2.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_image.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_lut.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_rmenu.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_sound.cpp"
    click CoreMod "https://github.com/merryou6/onscripter-jh/blob/master/ONScripter_text.cpp"
    click LU "https://github.com/merryou6/onscripter-jh/blob/master/LUAHandler.cpp"
    click LU "https://github.com/merryou6/onscripter-jh/blob/master/LUAHandler.h"
    click DR "https://github.com/merryou6/onscripter-jh/blob/master/DirectReader.cpp"
    click DR "https://github.com/merryou6/onscripter-jh/blob/master/DirectReader.h"
    click SR "https://github.com/merryou6/onscripter-jh/blob/master/SarReader.cpp"
    click SR "https://github.com/merryou6/onscripter-jh/blob/master/SarReader.h"
    click NR "https://github.com/merryou6/onscripter-jh/blob/master/NsaReader.cpp"
    click NR "https://github.com/merryou6/onscripter-jh/blob/master/NsaReader.h"
    click AD "https://github.com/merryou6/onscripter-jh/blob/master/nsadec.cpp"
    click AD "https://github.com/merryou6/onscripter-jh/blob/master/sardec.cpp"
    click AD "https://github.com/merryou6/onscripter-jh/blob/master/sarconv.cpp"
    click AD "https://github.com/merryou6/onscripter-jh/blob/master/nsaconv.cpp"
    click AD "https://github.com/merryou6/onscripter-jh/blob/master/conv_shared.cpp"
    click AD "https://github.com/merryou6/onscripter-jh/blob/master/nscriptdecode.cpp"
    click FI "https://github.com/merryou6/onscripter-jh/blob/master/FontInfo.cpp"
    click FI "https://github.com/merryou6/onscripter-jh/blob/master/FontInfo.h"
    click FI "https://github.com/merryou6/onscripter-jh/blob/master/coding2utf16.cpp"
    click FI "https://github.com/merryou6/onscripter-jh/blob/master/coding2utf16.h"
    click FI "https://github.com/merryou6/onscripter-jh/blob/master/gbk2utf16.cpp"
    click FI "https://github.com/merryou6/onscripter-jh/blob/master/gbk2utf16.h"
    click FI "https://github.com/merryou6/onscripter-jh/blob/master/sjis2utf16.cpp"
    click FI "https://github.com/merryou6/onscripter-jh/blob/master/sjis2utf16.h"
    click IMG "https://github.com/merryou6/onscripter-jh/blob/master/AnimationInfo.cpp"
    click IMG "https://github.com/merryou6/onscripter-jh/blob/master/AnimationInfo.h"
    click IMG "https://github.com/merryou6/onscripter-jh/blob/master/DirtyRect.cpp"
    click IMG "https://github.com/merryou6/onscripter-jh/blob/master/DirtyRect.h"
    click IMG "https://github.com/merryou6/onscripter-jh/blob/master/resize_image.cpp"
    click IMG "https://github.com/merryou6/onscripter-jh/blob/master/resize_image.h"
    click PAR "https://github.com/merryou6/onscripter-jh/blob/master/Parallel.cpp"
    click PAR "https://github.com/merryou6/onscripter-jh/blob/master/Parallel.h"
    click MU "https://github.com/merryou6/onscripter-jh/blob/master/Utils.h"
    click GLES "https://github.com/merryou6/onscripter-jh/blob/master/renderer/gles_renderer.cpp"
    click GLES "https://github.com/merryou6/onscripter-jh/blob/master/renderer/gles_renderer.h"
    click GLES "https://github.com/merryou6/onscripter-jh/blob/master/renderer/gles2funcs.h"
    click SHD "https://github.com/merryou6/onscripter-jh/blob/master/renderer/shader/post_cas.h"
    click AVI "https://github.com/merryou6/onscripter-jh/blob/master/AVIWrapper.cpp"
    click AVI "https://github.com/merryou6/onscripter-jh/blob/master/AVIWrapper.h"
    click SimAV "https://github.com/merryou6/onscripter-jh/blob/master/simple_aviplay.cpp"
    click Sound "https://github.com/merryou6/onscripter-jh/blob/master/Parallel.cpp"
    click Sound "https://github.com/merryou6/onscripter-jh/blob/master/onscripter_sound.cpp"
    click PMC "https://github.com/merryou6/onscripter-jh/blob/master/builtin_dll/ONScripter_effect_cascade.cpp"
    click PTT "https://github.com/merryou6/onscripter-jh/blob/master/builtin_dll/ONScripter_effect_trig.cpp"
    click OLDO "https://github.com/merryou6/onscripter-jh/blob/master/builtin_dll/layer_oldmovie.cpp"
    click SNOW "https://github.com/merryou6/onscripter-jh/blob/master/builtin_dll/layer_snow.cpp"
    click BL "https://github.com/merryou6/onscripter-jh/blob/master/builtin_dll/builtin_layer.h"
    click MAIN "https://github.com/merryou6/onscripter-jh/blob/master/onscripter_main.cpp"
    click VER "https://github.com/merryou6/onscripter-jh/blob/master/version.h"
    click BLDS "https://github.com/merryou6/onscripter-jh/blob/master/README.md"
    click BLDS "https://github.com/merryou6/onscripter-jh/tree/master/COPYING"

    classDef core fill:#cce5ff,stroke:#000,stroke-width:1px
    classDef render fill:#d4edda,stroke:#000,stroke-width:1px
    classDef io fill:#fff3cd,stroke:#000,stroke-width:1px
    classDef util fill:#e2c3fc,stroke:#000,stroke-width:1px
    classDef plugin fill:#fff5ba,stroke:#000,stroke-width:1px
```
