Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

3.1 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    TimelineSerializer.ts
 * @brief   Serialize an in-memory TimelineAsset back to .estimeline JSON — the
 *          inverse of TimelineLoader.parseTimelineAsset.
 *
 * The runtime previously only LOADED timelines; authoring (the Sequencer editor)
 * needs to write them back. Field names and the wrapMode
 * string encoding mirror the loader exactly so parse(serialize(x)) round-trips —
 * notably AnimFrames tracks persist under the on-disk key `animFrames` (the loader
 * reads `raw.animFrames` into the in-memory `frames`).
 */

import { wrapModeToName, TrackType, TIMELINE_FORMAT_VERSION, type TimelineAsset, type Track } from './TimelineTypes';

function serializeTrack(track: Track): Record<string, unknown> {
    const base = { type: track.type, name: track.name, childPath: track.childPath };

    switch (track.type) {
        case TrackType.Property:
            return {
                ...base,
                component: track.component,
                channels: track.channels.map(ch => ({
                    property: ch.property,
                    keyframes: ch.keyframes.map(kf => ({
                        time: kf.time,
                        value: kf.value,
                        inTangent: kf.inTangent,
                        outTangent: kf.outTangent,
                        ...(kf.interpolation ? { interpolation: kf.interpolation } : {}),
                    })),
                })),
            };
        case TrackType.Spine:
            return { ...base, clips: track.clips, blendIn: track.blendIn };
        case TrackType.SpriteAnim:
            return { ...base, clip: track.clip, startTime: track.startTime };
        case TrackType.Particle:
        case TrackType.Audio:
            return { ...base, events: track.events };
        case TrackType.Activation:
            return { ...base, ranges: track.ranges };
        case TrackType.Marker:
            return { ...base, markers: track.markers };
        case TrackType.CustomEvent:
            return { ...base, events: track.events };
        case TrackType.Camera:
            return { ...base, childPath: '', clips: track.clips };
        case TrackType.Sequence:
            return { ...base, childPath: '', clips: track.clips };
        case TrackType.AnimFrames:
            // On-disk key is `animFrames` (matches the loader's read path).
            return { ...base, animFrames: track.frames };
        default:
            return base;
    }
}

/** Serialize to a plain JSON-ready object (the `.estimeline` document shape). */
export function serializeTimelineAsset(asset: TimelineAsset): Record<string, unknown> {
    return {
        version: TIMELINE_FORMAT_VERSION,
        type: 'timeline',
        duration: asset.duration,
        wrapMode: wrapModeToName(asset.wrapMode),
        tracks: asset.tracks.map(serializeTrack),
    };
}

/** Serialize to a pretty-printed JSON string for writing to disk. */
export function serializeTimelineToJson(asset: TimelineAsset, indent = 2): string {
    return JSON.stringify(serializeTimelineAsset(asset), null, indent);
}