Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-usd

Source of release 0.9.2

0.9.2 / src / pcp / layers.lucb

## The layers a composition opens, each once (SdfLayer::FindOrOpen's
## registry): by identifier, read into LayerData sharing one Names, so
## paths and tokens compare across layers. A layer that cannot be read is
## remembered as failed.

## One opened (or failed) layer.
struct LayerEntry:
    var identifier: Growing[u8]
    var layer: LayerData*?
    ## Why a failed layer could not be read.
    var problem: Growing[u8]

## Every layer a composition has opened.
pub struct Layers:
    var names: Names*
    var entries: Growing[LayerEntry]

    pub func init(names: Names*):
        self.names = names
        self.entries = Growing[LayerEntry]()

    pub func close():
        for at in 0..<self.entries.count:
            self.entries.items[at].identifier.close()
            self.entries.items[at].problem.close()
            release_layer(self.entries.items[at].layer)
        self.entries.close()

    pub func count() -> usize:
        return self.entries.count

    pub func identifier(index: usize) -> str:
        return (str)self.entries.items[index].identifier.view()

    pub func layer(index: usize) -> const LayerData*:
        return self.entries.items[index].layer else trap("a failed layer")

    ## How many layers were read from files (UsdStage::GetUsedLayers
    ## without the anonymous ones).
    pub func file_count() -> usize:
        var count: usize = 0
        for entry in self.entries.view():
            let identifier = (str)entry.identifier.view()
            let anonymous = identifier.length >= 5 and (str)identifier.bytes[..<5] == "anon:"
            if entry.layer != none and not anonymous: count += 1
        return count

    ## Why the layer `identifier` names could not be read ("" when it was
    ## read, or never tried).
    pub func problem(identifier: str) -> str:
        for entry in self.entries.view():
            if (str)entry.identifier.view() == identifier: return (str)entry.problem.view()
        return ""

    ## Layer `index` to change (a session layer being authored).
    pub func editable(index: usize) -> LayerData*:
        return self.entries.items[index].layer else trap("a failed layer")

    ## The layer `identifier` names, opened now if new; none when it cannot
    ## be read (running out of memory fails).
    pub func find_or_open(identifier: str) -> usize?!:
        for at in 0..<self.entries.count:
            if (str)self.entries.items[at].identifier.view() == identifier:
                return at if self.entries.items[at].layer != none else none
        let storage = try new LayerData[1] --- in memory.heap
        storage[0] = LayerData(self.names) catch failure:
            free(storage) in memory.heap
            error(failure.code, failure.message)
        var problem = Growing[u8]()
        let read = readable(identifier, storage.data, &problem) catch failure:
            problem.close()
            storage[0].close()
            free(storage) in memory.heap
            error(failure.code, failure.message)
        if not read:
            storage[0].close()
            free(storage) in memory.heap
        var name = Growing[u8]()
        let opened: LayerData*? = storage.data if read else none
        name.assign(identifier.bytes) catch failure:
            name.close()
            problem.close()
            release_layer(opened)
            error(failure.code, failure.message)
        self.entries.push(LayerEntry(identifier = name, layer = opened, problem = problem)) catch failure:
            name.close()
            problem.close()
            release_layer(opened)
            error(failure.code, failure.message)
        return self.entries.count - 1 if read else none

    ## A new empty layer named `identifier` (an anonymous layer).
    pub func anonymous(identifier: str) -> usize!:
        let storage = try new LayerData[1] --- in memory.heap
        storage[0] = LayerData(self.names) catch failure:
            free(storage) in memory.heap
            error(failure.code, failure.message)
        var name = Growing[u8]()
        name.assign(identifier.bytes) catch failure:
            name.close()
            release_layer(storage.data)
            error(failure.code, failure.message)
        self.entries.push(LayerEntry(identifier = name, layer = storage.data, problem = Growing[u8]())) catch failure:
            name.close()
            release_layer(storage.data)
            error(failure.code, failure.message)
        return self.entries.count - 1

    ## The layer's metadata field `key` (on its pseudo-root).
    pub func field(index: usize, key: u32) -> const Value*?:
        return self.layer(index).get(0, key)

    ## The layer's time codes per second (framesPerSecond, else 24, when
    ## unauthored), as SdfLayer::GetTimeCodesPerSecond.
    pub func time_codes_per_second(index: usize) -> f64:
        let keys = &self.layer(index).keys
        if let found = self.field(index, keys.time_codes_per_second): return found.as_double()
        if let found = self.field(index, keys.frames_per_second): return found.as_double()
        return 24.0

## Whether the layer file reads; failures other than running out of
## memory make it unreadable, and `problem` says why.
func readable(identifier: str, layer: LayerData*, problem: Growing[u8]*) -> bool!:
    if not ar.exists(identifier):
        try problem.assign("no such file".bytes)
        return false
    let found = layer_file.read_layer_file(identifier, layer) catch failure:
        if failure.code == memory.exhausted: error(failure.code, failure.message)
        try problem.assign(failure.message.bytes)
        return false
    _ = found
    return true

func release_layer(layer: LayerData*?):
    if let opened = layer:
        opened.close()
        free(LayerData[](opened, 1)) in memory.heap