tycho_io

    Tycho IO — backend-neutral file, asset and clock access shared by the desktop, mobile and browser targets

    io
    assets
    file
    platform
    tycho
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    1 hour ago
    Downloads
    3

    No dependencies

    #laplaceliu/tycho_io

    Part of the Tycho graphics stack — mounted there as the io/ submodule.

    Backend-neutral file, asset and clock access for the Tycho stack: four stateless functions, two implementations (native / js), selected per target through moon.pkg options.

    #What it provides

    • read_file(path) -> Bytes — read a file (or asset) into memory; empty Bytes when the path cannot be resolved.
    • write_file(path, data) -> Bool — replace path with data; false on failure.
    • asset_exists(path) -> Bool — whether path can be resolved and read.
    • now_ms() -> UInt64 — monotonic milliseconds; only differences between two readings are meaningful.

    Callers that must distinguish "missing" from "empty" should ask asset_exists first.

    #Targets

    TargetImplementationBehaviour
    nativeimpl_native.mbt + io_stub.cReal file system. A relative path is tried as given, then through a walk-up from the executable directory (up to 12 levels), because examples run from _build/....
    jsimpl_js.mbtIn-page virtual file system on globalThis.__tycho_io.files. A write_file is visible to a later read_file within one page; nothing persists across page loads, so real URLs must be preloaded into the map.

    now_ms on the browser is Date.now(): wall-clock, but the only millisecond source available without adding a worker — correct for measuring elapsed time as long as the clock is not stepped during the measurement.

    #Why not a virtual package?

    A virtual dependency forces every executable that transitively reaches it to declare an override, and 2d/3d pull this in through image — that is 89 example packages plus every library's own test executable. With no state, no descriptors and no handles to abstract, per-target file selection costs one option and no overrides. (The RHI and platform boundaries stay virtual because they must be selected, not merely compiled for a target.)

    #Example

    let bytes = @io.read_file("assets/scene.json")
    if @io.asset_exists("assets/scene.json") {
    @io.write_file("out/copy.json", bytes)
    }
    let t0 = @io.now_ms()

    #Smoke tests

    moon run smoke --target native # round trip through the real file system moon run smoke_js --target js # same contract against the in-page map

    From the Tycho meta-repo the same packages are addressed through the workspace member: moon run io/smoke --target native.

    asset_exists

    fn asset_exists(path : String) -> Bool

    Whether path can be resolved and read.

    now_ms

    fn now_ms() -> UInt64

    Monotonic milliseconds. Only differences between two readings are meaningful; the epoch is unspecified.

    read_file

    fn read_file(path : String) -> Bytes

    Read a file (or asset) into memory.

    Returns empty Bytes when the path cannot be resolved or read, matching the behaviour the shader package had. Callers that must distinguish "missing" from "empty" should ask asset_exists first.

    write_file

    fn write_file(path : String, data : Bytes) -> Bool

    Write data to path, replacing any existing file.

    Returns false on failure. On the browser target writes go to an in-page virtual file system, so a round trip inside one page works but nothing is persisted to disk.